4

I'd like to mount a directory from a remote machine in my /home/stew/shared. After installing sshfs and using ssh-copy-id to my remote machine, I can do this:

stew@stewbian:~$ sshfs [email protected]:/path/to/remote-dir ~/shared

and then unmount with

stew@stewbian:~$ umount ~/shared

or

stew@stewbian:~$ fusermount -u ~/shared

Works great, but I'd like to mount this automatically when stew logs in, and unmount it when stew logs out. One working option is to use a systemd .service on the user bus:

# ~/.config/systemd/user/shared.service
[Unit]
Description=Mount ~/shared

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=sshfs %[email protected]:/path/to/remote-dir %h/shared
ExecStop=umount %h/shared

[Install]
WantedBy=default.target

systemctl --user {start,stop} shared.service also works great! But I'm wondering if .mount units would be more robust.


I tried using a mount unit like so:

# ~/.config/systemd/user/home-stew-shared.mount 
[Unit]
Description=~/shared

[Mount]
What=%[email protected]:/path/to/remote-dir
Where=%h/shared
Type=fuse.sshfs

[Install]
WantedBy=default.target

Starting this mount unit works great, but stopping it causes this:

$ systemctl --user status home-stew-shared.mount
● home-stew-shared.mount - ~/shared
     Loaded: loaded (/home/stew/.config/systemd/user/home-stew-shared.mount; static)
     Active: active (mounted) (Result: exit-code) since Mon 2021-05-24 16:49:40 CEST; 6min ago
     ...
May 24 16:49:40 stewbian systemd[1046]: Unmounting ~/shared...
May 24 16:49:40 stewbian umount[22256]: umount: /home/stew/shared: must be superuser to unmount.
May 24 16:49:40 stewbian systemd[1046]: home-stew-shared.mount: Mount process exited, code=exited, status=32/n/a
May 24 16:49:40 stewbian systemd[1046]: Failed unmounting ~/shared.

I can $ umount ~/shared to unmount the directory and fail the unit.


Questions:

  1. Is there a reason why I should prefer *.mount units over *.service units?
  2. If I really should be using *.mount, is there a trick to getting this to work on the user-bus, or do I need to go to the system bus and figure out how to do lazy mounting and manually set UIDs and GIDs?

One of the nice things about using the *.service is that I can add this service to the skel, so each user will automount their own private shared directories which are effectively sync'd between all machines in the house. The *.mount files need the username in the filename to access the correct home.

3
  • I can't reproduce the error you are showing when using a .mount unit. systemctl --user start ... and stop both works as expected, mounting and unmounting the remote directory. systemd 248.3 here, using a testing unit almost identical to your home-stew-shared.mount. The .mount unit is also successfully started when the user logs in, though it is not stopped automatically when the user logs out (and hence their session is kept active; but I only tried logging in/out through SSH).
    – fra-san
    May 24, 2021 at 17:05
  • I'm on 247.3. It seems unlikely to be caused by differences in versions. It's the stopping I'm having problems with. Thanks for letting me know you can't reproduce it, that means it could work and I just need to keep looking.
    – Stewart
    May 24, 2021 at 17:13
  • 1
    “User-space” usually means something quite different ;-). May 24, 2021 at 19:15

1 Answer 1

4

I have the same issue on Ubuntu 21.04 with systemd 246.6 - when you try to unmount a mount unit, systemd first tries to find a umount helper at /sbin/umount.<type> (i.e. for sshfs it would be /sbin/umount.fuse.sshfs), and when that fails - it will call umount2(<where>) - and that will fail when it is run by the user's systemd.

I'm not sure why this works for @fra-san - I think they may have an unmount helper.

As to those questions:

  1. You can do anything with a service unit, like you could do anything with a SysV style init script, but the idea of systemd is that we understand common system management tasks and give the minimal descriptive syntax to achieve what you need without over complicating things (that makes it harder to maintain). If you can use a mount unit to mount file systems, that is preferable to basically writing a script. Of course the infrastructure has to be able to support what you need to do and while the current status of user mount units is much better than it was a few years ago, as of Ubuntu 21.04 - it is still not 100% there for FUSE file systems.
  2. To get fuse.sshfs user mount units to stop (unmount), I created an umount helper in /sbin/umount.fuse.sshfs as
#!/bin/sh
/bin/fusermount -u "$1"

then stopping the mount unit works well - systemd will call the umount helper and will correctly unmount the filesystem (don't call umount from the umount helper because umount also calls helpers and you'd get into an infinite loop that will consume all pids). This is likely not a great solution and systemd should do what umount does when you call it as a user (which I can't actually figure out what is does), but it works for me.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .