<<

Command Line: Using udev LINUXUSER DEVICEUsing udev JUNGLE

Learn how to create your own udev rules and deploy command-line

tools to monitor and control udev events to clear your way through www.sxc.hu

the device jungle. BY HEIKE JURZIK

n the past few years, Linux has pro- To keep everything running smoothly, udev on /dev type tmpfs (rw,mode gressed in the field of hardware de- udev needs rules that it references to dis- =0755) Itection and management. The days cover a device’s name and how to han- ... of static device files huddled below the dle the device. /dev directory are long gone, and Devfs Listing 1: Rules is also obsolete. Since kernel 2.6, udev Device Garage 01 $ ls -la /etc/udev/rules.d/ [1] has been responsible for handling The udev “repository” is the /dev direc- 02 ... both the device filesystem and Hotplug, tory, which is where the device files that which includes setting up the required allow applications to access hardware 03 lrwxrwxrwx 1 root root 20 device files, setting permissions, and reside. If you look inside the folder, you 2007-05-09 23:40 020_ optionally launching certain programs. will see not only the file names for your permissions.rules -> ../ hard disks and partitions (for example, permissions.rules sda, as in sda1, sda2), but also files for 04 lrwxrwxrwx 1 root root 19 In contrast to its predecessors, udev runs CD/ DVD drives (a symlink to cdrom typ- 2007-05-10 00:39 025_ in user space – that is, as a “normal” ically points to the “genuine” device, libgphoto2.rules -> ../ program. This has a number of advan- e.g., hdc), serial ports (e.g., ttyS0, ttyS1), libgphoto2.rules tages, including freeing the kernel from and so on (see Figure 1). 05 lrwxrwxrwx 1 root root 16 the responsibility of management and The stores the con- 2007-05-10 00:39 025_libsane. naming attached devices, which facili- tent of the /dev folder in a temporary rules -> ../libsane.rules tates configuration. filesystem, as you can see by listing the 06 ... Udev has more benefits for users; for currently mounted RIS: example, you can choose your own de- 07 lrwxrwxrwx 1 root root 12 vice names, and hardware assignments $ mount 2007-05-10 00:36 z99_hal.rules are always unique. ... -> ../.rules

FEBRUARY 2008 ISSUE 87 91

091-093_command.indd 91 13.12.2007 16:39:24 Uhr LINUXUSER Command Line: Using udev

Creating your own udev rules, thus ex- tending hardware detection functional- ity, is fairly easy. If you use external USB memory devices, they are assigned dif- ferent names depending on the order you plug them in. In this case, I’ll as- sume you have a hard disk on which you regularly back up your data. Saving Steps Of course, you could plug in the disk, wait until it is correctly mounted, then discover the mount points and launch a backup script to complete the job. Thanks to udev, you can save a couple of these steps. To do so, you need to cre- ate a rule that automatically assigns the same device name to the hard disk. If you like, you can add a mount command to the rule or run a shell script to launch the backup. Figure 1: The Linux device garage is located in the /dev folder. First, you need some information about the device; you can use the lsusb Linux always sets up the files in /dev at behavior. Some distributions, such as (Listing 2) program for this. Make a note boot time, but it also sets up a file when Debian, use symbolic links in the latter of the details that follow idVendor, id- a new device is attached. It only makes directory to point to the rules in Product, or iSerial, which you need to sense that a device name will disappear /etc/udev/. uniquely identify the device. when you remove the matching hard- As you can see from Listing 1, rules ware. What keeps the directory clean are enumerated or start with a letter be- Creating Your Own Rule and dynamic, while avoiding user con- cause udev works through the directory Now, you need to write a new rule file. fusion over changing device names, are in alphabetic order when an event oc- The file will reside in /etc/udev/rules.d. the udev rules. curs. This method ensures that critical Working as root, launch your favorite rules are processed first. The last port of editor to create the file. Udev Rules! call is normally the HAL . (Udev Use a unique number to insert the rule Various rule files, located in /etc/udev/ notifies the daemon of the new device.) into the existing sequence, and add a and /etc/udev/rules.d/, control udev’s If you take a closer look at a rule file, .rules prefix. On my Debian Etch test you will discover one or multiple in- system, I assigned the name z98_usb- GLOSSARY structions in a single line (without line platte.rules for the file and then added breaks). The instructions contain vari- the following content: Devfs: The predecessor to udev. Up to kernel 2.4, the device filesystem was re- ous comma-separated conditions and sponsible for managing files in the /dev control commands. SUBSYSTEM=="block", {idProd directory. Devfs tasks included creating Shell-style wildcards, such as *, ?, [...], uct}=="2338", SYSFS{idVendor}==" and deleting device files for removable and so on, are supported. Additionally, 152d", NAME="backup" media. On top of this, Devfs was respon- various operators are typically used (see sible for rights management and thus Table 1). Table 2 shows you an overview Apart from saying that the device is a controlled user access to devices. of frequently used constants and vari- block device, the rule states the idProd- Hotplug: The Hotplug system controls ables. The man page (man udev) also uct and idVendor information discovered hardware initialization, which means has a full list of operators, constants, by running lsusb and then specifies the any hardware discovered in the course and variables. name of the . of the boot as well as devices that are added and removed at run time. Table 1: Operators User space: Memory area reserved for Operator Type Meaning user applications (i.e., for programs and == Comparison True if the two expressions are identical data that are not directly related to the != Comparison True if the two expressions are not identical kernel.) = Assignment Assigns the value on the right to the expression on HAL: Hardware Abstraction Layer. The the left HAL daemon provides the details (e.g., := Assignment Like =, protects the left-hand side against later driver information, drive types, and so changes on) of available devices to programs. += Assignment Adds the value on the right to that on the left

92 ISSUE 87 FEBRUARY 2008

091-093_command.indd 92 13.12.2007 16:39:40 Uhr Command Line: Using udev LINUXUSER

Listing 2: lsusb A quick check of the /dev directory re- rule, you would then replace "/bin/ veals that udev really has created a /dev/ mount ... with the path to the backup 01 $ lsusb -v backup device file, just like the doctor script: 02 ... ordered. If you unplug the USB disk, the U 03 Bus 003 Device 010: ID entry disappears again. ..., RUN+="/home/chicken/bin/ backup.sh" 152d:2338 Mount or Script? 04 Device Descriptor: As Table 2 shows, you can use RUN to Fighting NIC Chaos 05 ... define the program to execute in your If your computer has a single Ethernet 06 idVendor 0x152d rule file. card, the card will typically go by the 07 idProduct 0x2338 If you want to mount /dev/backup in name eth0. If you add a second card, /media/backup in one fell swoop, you such as a WLAN device to the machine, 08 bcdDevice 1.00 can extend the rule like this: the card could be ath0 or eth1 depending 09 iManufacturer 1 on the driver. If you prefer your network 10 iProduct 2 SUBSYSTEM=="block", SYSFS{idProd cards to use the same names, you could 11 iSerial 5 uct}=="2338", SYSFS{idVendor}==" create a rule to enforce this behavior. In- 152d", NAME="backup", RUN+="/bin idProduct idVendor 12 bNumConfigurations 1 stead of or , type the /mount /dev/backup U MAC address for your NIC, as shown 13 ... /media/backup" after SYSFS{address} here. To discover the MAC, you can run ifconfig: Running the command Make sure you enter the pull path to the program (/bin/mount in this case), $ /sbin/ifconfig udevcontrol reload_rules but you need to create the mount point eth0 Protocol:Ethernet Hardwa /media/backup with the mkdir com- re Address 00:10:A7:21:36:7C as root reparses the rules. Alternatively, mand (working as root) first: ... you can unplug your USB device and ath0 Protocol:Ethernet Hardwa then plug it back in. The udevmonitor $ mkdir /media/backup re Address 00:00:C0:77:D8:F5 tool shows you what is going on behind ... the scenes. In combination with the Then, you can launch the backup pro- --env option, the tool becomes more ver- gram manually. As an alternative, you Listing 3 shows you how to create a bose and shows various details related could create an entry for the device in matching rule. I to the rule on the test machine: your /etc/fstab file as follows: Listing 3: Matching Rule # udevmonitor --env /dev/backup /media/backup ext3 u 01 KERNEL=="eth*", ... sers,atime,noauto,rw,nodev,exec, SYSFS{address}=="00:10: ACTION=add nosuid 0 0 A7:21:36:7C", NAME="eth0" DEVPATH=/block/sda/sda1 02 KERNEL=="eth*", SUBSYSTEM=block This would let you add the mount SYSFS{address}=="00:00:C0:77: ... command to the backup script to make D8:F5", NAME="wlan0" DEVNAME=/dev/backup sure that the external disk is mounted ... before backing up your data. In the udev INFO Table 2: Udev: Constants and Variables [1] Udev homepage: http:// www. kernel. Name Meaning org/ pub/ linux/ utils/ kernel/ hotplug/ ACTION Event, either add or remove udev. html SUBSYSTEM Device type – for example, usb_device, block, and so on BUS Device bus system – for example, ieee1394 or usb ID Device ID (in relation to the bus) Heike Jurzik studied NAME Name of network device (eth0, eth1, etc.) or the device file in /dev German, Computer Science and English KERNEL Device name according to the kernel at the University of SYSFS{Datei} Uses information from various files in /sys (containing hardware details); Cologne, Germany. maximum of five SYSFS constants per rule She discovered OWNER Owner (user name or UID) Linux in 1996 and GROUP Group (name or GID) has been fascinated MODE Permissions (octal number) with the scope of the Linux com- RUN Executes the specified program

THE AUTHOR mand line ever since. In her leisure SYMLINK Creates a symbolic link that points to the device name proper time you might find Heike hanging GOTO Jumps to a specified label out at Irish folk sessions or visiting LABEL Name of label Ireland.

FEBRUARY 2008 ISSUE 87 93

091-093_command.indd 93 13.12.2007 16:39:43 Uhr