Persistent device names to insure proper Linux reboots


During the Linux boot process, disk devices are recognized by the kernel and accessed via a “virtual” block device file residing in the /dev directory. For example:

/dev/sda – first SCSI hard drive or CDROM
/dev/hda – first IDE hard drive or CDROM
/dev/sdb – second SCSI hard drive or CDROM
and so forth.

If device startup timing changes, the virtual device name assign to a physical device can change causing system boot and other OS failures.

udev to the rescue

To avoid this issue, udev was added to the Linux kernel to manage both persistent and non-persistent device names. In many Linux distributions, the /dev directory is a temporary file system that is created by udev every time the system boots.

The assignment of Persistent device names are controlled by udev rules defined in /etc/rules.d

In addition to non-persistent, virtual device names such as /dev/sda, udev creates persistent device names such as /dev/disk/by-id or /dev/disk/by-uuid. These could be unique UUID numbers or strings generated from the combination of hard drive vendor’s name and serial numbers. udev attempts to create a “permanent” description of a device at the kernel level.

By referring to devices using persistent device names we can avoid boot and OS configuration issues when servers are rebooted and non-persistent names shift.

To find the UUID assigned to each device name use the blkid command:

# blkid

WARNING: UUIDs are assigned when a file system is created on the physical or logical device. A UUID does not exist prior to the formating of the file system. For this reason, I prefer to use /dev/disk/by-id .vs /dev/disk/by-uuid name(s).

Using persistent naming

Persistent naming should be used for Linux boot and OS applications to avoid failures.

Boot managers

To use persistent names in your boot manager, the following prerequisites must be met:

  • You are using a mkinitcpio initial RAM disk image
  • You have udev enabled in /etc/mkinitcpio.conf

In our example, /dev/sda1 is the root partition. In the grub.cfg or menu.lst file, our server is configured to boot from /dev/sda1:

linux /boot/vmlinuz-linux root=/dev/sda1 rw quiet

Use one of the persistent naming schemes to change grub.cfg or menu.lst to:

linux /boot/vmlinuz-linux root=/dev/disk/by-label/root_myhost rw quiet

or

linux /boot/vmlinuz-linux root=LABEL=root_myhost rw quiet

or

linux /boot/vmlinuz-linux root=UUID=2d781b26-0285-421a-b9d0-d4a0d3b55680 rw quiet

# vi /boot/grub/menu.lst

	module	/boot/vmlinuz-2.6.16.60-0.33-xenpae	root=/dev/disk/by-id/cciss-3600508b1001035303620202020200003-part6	vga=0x317	resume=/dev/cciss/c0d0p5 splash=silent  showopts

	kernel	/boot/vmlinuz-2.6.16.60-0.33-bigsmp	root=/dev/disk/by-id/cciss-3600508b1001035303620202020200003-part6 vga=0x317 resume=/dev/cciss/c0d0p5 splash=silent  showopts

	kernel	/boot/vmlinuz-2.6.16.60-0.33-bigsmp	root=/dev/disk/by-id/cciss-3600508b1001035303620202020200003-part6 vga=normal showopts ide=nodma apm=off acpi=off noresume nosmp noapic maxcpus=0 edd=off 3

fstab

The /etc/fstab file can be used to define how disk partitions, various other block devices, or remote filesystems should be mounted into the filesystem.

Each filesystem is described in a separate line. These definitions will be converted into systemd mount units dynamically at boot, and when the configuration of the system manager is reloaded. The default setup will automatically fsck and mount filesystems before starting services that need them to be mounted. For example, systemd automatically makes sure that remote filesystem mounts like NFS orSamba are only started after the network has been set up. Therefore, local and remote filesystem mounts specified in /etc/fstab should work out of the box. The mount command uses fstab, if just one of either directory or device is given, to fill in the value for the other parameter. When doing so, mount options which are listed in fstab will also be used.

Examples

A simple /etc/fstab, using non-persistent device names:

/etc/fstab
# <file system>        <dir>         <type>    <options>             <dump> <pass>
/dev/sda1              /             ext4      defaults,noatime      0      1
/dev/sda2              none          swap      defaults              0      0
/dev/sda3              /home         ext4      defaults,noatime      0      2

/etc/fstab changed to use persistent device names:

# vi /etc/fstab

/dev/disk/by-id/cciss-3600508b1001035303620202020200003-part6    /                    ext3       acl,user_xattr        1 1 
/dev/disk/by-id/cciss-3600508b1001035303620202020200003-part8    /tmp                 ext3       acl,user_xattr        1 2 
/dev/disk/by-id/cciss-3600508b1001035303620202020200003-part7    /var                 ext3       acl,user_xattr        1 2 
/dev/disk/by-id/cciss-3600508b1001035303620202020200003-part5    swap                 swap       defaults              0 0

Field definitions

Each line in the /etc/fstab file contains the following fields separated by spaces or tabs:

file_system    dir    type    options    dump    pass
file system
The partition or storage device to be mounted.
dir
The mountpoint where <file system> is mounted to.
type
The file system type of the partition or storage device to be mounted. Many different file systems are supported: ext2, ext3, ext4, btrfs, reiserfs, xfs, jfs, smbfs, iso9660, vfat, ntfs, swap and auto. The auto type lets the mount command guess what type of file system is used. This is useful for optical media (CD/DVD).
options
Mount options of the filesystem to be used. Please note that some options are specific to filesystems; to discover them see below in the aforementioned mount man page.
dump
Used by the dump utility to decide when to make a backup. Dump checks the entry and uses the number to decide if a file system should be backed up. Possible entries are 0 and 1. If 0, dump will ignore the file system; if 1, dump will make a backup. Most users will not have dump installed, so they should put 0 for the dump entry.
pass
Used by fsck to decide which order filesystems are to be checked. Possible entries are 0, 1 and 2. The root file system should have the highest priority 1 (unless its type is btrfs, in which case this field should be 0) – all other file systems you want to have checked should have a 2. File systems with a value 0 will not be checked by the fsck utility.

Identifying filesystems

There are different ways to identify file systems that will be mounted. /etc/fstab does support several methods: kernel name descriptor, label or UUID, and GPT labels and UUID for GPT disks.

UUIDs

Run lsblk -f to list the partitions, and prefix the values in the UUID column with UUID=:

/etc/fstab
# <file system> <dir> <type> <options> <dump> <pass>
UUID=CBB6-24F2                            /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
UUID=0a3407de-014b-458b-b5c1-848e92a327a3 /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
UUID=b411dc99-f0a0-4c87-9e05-184977be8539 /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
UUID=f9fe0b69-a280-415d-a03a-a32752370dee none  swap   defaults                                                                                             0      0
If you would like to return just the UUID of a specific partition: $ lsblk -no UUID /dev/sda2

Kernel name descriptors

Run lsblk -f to list the partitions and prefix the values in the NAME column with /dev/.

/etc/fstab
# <file system> <dir> <type> <options> <dump> <pass>
/dev/sda1       /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
/dev/sda2       /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
/dev/sda3       /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
/dev/sda4       none  swap   defaults                                                                                             0      0

Labels

Run lsblk -f to list the partitions, and prefix the values in the LABEL column with LABEL=:

/etc/fstab
# <file system> <dir> <type> <options> <dump> <pass>
LABEL=EFI       /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
LABEL=SYSTEM    /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
LABEL=DATA      /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
LABEL=SWAP      none  swap   defaults                                                                                             0      0

GPT labels

Run blkid to list the partitions, and use the PARTLABEL values without the quotes:

/etc/fstab
# <file system> <dir> <type> <options> <dump> <pass>
PARTLABEL=EFI\040SYSTEM\040PARTITION /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
PARTLABEL=GNU/LINUX                  /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
PARTLABEL=HOME                       /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
PARTLABEL=SWAP                       none  swap   defaults                                                                                             0      0

GPT UUIDs

Run blkid to list the partitions, and use the PARTUUID values without the quotes:

/etc/fstab
# <file system> <dir> <type> <options> <dump> <pass>
PARTUUID=d0d0d110-0a71-4ed6-936a-304969ea36af /boot vfat   rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0      2
PARTUUID=98a81274-10f7-40db-872a-03df048df366 /     ext4   rw,relatime,discard,data=ordered                                                                     0      1
PARTUUID=7280201c-fc5d-40f2-a9b2-466611d3d49e /home ext4   rw,relatime,discard,data=ordered                                                                     0      2
PARTUUID=039b6c1c-7553-4455-9537-1befbc9fbc5b none  swap   defaults                                                                                             0      0

atime options

The use of noatime, nodiratime or relatime can impact drive performance.

  • The strictatime option updates the atime of the files every time they are accessed. This is more purposeful when Linux is used for servers; it does not have much value for desktop use. The drawback about the strictatime option is that even reading a file from the page cache (reading from memory instead of the drive) will still result in a write!
Using the noatime option fully disables writing file access times to the drive every time you read a file. This works well for almost all applications, except for a rare few like Mutt that needs such information. For mutt, you should only use the relatime option.
The nodiratime option disables the writing of file access times only for directories while other files still get access times written.
  • relatime enables the writing of file access times only when the file is being modified (unlike noatime where the file access time will never be changed and will be older than the modification time). The best compromise might be the use this option since programs like Mutt will continue to work, but you will still have a performance boost as the files will not get access times updated unless they are modified. This option is used when the defaults keyword option, atime option (which means to use the kernel default, which is relatime; see man 8 mount and [1]) or no options at all are specified in fstab for a given mount point.

Remounting the root partition

If for some reason the root partition has been improperly mounted read only, remount the root partition with read-write access with the following command:

# mount -o remount,rw /

Leave a comment