Tell me more ×
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), 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
show 1 more comment

1 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.