Monday, May 24, 2010

Disk Partitioning

Working with disks is an important part to maintaining storage space on your system. When adding more storage or creating it for the first time you will want to partition your disk space into a logical format for use later on. Many installers come with an "automated" partitioning option for those that don't need a custom layout, however most system admins like to layout their own partitions. In my virtual environment I have a server that requires more space which already has Redhat install. To start I created two new IDE disks and made them 4GB each (I'm using virtual box). After the disk creation, boot the server and sign in. The first thing to look at is the amount of disks that are available.

# cat /proc/partitions | grep hd
   3     0   10485760 hda
   3     1     104391 hda1
   3     2   10377990 hda2
   3    64    4194304 hdb
   3    65    4194256 hdb1
  22    64    4194304 hdd 
 
If you are using SCSI disks instead of IDE disks you can just change the command to:

# cat /proc/partitions | grep sd

As you can see we get a listing of three different disks. These are hda, hdb, and hdd. Now that we know there are three disks we want to check out which partitions are currently created on those disks. We can use the fdisk command to view the current information.

# fdisk -l
Disk /dev/hda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        1305    10377990   8e  Linux LVM

Disk /dev/hdb: 4294 MB, 4294967296 bytes
16 heads, 63 sectors/track, 8322 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1        8322     4194256+  83  Linux

Disk /dev/hdd: 4294 MB, 4294967296 bytes
16 heads, 63 sectors/track, 8322 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Disk /dev/hdd doesn't contain a valid partition table
You could have also run this command first to gain information about both disks and their current partitions, but it is good to know different ways of obtaining the same information.  Based on the current information above we can see a few things about the different disks.  The first line specifies the disk and the total amount of storage available for it (IE. Disk /dev/hda: 10.7 GB).  Under the disk info we can see the current partitions layed out for the disk.  Notice the last line however about "Disk /dev/hdd".  It gives information about the disk (it has 4GB of space), however there are currently no partitions on it.  Using the fdisk utility we will partition the 3rd disk and create two partitions which we will use later on.

# fdisk /dev/hdd

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) 
 
First we will use the 'p' command to print out the current partition table (which shouldn't exist but let's just verify).

Command (m for help): p

Disk /dev/hdd: 4294 MB, 4294967296 bytes
16 heads, 63 sectors/track, 8322 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System 
 
As you can see there is nothing here. However we can see that we have 4294 MB of space to work with when dividing up the partitions on this disk. For ease of use we are going to cut this disk directly in half. We will create two paritions each with half of the disk space available, verify they are correct, and then write the changes to the disk.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-8322, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-8322, default 8322): +2147M 
 
Since we are cutting the disk in half based on the MB available we use the +sizeM syntax (as shown above). We could have divided the disk based on KB or cylinders if we wanted too. This creates the first partition, now lets make the second.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (4162-8322, default 4162): 
Using default value 4162
Last cylinder or +size or +sizeM or +sizeK (4162-8322, default 8322): [Press Enter]
Using default value 8322 
 
For this partition we don't need to specify a partition size because by default it will grow the partition to the size remaining on the disk. When asked what the size should be, pressing enter will just allocate the remaining disk space available. Next let's verify our newly created partitions:

Command (m for help): p

Disk /dev/hdd: 4294 MB, 4294967296 bytes
16 heads, 63 sectors/track, 8322 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdd1               1        4161     2097112+  83  Linux
/dev/hdd2            4162        8322     2097144   83  Linux 
 
You can see here the two partitions that we just created and based on their block size they are almost identical in size (there is some variation because of reserved space, superblocks, and other factors). Finally let's write the changes to disk.

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

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

And verify one last time:

# fdisk -l
Disk /dev/hda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14        1305    10377990   8e  Linux LVM

Disk /dev/hdb: 4294 MB, 4294967296 bytes
16 heads, 63 sectors/track, 8322 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1        8322     4194256+  83  Linux

Disk /dev/hdd: 4294 MB, 4294967296 bytes
16 heads, 63 sectors/track, 8322 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdd1               1        4161     2097112+  83  Linux
/dev/hdd2            4162        8322     2097144   83  Linux 
 
It is important to note that the default is to create the partition using a Linux System ID. Should you want to create the partition in a different format you can use the 'l' option to list all the supported System IDs and the 't' option to change the partition type. The fdisk utility is quiet useful and quick if you'd like to divide the disk in a hurry. There is another utility called "parted" that offers similar functionality and more options as well. I would advise looking into both fdisk and parted, using both to create partitions, and seeing which one you are more comfortable with. The last utility that I would mention as well is the "partprobe" command. When run it forces the kernel to re-read the partition tables. While utilities like fdisk and parted do a decent job of updating the partition tables if you ever find something off after making changes to partitions, try running partprobe and letting the kernel update the partition tables.

3 comments:

  1. cool blog mate :) try to use parted more instead of fdisk you will find that parted reads TB disks better (or actually reads them right)

    cheers,

    nik

    ReplyDelete
  2. I prefer parted, however for simplicity I use fdisk and I doubt that the RHCE will have TB disks hooked up to their workstations.

    ReplyDelete