4

I'd like to have a program executed before a mount attempt is made for a particular device/share/mount. For example, I'd like for autofs/amd to control /data/{1..10}, and when a process opens /data/4 (and /data/4 is not currently mounted), a script is invoked, such as '/usr/local/bin/preparedata 4' (4 being the mount point name within the autofs controlled directory), prior to the attempt to mount. For example, I could dynamically attach an iSCSI LUN (which would be referenced in the autofs map), or startup a remote system/VM which has an NFS export (which is specified in the map).

I'd be glad to add details if missing.

Update: I've noticed that systemd appears to be intercepting open() calls, is there some way to do this particularly in systemd?

Brian Chrisman
  • 2,732
  • 1
  • 12
  • 14
  • Any luck with this? I have a similar need. See http://unix.stackexchange.com/questions/189404/how-to-keep-a-redundant-home-directory-for-offline-use – JFlo Mar 12 '15 at 18:58
  • nope.. only thing I can imagine is modifying some underlying code... – Brian Chrisman Mar 30 '15 at 03:19

2 Answers2

5

Autofs itself can run a custom script or program to dynamically provide "the map", i.e. the mount options and arguments autofs uses for mounting.

As an example, to automount home directories from an NFS-server one may prefer to use a pattern like "/home/user12/user123456" for the homedir paths to limit the number of sub-directories on a server when there are many users.

To dynamically mount such home directories, you could put this in your /etc/auto.master:

/home    program:/usr/local/sbin/autofs-home-mapper.sh

The script /usr/local/sbin/autofs-home-mapper.sh could look like this:

#!/bin/bash
echo "-fstype=nfs4,relatime nfs.example.com:/exported/${1%????}/${1}"

When the local directory /home/johndoe is accessed, autofs will run the script with one argument: johndoe

Output of this script will then be:

-fstype=nfs4,relatime nfs.example.com:/exported/joh/johndoe

...which is then used by autofs to mount /home/johndoe

Don't forget to set eXecute permission on the script, as it can be difficult to track down a bug like that.

More information in man 5 auto.master (look under "map-type") and man 5 autofs.

Hkoof
  • 675
  • 5
  • 12
  • ahh, I saw that when originally researching, but thought it was run once to generate the map, not run each time a keyword is looked up in the map. If the latter is the case, then this solves the problem quite nicely! – Brian Chrisman Aug 15 '18 at 20:43
  • It'll be run every time it needs to get mounted. – Hkoof Aug 16 '18 at 09:48
1

Take a look at x-systemd.requires=, and x-systemd.after= for hooks executed before mounting.

These options can be specified inside /etc/fstab.


Related question: Hook before an .automount unmounts.

Tom Hale
  • 25,410
  • 16
  • 132
  • 172