Linux

udev 規則執行 2 次

  • June 2, 2015

我寫了一個這樣的udev規則:

SUBSYSTEM=="usb", ACTION=="add", RUN+="//root/usbmon/usb add %b"
SUBSYSTEM=="usb", ACTION=="remove", RUN+="//root/usbmon/usb remove %b"

現在,只要連接和移除 USB,這個 USB 腳本就會發送電子郵件。

但問題是它每次發送 2 封電子郵件。這意味著規則正在執行 2 次。這就是我在日誌中得到的

Apr 26 14:52:00 box2 sendEmail[3374]: Email was sent successfully!
USB Added: Email sent to box2@dmz.domain.com Using Udev Rule at 04-26-14-52-00. 
Apr 26 14:52:00 box2 sendEmail[3394]: Email was sent successfully!
USB Added: Email sent to box2@dmz.domain.com Using Udev Rule at 04-26-14-52-00. 

我不知道它為什麼執行 2 次以及如何阻止它工作一次?我已經驗證該問題在 udev 結束,但不知道如何解決。這是連接 USB 時 udev 監視器的輸出:

KERNEL[1335803018.283341] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4 (usb)
KERNEL[1335803018.284456] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0 (usb)
KERNEL[1335803018.284835] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6 (scsi)
KERNEL[1335803018.284863] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/scsi_host/host6 (scsi_host)
KERNEL[1335803023.524167] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0 (scsi)
KERNEL[1335803023.524735] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0 (scsi)
KERNEL[1335803023.525043] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0 (scsi_disk)
KERNEL[1335803023.525154] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0 (scsi_device)
KERNEL[1335803023.525505] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg6 (scsi_generic)
KERNEL[1335803023.525792] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0 (bsg)
KERNEL[1335803023.526534] add      /devices/virtual/bdi/8:80 (bdi)
UDEV  [1335803023.526895] add      /devices/virtual/bdi/8:80 (bdi)
KERNEL[1335803023.543043] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf (block)
KERNEL[1335803023.543166] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf/sdf1 (block)
UDEV  [1335803028.541785] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4 (usb)
UDEV  [1335803038.850181] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0 (usb)
UDEV  [1335803038.850423] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6 (scsi)
UDEV  [1335803038.850541] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0 (scsi)
UDEV  [1335803038.850568] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/scsi_host/host6 (scsi_host)
UDEV  [1335803038.851736] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0 (scsi)
UDEV  [1335803038.851912] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_disk/6:0:0:0 (scsi_disk)
UDEV  [1335803038.861401] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_device/6:0:0:0 (scsi_device)
UDEV  [1335803038.862757] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/bsg/6:0:0:0 (bsg)
UDEV  [1335803038.863880] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/scsi_generic/sg6 (scsi_generic)
UDEV  [1335803039.002743] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf (block)
UDEV  [1335803039.072845] add      /devices/pci0000:00/0000:00:1a.7/usb1/1-4/1-4:1.0/host6/target6:0:0/6:0:0:0/block/sdf/sdf1 (block)

現在上面的輸出清楚地表明 udev 在 10 秒後再次附加設備,那是我再次收到電子郵件的時間(10 秒後)。同樣,當腳本不可執行時,我在日誌文件中收到以下消息 2 次:

Apr 30 10:22:37 box2 udevd-work[1990]: exec of program '/root/usbmon/usb' failed
Apr 30 10:22:37 box2 udevd-work[1991]: exec of program '/root/usbmon/usb' failed

現在我想知道我需要更改這些設置以使 udev 執行一次。我已經在 2 台伺服器上驗證了同樣的事情。CentOS 和 ubuntu。

該問題已通過添加ENV{DEVTYPE}=="usb_device"規則解決。

我猜您的規則與/dev/sdf設備匹配,但也與第一個分區匹配/dev/sdf1,因此它被觸發了兩次。

如果你只想匹配/dev/sdf1,你可以嘗試類似KERNEL=="sd*1".

引用自:https://serverfault.com/questions/384650