Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a Debian sid system (Wheezy) (and same for arch), without any desktop environment (and no Xorg at all).

I can mount my SD-cards, USB sticks, external HDD by label into a specified directory in /media/ manually with mount / umount and the suitable entries in /etc/fstab, or automatically at boot time, but this is compelling, to restrictive and not dynamic enough for my needs: if I want them to be mounted in /media/<LABEL>, each device with a different <LABEL> needs its own entry, and each <LABEL> subdirectory needs to be created / removed manually in /media/ as well).

So, what is the best way to mount them automatically in /media/<LABEL> at insertion (and to unmount them from the filesystem as soon as they are unplugged no matter how risky it is for the data)?

The ideal solution would:

  1. detect when a removable media is plugged-in (i.e. when added in /dev/ as sdax, sdbx, ... by udev)
  2. create a directory in /media/ according to its label (label of the removable media)
  3. mount it in the directory /media/<LABEL> in RW mode (if it's filesystem supports that) with flush option (for a vfat)
  4. detect if the media has been unplugged
  5. if then, unmout it from the filesystem
  6. remove the corresponding directory from /media/

(the devices should be mounted in synchronous mode oviously, to avoid any data loss when hot unplugged because of caching edit: sync is way too much for vfat removable media so flush has been created as a compromise, less secure than sync, but less risky according to the life-cycles of flash memories)

I found some info about autofs, HAL, udisks, udisks2, usbmount etc., but it's unclear which one is deprecated or preferred, and anyway, I haven't figured out how to configure them easily on my system to do that, up to now ...

share|improve this question
    
First, have you searched for similar questions? I remember it has been already asked. Second, the unmount should happen before the unplugging. –  enzotib Aug 1 '12 at 9:06
    
Of course I did :o] Here and on superuser. The problem is that tools evolves (udisks/udev), or are deprecated (HAL?), other "not preferred" (udisks2?), and so on. Anyway, I haven't been able to find any working nor clear solution. Most of what I found out up to know rely upon a desktop environment mechanism (Gnome, KDE or XFCE); I don't want having to install any. About unmouting, not necessarily. If the device is set to sync, a deamon can unmount the device from the fs if it detects that it has been unplugged with no data loss. All desktop distros do that. I want that in terminal. –  cedbeu Aug 1 '12 at 9:18
    
Look at this Replacement for halevt. About unmount, generally DE expect you to eject the device from the file manager, before physically unplugging it. –  enzotib Aug 1 '12 at 9:24
    
Yes, true. But I've seen that some (formerly used) daemons could unmount devices from fs that hasn't been unmounted before unplugging, and even delete automatically their corresponding directory from /media/, and that staying within the normal way of doing things (if devices are configured to be synchronous of course). Thank you for your link. I'll have a look as soon as I'm back home on udisksevt. I already have a look on devmon and udiskie but they aren't in the Debian packages. If I can find anything else I'll try that but I can't believe there is no native solution on Debian to do that. –  cedbeu Aug 1 '12 at 9:30
1  
I'm on Debian too and I use fdisk -l and then mount - that's two commands, but OK, if you want it automatized, you'll have to parse the fdisk -l output, and then put it in the mount - I do such stuff with head, tail, tr, cut, etc. but the pros usually do it with awk or, even better, look for the data in the correct place. About deletion, at least for me when I mount to /mnt, that's done automatically. About the trigger (when you plug in) I have no clue, but please tell us when you solve this. –  Emanuel Berg Aug 1 '12 at 10:40

2 Answers 2

up vote 1 down vote accepted

Ok, it's been a long time, but I'll still answer my question with the best option I found as of now.

The best way is to create a udev rule, associated with some scripts (that will create / remove directories and mount / unmount removable devices), and attached to partition udev device event type.

1 - Creating add / remove scripts

Add this script storage-automount.sh in /lib/udev/ and set it to executable (sudo chmod +r /lib/udev/storage-automount.sh):

#!/bin/sh

# set the mountpoint name according to partition or device name
mount_point=$ID_FS_LABEL
if [ -z $mount_point ]; then
    mount_point=${DEVNAME##*/}
fi

# if a plugdev group exist, retrieve it's gid set & it as owner of mountpoint
plugdev_gid="$(grep plugdev /etc/group|cut -f3 -d:)"
if [ -z $plugdev_gid ]; then
    gid=''
else
    chown root:plugdev $mount_point
    gid=",gid=$plugdev_gid"
fi

# create the mountpoint directory in /media/ (if not empty)
if [ -n $mount_point ]; then
    mkdir -p /media/$mount_point
    # other options (breaks POSIX): noatime,nodiratime,nosuid,nodev
    mount -t $ID_FS_TYPE \
      -o rw,flush,user,uid=0$gid,umask=002,dmask=002,fmask=002 \
      $DEVNAME /media/$mount_point
fi

Add this script storage-autounmount.sh in /lib/udev/ and set it to executable (sudo chmod +r /lib/udev/storage-autounmount.sh):

#!/bin/sh

# set the mountpoint name according to partition or device name
mount_point=$ID_FS_LABEL
if [ -z $mount_point ]; then
    mount_point=${DEVNAME##*/}
fi

# remove the mountpoint directory from /media/ (if not empty)
if [ -n $mount_point ]; then
    umount -l /media/$mount_point
    rm -R /media/$mount_point
fi

2 - Creating the udev rule to attach those scripts to events

And finally, add a udev rule in etc/udev/rules.d, for instance 85-storage-automount.rules:

ENV{DEVTYPE}=="partition", RUN+="/lib/udev/storage-automount.sh", ENV{REMOVE_CMD}="/lib/udev/storage-autounmount.sh"

And that's it.

Now, when you plug a storage device in, a directory will be created in /media/ according to the partition name (I don't remember but I think it's working with NTFS partition as well) and your partition will be mounted into it. It's R/W for users if you have a plugdev group on your system. Also, the devices are mounted in synchronous mode in order to limit the risks of data loss in case of hot unplugging.

When the device is removed, it's unmounted and the directory is removed from /media

Also, the tool to monitor the udev events is udevadm monitor, with options like --env or --property:

$ udevadm monitor --env

This is tested and working fine on both debian and arch, but probably work on all distributions that rely on udev.

share|improve this answer

I think you're looking for pmount.

If you want automatic mounting upon insertion, see Automounting USB sticks on Debian. The program that reacts when a new device appears is udev, so automatic mounted is triggered by a udev rule. The usbmount package provides udev rules to automatically mount USB storage devices and a few others.

You cannot automatically unmount media when they are removed because causality doesn't work that way. The media must be unmounted before it is removed. For read-only media, you can get away with unmounting after removal, although this might confuse some applications if they have files open on the suddenly-vanished filesystem. For writable media, if you unplug a mounted filesystem, data corruption is likely.

share|improve this answer
    
Thanks a million for the answer. I'll try that as soon as possible and will let the people here know. About unmouting. Guys, please, stop telling me that's a matter of causality where it is not :o] Unmounting after the device is unplugged perfectly make sense (and is possible) as soon as the the device is configured to be synchronous. Then, it is only a matter of filesystem. If data have been transferred without caching (synchronously), then no issue to unplug without unmounting. Then, unmounting can be performed and is only useful to inform the system that the device is not there anymore. –  cedbeu Aug 2 '12 at 8:08
    
@cblab You're oversimplifying. If a file is open for writing and you pull the device out, the file is likely to be damaged; unmounting ensures that no file is open. On many filesystems (not FAT though), unmounting is more than setting the dirty bit to 0, they need to e.g. flush a log. For flash devices, a major concern with sync mounts is that it wears the device down a lot faster, because it prevents write grouping. See superuser.com/questions/180722/… and readlist.com/lists/vger.kernel.org/linux-kernel/22/111748.html –  Gilles Aug 2 '12 at 9:08
    
Hi @Gilles. Thanks for your replies. I don't think I'm oversimplifying, though. Your're right on some points. But, the user decides whether he wants to live risky. You'd agree, your post is 7 yrs old; now, a flush mount option designed especially for vfat avoids those issues: writing isn't performed after each block, but as soon as the device seems to be inactive. And flash memories have greatly increased their dead-end life-cycles as well. Now, obviously the user has to be aware not to unplug devices while files are opened or during a copy (or soon after). But it's the good compromise. –  cedbeu Aug 6 '12 at 16:58
    
anyway, the usbmount could have been the good option for me, but unfortunately, it doesn't mount the devices on a folder depending on their label names, but in /media/usb-*, which doesn't fit my needs. I want to be able to mount automatically the devices in /media/<DEV_LABEL> with flush parameter when they are detected (if possible with dynamic creation of the directory), and to unmount them and remove the directories from /media/ automatically if they are not detected anymore. The risk I take is up to me. –  cedbeu Aug 6 '12 at 17:03
    

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.