Linux tip, Fedora tip / howto: automatically mounting an external USB hard disk

 
back to notes and tips index

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

automatically mounting an external USB hard disk

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):
  • connect and power up the drive, and wait a few seconds, then type:
    dmesg | tail
    it'll say something like: usb-storage: waiting for device to settle before scanning Vendor: Maxtor 6 Model: Y200P0 Rev: 0000 Type: Direct-Access ANSI SCSI revision: 00 SCSI device sdf: 398297088 512-byte hdwr sectors (203928 MB) sdf: assuming drive cache: write through SCSI device sdf: 398297088 512-byte hdwr sectors (203928 MB) sdf: assuming drive cache: write through sdf: sdf1 sdf2 Attached scsi disk sdf at scsi2, channel 0, id 0, lun 0 usb-storage: device scan complete
  • the line sdf: sdf1 sdf2 tells you there are two partitions on this drive. If you don't have the information about what type each partition is, run:
    fdisk /dev/sdf
    and type p to get the partition information
  • create a mount point for each partition, for example mkdir /maxtor1 and mkdir /maxtor2
  • add corresponding entries to /etc/fstab:
    /dev/maxtor1 /maxtor1 reiserfs defaults,noauto
    /dev/maxtor2 /maxtor2 vfat noauto,noatime,user,rw,uid=500 0 0
    In this example, we have a ReiserFS and a FAT32 partition on the drive (other common partition types are ext2 and ext3 (Linux), and ntfs (Windows)). For the uid number, fill in your own user id (type id to get it)
  • run the command
    udevinfo -a -p /sys/block/sdf | more
    You'll see blocks of information preceded by "looking at the device chain at .." Find the block that has a line like SYSFS{vendor}=="Maxtor 6" (in our example, the drive was made by Maxtor) (for later versions of udev, 098 and up, replace SYSFS with ATTRS)
  • from this block only, copy the information for SYSFS{vendor} and SYSFS{model} (the goal here is to get some uniquely identifying information for your disk. If you have multiple external USB hard disks, and some are the same make & model, you may want to use other information) (for later versions of udev, 098 and up, replace SYSFS with ATTRS)
  • create the file /etc/udev/rules.d/extusb.rules:
    KERNEL=="sd[a-z][0-9]", SYSFS{vendor}=="Maxtor 6", SYSFS{model}=="Y200P0", NAME="%k", SYMLINK="maxtor%n"
    Fill in the values you found for the vendor and model. (for later versions of udev, 098 and up, replace SYSFS with ATTRS) This will make sure that udev creates symlinks /dev/maxtor1 and /dev/maxtor2 once the disk is connected and powered up.
  • for older versions of Fedora (tested and working under Fedora Core 4): create the file /etc/dev.d/block/mount.dev (you may have to create the block subdir): #!/bin/bash base=$(basename $DEVNAME) logger -t mount.dev "`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_maxtor:
    #!/bin/tcsh
    mount /maxtor1
    mount /maxtor2

    and add to the end of the matching line in /etc/udev/rules.d/extusb.rules:
    , RUN += "/root/scripts/mount_maxtor"
    It isn't pretty, but it'll work..
  • now try connecting and powering up the disk, and check the contents of /maxtor1 and /maxtor2
  • 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.