Skip to content

Automount usb devices with systemd

April 24, 2016


Ever since udisk-glue was obsoleted with udisk (the first generation), I’ve been struggling to find a solution to automatically mount a usb drive when such a device is connected to a kodi based home cinema PC. I wanted to avoid writing dedicated scripts or udev rules. Systemd is quite powerful and I thought that a simple solution should be possible using systemd configuration.

Actually, auto-mount notion covers 2 scenario :

  1. A device is mounted after being plugged in
  2. An already available device is mounted when a process accesses its mount point

The first case is the one that is needed with Kodi. The second may be useful so is  also documented in this post.

For the first case, add a line like the following in /etc/fstab:

/dev/sr0 /mnt/br auto defaults,noatime,auto,nofail 0 2

and reload systemd configuration:

sudo systemctl daemon-reload

The important parameters are “auto” and “nofail”: with “auto”, systemd mounts the filesystem as soon as the device is available. This behavior is different from sysvinit where “auto” is taken into account only when “mount -a” is run by init scripts. “nofail” ensures that boot does not fail when the device is not available.

The second case is handled by a line like the following one (even if the line is split here to improve readability):

/dev/sr0 /mnt/br auto defaults,x-systemd.automount,\
   x-systemd.device-timeout=5,noatime,noauto 0 2

With the line above in /etc/fstab, the file system is mounted when user does (for instance) “ls /mnt/br” (actually, the first “ls” fails and triggers the mount. A second “ls” gives the expected result. There’s probably a way to improve this behavior, but I’ve not found it… this is now fixed).

While testing automount, I realized that the unit may not be started after the daemon-reload command. To check the status of the created unit, run:

sudo systemctl status mnt-br.automount
● mnt-thumb.automount
   Loaded: loaded (/etc/fstab; generated)
   Active: inactive (dead)
    Where: /mnt/br
     Docs: man:fstab(5)

In the example above, the unit was not started. Instead of rebooting, you can start the unit:

sudo systemctl start mnt-br.automount
● mnt-thumb.automount
   Loaded: loaded (/etc/fstab; generated)
   Active: active (waiting) since Sat 2018-03-31 12:09:53 CEST; 2s ago
    Where: /mnt/br
     Docs: man:fstab(5)

“x-systemd.*” parameters are documented in systemd.mount(5).

Last but not least, using a plain device file (like /dev/sr0) works fine to automount optical devices. But it is difficult to predict the name of a device file created for a usb drive, so a LABEL or a UUID should be used in /etc/fstab instead of a plain device file. I.e. something like:

LABEL=my_usb_drive /mnt/my-drive auto defaults,auto,nofail 0 2

All the best


From → computer

  1. rbnb permalink

    Thank you for this post. I’ve been searching and searching on how to continue booting when the USB drive is not plugged in.

  2. Is there a way to mount any USB device? And at same time?

    • I would not recommend to mount a usb drive that you don’t trust. UUID can be used to identify and check whether a usb drive can be mounted automatically. But I don’ think mounting any drive via /etc/fstab is possible.

      I think you can mount several drives by adding more lines in /etc/fstab like the one explained in thist post.

      Hope this helps

      • I trust people into my home, but they have multiple different devices. Something like an infinite number. Because I meet people.

      • Then you could try udisks2 to auto mount these devices

  3. Brian permalink

    A very nice article. I encountered this aspect of systemd by accident when I noticed the USB drive on a system similar to yours was being automounted in spite of removing the usbmount package.

    A very minor point: scenario 1 doesn’t need “auto” on the fstab line because it is part of “defaults”. The explanation of the meaning of th line is excellent.

    For me, the first “ls” succeeds on Debian stretch. To avoid having to unmount a USB device after it has been written to the x-systemd.device-timeout x-systemd.idle-timeout option is super.



    • I’m very sorry for the late approval and reply of your post.

      The first “ls” now succeeds on my system. I guess it was a systemd bug. I’ll update my blog.

      Thanks for the tip about x-systemd,idle-timeout. I did not know this option.

      All the best

  4. Brian permalink

    Not minor: I should have said x-systemd.idle-timeout (not x-systemd.device-timeout)

Trackbacks & Pingbacks

  1. How to automount optical media on Debian Linux for Kodi | Ddumont's Blog
  2. Hawkeye Pi Camera – the software systems – Elder Ninja

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: