15

With every new release the way to automount USB drives in Linux seems to change (fortunately I'm using Debian, so I'm only losing a few days on this every 2 years). We used to have usbmount, udisks, udisks2, udisks-glue, pmount, custom udev rules, and I'm probably forgetting many more. (A quick look shows that at least a thing named afuse seems to exist, but is not documented too well). None of these work anymore (for me at least).

What is the "current" way to automount USB drives in Debian? I used the following udev rule, but since updating from stretch to buster this stopped working:

SUBSYSTEM=="usb", DRIVERS=="usb-storage", ACTION=="add", \
RUN+="mkdir /media/usb%n; mount -o gid=plugdev,umask=002,fmask=111,users /dev/%k%n /media/usb%n"

Also: what is the stable solution to do this, that will reliably work even after updating to a new release, that I probably missed?

muru
  • 69,900
  • 13
  • 192
  • 292
Circonflexe
  • 279
  • 1
  • 2
  • 7

3 Answers3

8

You can create systemd.mount and systemd.automount unit files. Here is an example:

To mount /dev/sdb1 under /mnt/mountpoint , create a mnt-mountpoint.mount file:

sudo nano /etc/systemd/system/mnt-mountpoint.mount

Note: The name of the unit file should be dir-sub-dir.mount extracted from the mount point /dir/sub-dir (if you need to mount the device under /media/mountpoint the name will be media-mountpoint.mount)

Then paste the following lines:

[Unit]
Description=Mount sdb1

[Mount]
What=/dev/disk/by-uuid/UUID_here
Where=/mnt/mountpoint
Type=auto
Options=defaults

[Install]
WantedBy=multi-user.target

Use blkid to replace the UUID_here with the uuid of /dev/sdb1.

Create the mnt-mountpoint.automount file:

sudo nano /etc/systemd/system/mnt-mountpoint.automount

To contain the following lines:

[Unit]
Description=Automount usb

[Automount]
Where=/mnt/mountpoint

[Install]
WantedBy=multi-user.target

Attach your USB then enable and start the units:

sudo systemctl daemon-reload
sudo systemctl enable --now  mnt-mountpoint.mount mnt-mountpoint.automount
GAD3R
  • 63,407
  • 31
  • 131
  • 192
  • 8
    Thanks for your answer. This is for mounting a particular device on a particular mount point (which can be solved by a litteral one-liner in `/etc/fstab`). My question was about how to automount generic USB devices in the smallest available /media/usb*N* directory. – Circonflexe Jan 07 '20 at 14:08
  • 1
    this does not answer the question – user6329530 Jun 25 '22 at 21:23
7

Update[2022-03-06]: Just apt install udisks2 should give you usb automounting.

I researched (reverse) package dependencies in Debian of nautilus, udisks2 and libglib2.0-bin (which contains the gio binary). Based on this and on Archlinux Udisks page I now believe:

  • The state of the art for the thing that actually does the mounting is udisks2. The next best thing to automounting is udisksctl unmount -b /dev/$DEVICE.
  • On a "standard" Debian Gnome installation, nautilus is controlling the icons on the Desktop. When an USB drive gets plugged in, an icon gets shown for the drive but the drive only gets mounted on click on the icon.
  • The best choice for minimal desktops for usb automounting is probably udiskie.

I added a user systemd service to start udiskie:

[Unit]
Description=Udiskie automount daemon

[Install]
WantedBy=graphical-session.target

[Service]
ExecStart=/usr/bin/udiskie --verbose --use-udisks2 --automount --no-config --notify --tray --appindicator
Thomas Koch
  • 621
  • 7
  • 11
  • This worked great for me. If you want to have the mounts user-accessible, add this "`[Service] User=user Group=plugdev`" above `ExecStart`. Also add a file "`/etc/polkit-1/localauthority/50-local.d/10-udiskie.pkla`" with the contents: "`[udisks] Identity=unix-group:plugdev Action=org.freedesktop.udisks.* ResultAny=yes [udisks2] Identity=unix-group:plugdev Action=org.freedesktop.udisks2.* ResultAny=yes`" – Kyle Dunn Feb 20 '22 at 04:18
  • Seems like the `--use-udisks2` option doesn't exist anymore, so it should be removed from the `ExecStart` command (tested on Ubuntu 21.10). – gsgx Mar 06 '22 at 05:24
1

Same issue happened to me after upgrade from Stretch to Buster (headless Raspberry Pi Zero W). In my case, I am using /etc/fstab, so systemd automatically generates mount units from it. All my disks are specified in fstab, and they were automatically mounted on hot plugin just fine in Stretch. It all stopped working in Buster. (The disks would mount fine on reboot, though.)

The culprit of the problem, it seems, was the change in new systemd version:

    * The .device units generated by systemd-fstab-generator and other
      generators do not automatically pull in the corresponding .mount unit
      as a Wants= dependency. This means that simply plugging in the device
      will not cause the mount unit to be started automatically. But please
      note that the mount unit may be started for other reasons, in
      particular if it is part of local-fs.target, and any unit which
      (transitively) depends on local-fs.target is started.

I verified it with systemctl show my-mount.mount command: the device was missing in the WantedBy= list, there was only local-fs.target. (While on Stretch there was local-fs.target and the correspondent dev-sdaX.device, both were listed.) As a result, when the disk was inserted, it did not trigger my-mount.mount unit to start.

So the solution which worked for me was creating the new udev rule that triggers local-fs.target each time the new disk is inserted:

$ cat /etc/udev/rules.d/98-usb-disk-mount.rules 
ACTION=="add", KERNEL=="sd?[0-9]", SUBSYSTEM=="block", RUN+="/bin/systemctl start local-fs.target"

(Note: the systemctl start local-fs.target command is recommended way to trigger fstab disks mount in systemd manual.)

Andriy
  • 31
  • 3
  • Did # udevadm control --reload-rules && udevadm trigger Following – GraemeV Jun 05 '23 at 13:06
  • Ahh, sadly I need to remove this, see: https://forums.debian.net/viewtopic.php?p=773685#p773685 It caused boot to fail (fall back to emergency mode) due to being unable to mount /boot. [ TIME ] Timed out waiting for device – GraemeV Jun 05 '23 at 22:53