Sunday, March 24, 2019

A guide for partitioning and mounting disk drive in Linux

Suppose you have 2 hard drive .
One is already pre-partitioned (sda) and the new one (sdb) is unpartitioned.
Now i am going to show you how to partition the new drive  'sdb' and auto-mount in startup.

Nevigate to your System Terminal or putty.

To see your existing disk drives/partitions, enter the following command  :


 lsblk  
press the Enter  Key.
You wiill see that the new disk drive sdb is unpartitioned, as it is named only as sdb and so sub name such as sdb1 [disk drive name sdX (e.g. sda or sdb) may differ in your case.]

Now we will partition, format & mout the sdb drive.
To do so.. enter the following command :
 fdisk /dev/sdb  
press the Enter  Key.
Enter n for new partition.

Enter 
p for primary partition.

Partition number will be (
default) 1 , as we are partitioning the disk for the first time.
To do so,

Enter
 1 

First Sector will be 
defaultso just press the
 Enter  Key.
If you want to use the whole free space in your new disk partition, then you have set the Last Sector as default. To do so, just press the Enter  Key.
Also you could define a custom partition size for your new disk. Size should be given as +sizeK / +sizeM / +sizeG  [e.g. +9437184K  / +9216M / +9G] 

To do so,  type  +9G and 
press the Enter  Key.

Now we have to save the partition table change.

To do so,
type
w 
and press the  Enter  Key.

Now we will check if the partition has been created or not. So, type:
 lsblk  
and press the  Enter  Key.
Now, we can see that new partition sdb1 has been created.
Now we have to format the partition. So, type :
 mkfs.ext4 /dev/sdb1  
and press the  Enter  Key.
Now.. sdb1 successfully formatted.

After this we have to create a mount directory to mount the sdb1 partition.
Type :
 mkdir /drive2  
and press the  Enter  Key.
Now we will manually mount the partition to  /drive2.Type :                                                                            
mount /dev/sdb1 /drive2  
and press the  Enter  Key.
We can see that our newly created partition is successfully mounted at /drive2.
Now, we have to find out the UUID of the newly created partition for auto-mounting the partition, when the system boots up.
Type :
 blkid /dev/sdb1   
and press the  Enter  Key.
Now we have to create a new entry in the fstab file for automount of the partition sdb1 using vim.
 vim /etc/fstab +  
and press the  Enter  Key.



We have to copy an existing line,
To do so, point your cursor to an existing line to copy (using mouse / keyboard).
Now to copy the line press 'y' key twice from the keyboard.
Point your cursor where you want to paste the line (using mouse / keyboard).
To paste the line, press 'p' key from the keyboard.
Then press the 'i' key to enter the insert mode of vim.
Now edit the lines and input the UUID of the sdb1 partition, then its mount point, its partition type, defaults and the 0 0 bits.















For saving the changes in the fstab file and exit, press the ESC key type :wq key and hit [Enter].
Now just reboot your linux server by typing the following command :
 reboot  

Press the[Enter]Key.

Thats It!!!!!
You have successfully partitioned and auto-mounted your disk drive successfully in your linux server.