How to have a filesystem mounted during user login

filesystemsfstabmountsystemd

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.

Best Answer

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.