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:
- detect when a removable media is plugged-in (i.e. when added in
/dev/as
sdax,sdbx, … byudev) - create a directory in
/media/according to its label (label of the removable media) - mount it in the directory
/media/<LABEL>inRWmode (if it’s filesystem supports that) withflushoption (for avfat) - detect if the media has been unplugged
- if then, unmount it from the filesystem
- 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 …
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I think you’re looking for pmount.
If you want automatic mounting upon insertion, see Automounting USB sticks on Debian.
If your system uses udisks2, that’s the preferred way of reacting to the insertion of a removable storage device. See the Arch Wiki for configuration tips for Udisks.
Otherwise, the program that reacts when a new device appears is udev, so automatic mounting is triggered by a udev rule. The usbmount package provides udev rules to automatically mount USB storage devices and a few others. Calling mount from udev rules doesn’t work for FUSE filesystems (such as ntfs-3g), so use udisks if you need that.
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.
Method 2
Here is how I recently did it, and I am quite happy with this approach now.
This is for Ubuntu 12.04 + gentoo, but I guess any distro, which allows to install udev and autofs should work.
Prerequisites: You have to have installed udev + autofs.
Step 1)
Create the following “/etc/udev/rules.d/90-usbsd-auto.rules” file (of course you might use any name as long as it ends with “.rules”). :
# Add symlink /dev/usbdisks/<label> to /dev/sd[a-z][1-9]
# if partition has a label
# Add symlink /media/usb/<label> to /media/autousb/<label>
# for automounter support
ACTION=="add", KERNEL=="sd*", ENV{DEVTYPE}=="partition",
ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}=="?*",
SYMLINK+="usbdisks/$env{ID_FS_LABEL_ENC}", MODE:="0660",
RUN+="/bin/rm /media/usb/$env{ID_FS_LABEL_ENC}",
RUN+="/bin/ln -sf /media/autousb/$env{ID_FS_LABEL_ENC} /media/usb/$env{ID_FS_LABEL_ENC}"
# Fallback: If partition has a NO label, use kernel name (sd[a-z][1-9])
ACTION=="add", KERNEL=="sd*", ENV{DEVTYPE}=="partition",
ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}!="?*",
SYMLINK+="usbdisks/%k", MODE:="0660",
RUN+="/bin/rm /media/usb/%k",
RUN+="/bin/ln -sf /media/autousb/%k /media/usb/%k"
# Some FileSystems emit a "change" event when they are unmounted.
# UDEV seems to delete the device symlink in this case :-(
# So we need to re-create it here
ACTION=="change", KERNEL=="sd*", ENV{DEVTYPE}=="partition",
ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}=="?*",
SYMLINK+="usbdisks/$env{ID_FS_LABEL_ENC}", MODE:="0660"
# Fallback: If partition has NO label, use kernel name
ACTION=="change", KERNEL=="sd*", ENV{DEVTYPE}=="partition",
ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}!="?*",
SYMLINK+="usbdisks/%k", MODE:="0660"
# When device is removed, also remove /media/usb/<label>
ACTION=="remove", KERNEL=="sd*", ENV{DEVTYPE}=="partition",
ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}=="?*",
RUN+="/bin/rm /media/usb/$env{ID_FS_LABEL_ENC}"
# Fallback: If partition has no label, remove /media/usb/%k
ACTION=="remove", KERNEL=="sd*", ENV{DEVTYPE}=="partition",
ENV{ID_BUS}=="usb", ENV{ID_FS_LABEL_ENC}!="?*",
RUN+="/bin/rm /media/usb/%k"
What does this do: It creates two symlinks, one for the USB storage device partition under “/dev/usbdisks/<…>”. It will either use the label of the USB storage partition or the kernel name, if there is no label.
The second symlink will link from “/media/usb/<…>” to “/media/autousb/<…>” this is done for automounter support (see step 2).
Note: I originally did not use the rules with the ID_FS_LABEL_ENC variables, since this is dangerous in my opinion. What happens if you plug in two USB sticks which use the same label ?
But the poster specifically wanted to use the label of the USB disk, so I modified the rules accordingly.
To make sure udev reads these rules use
sudo udevadm control --reload-rules
With this udev rules file everything is setup to automount the USB storage device partition. Note though that right now, the device will NOT be automounted (intentionally). It will be automounted once you use it with
Step 2)
Setup autofs to automount /media/autousb/<…> :
I added the following line to my “/etc/auto.master” file (for Ubuntu 12.04):
/media/autousb /etc/auto.usb --timeout=60
This means that AutoFS will unmount the device after 60 seconds of inactivity. You might want to use less or more, dependent on your taste.
For Gentoo you have to modify “/etc/autofs/auto.master” so it makes sense to use
/media/autousb /etc/autofs/auto.usb --timeout=60
Now I created “auto.usb” with the following content:
#!/bin/bash
key=${1}
fstype=$(/sbin/blkid -o value -s TYPE /dev/usbdisks/${key})
if [ "${fstype}" = "vfat" ] ; then
echo "-fstype=vfat,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/${key}"
exit 0
fi
if [ "${fstype}" = "ntfs" ] ; then
echo "-fstype=fuse.ntfs-3g,sync,uid=0,gid=plugdev,umask=007 :/dev/usbdisks/${key}"
exit 0
fi
if [ "${fstype}" = "ext4" ] ; then
echo "-fstype=ext4,sync,nocheck :/dev/usbdisks/${key}"
exit 0
fi
exit 1
This auto.usb needs to be executable, so that autofs uses this as a (bash) script. So for example
sudo chmod 0755 /etc/auto.usb
What does this do: This script will tell AutoFS (/usr/sbin/automount) how to mount the usb storage device partition.
The script will first use “/sbin/blkid” to find out what kind of file system is on the partition.
The script will then provide the right mount options depending on the device partition.
Note: I included sample code for “vfat” (probably most common for usb sticks), “ntfs” and “xfs” file systems. Of course it is quite easy to extend this to support more file systems.
Step 3)
Optional…
To “eject” == unmount your usb stick(s) (or partitions on your usb stick), create a script under /sbin/usbeject :
#!/bin/bash killall -s SIGUSR1 /usr/sbin/automount
With this script you might use “sudo usbeject” to unmount all mounted USB device partitions (by telling automount to unmount them).
Of course you can simply make sure that the partition is not used anywhere; automounter will then unmount the partition after the 60 second timeout…
The real trick here is to use symlinks from “/media/usb” to “/media/autousb”:
- The “/media/usb” symlinks will be created by udev, giving a user a simple overview which usb storage device partitions are there
- AutoFS will then automount the partition on demand if you use it via /media/usb
- With the scripted “auto.usb” file you might support any kind of file system and additionally you also are able to support any kind of naming scheme.
- This approach supports USB sticks with multiple partition, even if these partitions use different file system types.
Method 3
Ok, it’s been a long time, but I’ll still answer my question with the best option I found as of now. To summarize: create a udev rule, associated with some scripts (that will create/remove directories and un/mount removable devices), and attached to udev device event type=partition.
1 – Creating add / remove scripts
Save following script storage-automount.sh to /lib/udev/ and make it executable (sudo chmod a+x /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
Save following script storage-autounmount.sh to /lib/udev/ and make it executable (sudo chmod a+x /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 make it have the same permissions as the other rules in that dir/folder
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.
Method 4
Please see the ArchWiki Udev page. You are not supposed to execute mount from Udev rules or from scripts that are executed by Udev rules.
However, as shown in the udev-media-automount project, it is pretty simple to do it by invoking a Systemd unit from the Udev rule, which then calls a script which does the mounting. Unlike udisks2 and other solutions, udev-media-automount is geared towards the simple case where you want to mount something as the root user without necessarily waiting for a desktop environment or “user session” to be present. It seems to be exactly what the OP is asking for, and I found it to be functional and easy to understand.
Method 5
I think its pretty late but I will post this if anyone needs it : (also my first StackExchange answer)
After checking for the solution in many places, I found a simple solution in Arch Wiki:
Link to Arch Wiki.
We can add a udisks2 rule to change default mount directory from /run/media/$USER/ to /media/<Label>.
Add the following rule in /etc/udev/rules.d/ with name like 99-udisks2.rules:
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1"
Then we can just install a simple tool like Udiskie for automount.
Method 6
I found a way in doing this by editing /etc/fstab file. A simple entry looks like:
/dev/sdb1 /media/robot/hdd1 ntfs defaults 0 0
<file system> <mount point> <type> <options> <dump> <pass>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0