You are not logged in.

#1 2013-07-13 17:44:16

DonOregano
Member
From: Uppsala, Sweden
Registered: 2013-07-13
Posts: 6

Can systemd automounter unmount inactive fileystems?

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

#2 2013-07-13 19:55:06

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: Can systemd automounter unmount inactive fileystems?

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

#3 2013-07-14 05:47:51

donniezazen
Member
From: Salt Lake City
Registered: 2011-06-24
Posts: 671
Website

Re: Can systemd automounter unmount inactive fileystems?

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.

Source:-

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

#4 2013-07-14 06:00:59

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: Can systemd automounter unmount inactive fileystems?

@donniezazen, please read the systemd.mount(5).

Offline

#5 2013-07-14 08:51:02

demizer
Member
From: Gilroy, CA
Registered: 2010-03-03
Posts: 116
Website

Re: Can systemd automounter unmount inactive fileystems?

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

#6 2013-07-14 20:30:08

demizer
Member
From: Gilroy, CA
Registered: 2010-03-03
Posts: 116
Website

Re: Can systemd automounter unmount inactive fileystems?

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

#7 2013-07-14 21:49:24

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Can systemd automounter unmount inactive fileystems?

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.

https://bbs.archlinux.org/viewtopic.php … 0#p1260240


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#8 2013-07-14 23:46:53

demizer
Member
From: Gilroy, CA
Registered: 2010-03-03
Posts: 116
Website

Re: Can systemd automounter unmount inactive fileystems?

graysky wrote:
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.

https://bbs.archlinux.org/viewtopic.php … 0#p1260240

Much better. Thanks! Have you thought about adding this to the NFS wiki?

Last edited by demizer (2013-07-14 23:48:03)

Offline

#9 2013-07-14 23:59:09

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Can systemd automounter unmount inactive fileystems?

No, but feel free.  It's a wiki after all wink


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#10 2013-07-15 03:25:23

demizer
Member
From: Gilroy, CA
Registered: 2010-03-03
Posts: 116
Website

Re: Can systemd automounter unmount inactive fileystems?

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

#11 2013-07-15 06:48:17

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,597
Website

Re: Can systemd automounter unmount inactive fileystems?

@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 packagesZsh and other configs

Offline

#12 2013-07-15 15:34:56

demizer
Member
From: Gilroy, CA
Registered: 2010-03-03
Posts: 116
Website

Re: Can systemd automounter unmount inactive fileystems?

graysky wrote:

@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

#13 2013-07-15 15:37:10

WonderWoofy
Member
From: Los Gatos, CA
Registered: 2012-05-19
Posts: 8,414

Re: Can systemd automounter unmount inactive fileystems?

noauto should make it fine.

Offline

#14 2013-07-15 18:44:54

DonOregano
Member
From: Uppsala, Sweden
Registered: 2013-07-13
Posts: 6

Re: Can systemd automounter unmount inactive fileystems?

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

#15 2013-07-15 19:07:08

demizer
Member
From: Gilroy, CA
Registered: 2010-03-03
Posts: 116
Website

Re: Can systemd automounter unmount inactive fileystems?

DonOregano wrote:

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

#16 2013-07-15 19:28:32

DonOregano
Member
From: Uppsala, Sweden
Registered: 2013-07-13
Posts: 6

Re: Can systemd automounter unmount inactive fileystems?

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

#17 2013-07-15 22:00:39

demizer
Member
From: Gilroy, CA
Registered: 2010-03-03
Posts: 116
Website

Re: Can systemd automounter unmount inactive fileystems?

DonOregano wrote:

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

#18 2013-07-19 15:42:50

donniezazen
Member
From: Salt Lake City
Registered: 2011-06-24
Posts: 671
Website

Re: Can systemd automounter unmount inactive fileystems?

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

#19 2013-07-19 16:25:28

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Can systemd automounter unmount inactive fileystems?

DonOregano wrote:

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

Board footer

Powered by FluxBB