Given a mountpoint (directory) name, how do I start / stop the systemd automounter only on that mountpoint?
I want a solution which works even if the mountpoint:
- is currently mounted
- has
[^0-9A-Za-z]characters in the pathname
Given a mountpoint (directory) name, how do I start / stop the systemd automounter only on that mountpoint?
I want a solution which works even if the mountpoint:
[^0-9A-Za-z] characters in the pathnameIIRC this was added relatively recently, but it sounds like you might want to use systemd-mount --umount WHERE. Making sure WHERE is passed correctly in the scripting language of your choice is your problem :).
Or you should be able to use the regular umount command. systemd picks up unmounts using kernel events or something, so you won't end up with a misleading status on the corresponding .automount unit.
Here is an example expressed in shell.
MOUNT="/media/backup"
MOUNT_UNIT="$(systemctl show --property=Id "$MOUNT" | sed -e s/^[^=]*=// )"
AUTOMOUNT_UNIT="$(echo "$MOUNT_UNIT" | sed -e s/[.]mount$/.automount/)"
systemctl stop "$AUTOMOUNT_UNIT"
To try and answer this question completely, you could instead use systemd-escape to generate the unit name, as described in the answer to the linked question.
With thanks to this answer:
#!/bin/bash
if [ $# -ne 1 ]; then
{
printf "Stop systemd automount at <mountpoint>\n" "${0##*/}"
printf "Usage: %s <mountpoint>\n" "${0##*/}"
} >&2
exit 1
fi
MOUNT=$1
sudo systemctl stop "$(systemd-escape -p --suffix=automount "$MOUNT")"