简体   繁体   中英

Mounting remote fs - post mount and pre unmount scripts (linux)

I'm trying to figure out a way to run a script when a specific remote fs like cifs or nfs is mounted and also when it's about to be unmounted. I have entries in my fstab so mounting icons are automatically created on my desktop. But I need to mount an overlay fs when some specific remote fs is mounted and unmount it before remote fs gets unmounted. With the udev monitor I see add/remove notifications but attributes are pretty useless:

~$ udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[41113.912505] add      /devices/virtual/bdi/cifs-2 (bdi)
UDEV  [41113.913868] add      /devices/virtual/bdi/cifs-2 (bdi)
^

~$ udevadm info -a -p /devices/virtual/bdi/cifs-2

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/virtual/bdi/cifs-2':
    KERNEL=="cifs-2"
    SUBSYSTEM=="bdi"
    DRIVER==""
    ATTR{min_ratio}=="0"
    ATTR{stable_pages_required}=="0"
    ATTR{read_ahead_kb}=="1024"
    ATTR{max_ratio}=="100"

Is there anything I can use instead then? Thanks

You did not mention a programming language, so I am going to go ahead with pyudev , which is a wrapper to udev.

It provides an easy way to monitor for events emitted by udev and react to them. Here is the example from their documentation:

The Linux kernel emits events whenever devices are added, removed (eg a USB stick was plugged or unplugged) or have their attributes changed (eg the charge level of the battery changed). With pyudev.Monitor you can react on such events, for example to react on added or removed mountable filesystems:

>>> monitor = pyudev.Monitor.from_netlink(context)
>>> monitor.filter_by('block')
>>> for device in iter(monitor.poll, None):
...     if 'ID_FS_TYPE' in device:
...         print('{0} partition {1}'.format(action, device.get('ID_FS_LABEL')))
...
add partition MULTIBOOT
remove partition MULTIBOOT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM