9

I would like a file based filesystem (~/Archives/inventory.locker) mounted upon user login and unmounted upon logout (~/Documents/Inventory).

pam_mount seems to provide the functionality I am after, but it has incompatibilites with pam_systemd.

I have tried writing a user based systemd.mount unit, but it fails with:

mount: only root can do that

Even though I have the 'user' mount option defined and can successfully mount as user manually.

The systemd method seems ideal because it requires no other dependencies and is also per user process and not per login session.

I am open to alternative solutions too.

beanaroo
  • 253
  • Have you tried defining the command in ~/.bash_profile? this file is automatically executed upon login. – Cestarian Jan 29 '16 at 04:52
  • @Cestarian, problem is this would be sourced for every login session, not just the initial. (Though a condition could probably work). How would I ensure it gets unmounted on the last logout? – beanaroo Jan 29 '16 at 05:07
  • You can probably ensure the unmount with ~/.bash_logout. Conditions can be created, for example you can create a condition that you are on a specific tty (like tty1). I believe this is how: [[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && insert command here there are probably other more clever conditions you can make as well, but i'm not an expert. – Cestarian Jan 29 '16 at 05:11
  • @Cestarian, tty is unfortunately not predictable and remote login sessions need to be considered too. – beanaroo Jan 29 '16 at 05:14
  • these commands do support ssh logins. But maybe you can do a simple if command (e.g. if the user does not have an existing session, execute the command, this shouldn't be hard to do. To check if there exists a session, run who | grep username to list existing user sessions, if the output is null then the command should be executed) – Cestarian Jan 29 '16 at 05:20
  • @beanaroo have you considered using automount? It will mount the filesystem only when it's path is accessed. If the mountpoint is within the users home directory it should only be accessible by that user, so only when they're logged in can it be mounted. – Centimane Oct 17 '16 at 16:56

1 Answers1

13

Latecomer here. It may be a little counterintuitive, but I use the service (rather than mount) systemd user unit and it works for me. I had to add the user and noauto options to /etc/fstab entry.

cat ~/.config/systemd/user/mount@.service
[Unit]
Requires=home-me.mount
After=home-me.mount

[Service]
ExecStart=/bin/mount %h/%I
ExecStop=/bin/umount %h/%I
RemainAfterExit=yes

[Install]
WantedBy=default.target

You should enable the unit instance with a command such as:

systemctl --user enable mount@some-directory

Help with the @ in the filename, can be found reading about systemd instantiated units.

byly
  • 131
  • 1
    It's worth noting that the some-direectory should be escaped using systemd-escape --path command (i.e. /home/foobar/.local becomes home-foobar-.local). – jirislav Sep 20 '22 at 19:37
  • Complementary info: The systemd user instance is started after the first login of a user and killed after the last session of the user is closed. (https://wiki.archlinux.org/title/systemd/User). – jehon Nov 18 '22 at 09:00
  • Your answer helped me big time, but only after modification. For me at least the service failed to start because of the directives in the [unit] section. Based on the fstab entry you suggested systemd will automatically generate a mount unit file in the /run/systemd/generator directory. This directory is not visible to systemd in user mode, so when starting the mount@some-directory.service in user mode it fails to find the mount unit file and exits. Everything worked fine though after removing the directives in [unit] section. – yesno Feb 27 '23 at 18:17