4

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
  • 28,728
  • 32
  • 139
  • 229
  • Isn't systemd.automount always set up per mount point via fstab option or unit file? – Tom Yan Sep 06 '17 at 02:17
  • If you want to stop but not disable you can always look for the name of the unit (iirc hyphenated-mountpoint-path.automount) with `systemctl` and stop it. – Tom Yan Sep 06 '17 at 02:19
  • @TomYan [stopping the `.automount` has unexpected behaviour](https://unix.stackexchange.com/questions/390514/start-stop-systemd-automount-on-a-single-mountpoint?noredirect=1#comment696057_390521) – Tom Hale Sep 06 '17 at 06:35
  • I am not sure why stopping the automount unit should also stop the mount unit (which is equivalent to unmount the mountpoint AFAIK) – Tom Yan Sep 06 '17 at 12:04
  • Related: [Stopping an `.automount` without unmounting the currently mounted filesystem](https://unix.stackexchange.com/q/405597/143394) – Tom Hale Nov 19 '17 at 13:23

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
  • 48,311
  • 17
  • 143
  • 296
  • `systemd-umount` can be used to unmount a mount or automount point. It is the same as `systemd-mount --unmount` – Tom Hale Sep 05 '17 at 15:40
  • @TomHale heh, I think I'd go for `systemd-mount -u` myself. – sourcejedi Sep 05 '17 at 15:43
  • Yup, it's easy to miss the `u` in `umount`. – Tom Hale Sep 05 '17 at 15:45
  • `Discovery only supported for block devices, don't know what to do.` Any tips? – Tom Hale Sep 05 '17 at 15:47
  • That sounds bogus and bug-worthy. The implicit `--discover` shouldn't apply to unmount. "Or you should be able to use the regular `umount` command." – sourcejedi Sep 05 '17 at 15:59
  • 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 05 '17 at 16:03
  • Since you mentioned it was bug-worthy, I raised it [here](https://github.com/systemd/systemd/issues/6756). – Tom Hale Sep 06 '17 at 14:11
  • so, raising bugs where possible is good, but that's totally not the bug I referred to :). – sourcejedi Sep 06 '17 at 14:51
  • https://github.com/systemd/systemd/issues/6758 "documented `systemd-umount WHERE` does not work" – sourcejedi Sep 06 '17 at 15:18
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
  • 48,311
  • 17
  • 143
  • 296
  • `media-backup.automount` will need to be stopped as well. See [this question](https://unix.stackexchange.com/q/390522/143394) 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](https://unix.stackexchange.com/q/405597/143394) 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
  • 28,728
  • 32
  • 139
  • 229