... {include:TOC for Tech Tips}
{column} {column:width=55%}
{panel:title=Disk Labels in Linux| borderStyle=dashed| borderColor=#ccc| titleBGColor=#F7D6C1| bgColor=#FFFFCE}
{panel}
by [Joseph H Kwan|http://wikis.sun.com/display/~rabbit6]
h2. 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_: {noformat} 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. {noformat}
*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.
h3. 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: {noformat} mkfs.ext3 -L ROOT /dev/sda1 mkfs.xfs -L BIGRAID /dev/sde {noformat}
Examples of *e2label* and *xfs_admin* for existing files systems: {noformat} e2label /dev/sda1 PRIMARY_ROOT e2label /dev/sda1
xfs_admin -L DATA1 /dev/sdf xfs_admin /dev/sdf {noformat}
h3. labeling swap devices You can label a swap device by using the *mkswap -L* label option. {noformat} mkswap -L SWAP0 /dev/sdb5 {noformat}
h3. 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.
{noformat} /dev/disk/by-id/scsi-3500000e01632b7d0-part2 swap swap defaults 0 0 {noformat}
h2. 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: {noformat} 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 {noformat}
Example of */boot/grub/grub.conf* with disk labels: {noformat} 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 {noformat}
h2. 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.
|