1

I wish to utilize several conditions to either do a bind mount or an NFS mount as described below. I have seen (limited) discussion on simple conditional mounts, but I have not seen any examples for using conditions similar to "(bind) mount X if Y else mount Z."

Details: I have a USB HDD that I can optionally connect to my laptop and it is automatically mounted upon boot using an /etc/fstab entry like this:

/dev/mapper/xusbhdd /path/to/usb/mount  btrfs  nofail,x-systemd.device-timeout=10,noatime,nodiratime,space_cache,compress=lzo,defaults,subvol=/@mysubvol/              0 0

Inside that mount point is a folder: /path/to/usb/mount/my_special_folder

When that device is mounted, I wish to create a bind mount like this:

mount --bind /path/to/usb/mount/my_special_folder /path2/my_special_folder

However, if that USB device is not connected, but I am connected to my company LAN, I wish to mount an NFS share at that location instead, using an /etc/fstab entry like this:

fileserver:/my_special_folder/   /path2/my_special_folder/ nfs     _netdev,defaults,noatime,nodiratime,soft,retrans=6,timeo=20,retry=0,rsize=32768,wsize=32768,proto=tcp   0 0

Obviously, I can do all that manually. My question is how to make it automatic. If my laptop sees the USB HDD connected, it establishes the desired mount and bind mount. If, instead, the company file server is accessible, it establishes the NFS mount.

I found some clues in this answer: https://unix.stackexchange.com/a/568351/15010

I also found a very old answer (2010) that uses a bash script, which is not the approach I wish to take: Conditional mount in Linux: dev or nfs - Server Fault

I also found a related answer here: Systemd mount with a condition. However, my requirements are more complex. From that link, I believe I could utilize a unit file similar to this:

[Unit]
Description=Mount my_special_folder from USB HDD
ConditionPathExists=/path2/my_special_folder/NOT_MOUNTED

[Mount]
What=/dev/mapper/xusbhdd
Where=/path/to/usb/mount
Type=auto
Options=nofail,x-systemd.device-timeout=10,noatime,nodiratime,space_cache,compress=lzo,defaults,subvol=/@mysubvol/

[Install]
WantedBy=local-fs.target

However, that actually accomplishes nothing for me, because the fstab options already mount the USB device if it exists, or skip it without failing.

Therefore, I need a unit file to do the bind mount conditionally and a unit file for the NFS mount with a condition. Furthermore, I need a way to ensure that the NFS share is not mounted if the USB device has already been mounted.

2
  • 1
    Can't you simply leave out the fstab stuff altogether? If I understand it correctly systemd "synthesizes" those mount units (whose names also unhelpfully appear in logs and elsewhere, but which you cannot "grasp" via systemctl at all) and so dropping your own .mount units should do the job, no? But cool question. Looking forward to some input from others. Commented Sep 13, 2020 at 20:16
  • @0xC0000022L - yes, I am OK with skipping fstab entries and only using unit files. I am studying this article for clues, while hoping for helpful answers here. blog.iwakd.de/systemd-fstab-and-bind-mounts-with-options Commented Sep 13, 2020 at 20:23

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.