Auto unmount a local partition on idle

I am looking to have a local partition unmount automatically after a set timeout. I set the parameters on fstab based on this ArchWiki Guide but the unmount doesn’t happen after the timeout has passed.

Any ideas on how to make it work? Or debug (which log files to search? What to look for?)?

The partition in question is /dev/vda5

Partition Information

[jetstream@localhost ~]$ lsblk -f
NAME   FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINT
vda                                                                           
├─vda1 vfat   FAT16       EFEA-3426                             179.5M    10% /boot/efi
├─vda2 ext4   1.0         e8f13661-70f4-486e-8937-555b458aa79e  291.6M    33% /boot
├─vda3 swap   1           48c5e3ae-b226-4464-95b4-3c6fd5e96f90                [SWAP]
├─vda4 ext4   1.0         b9ebb26f-10bd-4c9b-af54-1603b5187432    5.8G    53% /
└─vda5 ext4   1.0         426ac462-c1ef-4837-970c-bd064a4760d7    1.2G     0% /mnt/testmount

fstab configuration

UUID=426ac462-c1ef-4837-970c-bd064a4760d7 /mnt/testmount          ext4    noauto,x-systemd.automount,x-systemd.mount-timeout=30,x-systemd.idle-timeout=60        0 0

Update 1

Found log information. Can see it auto-mount … but no unmounts

[jetstream@localhost ~]$ journalctl --boot=0 | grep testmount
Feb 09 23:25:55 localhost.localdomain systemd[1]: Set up automount mnt-testmount.automount.
Feb 09 23:28:15 localhost.localdomain systemd[1]: mnt-testmount.automount: Got automount request for /mnt/testmount, triggered by 2198 (pool-org.gnome.)
Feb 09 23:28:15 localhost.localdomain systemd[1]: Mounting /mnt/testmount...
Feb 09 23:28:15 localhost.localdomain systemd[1]: Mounted /mnt/testmount.   <-- can see it automount

Update 2

Does not unmount automatically after the timeout lapses :frowning: hours later even. Even after logging out completely.

Anything else I could try? Command to run?

1 Like

Make sure there are no open files/directories:

sudo lsof | grep -e /mnt
2 Likes

Thanks for that command @vgaetera I was wondering how to confirm once I closed all the apps / windows.

Ran it after closing all applications and waiting for several minutes. No new entries on journaltctl logs :frowning: I’m going to step away from the system and check back later.

[jetstream@localhost ~]$ sudo lsof | grep -e /mnt
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
  Output information may be incomplete.
1 Like

Posted Update 2

I think the option x-systemd.idle-timeout= should be converted to the property TimeoutIdleSec=:

systemctl show --property TimeoutIdleUSec mnt-testmount.automount

ok, so I changed the fstab entry to use x-systemd.idle-timeout=60 still doesn’t auto unmount after a minute :frowning: This is so puzzling

UUID=426ac462-c1ef-4837-970c-bd064a4760d7 /mnt/testmount          ext4    noauto,x-systemd.automount,x-systemd.mount-timeout=30,x-systemd.idle-timeout=60        0 0

Executing the command does not return a value (strange ??)

$ sudo systemctl show --property TimeoutIdleSec mnt-testmount.automount

journalctl log

[jetstream@localhost ~]$ journalctl --boot=0 | grep testmount
Feb 11 21:06:02 localhost.localdomain systemd[1]: Set up automount mnt-testmount.automount.
Feb 11 21:07:27 localhost.localdomain systemd[1]: mnt-testmount.automount: Got automount request for /mnt/testmount, triggered by 2107 (pool-org.gnome.)
Feb 11 21:07:27 localhost.localdomain systemd[1]: Mounting /mnt/testmount...
Feb 11 21:07:27 localhost.localdomain systemd[1]: Mounted /mnt/testmount.
Feb 11 21:20:50 localhost.localdomain sudo[2276]: jetstream : TTY=pts/0 ; PWD=/home/jetstream ; USER=root ; COMMAND=/usr/bin/systemctl show --property TimeoutIdleSec mnt-testmount.automount
1 Like

Try to replace the /etc/fstab entry with the respective mount and automount units.

I’m at the limits of my knowledge here @vgaetera.

Could you please help me with the syntax of the fstab entry? I can see that the automount mount point should be named mnt-testmount.automount but I am clueless beyond that. Thanks in advance!!

sudo sed -i -e "/\s\/mnt\/testmount\s/s/^/#/" /etc/fstab
sudo tee /etc/systemd/system/mnt-testmount.mount << "EOF" > /dev/null
[Unit]
Description=/mnt/testmount
[Mount]
What=/dev/disk/by-uuid/426ac462-c1ef-4837-970c-bd064a4760d7
Where=/mnt/testmount
Type=ext4
Options=noauto
TimeoutSec=30
[Install]
WantedBy=multi-user.target
EOF
sudo tee /etc/systemd/system/mnt-testmount.automount << "EOF" > /dev/null
[Unit]
Description=/mnt/testmount automount
[Automount]
Where=/mnt/testmount
TimeoutIdleSec=60
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl --now enable mnt-testmount.mount mnt-testmount.automount

It appears to be working:

> sleep 65; grep -e /mnt /etc/mtab
systemd-1 /mnt/testmount autofs rw,relatime,fd=57,pgrp=1,timeout=60,minproto=5,maxproto=5,direct,pipe_ino=62781 0 0

> ls /mnt; sleep 5; grep -e /mnt /etc/mtab
testmount
systemd-1 /mnt/testmount autofs rw,relatime,fd=57,pgrp=1,timeout=60,minproto=5,maxproto=5,direct,pipe_ino=62781 0 0
/dev/loop0 /mnt/testmount ext4 rw,seclabel,relatime 0 0
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.