Tuesday, 1 August 2017

Format a new disk and add it to Linux machine

Most of the time when we purchase a new disk for adding to the Linux server or machine, we may face partition issues or mounting issues.


Following are the steps that helped me to get rid of those issue.

Step 1:
Connect the disk to the Linux machine or server. In my scenario Linux flavor CentOS 5.2

Step 2:
Open a terminal in administrator mode or please add sudo for following commands.
  • Run the fdisk -l command. the output will like 
    Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14       13067   104856255   83  Linux
/dev/sda3           13068       26121   104856255   83  Linux
/dev/sda4           26122       60801   278567100    5  Extended
/dev/sda5           26122       32648    52428096   83  Linux
/dev/sda6           32649       33170     4192933+  82  Linux swap / Solaris

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

  • Here the /dev/sdb is disk that I have to add to my server.
  • To work on the specific disk open that disk in using fdisk.
  • sudo fdisk /dev/sdb and give option p to print the partition table.
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
Command (m for help): p

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

  • In same fdisk console give option n to create new partition. and select primary partition(P)or extended (e). I have used +512000M for creating partition of 500GB

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-121601, default 1):<Press Enter to choose default>
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-121601, default 
121601): +512000M

  • Follow the above step to create next partition too.
  • After creating the partition, that must be written to disk. 
Command (m for help): w

Disk /dev/sdb: 1000.2 GB, 1000204886016 bytes255 heads, 63 sectors/track, 121601 cylinders

  • Next primary step is to mount it to local file system .
  • Before mounting the fdisk should be give extension type , like ext2 or ext3 etc..
  • Run 
mkfs -t ext3 /dev/sdb1
  • then Create local for mounting like /storage01
mkdir storage in home of any mount location.
  • Mount the /dev/sdb1 to /storage01
mount -t ext3 /dev/sdb1 /storage01
  • Follow the same steps to the partitions you have created like sdb2 , sdb3.....

Hope it helped you. Happy storage :)