5

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
Tom Hale
  • 30,455

3 Answers3

6

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.

sourcejedi
  • 50,249
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.

sourcejedi
  • 50,249
  • 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 05 '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 05 '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")"
Tom Hale
  • 30,455