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:
- Is there a reason why I should prefer
*.mountunits over*.serviceunits? - 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.