You are not logged in.
Hi,
tl;dr: Can systemd automounter unmount fileystems after a period of inactivity, like autofs does?
I've just started discovering the wonderful new world of systemd, and to my great joy I found that it contains an automounter, so I thought that I would be able to use that instead of using autofs.
The single thing that I like to automount is my home NAS on my laptop (via NFSv4) that I every day take from home to work and then back home.
To do this with autofs is kind of like killing mosquitoes with a flamethrower. I would much rather just have my automounted NAS defined in my fstab than having to force the slightly mysterious autofs.xxx-files to mount the drive with the options I want it to use.
So, I got systemd to automount my NAS and all was joy! Until I realized that it wasnt unmounting it after a time of non-use. I have googled quite a bit, searched the forums, but have found no mention that systemd either has or doesnt have this kind of functionality.
So my purpose with this post is to get a definitive answer, does systemd do this? And if it doesn't, maybe others searching for the same information will end up here, and find the answer...
/DonO
Offline
StopWhenUnneeded=true in the mount unit will do this. To do this, you cannot rely on the fstab, but instead have to create native systemd mount and automount units for this, as I don't think there is a way to have the fstab generator include that. I found that although this works, it is not pretty and tends to spam your logs with messages about the filesystem being busy before actually unmounting it.
Offline
What does x-systemd.device-timeout=# do? Will it auto-umount NFS share once you are out of your network? So it keeps you connected when you are in a network that has your NFS share and umount once you are out in different network. Or will it keep trying to connect to NFS mount as long as you are in any wifi region. It is really annoying when systemd takes forever to umount your NFS share or probably never on shutdown and you have to hard reset your system. Do share your systemd files if you set it up.
The same applies to remote filesystem mounts. If you want them to be mounted only upon access,
you will need to use the noauto,x-systemd.automount parameters. In addition, you can use the x-systemd.device-timeout=#
option to specify a timeout in case the network resource is not available.
Offline
@donniezazen, please read the systemd.mount(5).
Offline
I've been having this problem for as long as I can remember. I have finally decided to sit down and work something out instead of my laptop hanging all the time when it can't find the nfs server. The systemd.automount feature is nice, but it really sucks that it does not auto unmount when the share is not found.
Also, having systemd.automount means that every time I open vim, or a manpage, and there is no network connection, systemd tries to mount the shares. I'm not sure why because I am not directly accessing them. Everything loads slowly when systemd automount is active for my shares and there is no network connection.
Using soft instead of hard is a workaround, but then when the network connection is lost, corruption can occur.
It seems like the only way to really solve this problem is to have some kind of script run when the network comes up to mount the shares, and to unmount when it goes down. This would require custom systemd units or something. Diving into the systemd manuals is something I have been putting off because they are so hefty.
For anyone that is interested, here is my fstab:
lithium:/mnt/data /mnt/data nfs noauto,noatime,rsize=32768,wsize=32768,soft,retry=0,timeo=3,x-systemd.automount,x-systemd.device-timeout=1ms 0 0
lithium:/var/cache/pacman /var/cache/pacman nfs noauto,noatime,rsize=32768,wsize=32768,soft,retry=0,timeo=3,x-systemd.automount,x-systemd.device-timeout=1ms 0 0
Last edited by demizer (2013-07-14 09:00:23)
Offline
Ok. After some six hours of experimentation, including a lot of wasted effort with autofs, I finally have the setup I want. I haven't tested it fully, but it seems to behave in the way that I expect. This only works with NetworkManager.
Add the nfs mount information to /etc/fstab:
lithium:/mnt/data /mnt/data nfs noauto,noatime,rsize=32768,wsize=32768,intr,hard 0 0
lithium:/var/cache/pacman /var/cache/pacman nfs noauto,noatime,rsize=32768,wsize=32768,intr,hard 0 0
I did not really like the behavior of the soft mount. If something fails, I don't want it to happen silently. With the hard option, it will become very apparent something is wrong.
Open /etc/NetworkManager/dispatcher.d/nfs.sh as root and give it 755 permissions:
#/bin/sh
ISNETUP=`nmcli dev wifi | \grep masputo | tr -s ' ' | cut -f 10 -d ' '` 2>/dev/null
echo "$ISNETUP" >> /tmp/nm_dispatch_log
if [[ "$ISNETUP" == "yes" ]]; then
mount /mnt/data
mount /var/cache/pacman
else
umount -l "/mnt/data"
umount -l "/var/cache/pacman"
fi
Replace masputo with the name of the SSID containing your network shares.
Enable the NetworkManager dispatcher to auto run the mount handler script when the required network is brought up or down.
# systemctl start NetworkManager-dispatcher
# systemctl enable NetworkManager-dispatcher
Offline
It seems like the only way to really solve this problem is to have some kind of script run when the network comes up to mount the shares, and to unmount when it goes down.
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
demizer wrote:It seems like the only way to really solve this problem is to have some kind of script run when the network comes up to mount the shares, and to unmount when it goes down.
Much better. Thanks! Have you thought about adding this to the NFS wiki?
Last edited by demizer (2013-07-14 23:48:03)
Offline
No, but feel free. It's a wiki after all
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
I have added a tips and tricks section to the nfs wiki. https://wiki.archlinux.org/index.php/NF … and_tricks
I also improved the scripts to use the shares indicated in /etc/fstab.
Last edited by demizer (2013-07-15 03:26:36)
Offline
@demizer - I'm pretty sure that if you boot your machine with the shares defined in /etc/fstab, startup is delayed as indicated by maggie here: https://bbs.archlinux.org/viewtopic.php … 0#p1260240
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
@demizer - I'm pretty sure that if you boot your machine with the shares defined in /etc/fstab, startup is delayed as indicated by maggie here: https://bbs.archlinux.org/viewtopic.php … 0#p1260240
Even with the noauto mount option? I have not seen any degradation in boot performance with or without the network.
Offline
noauto should make it fine.
Offline
So, I guess the summary so far is: Yes, it is possible, but it doesn't really work as well as it could/should. Better to use a scripted solution.
I think I will make a cross between the two scripts demizer added to the wiki. I like the idea of using nm-dispatcher, but I also like the idea of using pings instead of SSIDs, since I connect to my home network in multiple ways (2.4 and 5Ghz network, VPN, and wired). If I come up with something appealing that works I'll add it to the wiki.
Thank you very much for your help!
Last edited by DonOregano (2013-07-15 18:45:18)
Offline
So, I guess the summary so far is: Yes, it is possible, but it doesn't really work as well as it could/should. Better to use a scripted solution.
I think I will make a cross between the two scripts demizer added to the wiki. I like the idea of using nm-dispatcher, but I also like the idea of using pings instead of SSIDs, since I connect to my home network in multiple ways (2.4 and 5Ghz network, VPN, and wired). If I come up with something appealing that works I'll add it to the wiki.
Thank you very much for your help!
I am using both scripts as well. So far it is working fabulous. Finally I am free from the mount hangs.
Offline
Oh, are you actually using both scripts at the same time?
What I have just done is to get the pinging part from the cron script and put it in the nm-dispatcher script. That way every time any network comes up or goes down the script pings my NAS and mounts or unmounts accordingly.
Offline
Oh, are you actually using both scripts at the same time?.
I was initially, now I have symlinked to the auto_share script. I am probably going to rewrite the script to ping my server, and then use "showmount -e <host>" if it is up to get the list of shares and compare them to what I have in /etc/fstab to prevent mount from trying to mount something that is not there or that I am not expecting.
Offline
KDE tells me otherwise. I am running NFS on Ubuntu 12.04 server. I can mount it manually with sudo. It is suppose to be nfs4 but nfs4 seems to be only able to mount client-ip:/ and not client-ip:/export/folder and nfs with rpc-statd enabled is able to mount client-ip:/export/folder.
An error occurred while accessing 'Home', the system responded: mount: only root can mount 192.168.0.86:/export/wd on /mnt/wd
client-ip:/export/wd /mnt/wd nfs4 noauto,noatime,rsize=32768,wsize=32768,intr,hard 0 0
cat /bin/auto_share
#!/bin/bash
SERVER="client-ip"
MOUNT_POINTS=`sed -e '/^.*#/d' -e '/^.*:/!d' -e 's/\t/ /g' /etc/fstab | tr -s " " | cut -f2 -d" "`
ping -c 1 "${SERVER}" &>/dev/null
if [ $? -ne 0 ]; then
# The server could not be reached, unmount the shares
for umntpnt in ${MOUNT_POINTS}; do
umount -l -f $umntpnt &>/dev/null
done
else
# The server is up, make sure the shares are mounted
for mntpnt in ${MOUNT_POINTS}; do
mountpoint -q $mntpnt || mount $mntpnt
done
fi
chmod +x /bin/auto_share
crontab -l
* * * * * /bin/auto_share
systemctl enable NetworkManager-dispatch
ln -s /bin/auto_share /etc/NetworkManager/dispatcher.d/30_nfs.sh
Offline
So, I guess the summary so far is: Yes, it is possible, but it doesn't really work as well as it could/should. Better to use a scripted solution.
Better to fix systemd to handle the timeout packet from autofs and actually support this... I believe there's a monetary bounty for whomever implements this.
Offline