3

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
5
4

IIRC 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.

9
  • systemd-umount can be used to unmount a mount or automount point. It is the same as systemd-mount --unmount
    – Tom Hale
    Sep 5 '17 at 15:40
  • @TomHale heh, I think I'd go for systemd-mount -u myself.
    – sourcejedi
    Sep 5 '17 at 15:43
  • Yup, it's easy to miss the u in umount.
    – Tom Hale
    Sep 5 '17 at 15:45
  • Discovery only supported for block devices, don't know what to do. Any tips?
    – Tom Hale
    Sep 5 '17 at 15:47
  • 1
    umount -vt autofs /media/backup has weird behaviour - somtimes it says umount: /media/backup (systemd-1) unmounted and sometimes umount: /media/backup unmounted. In the latter case, it actually mounts the mountpoint!
    – Tom Hale
    Sep 5 '17 at 16:03
1

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.

3
  • media-backup.automount will need to be stopped as well. See this question which hopes for a more generic way of converting a path into a unit file name.
    – Tom Hale
    Sep 5 '17 at 16:21
  • @TomHale right. So more like this. What happens when you stop the automount unit, should the answer be editted to stop the mount unit as well? If so, before or after stopping the automount?
    – sourcejedi
    Sep 5 '17 at 16:25
  • See this question about stopping the .automount without unmounting the currently mounted filesystem.
    – Tom Hale
    Nov 19 '17 at 13:22
1

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")"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.