Formatting, Partitioning, and Mounting Drives on linux

Three days back (02-Mar-2014), One of our web hosting support client ask me to Format, Partition, and to Mount the new drive to his cloud server, because he signed up the FREE cloud server on SOFTLAYER (25GB storage), after a week he decided himself (There is no issues on server, like downtime) to migrate all the existing account (Dedicated server) to cloud, but he got only 25GB, so he added additional 100GB hard drive and request me to proceed further. I had prepared this article based on that.

CentOS 6.5 :Tested Environment

1) How to check new hard drive is added in server ?

Before that, i want to check whether the new hard drive is added in server or not. Use the below command to check it.

# ls -la /dev/xvd*
brw-rw---- 1 root disk 202,  0 Mar  4 02:16 /dev/xvda
brw-rw---- 1 root disk 202,  1 Mar  4 02:16 /dev/xvda1
brw-rw---- 1 root disk 202,  2 Mar  4 02:16 /dev/xvda2
brw-rw---- 1 root disk 202, 16 Mar  4 02:16 /dev/xvdb
brw-rw---- 1 root disk 202, 17 Mar  4 02:16 /dev/xvdb1
brw-rw---- 1 root disk 202, 32 Mar  4 02:16 /dev/xvdc

The above output clearly shows there is no partition on /dev/xvdc drive.

2) How to check new hard drive size ?

Use fdisk command with lto show list of device available on your server.

[email protected] [~]# fdisk -l

Disk /dev/xvdc: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/xvda: 26.8 GB, 26843545600 bytes
255 heads, 63 sectors/track, 3263 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000e6b7

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1          33      262144   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/xvda2              33        3264    25951232   83  Linux

Disk /dev/xvdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00025cdb

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1               1         261     2096451   82  Linux swap / Solaris

The first part of output is shows, the new hard disk size is 107.4 GB and device name is /dev/xvdc. The Disk identifier: 0x00000000 is shows only zero, its mean the hard drive not yet partitioned. If the hard drive is partitioned your output looks like a second and third part.

3) Creating Partition ?

Use fdisk command and mention your device name to proceed further.

[email protected] [~]# fdisk /dev/xvdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf5995738.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

As per fdisk instruction, i’m going to switch off DOS-compatible mode and enable the units to sectors.

Command (m for help): c
DOS Compatibility flag is not set
Command (m for help): u
Changing display/entry units to sectors

If you are a beginner and doing this work for the first time means first of all you have to check the available list of command actions for your convenience and then goahead.

Command (m for help): m
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)

We had already known that, this is a new hard drive, so we want to create new partition. Press n to add new partition. After that, we want to mention the partition type, like primary or extended. Here i’m going to create the primary partition, so i have mentioned p to create primary partition. After mentioned the partition, we want to mention the partition number. We know this is our first partition on this device, so i have mention 1. reset of things just enter, its enough because we are going to create only one partition on this device. If you want to create multiple partition you need to mention the cylinder begin and end values, so that you will be create multiple partition.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-13054, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): 
Using default value 13054

If you want to view the partition table after create partition, use P to view it.

Command (m for help): p

Disk /dev/xvdc: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf5995738

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdc1               1       13054   104856223+  83  Linux

As of now we have created the partition, now i’m going to write the partition table to disk using w command.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Now, i’m going to check whether the partition got created or not. Yes got created because i can able to saw /dev/xvdc1.

[email protected] [~]# ls -la /dev/xvd*
brw-rw---- 1 root disk 202,  0 Mar  4 02:16 /dev/xvda
brw-rw---- 1 root disk 202,  1 Mar  4 02:16 /dev/xvda1
brw-rw---- 1 root disk 202,  2 Mar  4 02:16 /dev/xvda2
brw-rw---- 1 root disk 202, 16 Mar  4 02:16 /dev/xvdb
brw-rw---- 1 root disk 202, 17 Mar  4 02:16 /dev/xvdb1
brw-rw---- 1 root disk 202, 32 Mar  4 02:16 /dev/xvdc
brw-rw---- 1 root disk 202, 32 Mar  4 02:16 /dev/xvdc1

4) Creating a File System on Disk Partition ?

There are two major type of linux file system is available.

  • ext4 : Latest Linux file system type which is present now.
  • ext3 : This is the most common Linux file system type which is used a couple of years back.

Now, i’m going to check linux file system type which is present on our server and use the same one. I found that we are currently using ext3 in our server.

Use the below command to create the file system on your device.

[email protected] [~]# mkfs.ext3 /dev/xvdc1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214055 blocks
1310702 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables:   0/800  1/800  2/800  3/800  4/800  5/800  6/800  7/800  
8/800  9/800 10/800 11/800 12/800 13/800 14/800 15/800 16/800
.
.
96/800 797/800 798/800 799/800 done

Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

5) Mounting a File System ?

To mount the device, you need to create the directory which you want to mount this device. I’m going to create directory called /home1.

[email protected] [~]# mkdir /home1 

Before mounting the device, you need to edit fstab manually and add the device,directory and mention the file systems like below.

[email protected] [~]# nano /etc/fstab
  GNU nano 2.0.9                                           File: /etc/fstab


#
# /etc/fstab
# Created by anaconda on Mon Feb  3 16:21:50 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=51571c83-d99b-49bc-a891-08d8a5d7eaa3       /       ext3    noatime,usrjquota=quota.user,jqfmt=vfsv0        1       1
UUID=b90ef47e-d438-4a0c-aac0-1d1de719600d /boot                   ext3    defaults,noatime        1 2
LABEL=SWAP-xvdb1 swap swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/usr/tmpDSK             /tmp                    ext3    defaults,noauto        0 0
/dev/xvdc1              /home1                  ext3    defaults        0 0

Now, i’m going to run the mount command to mount the device

[email protected] [~]# mount /dev/xvdc1 /home1

[email protected] [~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda2       25G   23G 1003M  96% /
tmpfs           494M     0  494M   0% /dev/shm
/dev/xvda1      248M  101M  135M  43% /boot
/usr/tmpDSK     485M   11M  449M   3% /tmp
/dev/xvdc1       99G  188M   94G   1% /home1

The output is clearly shows the new hard drive is mounted with /home1 directory, that’s it. For our safety purpose, i’m going to reboot the server then start the migration.

[email protected] [~]# reboot
[email protected] [~]# 

Broadcast message from [email protected]

	(/dev/pts/1) at 2:21 ...




The system is going down for reboot NOW!	

We are preparing all articles in-depth to understand by all level/stage Linux administrators. If the article is useful for you, then please spend less than a minute to share your valuable comments in our commenting section.

Please stay tune with us…Good Luck 🙂

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...