Mounting file-systems by label rather than device name

Posted by Steve on Tue 1 May 2007 at 22:26

When you're dealing with multiple drives, both fixed and removable, it can get hard to remember which is which. Remembering to mount /dev/sda1 in one place and /dev/sdc5 in another. The solution to this problem is to use labels instead of partition names when referring to them, and here we'll show how that can be done.

There are two things you need to do to start using labels:

To add a label you must use the tune2fs command, specifying the partition to modify and the label to add.

Upon my home system I have only a single disk /dev/sda which has three partitions:

    Name        Flags           Size (MB)
 -------------------------------------------
    sda1        Linux ext3       20003.89 
    sda2        swap              3002.23
    sda3        Linux LVM       297064.22

(Irony is .. my main disk drive died between the writing and publication of this article. Shortly I will be moving to a RAID-based system.)

To add the label "root" to my root partition, /dev/sda1, I'd run:

root@mine:~# tune2fs -Lroot /dev/sda1

Once I've got a label in place it becomes visible in my partition list when I run "cfdisk /dev/sda", so I know it has worked.

Once the label has been put into place there are now two places which I need to update the system in order to use it:

In both cases the change is the same, instead of specifying /dev/sda1 I specify LABEL=root.

My /etc/fstab file now looks like this:

#
# /etc/fstab - static file system information.
#
LABEL=root      /        ext3    defaults,errors=remount-ro 0       1
/dev/sda2       none     swap    sw                         0       0
...
...

The grub configuration file was updated to change:

# kopt=root=/dev/sda1 ro acpi=off noapic

into this:

# kopt=root=LABEL=root ro acpi=off noapic

Once this was done the kernel(s) can be updated by running update-grub. This updated my Xen kernels and my Debian kernels to include the new root - for example:

title           Xen 3.0.3-1-amd64 / Debian GNU/Linux, kernel 2.6.18-4-xen-amd64
root            (hd0,0)
kernel          /boot/xen-3.0.3-1-amd64.gz
module          /boot/vmlinuz-2.6.18-4-xen-amd64 root=LABEL=root ro acpi=off noapic console=tty0
module          /boot/initrd.img-2.6.18-4-xen-amd64
savedefault

The advantage of doing this is that I can now use identical configuration files upon each of my hosts, and rely upon the labels to make sure the correct partition is used as my root filesystem. This is very useful when you're configuring your systems via CFEngine, or similar.


This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2007 Steve - please ask for permission to republish or translate.