Automount disks on headless systems

2023-01-24

A quick overview on how to automount partitions on headless linux systems.

systemd

The easiest possible setup is using systemd - there's an Arch wiki reference with details on this setup. In a one-liner, add this line to /etc/fstab :

/dev/disk/by-label/DISK_LABEL /media/DISK_LABEL ext4 defaults,noauto,x-systemd.automount,x-systemd.device-timeout=10ms 0 2

where DISK_LABEL is the label of your disk partition. You may set ext4 partitions' label with

$ sudo e2label /dev/sdXY NEW_DISK_NAME

where sdXY is the partition's identifier, as reported by lsblk. Note that this will also work with FAT partitions, using fatlabel from the dosfstools package under Arch Linux.

udev-media-automount

In Arch linux you may also automount removable disks with the udev-media-automount package from the AUR. To have it served by a systemd user service, it needs permissions from Polkit (see the Arch wiki article on udisks2 for details) :

    /etc/polkit-1/rules.d/50-udisks2.rules

    polkit.addRule(function(action, subject) {
      var YES = polkit.Result.YES;
      // NOTE: there must be a comma at the end of each line except for the last:
      var permission = {
        // required for udisks2:
        "org.freedesktop.udisks2.filesystem-mount": YES,
        "org.freedesktop.udisks2.encrypted-unlock": YES,
        "org.freedesktop.udisks2.eject-media": YES,
        "org.freedesktop.udisks2.power-off-drive": YES,
        // required for udisks2 if using udiskie from another seat (e.g. systemd):
        "org.freedesktop.udisks2.filesystem-mount-other-seat": YES,
        "org.freedesktop.udisks2.filesystem-unmount-others": YES,
        "org.freedesktop.udisks2.encrypted-unlock-other-seat": YES,
        "org.freedesktop.udisks2.eject-media-other-seat": YES,
        "org.freedesktop.udisks2.power-off-drive-other-seat": YES
      };
      if (subject.isInGroup("storage")) {
        return permission[action.id];
      }
    });

Autofs

The Arch wiki also describes Autofs that looks pretty complete ; I haven't tested it though.

Udisks2

As mentioned earlier, there is the udisks package. I find it too heavy and invasive for this task. I like the simplicity of the first two solutions.