简体   繁体   English

挂载远程fs-挂载后和卸载前脚本(Linux)

[英]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. 我正在尝试找出一种方法,用于在挂载诸如cifs或nfs的特定远程fs以及即将被挂载时运行脚本。 I have entries in my fstab so mounting icons are automatically created on my desktop. 我的fstab中有条目,因此会在桌面上自动创建安装图标。 But I need to mount an overlay fs when some specific remote fs is mounted and unmount it before remote fs gets unmounted. 但是我需要在安装某些特定的远程fs时安装覆盖fs,并在卸载远程fs之前先将其卸载。 With the udev monitor I see add/remove notifications but attributes are pretty useless: 使用udev监视器,我可以看到添加/删除通知,但是属性几乎没有用:

~$ 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. 您没有提到编程语言,因此我将继续使用pyudev ,它是udev的包装器。

It provides an easy way to monitor for events emitted by udev and react to them. 它提供了一种监视udev发出的事件并对它们做出反应的简便方法。 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). 每当添加,移除设备(例如,插入或拔出USB棒)或更改其属性(例如,更改电池的电量)时,Linux内核都会发出事件。 With pyudev.Monitor you can react on such events, for example to react on added or removed mountable filesystems: 使用pyudev.Monitor可以对此类事件做出反应,例如对添加或删除的可挂载文件系统做出反应:

>>> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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