Card Readers and USB keys using udev

Posted by chris on Sun 24 Apr 2005 at 21:08

I have three different memory card readers - each providing access to 8 or 9 different card formats. In addition I have a MobileDisk USB keyring. Debian supports these quite well out of the box but the device node changes with each time you plug and unplug the devices - is my CF reader /dev/sdd1 or /dev/sde1?

While you can look this up each time it makes it hard to set up fstab so that non-root users can mount the device.

Background

This article is written based on a debian unstable installation using a 2.6 kernel. It should work just fine on sarge - but you will need to be running 2.6. It is very much a "what-I-did" article :)

Under the 2.4 kernel this was to some extent changed by the implementation of devfs. However - the devfs design has some problems - so for 2.6 udev is available.

Two of the card readers I have are Lexar - the third is a Unomas omnicard reader. This article will concentrate on the Lexar readers - since the Unomas reader does not report the necessary information for sysfs (and so I couldn't distinguish between the card slots - if anyone gets this working please comment this article).

Getting udev

Simply run

apt-get install udev
as root. It is strongly advised that you restart the machine after this (sadly)

Am I running udev?

Have you got a udevd process (ps aux | grep udevd)? Earlier versions of udev also created a /.dev directory - at least on the two sid boxen I have this is no longer present.

Using udev to generate a known device

Well - first we need to find out how the device identifies itself.

Run the following and then plug in the device

tail -f /var/log/messages

For the Lexar reader I see

Apr 22 13:52:18 localhost kernel: usb 1-2: new full speed USB device using uhci_hcd and address 6
Apr 22 13:52:18 localhost kernel: scsi3 : SCSI emulation for USB Mass Storage devices
Apr 22 13:52:19 localhost usb.agent[25897]:      usb-storage: already loaded
Apr 22 13:52:23 localhost kernel:   Vendor: Lexar     Model: USB Storage-SMC   Rev: I18A
Apr 22 13:52:23 localhost kernel:   Type:   Direct-Access                      ANSI SCSI revision: 00
Apr 22 13:52:23 localhost kernel: Attached scsi removable disk sda at scsi3, channel 0, id 0, lun 0
Apr 22 13:52:23 localhost kernel:   Vendor: Lexar     Model: USB Storage-CFC   Rev: I18A
Apr 22 13:52:23 localhost kernel:   Type:   Direct-Access                      ANSI SCSI revision: 00
Apr 22 13:52:23 localhost kernel: Attached scsi removable disk sdb at scsi3, channel 0, id 0, lun 1
Apr 22 13:52:23 localhost kernel:   Vendor: Lexar     Model: USB Storage-SDC   Rev: I18A
Apr 22 13:52:23 localhost kernel:   Type:   Direct-Access                      ANSI SCSI revision: 00
Apr 22 13:52:23 localhost kernel: Attached scsi removable disk sdc at scsi3, channel 0, id 0, lun 2
Apr 22 13:52:23 localhost kernel:   Vendor: Lexar     Model: USB Storage-MSC   Rev: I18A
Apr 22 13:52:23 localhost kernel:   Type:   Direct-Access                      ANSI SCSI revision: 00
Apr 22 13:52:23 localhost kernel: Attached scsi removable disk sdd at scsi3, channel 0, id 0, lun 3
Apr 22 13:52:23 localhost scsi.agent[25951]:      sd_mod: loaded sucessfully (for disk)
Apr 22 13:52:23 localhost scsi.agent[25973]:      sd_mod: loaded sucessfully (for disk)
Apr 22 13:52:23 localhost scsi.agent[25975]:      sd_mod: loaded sucessfully (for disk)
Apr 22 13:52:23 localhost scsi.agent[25941]:      sd_mod: loaded sucessfully (for disk)

This shows me that the device is located on the disks /sda, /sdb, /sdc and /sdd (it has 4 reader slots) - however - next time this could be /sde, /sdf etc). More importantly - it shows some information that sysfs (/sys) knows about - The Vendor and Model lines. This information will be repeated within the /sys sysfs file system in files called vendor and model ...

... and udev can use it to recognize the devices. Udev installs a set of config files under /etc/udev. Part of this is /etc/udev/rules.d/ - each file here will be processed to generate the ruleset that udev will use to determine device names for each device.

By adding a new file (local.rules) in this directory you can add new rules (it's a standard .d directory - the files will be read in alphanumeric order to create the rules that udev will use). The rules for the above card reader (one per card type) look like:

BUS="scsi", SYSFS{model}="USB Storage-SMC", NAME{all_partitions}="card_sm"
BUS="scsi", SYSFS{model}="USB Storage-CFC", NAME{all_partitions}="card_cf"
BUS="scsi", SYSFS{model}="USB Storage-SDC", NAME{all_partitions}="card_sd"
BUS="scsi", SYSFS{model}="USB Storage-MSC", NAME{all_partitions}="card_ms"

Things to note

Restarting udev then plugging in the device should therefore result in the creation of (amongst several others) /dev/card_sm1, /dev/card_cd1, /dev/card/sd1 and /dev/card_ms1 - the first partition on each device. These names are the same whatever scsi host the reader has been allocated to. This means that in /etc/fstab you can add something like

/dev/card_sd1   /media/sd       vfat    rw,user,noauto  0       0
/dev/card_cf1   /media/cf       vfat    rw,user,noauto  0       0
/dev/card_sm1   /media/sm       vfat    rw,user,noauto  0       0
/dev/card_ms1   /media/ms       vfat    rw,user,noauto  0       0

And users can then "mount /media/cf" for themselves.

The MobileDisk USB key is slightly different. This has a model of "MOBILE Disk " (including spaces). However - when plugged in it generated /dev/sdx and /dev/sdx1 (where x is the letter for the scsi host that was allocated. This means that we have the partition mount point. We simply need to rename it.

BUS="scsi", SYSFS{model}="MOBILE Disk     ", KERNEL="sd?1", NAME="%k", SYMLINK="mobiledisk"

Things to note

Then you can have an /etc/fstab line that looks like

/dev/mobiledisk   /media/mobiledisk       vfat    rw,user,noauto  0       0

Where next?

A following article will cover getting autofs to automount these devices - giving plugin/use/remove functionality to linux.


This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2005 chris - please ask for permission to republish or translate.