Using Disk Labels on Linux File Systems

Disk Labels in Linux

by Joseph H Kwan

Background

One of the annoying things with PC hardware is the mapping of disk devices
(which one becomes the "C:" drive etc). Adding or removing a disk can
easily change that mapping and your previously working configuration
suddenly doesn't work.

On PC Linux systems, hard coding of disk devices should be avoided (like in
fstab and grub.conf) since it's easy to have that break when another disk
is added or removed. For example, if you have your system disk on /dev/sda,
the system might map a different disk as /dev/sda then your fstab is messed
up and the system won't come up properly. I've had such things happen
having an internal SAS disk on a Sun Fire server working perfectly, then
connecting a fiber channel RAID and all of a sudden the system won't come
up correctly because the fiber channel RAID becomes /dev/sda.

From man fstab:

Instead of giving the device explicitly, one may indicate the
(ext2 or xfs)  filesystem that is to be mounted by its UUID or
volume label (cf. e2label(8) or  xfs_admin(8)),  writing
LABEL=<label>  or  UUID=<uuid>, e.g., LABEL=Boot or UUID=3ede-813...
This will make the system more robust: adding or removing a SCSI
disk changes the disk device name but not the filesystem volume label.

ext2, ext3, xfs and swap file systems can have labels. FAT file
systems don't have any mechanism to support disk labels. Instead you
should use the udev by-id device specification. Disk labels don't
seem to work with LVM file systems (the udev stuff seems to configure
before the LVM stuff is set up).

udev configures the disk labels and other device naming (by-id,
by-uuid) aliases in /dev/disk. As far as I can tell, this is done
once at boot time. So if you partition a disk, the /dev/disk stuff
is not be configured for your new partitions until after you reboot.

labeling for ext3 and xfs file systems

For creating ext3 and xfs file systems, mkfs.ext3 and mkfs.xfs have
the -L option to specify the disk label that should be used. For
existing file systems, use e2label to label an ext2/ext3 file
system. And for xfs file systems, use xfs_admin. Both of these
commands can be used with the device to display the existing disk
label.

Examples of initializing new file systems with a label:

mkfs.ext3 -L ROOT /dev/sda1
mkfs.xfs -L BIGRAID /dev/sde

Examples of e2label and xfs_admin for existing files systems:

e2label /dev/sda1 PRIMARY_ROOT
e2label /dev/sda1

xfs_admin -L DATA1 /dev/sdf
xfs_admin /dev/sdf

labeling swap devices

You can label a swap device by using the mkswap -L label option.

mkswap -L SWAP0 /dev/sdb5

Alternative / by-id

Alternatively, you can use the udev by-id specification (look
in /dev/disk/by-id). The ID paths are usually pretty long
and less meaningful, but they are device specific and won't
change as a result of hardware changes. It's probably best to
use a disk label as above. However, for vfat/fat file systems
disk labels are not available so the by-id specification
should be used.

 /dev/disk/by-id/scsi-3500000e01632b7d0-part2 swap swap defaults 0 0

Examples of Use

Finally, the following are examples of using disk labels in
two key system files, fstab and grub.conf.

Example of /etc/fstab with disk labels:

LABEL=ROOT          /         ext3    defaults        1 1
LABEL=BOOT          /boot     ext3    defaults        1 2
LABEL=SWAP          swap      swap    defaults        0 0
LABEL=HOME          /home     ext3    nosuid,auto     1 2

Example of /boot/grub/grub.conf with disk labels:

title astrid CentOS primary system
  root (hd0,0)
  kernel (hd0,0)/vmlinuz ro root=LABEL=ASTRID_ROOT0 rhgb quiet
  initrd (hd0,0)/initrd-astrid.img

Summary

Linux systems support disk labels via the udev device manager. Using disk
labels avoids hard coding device names which can change if there's a
change in the hardware configuration (disk added/removed). This will result
in a more robust system.

Labels

bigadmin bigadmin Delete
sysadmin sysadmin Delete
disklabels disklabels Delete
linux linux Delete
filesystem filesystem Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

© 2010, Oracle Corporation and/or its affiliates
Powered by Atlassian Confluence
Oracle Social Media Participation Policy Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact