我正在尝试找出一种方法,用于在挂载诸如cifs或nfs的特定远程fs以及即将被挂载时运行脚本。我的fstab中有条目,因此会在桌面上自动创建安装图标。但是我需要在安装某些特定的远程fs时安装覆盖fs,并在卸载远程fs之前先将其卸载。使用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"


那有什么我可以代替的吗?
谢谢

最佳答案

您没有提到编程语言,因此我将继续使用pyudev,它是udev的python包装器。

它提供了一种监视udev发出的事件并对它们做出反应的简便方法。以下是他们的文档中的示例:


  每当添加,删除设备时,Linux内核都会发出事件
  (例如已插入或拔出USB记忆棒)或具有其属性
  已更改(例如,电池的电量已更改)。用
  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

关于linux - 挂载远程fs-挂载后和卸载前脚本(Linux),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23968130/

10-10 17:37