X
Tech

Linux and Disk Partitions

As usual, this blog post comes out of something I have been working on (read as: struggling with) for the past few days. The purpose is to give an overview of disk partitioning under Linux, specifically as it relates to PC BIOS systems, and to make a suggestion about partition names and references.
Written by J.A. Watson, Contributor

As usual, this blog post comes out of something I have been working on (read as: struggling with) for the past few days. The purpose is to give an overview of disk partitioning under Linux, specifically as it relates to PC BIOS systems, and to make a suggestion about partition names and references.

A typical PC BIOS will allow a maximum of four partitions to be created on one disk. Corresponding to this, Linux reserves the first four device names on a disk drive to match these four partitions - regardless of how many partitions you actually create. This means that in the typical case the device names /dev/sda1 ... /dev/sda4 (or hda1 ... hda4, depending on what version of Linux you are using and what kind of disk controller you have) will be assigned to those partitions. If you don't create all four partitions you will not see all of these names in the /dev directory, but they will be reserved none the less, and as we will see next, they will not be used for any subsequent partitions that may be created. Likewise, GRUB reserves its first four identifiers, (hd0,0) ... (hd0,3) for the four physical partitions.

Quite a long time ago, four partitions proved not to be nearly enough. So the concept of an "Extended Partition" was created, which gave one partition (but ONLY one) the ability to contain a number of additional "Logical Partitions". The limit of four physical partitions remains, so now you can have three "Primary Partitions" and one "Extended Partition".

When you create "Logical Partitions" within the "Extended Partition", Linux (and GRUB) start naming them after the four reserved names. So the first "Logical Partition" is going to be called /dev/sda5 (or /dev/hda5) by Linux and (hd0,4) by GRUB, and the naming will continue sequentially from there. But it can get confusing if you delete and recreate Logical Partitions, especially if you split one into several, or merge several into one... which brings us to the real point of this post.

Linux (and Unix) have generally referred to disk partitions by their "traditional" device name, such as /dev/sda2 or /dev/hda5. However, this kind of name for a particular partition can (and will) change as a result of changes made outside of it - most commonly adding or removing another disk, or changing the partition layout of the disk it is on. When this happens, things can get ugly on Linux in a hurry, to say the least.

Newer versions of Linux have an alternative partition identification system, using each partition's UUID (unique identifier), which will remain the same as long as the partition itself is not destroyed or recreated. There are a number of ways to find the UUID for a parition; the one that I use is the Linux blkid command, which looks like this:

$ blkid /dev/sda5 /dev/sda5: UUID="1e7e4998-5f49-405e-9b68-c81ab19bac49" TYPE="ext3" LABEL="Ubuntu"

Once you know the UUID, you can use it in /etc/fstab like this:

UUID=1e7e4998-5f49-405e-9b68-c81ab19bac49 / ext3 relatime,errors=remount-ro 0 1

The "UUID=..." syntax can similarly be used to specify the swap area, of course.

Equally as important, this syntax can be used on the GRUB configuration file, /boot/grub/menu.lst, like this:

kernel /boot/vmlinuz-2.6.27-9-generic root=UUID=1e7e4998-5f49-405e-9b68-c81ab19bac49 ro quiet splash

It is worth mentioning that there are other ways of naming and identifying disk partitions under Linux, which you can see by looking at the contents of the /dev/disk directory. One of these, by-id, is used by openSUSE and others. This will protect you from problems when other disks are added or removed, but unfortunately the partition number is part of this naming convention, so there are still cases where it will change when you change the partition table of the disk. For this reason I have modified the configuration files on my openSUSE installations to use the UUID instead.

jw 20/1/2009

Editorial standards