Systemd – Stop .automount Unit Without Unmounting Filesystem

automountingsystemd

Is there a way that I can stop a .automount without unmounting the filesystem?

I just want to prevent any future automounting, but I don't want to unmount the currently mounted filesystem.

Why can't I just remount the filesystem you ask?

Well, just as umount --lazy is dangerous and it's impossible to know when it has completed, it seems that stopping the .automount unit also uses the same MNT_DETACH flag.

A demonstration:

$ # Call this shell #1:
$ # I block permissions on the unmounted mountpoint:
$ ls -ld /media/backup/
dr-------- 1 root root 0 Nov 19 14:16 /media/backup/
$ ls /media/backup
ls: cannot open directory '/media/backup': Permission denied
# Enable the automounter:
$ sudo systemctl start media-backup.automount
$ # Now I can see the contents:
$ ls /media/backup
blockchain  btrbk  syncthing
$ # Pin it down with a working directory:
$ cd /media/backup/
# Stop the automounter:
$ sudo systemctl stop media-backup.automount
$ # Note there is no complaint about a mounted filesytem...

At this point in time, in another shell:

$ # Shell #2
$ ls /media/backup/
ls: cannot open directory '/media/backup/': Permission denied

So, it's unmounted when viewed from shell #2, but still accessible in Shell #1:

$ # Shell #1
$ ls
blockchain  btrbk  syncthing

How do I stop the .automount unit without unmounting the currently mounted filesystem?

Best Answer

Masking the unit prevents future running by simply creating symlink to /dev/null:

$ sudo systemctl --runtime mask "$(systemd-escape -p --suffix=automount /media/backup)"
Created symlink /run/systemd/system/media-backup.automount → /dev/null.

The --runtime flag will only mask until the next boot.

To re-enable the automount, simply delete the symlink.


Note:

After masking and unmounting the currently mounted filesystem, future access attempts may show something like:

$ ls backup
ls: cannot open directory 'backup': Host is down
Related Question