Linux tip, Fedora tip / howto: automatically mounting a USB key

Note that these tips are mostly outdated

back to notes and tips index

Try my online puzzle page with Calcudoku, Killer Sudoku and online Sudoku.

automatically mounting a USB key

Apparently this is possible using the hal daemon, pmount and/or gnome-volume-manager. Because I'm running a lean configuration (not running the hal daemon, for example), I was looking for the minimal requirements (all steps as root):
  • create a mount point, for example mkdir /usbkey
  • add the corresponding entry to /etc/fstab:
    /dev/usbkey1 /usbkey vfat noauto,user,noatime,rw,uid=500 0 0
    For the uid number, fill in your own user id (type id to get it)
  • plug in the USB key, after a few seconds type: dmesg | tail to see which device name it got. It'll say, for example:
    Attached scsi removable disk sde at scsi1, channel 0, id 0, lun 0
    In this case, the name is sde
  • run the command udevinfo -a -p /sys/block/sde | more. You'll see blocks of information preceded by "looking at the device chain at .." Find the block that has a line like SYSFS{product}=="USB Mass Storage Device" (for later versions of udev, 098 and up, replace SYSFS with ATTRS)
  • from this block only, copy the information for SYSFS{idVendor}, SYSFS{idProduct}, and SYSFS{serial} (the goal here is to get some uniquely identifying information for your USB key) (for later versions of udev, 098 and up, replace SYSFS with ATTRS)
  • create the file /etc/udev/rules.d/usbkey.rules:
    KERNEL=="sd[a-z][0-9]", SYSFS{idProduct}=="0161", SYSFS{idVendor}=="126f", SYSFS{serial}=="12341a2b3c4d5f", NAME="%k", SYMLINK="usbkey%n"
    Fill in the values you found for the product id, vendor id, and serial number. (for later versions of udev, 098 and up, replace SYSFS with ATTRS) This will make sure that udev creates a symlink /dev/usbkey1 once the key is inserted (it will create a symlink for each partition on the USB key, my key has 2 partitions for example, so dev/usbkey1 and /dev/usbkey2 will be created. Add mount point(s) and modify /etc/fstab accordingly if your USB key has multiple partitions)
  • for older versions of Fedora (tested and working under Fedora Core 4): create the file /etc/dev.d/block/ (you may have to create the block subdir): #!/bin/bash base=$(basename $DEVNAME) logger -t "`env`" if [[ $base != sd[a-z][1-9] ]] then exit fi if [ "$ACTION" == "add" ] then target=$(for i in $(find /dev/ -type l); do file $i | grep $base | cut -d: -f1 ; done) mount $target mpoint=$(cat /etc/mtab | grep $DEVNAME | cut --delim=' ' -f2) echo "$DEVNAME:$target:$mpoint" > /tmp/$base elif [ "$ACTION" == "remove" ] then target=$(cat /tmp/$base | grep $DEVNAME | cut -d: -f3 ) umount -l $target rm -f /tmp/$base fi (this script copied from this page)
  • for newer versions of Fedora (tested and working under Fedora 8): apparently the scripts in /etc/dev.d are no longer called (if you know why/when, please send me a note!). A crappy hack is to create a separate script for each disk, in our example /root/scripts/mount_usbkey:
    mount /usbkey

    and add to the end of the matching line in /etc/udev/rules.d/usbkey.rules:
    , RUN += "/root/scripts/mount_usbkey"
    It isn't pretty, but it'll work..
  • now try removing and reinserting your USB key, and check the contents of /usbkey
  • if things don't work, check the bottom part of /var/log/messages for errors

← back to notes and tips index
Please do not copy the text of this tip (© Patrick Min) to your web site.