【问题标题】:Mounting remote fs - post mount and pre unmount scripts (linux)挂载远程 fs - 挂载后和卸载前脚本 (linux)
【发布时间】:2014-05-31 08:44:01
【问题描述】:

我正在尝试找出一种方法来在安装特定远程 fs(如 cifs 或 nfs)时以及即将卸载时运行脚本。我的 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"

那有什么我可以用的吗? 谢谢

【问题讨论】:

  • 如果我不清楚:问题是 udev 没有提供足够的信息来创建规则。在上面的示例中,我无法确定安装了哪些远程 fs。因为显然我可以在 fstab 中定义多个 cifs 挂载点

标签: linux mount nfs udev cifs


【解决方案1】:

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

它提供了一种简单的方法来监视 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

【讨论】:

    猜你喜欢
    • 2018-10-11
    • 2017-12-15
    • 2011-10-21
    • 2013-03-02
    • 1970-01-01
    • 2012-12-14
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多