2

we create the following service on redhat version 7.2

more nfsshare.mount
[Unit]
  Description=nfs mount script
  Requires=network-online.target
  After=network-online.service

[Mount]

  What=master1:/nfs
  Where=/nfsshare
  Options=noauto,user,rw,group
  Type=nfs
  TimeoutIdleSec=10

[Install]
  WantedBy=multi-user.target

after restart the service the mount point is mounted as should be

master1:/nfs             41932800 6559744  35373056  16% /nfsshare

now we umount the /nfsshare folder

umount /nfsshare

now we expecting /nfsshare folder will be mounted again and service will automatically mout the folder

but nothing is happens and folder not mounted

where I am wrong here?

I also try

 more nfsshare.automount
[Unit]
  Description=nfs mount script
  Requires=network-online.target
  After=network-online.service

[Automount]
  What=master1:/nfs
  Where=/nfsshare
  Options=noauto,user,rw,group
  Type=nfs
  TimeoutIdleSec=10

[Install]
  WantedBy=multi-user.target

but this service even not mount the folder after restart

3

The correct setup is for unit nfsshare.mount to contain:

[Unit]
Description=Mount nfsshare
Wants=network-online.target
After=network-online.target

[Mount]
What=master1:/nfs
Where=/nfsshare
Options=rw,group
Type=nfs

And for unit nfsshare.automount to contain:

[Unit]
Description=Automount nfsshare
Wants=network-online.target
After=network-online.target

[Automount]
Where=/nfsshare
TimeoutIdleSec=10

[Install]
WantedBy=multi-user.target

And you typically want to enable the .automount unit only:

$ sudo systemctl enable nfsshare.automount

With this setup, right after you boot (assuming no running daemons have accessed the NFS share), then the share will not be mounted. But once it is accessed (with e.g. ls /nfsshare) then it will be mounted on demand. It will stay mounted for 10s and then unmounted, but mounted once again after accessed another time.

The two units .mount and .automount work together to trigger this behavior. The .automount unit simply needs to know the mount point directory where to set up an automount. Once that directory is accessed, then it will trigger a request for the .mount unit with the same name (for the same path) and will wait until that unit is up before proceeding.

See the man page for automount units for more details.


What else have I changed from your setup?

For networking dependencies, you should use network-online.target, not .service which doesn't exist. I'm also using Wants=, which is the recommended directive for this case (see this wiki page for a deeper discussion on the network targets.)

I removed noauto and user from your mount options, as those only really make sense in /etc/fstab. I'd also argue you don't really want to have user in there, just have it automounted instead of allowing unprivileged users to mount it calling the mount util, automounting is better.

The directives taken on which unit are distinct (except for Where= that is taken by both), so I only kept the valid ones there.

I removed the [Install] section of the .mount unit, since you don't need to enable it. You only need to enable the .automount unit so it's brought up at boot, then that unit will be responsible for bringing up (and down) the mount unit whenever needed (depending on the target directory being accessed.)


Note that you can also configure all this (including the automount) in /etc/fstab. In fact, systemd typically recommends using /etc/fstab for configuration rather than creating mount units.

For automounting, all you need to do is add a x-systemd.automount pseudo-option to the list of mount options of your /etc/fstab entry for this filesystem. (You typically want to include noauto as well in that case.)

See this section of the Arch Linux wiki for more details. (None of that is really specific to Arch Linux, it should all work exactly the same on RHEL 7.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.