• No results found

User-Defined Device Names

The/dev/directory contains virtual files that represent devices. Each virtual file represents a device for the system such as a storage device, USB device, or printer. These virtual files are called device names.

Device names for IDE devices begin withhd, and device names for SCSI devices begin withsd. The prefix is followed by a letter, starting with a, that represents the drive order. For example,/dev/hda is the first IDE hard drive,/dev/hdbis the second IDE hard drive,/dev/hdcis the third IDE drive, and so on.

If the device name is followed by a number, the number represents the partition number. For example, /dev/hda1represents the first partition on the first IDE drive.

If a hard drive is physically moved to a different location in the machine, is removed, or fails to initialize, some of the device names will change, potentially leaving device name references invalid.

For example, as shown in Figure 7-1, if a system has three SCSI hard drives, and the second SCSI hard drive is removed,/dev/sdcbecomes/dev/sdb, causing any references to/dev/sdcto become invalid and any references to/dev/sdbinvalid as well since it is a different drive.

/dev/sda /dev/sdb /dev/sdc

/dev/sda /dev/sdb

Figure 7-1. Removing a Hard Drive

Every hard drive has a unique identifier associated with it, called a UUID. To solve the problem of changing device names,devlabelallows for user-defined device names that are associated with these UUIDs. A symbolic link is created from the user-defined device name to the actual device name. If the actual device name changes, the symbolic link is updated to point to the same drive according to its UUID. Thus, both IDE and SCSI storage devices can be referenced by their user-defined names.

Devlabelalso allows for automatically mounting hotplug devices such as removable hard drives and USB devices such as memory cards for digital cameras. If configured to mount automatically, after the device is plugged in, it is mounted with the user-defined device name.

7.1. Configuring

Devlabel

User-defined device names can be added based on the device name, partition name, or the UUID of the drive.

Use the following syntax to add a user-defined device name for a storage device. The device specified can be the entire device or a single partition on a device.

28 Chapter 7. User-Defined Device Names

devlabel add -d  device -s  symlink

For example, to add the symbolic link/dev/workto represent the/dev/hdb1partition, use the following command:

devlabel add -d /dev/hdb1 -s /dev/work

If the command was successful, the following is displayed:

Created symlink /dev/work -> /dev/hdb1 Added /dev/work to /etc/sysconfig/devlabel

To add a device name for a device based on a UUID, use the following syntax:

devlabel add -u  uuid -s  symlink

To usedevlabelto retrieve the UUID for a device (or to make sure it has one), use the following command:

devlabel printid -d  device

The symbolic link names must be unique. If an existing link already exists when an attempt is made to add it, the configuration file is not modified, and the following is displayed:

The file /dev/work already exists.

Failure. Could not create a symlink.

To remove a symbolic link from thedevlabellist, use the following command:

devlabel remove -s  symlink

The entry is removed from the configuration file, and the symbolic link is deleted.

To determine the status of thedevlabelsymbolic links, use the following command:

devlabel status

It returns output similar to the following:

lrwxrwxrwx 1 root 9 Apr 29 13:20 /dev/work -> /dev/hdb1 lrwxrwxrwx 1 root 9 Apr 29 13:41 /dev/tcf -> /dev/hda1

7.1.1. Hotplug Devices

A program called hotplug performs actions when a system event, such as hardware being added or removed, takes place while the system is running. For example, if a USB hard drive or a USB media card reader is attached to the system,hotplugnotifies users by logging a message in the system log file (/var/log/messages) and loads the proper kernel modules so the device works.

When a PCI, USB, or IEEE 1394 (also known as FireWire) device is plugged in, thehotplugscripts also restartdevlabelso that the removable storage media receives a user-defined device name (such as/dev/usbcard), and optionally it can automatically mount the storage device.

After inserting the USB card reader into the USB port of the computer, issue the following command as root (where/dev/sda1is the device name for the media card and/dev/usbcardis the user-defined device name to use):

devlabel add -d /dev/sda1 -s /dev/usbcard --automount

Chapter 7. User-Defined Device Names 29

This commands adds an entry for the mount point to/etc/sysconfig/devlabeland creates a symbolic link from/dev/usbcardto/dev/sda1. The--automountoption todevlabelspecifies that the device should be automatically mounted whendevlabelrestarts if an entry for it is located in/etc/fstaband if the device exists (a device with the same UUID is found).

Theupdfstabis a program that scans the IDE and SCSI buses for new devices and adds entries to /etc/fstabfor them if entries do not already exist. It also adds entries for USB devices since they appear as SCSI devices. Refer to theupdfstabman page for more information.

When a USB device is inserted,hotplugruns theupdfstabprogram, which adds an entry to /etc/fstabfor the storage device (such as the media card) if it exists. (If a card reader without a card in it is inserted, an entry is not added.) The line added contains the actual device name (such as/dev/sda1) and thekudzuoption. Thekudzuoption tellsKudzu1that it can remove the line if the device does not exist. Since the line is required bydevlabel, thekudzuoption must be removed so the line remains in the file. Also change the device name to thedevlabeldevice name (such as /dev/usbcard) and create the mount point (such as/mnt/usbcard).

After modifying the line, it should look similar to the following:

/dev/usbcard /mnt/usbcard auto noauto,owner 0 0

Because of--automount, whendevlabelis restarted, the storage media in the USB card reader device is mounted in/mnt/usbcardwhen the USB device is plugged into the computer. The trick is that when the USB card reader is plugged into the computer, the card must already be in the reader. If not,devlabelcan not find the storage device, and thus it can not automatically mount it.

If the USB card reader is already plugged in without a card, when the card is inserted, run the com-manddevlabel restartas root to mount the media card.

7.2. How It Works

The commanddevlabel restartis called from the/etc/rc.sysinitscript when the system is booted as well as by the appropriate scripts in the/etc/hotplug/directory.

The restart option to devlabel reads the list of devices in the configuration file (/etc/sysconfig/devlabel) and follows the symbolic links to determine if the device still exists in its former location, such as/dev/hdb1. If the symbolic link is invalid, an attempt is made to find the new location of the disk based on the disk’s UUID. If a disk with the same UUID is found, the symbolic link is updated to point to the new location of the drive, the configuration file is updated with the new location, and a message similar to the following is displayed:

Device name incorrectly detected for symlink /dev/work!

The device /dev/hdb1 is now /dev/hdd1.

The symlink /dev/work will now point to the new device name.

If a disk with the UUID is not found (for example, the disk was removed), the following is displayed:

The device /dev/hdb1 no longer seems to exist. Because of this, the symlink /dev/work -> /dev/hdb1 will not be available. The reference to this symlink in /etc/sysconfig/devlabel will be ignored.

The entry for the device is not removed from the configuration file; it is just ignored for this instance.

1. Kudzu is a hardware probing tool run at system boot time to determine what hardware has been added or removed from the system.

30 Chapter 7. User-Defined Device Names

7.3. Additional Resources

For more information concerningdevlabel, refer to these resources.

7.3.1. Installed Documentation

man devlabel— The man page fordevlabeldiscusses all of the options and includes a brief description of how it works.

man updfstab— The man page for theupdfstabprogram, which is called byhotplugwhen a USB device is inserted.

man hotplug— the man page forhotplug.

7.3.2. Useful Websites

http://www.dell.com/us/en/esg/topics/power_ps1q03-lerhaupt.htm — In Resolving Device Renam-ing Issues in Linux, the developer who wrote thedevlabelprogram explains how it works.

http://www.lerhaupt.com/devlabel/devlabel.html — The developer’s project page.

Chapter 8.