how I got usb sticks and card reads mounted/unmounted automatically (using udev)
First of all, here is what I have installed:
#> dpkg -l gnome-volume-manager gnome-desktop* udev hotplug | grep '^ii'
ii gnome-volume-m 1.0.2-3 GNOME daemon to auto-mount and manage
media
ii gnome-desktop- 2.6.1-2 Common files for GNOME 2 desktop apps
ii gnome-desktop- 60 The GNOME Desktop Environment
ii udev 0.040-1 /dev/ management daemon
ii hotplug 0.0.20040329-1 Linux Hotplug Scripts
To get icons of devices that you have listed in your /etc/fstab (whether
they be USB sticks, card readers or anything else) you need
gnome-volume-manager (KDE has something similar, but I have not
experimented with that). I have tried the following method successfully
in Debian Sarge and Sid.
Now to automatically detect if a device is connected hot (USB stick,
card reader, etc.) you need udev. Then to make udev make a /dev/<file>
for the device that you connect, you need to tell udev what to look for.
This is done in a rules file in /etc/udev/rules.d/. This directory can
have as many rules as as you need. I have made one single rule file for
all my gadgets:
> ls -l /etc/udev/rules.d/
total 1
-rw-r--r-- 1 root root 930 2004-10-30 17:22 udev_hs.rules
lrwxrwxrwx 1 root root 13 2004-10-18 12:10 udev.rules -> ../udev.rules
Here the udev.rules link is the one that is there by default. The other,
udev_hs.rules is the rules file that I created (yes, it is just a text
file).
Creating the Rules file:
To create the rules file, you need to know about what information udev
detects from the device that is connected. This is very nicely explained
here: http://www.reactivated.net/udevrules.php (a must read).
Using this information, I made this file:
#-----------------------------------------------------
> cat /etc/udev/rules.d/udev_hs.rules
#Rules created by HS,29Oct,2004
#In these rules, whatever devices I create, I give them a prefix of
"hs-" just
#to keep them separate from system devices. Not sure if this is
necessary but
#just in case ...
#to find out info a USB device, plug it in any USB port of laptop and give
#the command "udevinfo -a -p /sys/block/sd[ab]" where [ab] means either
a or b
# or appropriate character here.
#the following rule is for the Lexar 256MB jump drive
BUS="usb", KERNEL="sd*", SYSFS{product}="JUMPDRIVE ELITE",
NAME="hs-jd-%k", SYMLINK="hs-jd-usb%n"
#rule for the SanDisk ImageMat 6-in-1 memory cards reader
# the '1' at the end is because the cards are sda1, sdb1, e.g.
#The "?" in ID field is becuase kernel give the device ID's in that pattern,
#this was observed by trying out "udevinfo -a -p /sys/block/sda" command
with
#sda, sdb, sdc and sdd.
BUS="scsi", ID="*:0:0:0", SYSFS{model}="ImageMate 6 in 1",
NAME="hs-usb-cr-%k"
#-----------------------------------------------------
This file essentially is for two devices, the first one is the Lexar
Jumpdrive and the second one is a SanDisk 6-in-1 card reader.
The first one was simple, I just plugged in the jump drive and checked
what "udevinfo -a -p /sys/block/sd[abc..]" said ([abc..] mans try 'a'
and if that doesn't work then try 'b' if not c and so on.). I picked the
USB section (that is why there is BUS="usb") of the output. So the first
rule says (my rough translation):
a. On the BUS=usb
b. Look for kernel message of "sd*" where * means anything
c. If you see SYSFS{product}="JUMPDRIVE ELITE" then make a file in /dev
with the name of hs-jd-%k, where %k is replaced by sda, sdb, sdc or
whatever the kernel detected the drive as.
d. Make a symlink named hs-jd-usb%n which points to the file made in
step (c) above. (okay, not sure why I put %n there, but I guess I was
experimenting with multiple paritions in one USB stick and you may not
realy need that if you have just one partition on your jump drive).
So, if I now plug in my Lexar Jumpdrive, I get:
> udevinfo -a -p /sys/block/sda
<SNIP><SNIP>
looking at the device chain at
'/sys/devices/pci0000:00/0000:00:1f.2/usb1/1-1':
BUS="usb"
ID="1-1"
SYSFS{bConfigurationValue}="1"
SYSFS{bDeviceClass}="00"
SYSFS{bDeviceProtocol}="00"
SYSFS{bDeviceSubClass}="00"
SYSFS{bMaxPower}="100mA"
SYSFS{bNumConfigurations}="1"
SYSFS{bNumInterfaces}=" 1"
SYSFS{bcdDevice}="2000"
SYSFS{bmAttributes}="80"
SYSFS{detach_state}="0"
SYSFS{devnum}="3"
SYSFS{idProduct}="a400"
SYSFS{idVendor}="05dc"
SYSFS{manufacturer}="LEXAR MEDIA"
SYSFS{maxchild}="0"
SYSFS{product}="JUMPDRIVE ELITE"
SYSFS{serial}="0A4EEC11010415071004"
SYSFS{speed}="12"
SYSFS{version}=" 2.00"
<SNIP><SNIP>
And I get these files created in /dev:
> ls -1 /dev/*hs*
/dev/hs-jd-sda
/dev/hs-jd-usb
Why do I need the symlink? Because depending on other USB devices
connected to my computer, the drive could have been detected as sdb,
sdc. etc. No matter what it is detected as, as long as it is "sd*" it
will have a symlink hs-jd-usb pointing to it. This symlink will be used
in /etc/fstab to make a mount point for the USB stick.
Making a Mount Point in /etc/fstab
Now I have this line in /etc/fstab to tell gnome-volume-manager about
the usb stick mount point:
#the following it to mound USB Jump drive from udev created symlinks
/dev/hs-jd-usb /media/usbstick auto rw,user,noauto 0 0
Of course, I also have the /media/usbstick directory. And lo and behold,
gnome-volume-manager mounts the USB stick and shows an icon on the
desktop (the icons is named as "usbstick", same as the mount point). If
you pluck out the USB stick, it is unmounted and the icon dissappears
from the desktop. Pretty neat, eh.
Now, the second rule I have in my rules files is for the 6-in-1 SanDisk
card reader. It has 4 slots in all and each one seems to be seen as a
different parition at least. BUS="usb" didn't give me enough
information(or maybe I didn't look hard enough). I did notice that in
BUS="scsi" I was getting a unique ID="*:0:0:0" for each slot and also a
SYSFS{model}="ImageMate 6 in 1". Armed with this information I made the
rule in rules file (see above) and I made these lines in /etc/fstab:
/dev/hs-usb-cr-sda1 /media/card-cf auto
noauto,user,rw,sync,noatime,umask=0077 0 0
#/dev/hs-usb-cr-sdb1 /media/card-sm auto
noauto,user,rw,sync,noatime,umask=0077 0 0
#/dev/hs-usb-cr-sdc1 /media/card-sd auto
noauto,user,rw,sync,noatime,umask=0077 0 0
/dev/hs-usb-cr-sdd1 /media/card-ms auto
noauto,user,rw,sync,noatime,umask=0077 0 0
along with the corresponding mount point directories in /media.
Here I have only used sda1 and sdd1 until now so the other two are
commented out (I don't have other kind of memory cards). If I plug in
the card reader and put in a CF card, I get an icon on my desktop. Pluck
out the card and the icon dissappears.
However, with udev, you can get some rare quirks. For example, in my
case, ppp_generic has to be mentioned in /etc/modules (I am using 2.6.7
kernel, remember?) otherwise ppp0 connection fails to get fired up at
boot time and has to be done using pppoeconf.
If you have questions, feel free to ask.
GL,
->HS
Reply to: