<<

LINUXKNOW-HOW USER SchlagwortHotplugging sollte hier stehen

Hotplugging with , HAL, and D-Bus HOTHOT WIREDWIRED

Hardware which just works is what every user wants. Current distributions go a long way to fulfilling that dream. In this article, we will be investigating how the hotplug system works. BY OLIVER FROMMEL, MARCEL HILZINGER AND RENÉ REBE

s it really that difficult? You only wanted Linux to launch the right manages the actions associated with a Iprogram when you attached your hotplug event. In this case, the agent new , but the operating manages the task of adding and register- which in turn start other agents. It is system has decided to sit this one out. ing the new device. common for multiple hotplug agents to This scenario is all too common, The steps the agent performs may work together. For example, when you although the situation has started to vary depending on your distribution and connect an external hard disk, first the improve. Linux should handle any kind the type of hardware you are installing. USB agent loads, then the SCSI agent of hardware correctly, but the ability to Since most hotplug-capable hardware loads to the individual partitions manage USB and Firewire devices attaches to a USB port, the USB agent is as SCSI devices with the help of the usb- plugged in or unplugged while the com- perhaps a good example. The USB agent storage module. If you attach a Bluetooth puter is running (known as hotplugging first checks if a driver is available for the dongle, the USB agent runs first, fol- [1]) has become increasingly important. new device (for example, isdn) and then lowed by a Bluetooth agent called blue- This article explains what a modern calls modprobe to load the module. If the tooth.agent. Linux system does with the devices you agent finds a script with the same name attach to it, and why not everything as the driver in the /etc/hotplug/usb/ Blacklisting Drivers works as planned. directory, the agent runs that script. The /etc/hotplug/blacklist file has a list The act of loading modules typically of modules that no agent is allowed to Agents at Work triggers a few more hotplug events, load. The list includes the modules that When you plug in a device that supports hotplugging, the kernel sends an event signal to the hot- plug system. The hotplug system uses Udev to create a for the device and then calls the Figure 1: After you modify the appropriate agent. An agent is a Udev configuration file, the script, typically stored below Gnome desktop icon has a Figure 2: The -device-manager with detailed information for the the /etc/hotplug/ directory, that more intuitive label. Prism2 WLAN adapter.

48 ISSUE 53 APRIL 2005 WWW.LINUX- MAGAZINE.COM Hotplugging KNOW-HOW

the system loads via other services, as the global rules, you will need to assign This stops the Gnome desktop from dis- well as modules that prevent power a filename to ensure that this happens, playing the generic sda1 label; instead management. for example 10-local.rules. The following the label is a more intuitive mp3disk Linux typically requires a device file in entry is good for a no-name MP3 stick: (Figure 1). The lsusb tool, which lists the order to interact with a device. In a hot- attached USB devices, will help you find plug situation, that device file must be BUS="usb", {idProduct}=U the USB IDs. If a device file exists, created on the fly when the new device "1000", SYSFS{idVendor}="10d6",U udevinfo -q path -n /dev/devicefile dis- is detected. As you learned in the pre- NAME="mp3disk" plays the path in the SysFS (see “Box2: ceding section, hotplug uses Udev to cre- Device files with Udev”), but it does not ate the device file prior to summoning give you the mount point /sys. You need the agent. the path as a parameter (-p), with this command to get SysFS information: Device Files as Needed Udev [1] is the designated successor to udevinfo -a -p /block/hda/hda1 static device files, and most current dis- ... tributions now use it. The purpose of SYSFS{idVendor}="10d6" Udev is to create device files based on ... rules specified in a Udev configuration file. For example, if you want to assign a This command helps you discover the device file other than sda1 to your MP3 device-specific values you need for spe- stick, you could specify this requirement cial configurations. There is a detailed in your own Udev rule. how-to for creating Udev files at [2]. The Udev rules are stored in /etc/udev/ Fedora site has a short overview of the rules.d. If you look in that directory, you Udev system [3]. will find a file that describes typical devices: in this is udev.rules, Your Own Usermaps whereas Fedora uses 50-udev.rules. Udev Figure 3: In Gnome 2.8, -volume- A usermap file contains one or multiple reads the files in alphabetic order. If you properties specifies which application is run IDs that uniquely identify a hardware want to load your own Udev rule before for which hotplug event. component. When a device that matches

Box 1: Hotplug on Suse Linux The Suse Linux approach to hotplugging also has them for hard disks, CD/DVD tion when you attach a memory stick, is different in parts, and the current ver- drives, and a variety of USB devices. but if you have disabled this feature, or if sion does not use the HAL architecture. After hwup has finished, the hotplug you simply the window, it’s back to It distinguishes between (currently) agent launches. In the case of a USB My Computer. unknown and configured devices and event, the agent will be the USB agent; Suse Linux uses its own icons for the dri- uses the hwup, hwdown, hwstatus, and for network events, the agent will be the ves:/ URL. They are located below /usr/ hwscanqueue programs to support hot- network agent; and so on. If the hotplug share/hotplug/DesktopTemplates/. KDE plugging. Suse stores a configuration system fails to find a suitable agent, it adds the name to the ~/./share/con- file in /etc/sysconfig/hardware for config- launches a generic agent to create the fig/kio_drivesrc file. You can edit the file ured devices. When the kernel registers required device files. In Suse Linux 9.2, to assign intuitive and unique names to a hotplug event, /sbin/hotplug loads the the agent also looks in /etc/sysconfig for your devices. For example, if you have appropriate kernel module. a configuration file for the service and two memory sticks, you can simply edit No HAL launches the appropriate service. the entries below [Used Names] to dis- After loading the kernel module, hwup Suse Linux has a simple approach to tinguish between them. checks if there is a configuration file for bug hunting that makes hotplug slightly To enable the device icons for the KDE the device below /etc/sysconfig/hard- more verbose. To enable this, set the desktop in Suse, you first need to install ware and, if so, loads the modules speci- variable HOTPLUG_DEBUG in the kdebase3-extra and kdemultimedia3- fied by the file. If hwup fails to locate a /etc/syconfig/hotplug to yes or even to extra packages. Take care if you have configuration file, it attempts to locate max. The latter setting tells the system Suse 9.2: the packages are only on the the required modules by reading the to log every single step. DVD version! After completing the *.usermap files in /etc/hotplug/, just as Desktop Icons on Suse Linux install, right click on the KDE desktop other distributions do. Suse Linux Version 9.1 or later does not and select Configure desktop… in the drop-down menu. In the Behavior win- In the future, Suse is looking to create a create desktop icons for drives. Instead, dow, select Device icons and then check file below /etc/sysconfig/hardware for users are expected to use the My Com- Enable icons on desktop. You can then every hardware component and to com- puter icon (just like in Windows) or to use the list to specify which device icons pletely do without usermap files. Com- access drives directly via the drives:/ you want to display on the desktop. paring the last two Suse versions shows URL. This solution can be a less than After installing kdebase3-extra and kde- that Suse is already heading in this perfect for USB memory sticks. By multimedia3-extra, the devices:/ URL will direction. Whereas 9.1 only creates con- default, Suse opens a Konqueror win- also work in Konqueror. figuration files for network devices, 9.2 dow with the content of the stick parti-

WWW.LINUX- MAGAZINE.COM ISSUE 53 APRIL 2005 49 KNOW-HOW Hotplugging

one of these entries is plugged into the prism2 0x0003 0x0846 0x4110 The script we want to run, prism2, system, the subsystem automatically 0x0000 0x0000 0x00 0x00 0x00 needs to be in the same directory, and runs the specified program, which can 0x00 0x00 0x00 0x0 we need to run chmod +x to make it be a script. executable. In our example, it runs the This allows you to enable a WLAN USB Most maps look like this and only use rc.wlan start script from the Prism2 adapter that your distribution does not the first four values. The first value spec- package, configures the wlan0 network properly configure. In our lab, the hot- ifies which program the hotplug system interface, and then asks the DHCP server plug system detected the adapter with should run if the values that follow in for an IP address: the Prism2 chipset, but it failed to run that line apply. The first numerical value the script that launches the required is a bit field that specifies the required #!/bin/sh WLAN functions. number of matching values. If you want /etc/rc.wlan start The lsusb command gives us the USB hotplug to check the first two values, /sbin/ifconfig wlan0 up ID for the plugged in device. The vendor you need 0x0003 . The first bit repre- /sbin/dhclient wlan0 ID in our example is 0x0846, and the sents a value of 1, the second is 2, and device ID is 0x4110. that makes 3 altogether. Hotplug ignores Following these changes, the USB WLAN You now need to add the following the remaining columns in this file, and adapter works immediately when hexadecimal values to a new file called that explains why the remaining values plugged in. Unfortunately, our attempts /etc/hotplug/usb/prism2.usermap: are 0x00. to configure a DV video camera in the

Box 2: Device Files with Udev In Linux, applications use device files to Block devices are located below /sys/ manpage has an example for IDE CD access hardware. These special files block and character devices below /sys/ ROMs that checks if a directory called below the /dev directory are defined by class. The major and minor numbers are /proc exists to identify the device as a CD reference to their type, as well as their stored in a dev file. For example, the fol- ROM: major and minor numbers, which link lowing command gives you the num- KERNEL="hd[a-z]", U them to the kernel. bers for the first IDE hard disk, hda: PROGRAM="/bin/cat /U In the past, this directory has been a cat /sys/block/hda/dev proc/ide/%k/media", U repository for all kinds of device files; for 3:0 RESULT="cdrom", U IDE and SCSI hard disks, USB, IEEE Udev can parse any SysFS information, 1394, and virtual devices. This meant such as the device class, name, numbers, NAME="%k", SYMLINK="cdrom%e" that the /dev directory typically had etc., to create appropriate devices. If the In this example, Udev calls /bin/cat for thousands of entries. names are stable, Udev can even run the /proc file for all devices with names This system has a few disadvantages: it complex programs to check whether it that start with hd. If file specifies cdrom does not tell you which devices really should configure a printer that has just as the medium, Udev will remember the exist or have been correctly identified by been switched on as /dev/usb/lp0 or /dev/ name but will additionally create a sym- drivers. Additionally, device files can usb/lp1, basing the decision on the serial bolic link for cdrom. The %e tells Udev to change from case to case; in other number of the printer. It is even possible select the next free number if a file of the words the random order in which the to use arbitrary names and call nodes, same name already exists. device was attached to the system /dev/lp-epson and /dev/lp-kyocera, for Using device serial numbers to assign decides which SCSI device will react to example. an intuitive name is a useful approach: /dev/sg2. Setting up Udev BUS="usb", SYSFS{serial}=U New Worlds Udev has two configuration options. "HXOLL0012202323480", U Files in /etc/udev/rules.d/ specify the Udev [1] is the designated successor to NAME="lp-epson" static device files, and most current dis- device file names, others in /etc/udev/permissions.d/ specify the This rule tells Udev to create a device tributions now use it. Udev relies on the file, /dev/lp-epson, if it finds a device hotplugging mechanism that generates privileges. The default rules create device files that use the familiar Linux with the above mentioned number in its device files as required. When a device serial file below the SysFS tree. changes, the kernel calls the program names. Udev and Privileges specified in /proc/sys/kernel/hotplug; this At the start of each rule, there are one or is typically /sbin/hotplug. more conditions that need to be fulfilled Rules for access privileges comprise a single line with colon-separated values Depending on the device type, the pro- if Udev is to create a device file. The for the name, owner, group, and privi- gram will load modules, modify access name then follows. The following is a leges. privileges, configure network devices, or typical entry for USB printers: in the case of Udev, manage device BUS="usb", KERNEL="lp[0-9]*", U usb/lp*:root:lp:0660 nodes. NAME="usb/%k" All device files called usb/lp* belong to The Udev subsystem needs some If the device is attached to the usb bus, the user root and the group lp. Access details to be able to create a file: the and the internal kernel name is lp with privileges are specified in octal notation. device type (char or block) and the major an arbitrary number, Udev creates a file The new hotplugging model is so suc- and minor numbers. Udev on kernel 2.6 with the kernel name (as indicated by cessful that it is even used to boot the or later references the sys filesystem the %k) in the /dev/usb directory. system. It calls udev with appropriate (SysFS, which is normally located below Besides static rules of this type, it is also variables for any known devices in /sys) to discover this information. possible to call external programs. The /sys/class and /sys/block.

50 ISSUE 53 APRIL 2005 WWW.LINUX- MAGAZINE.COM Hotplugging KNOW-HOW

same way failed due to the sorry state of of handling hardware details (see “Box months old. An online database for the Firewire subsystem. The IEEE1394 1: Hotplug on Suse Linux”). hardware components might help; users driver for the current kernel does not In the future, applications will be able could contribute details they had worked give us the SysFS information we need, to query hardware details via the D-Bus out in FDI format. so we have no alternative but to run [6]. D-Bus is a communication The HAL project is moving in this mknod to create a device file. system into which applications can slot direction, as it gives the hotplugging sys- and register themselves for certain tem information that the kernel can’t From the Hardware to the events. For example, a video editing pro- provide. A number of distributions have Application gram might need to know when a new already started using it. We’ll hope that Another layer of the hotplugging system camera is attached to the PC. The Suse will follow the trend; the more con- provides an interface between the hard- gnome-volume-properties program, sistent hardware management is in ware and the application. The Hardware which recent Gnome versions have, uses Linux, the better. ■ Abstraction Layer (HAL, [4]) has D-Bus and HAL to associate applications detailed information for hardware, with hotplug events (Figure 3). INFO which is stored in device information We can expect D-Bus to play an impor- [1] Linux Hotplug: files (.fdi). tant role in inter-application communi- http://linux-hotplug.sourceforge.net You can use the HAL layer to make cations under Gnome, although there are [2] Writing your own Udev rules: http:// changes for special devices. For exam- not many applications that leverage this www.reactivated.net/udevrules.php ple, one user solved the issue of his iPod ability at present. [3] Fedora Udev documentation: not de-registering correctly [5]. http://fedora.redhat.com/docs/udev FDI files are XML-formatted files that All for the Best – Someday [4] HAL: http://www.freedesktop.org/ provide detailed device descriptions. The Despite all the progress that has been Software/hal lshal command gives us the details. The made with respect to device detection, [5] iPod with Udev: hal-device-manager has the same data things are still not perfect. The hotplug http://www.kgarner.com/blog/ but with a neat GUI (Figure 2). Suse system agents need detailed hardware archives/2005/01/11/fc3-hal-ipod/ users will have to make do without HAL information, and this information will be [6] D-Bus: http://www.freedesktop.org/ Software/dbus components, as Suse has a different way obsolete on a system that is a few WaWantnt to to knowknow what’swhat’s upup next?next? SubscribeSubscribe to Linux Magazine Preview, our free monthly email newsletter!

WWW.LINUX-MAGAZINE.COM/NEWSLETTER