This site is now 100% read-only, and retired.

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:

  • Add the label to your filesystem(s), or more properly partitions.
  • Use the names rather than anything else to refer to your partitions.

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:

  • /etc/fstab
    • This file lists the partitions/file-systems which should be mounted at boot-time
  • /boot/grub/menu.lst
    • This file configures my boot-loader so that the system will boot.

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.

 

 


Re: Mounting file-systems by label rather than device name
Posted by Anonymous (85.178.xx.xx) on Tue 1 May 2007 at 23:13
> 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.

mount -l

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by ramsamba (219.95.xx.xx) on Wed 2 May 2007 at 06:27
By mounting using labels, does it mean that if I were to move the partitions around, for e.g. move my root partition from /dev/hda6 to /dev/hda7 without making changes to GRUB or /etc/fstab, that my Linux will start up without any problems?

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (63.20.xx.xx) on Mon 7 Jul 2008 at 17:43
Yes. I use it this way since I have an old SIIG PCI card for my cd/dvd drives, and doing this avoids a grub edit everytime I boot - the root filesystem may show up as /dev/hda1 or /dev/hde1 - either way doesn't matter when using labels :)

Great article!

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (122.163.xx.xx) on Wed 9 Jul 2008 at 15:03
Wow, a reply after a year. Anyway, thanks Mr. Anonymous for confirming this. I haven't had the time to test this out and your reply helps.

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (59.178.xx.xx) on Wed 2 May 2007 at 07:29
Steve wrote:

(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.)

We look forward to your upcoming RAID article :-).

regards
PJ

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Steve (80.68.xx.xx) on Wed 2 May 2007 at 13:30
[ View Weblogs ]

Nothing much to write about that hasn't been done before.

I ordered a new drive, then when it arrived wiped the system and reinstalled Etch upon both the new drive and the failing one - so that I could use a RAID1 setup. Now I've removed the failing drive from the array, and marked it as missing.

Once I get my replacement for the failing drive back from the supplier I'll add it back into the array and it will rebuild to sync.

Right now I'm restoring backups and setting the machine up in a clean state. (I take backups of the system every 4 hours!)

Steve

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by cparker (192.200.xx.xx) on Fri 4 May 2007 at 22:37
[ View Weblogs ]
> I take backups of the system every 4 hours!

Wow! Where do you store them all?

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Steve (62.30.xx.xx) on Fri 4 May 2007 at 23:41
[ View Weblogs ]

I directly use five machines.

Two are dedicated servers living in a datacenter in London. One of those is the host which runs this site, the other one is my personal machine (which runs Xen).

The other three machines are in my flat and are broken down like this:

mine.my.flat

This is the machine Im using now, do my development work upon, and is my personal machine. It listens to music, surfs the web and is the machine I use every day. If I'm using a computer 99% of the time it is this one.

yours.my.flat

This machine runs Gnome so that my girlfriend has a computer to use when I'm using mine (hence the name. mine==Steve's, yours==Megan's).

Additionally this machine has a lot of local storage and takes rsnapshot backups of the remote machines, and my machine.

Before my girlfriend started using it so often its sole purpose in life was to be online 24x7 to function as a backup server in case of accidental deletion, or data-loss.

lappy.my.flat

Lives underneath the sofa for days on end, and only gets touched if I want to work/surf in bed, or in front of the TV.

Since most of my machines don't change very often even a full backup of each machine, rotated weekly, takes only 100Gb tops. Easily small enough to fit inside a 320Gb drive.

Steve

[ Parent ]

One more step
Posted by Anonymous (58.187.xx.xx) on Wed 2 May 2007 at 13:21
In addition to /etc/fstab, I found it is better to create mount points by device /label/ rather than device number (e.g., /media/CACHE, /media/cdrom). This makes working with scripts much easier and provides consistency in application configurations.

[ Parent ]

A word of advice
Posted by Anonymous (157.92.xx.xx) on Wed 2 May 2007 at 19:20
Label disk/partitions are nice, but it's kinda dangerous when you move your disk from one machine to another, specially with common names for partitions such as 'root', 'usr', 'var', etc...

[ Parent ]

Re: A word of advice
Posted by Anonymous (194.126.xx.xx) on Thu 3 May 2007 at 11:54
It's not really dangerous if you're smart about it.
Label it for example 'root-example.com' and 'var-example.com'.

[ Parent ]

Re: A word of advice
Posted by davux (89.128.xx.xx) on Thu 3 May 2007 at 16:36

Yes.

In addition to that, it can be a security problem, i.e. people can intentionally label their partitions in a way that would create name clashes with vital partitions.

I don't know what bad could be done with that, but I suppose that, for example, a USB disk labelled "root", "var", etc., could be used to gain access to a system: even if booting from a USB disk is disabled, having the device plugged at boot time may allow the partition to be mounted instead of the expected one (root partition, /var, etc.).

[ Parent ]

Re: A word of advice
Posted by Anonymous (213.164.xx.xx) on Sun 6 May 2007 at 19:53
So use uuids.

[ Parent ]

Re: A word of advice
Posted by Anonymous (72.66.xx.xx) on Mon 7 May 2007 at 20:27
You could use UUIDs instead (which can be gathered by running /sbin/blkid).

The only problem I've found is with Linux RAID1 devices - you have to specify /dev/md0 or whatever, otherwise the boot process may try to mount one of the mirrors first (/dev/sda1 for example) and then when the raid tries to assemble, it fails, halting the boot process.

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by undefined (192.91.xx.xx) on Wed 2 May 2007 at 20:38
i had a problem with mounting a raid-1 set by label when i tried it in the past.

the nice thing about raid-1 sets in linux: the physical partitions can be mounted individually or together as a raid-1 set. (meaning /boot can be on a raid-1 set but grub can still load the kernel and initrd without knowledge of raid-1.)

the bad thing: there are three partitions with the same label (the two physical partitions and the raid-1 set) and the first physical partition (hda1) was getting mounted as /boot instead of the raid-1 set (md0). the raid subsystem wasn't happy because hda1 was changing underneath md0. i think i had to unmount the physical partition (hda1), deactivate the raid-1 set, wipe the unchanged partition (hdb1), and let the raid subsystem sync it to the newer partition (hda1) upon reactivation.

all other file systems are on lvm (on raid-1), which already uses a logical descriptor (/dev/vg/root, /dev/vg/home, etc), so i don't see any benefit to labels for lvm on raid.

so i experimented with mounting-by-label, but found it problematic with raid-1.

now i just need to configure grub to try to load the kernel and initrd from hdb1 if hda1 fails (as both are in the /boot raid-1 set).

[ Parent ]

/dev/disk/by-label, and waiting for USB drives
Posted by endecotp (86.6.xx.xx) on Thu 3 May 2007 at 22:26
[ View Weblogs ]
I started using disk labels on my NSLU2 where it's pretty much essential: the numbering of the USB devices is random after each boot.

One subtle issue is whether to use the syntax 'LABEL=foo' or '/dev/disk/by-label/foo' in fstab and elsewhere. 'LABEL=' is more concise but requires that the program (e.g. mount in this case) understands it, whereas /dev/disk/by-label should work anywhere.

I strongly endorse the idea that you give unique-ish labels, having made a couple of screw-ups when not doing so. I use hostname-root for each machine's root drive. Also, don't label the root drive as '/'. This will work in some places but break in others (e.g. /dev/disk/by-label). I believe that RedHat may have used this as a default somewhere.


Off topic: when I list a USB drive in fstab (using a disk label, but I don't think that matters), /etc/init.d/mountall.sh doesn't wait for it to be readied by the USB/SCSI system. I have encountered two variants of this before: in the case of the root drive, there is a kernel command line parameter to make it wait, and in the case of nfs mounts the mount program itself will wait. But in this basic case, nothing happens. I was expecting to find a mount option meaning 'wait/retry until ready', but no. Any ideas?


Phil.

[ Parent ]

Can also label swap
Posted by endecotp (86.6.xx.xx) on Thu 3 May 2007 at 23:47
[ View Weblogs ]
You can also label your swap partitions using "mkswap -L", and then use labels for them in fstab too.

[ Parent ]

Re: Can also label swap
Posted by Anonymous (80.95.xx.xx) on Fri 11 May 2007 at 16:19
same with uuid

UUID=421535b5-e73d-1a2e-9a16-54b97d2ff4e6 none swap sw 0 3

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (24.238.xx.xx) on Wed 9 May 2007 at 17:42
I think this is a foolish thing to do. I admin about 8 linux boxes and three linux notebooks that are scattered about. These are not only my machines but those of other poeple. Some machines have a few partitions, others are multiple partition raid-1 arrays.

IMO, you are just asking for trouble by getting "cute" with hiding implementation behind labels. This sounds to be an admistrative burden of keeping more detailed information about each computer system recorded elsewhere.

No, for me, I would like the backup tape to have the paritioning structure device names in /etc/fstab.

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Steve (62.30.xx.xx) on Wed 9 May 2007 at 19:37
[ View Weblogs ]

To me "foolish" is a little harsh. If it doesn't work for you then fine, but I don't think the idea is foolish.

In fact it is similar to something that people frequently already do - namely creating /dev/root as a symlink to /dev/sda1. That gives a similar benefit..

Steve

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (80.95.xx.xx) on Fri 11 May 2007 at 16:23
/dev/root is sometimes a must because of /etc/mtab mount, df and initial ramdisk working together not so nicely -- sometimes.

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (129.33.xx.xx) on Wed 9 May 2007 at 19:04
Using labels can make life easier when moving from disks from IDE to libata/pata on kernels 2.6.19 or later. Labels allow /etc/fstab to remain the same as you boot back and forth between ide and libata/pata kernels. During testing, that is, once all is well it's time to move to libata/pata and stay there.

I have 2 machines where the hard disk /dev/sgX nodes get created, but the /dev/sdX nodes don't.

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (128.250.xx.xx) on Thu 10 May 2007 at 00:56
If you're using XFS as your file system (a good idea, especially with
battery-backed RAID configurations or drives that support write barriers),
you must use xfs_admin to set the label of the file system.
The remainder of the article applies as written.

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by andmalc (206.172.xx.xx) on Thu 10 May 2007 at 18:49
According to this recent Debian User thread, the 2.6.20 kernel will provide UUID, another device labelling scheme.

http://thread.gmane.org/gmane.linux.debian.user/286751

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (80.95.xx.xx) on Fri 11 May 2007 at 16:16
I prefer UUID then label.

UUID=dde10f91-59ad-4788-a4cd-ce5d94b5c465 / ext3 defaults,rw 0 1

tune2fs -l /dev/xyz | grep UUID

[ Parent ]

Re: Mounting file-systems by LVM naming convention
Posted by Anonymous (80.95.xx.xx) on Fri 11 May 2007 at 16:27
Anyway... labels, uuids. It's all the same history.

Today you are supposed to use LVM almost anywhere for the added flexibility and extremly low overhead with so much more completx disaster recovery ;-)

With lvm and initial ramdisk you do something similar in ramdisk to:

lvm vgscan
lvm vgchange -a y
lvm lvscan
lvm vgmknodes

and here you go...

/dev/VG/LV

and you don't care about any physical storage.

[ Parent ]

Re: Mounting file-systems by LVM naming convention
Posted by Anonymous (80.95.xx.xx) on Fri 11 May 2007 at 16:35
for partitioning - lvm - raid - lvm layout explanation see this partitioning
for initial ramdisk see this ramdisk linuxrc and rcS.d scripts
for the linuxrc script inside ramdisk see the above
for the /dev/ refreshing script after boot see the above
for the real-root-dev tool to update proc see the change root /proc/ tool to boot off the ramdisk with correct lvm device

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by cammoblammo (203.171.xx.xx) on Mon 14 May 2007 at 13:53
I'm trying to add labels my USB flash drives --- ever since I upgraded to kernel 2.6.20 I haven't been able to find the device name (they're no longer at /dev/sda1).

Anyway, they use vfat filesystems, so tune2fs doesn't work. I can't seem to use mlabel (from the mtools package) because it seems to expect a DOS type drive specification (drive letter plus colon) and none of those seem to work.

Any idea how to do this without resorting to Windows?

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (86.6.xx.xx) on Mon 14 May 2007 at 23:53
> I'm trying to add labels my USB flash drives
> they use vfat filesystems
> Any idea how to do this

man mkfs.msdos
see the -n option.

Of course this will erase the current content of the drive, which seems a bit excessive to just change the label. Maybe there is some sort of tunefatfs program out there, but I can't see one.

Phil.

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by cammoblammo (203.171.xx.xx) on Tue 15 May 2007 at 06:59

Aha! I've figured it out.

The trick is to map the USB device to a drive letter in /etc/mtools.conf. I added the following line:

drive z: file="/dev/sda1"

I attached the USB key then issued

mlabel z:usbkey

I checked with:

mlabel -s z:

and everything seemed to be fine!

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by ajt (204.193.xx.xx) on Wed 19 Dec 2007 at 09:59
[ View Weblogs ]
I believe that Red Hat use LABEL by default on their Red Hat Enterprise Linux systems. They use the "LABEL=/boot" for the /boot partition and so on.

--
"It's Not Magic, It's Work"
Adam

[ Parent ]

Re: Mounting file-systems by label rather than device name
Posted by Anonymous (84.176.xx.xx) on Fri 7 Mar 2008 at 15:28
the first time i heard about "mount by label" i got really exited, but fall back down to ground after realizing that it only works if you define the label in /etc/fstab too...

well, we could do a script that goes through the attached devices and sees if theres a label named "foobar", so that we could mount label "foobar" without having it in fstab...

but labeling partitions has atleas one good point, automount of flash disk use the label for the mount point! (atleast on ubuntu) so you would see "foobar" on your desktop when you plugged in your flash disk where the first label was named "foobar"

[ Parent ]