Bitcoin donations are welcome:14snQXeLcnJtWUduKZ6rC2MHdPYrYar1Tw

Thursday, May 21, 2009

UUID, Fstab and Automatically mount USB Drive

Finally found a solution on how to make my Linux (Debian):
A) Remember the unique USB drive(in my case a Western Digital Mirror Edition), and where to mount it
B) Automatically mount it when connected

First you have to identify your usb drive, make sure it is connected and you know the device name, in my case:
NoMore:/home# blkid /dev/sdc1
/dev/sdc1: UUID="6494BFE994BFBC3C" LABEL="My Book" TYPE="ntfs"
NoMore:/home#

As you see my drive has a unique ID. This is what we are gonna use in fstab instead of the device name. Smart huh? :) This way we avoid the problem of changing device names when plugging stuff in and out.

Next in fstab, make a line like this:
UUID=6494BFE994BFBC3C /home/user/DiSK/ ntfs-3g defaults,auto,umask=000,users,rw 0 0
Make the changes you need, but make sure you use your correct UUID(and remove the "") and it is set auto like above.

Next check if it works by typing:
mount -a
The -a parameter means it shall mount everything set to auto in fstab. If it works, umount your drive and let's continue:

Ok, make sure you got udev installed. This is what's gonna automaticly mount our drives when new ones are detected. Make a new file;
NoMore:/home# nano /etc/udev/rules.d/99-mount.rules
And add the following line:
SUBSYSTEM=="block", run+="/bin/mount -a"
Just make sure it uses the correct path to the mount command.
Next restart udev:
NoMore:/home# /etc/init.d/udev restart
Now unmount(umount) your drive, unplug it and try it out.

14 comments:

  1. run should be RUN

    ReplyDelete
  2. Very smart!. Thanks.

    ReplyDelete
  3. I have a USB hard disk which I use as backup. I install the disk only when making a back-up, the other time I store the disk off-site.
    This auto mounting prevents forgetting to mount the drive so a back-up is made on the root file system.
    So thanks a lot.

    jlinkels

    ReplyDelete
  4. Awesome info, thanks for un-obfuscating UUID a bit :)

    ReplyDelete
  5. I have found the udev part problematic: mount is executed during boot on all devices including root, and as root is also mounted "rw", fsck aborts the boot process. I found a more elaborate udev config post here: http://www.monperrus.net/martin/automounting+usb+flash+drives+on+linux+with+udev+and+pmount

    ReplyDelete
    Replies
    1. Kovács Péter, that's what you get for using desktop'ish approach on a system designed without these spontaneous hotplugs in mind. I doubt that fsck error will hurt any of my laptops/desktops much (Ctrl-D all the way), and there are still no reasons to set something like that on anything acting as mission-critical server.

      Delete
  6. Completely awesome tutorial!

    ReplyDelete
  7. Just what I needed but I am using rsnapshot and would like to to run only when the external drive is inserted

    ReplyDelete
  8. This is also a very good post which I really enjoy reading. It is not everyday that I have the possibility to see something like this. Mortgage Brokers in Canary Wharf

    ReplyDelete