我正在尝试使用udev规则和bash脚本在树莓派上设置外部USB驱动器插入时的自动驱动器扫描(clamscan)。
我在 /lib/udev/rules.d/11-myrules.rules 中编写了以下规则:
KERNEL=="sd*[!0-9]", ACTION=="add", SUBSYSTEM=="block", RUN+="/usr/bin/my_usb_inserted.sh"
KERNEL=="sd*[!0-9]", ACTION=="remove", SUBSYSTEMS=="usb", RUN+="/usr/bin/turn_off_leds.sh &"
/usr/bin/my_usb_inserted.sh
#!/bin/bash
/usr/bin/my_clamscan.sh & exit
/usr/bin/my_clamscan.sh
#!/bin/bash
sleep 5
file="/tmp/clamscan.log"
export DISPLAY=":0.0"
if [ -f $file ]; then
rm -f $file
fi
date > $file
echo "----------- START CLAMSCAN -----------" >> $file
/usr/bin/clamscan -r -i /media/pi/ >> $file
if [ $? -eq 0 ]; then
echo "----------- NO VIRUS FOUND -----------" >> $file
elif [ $? -eq 1 ]; then
echo "----------- VIRUS FOUND -----------" >> $file
else
echo "----------- ERRORS OCCURED -----------" >> $file
fi
exit 0
当我插入USB闪存驱动器时,脚本执行得很好。问题是启动扫描时尚未安装USB闪存驱动器。脚本完成后,我可以看到驱动器已安装。
这肯定是Udev规则的问题,但是我不知道如何解决它。我试图添加一个sleep 5
并在后台运行脚本,以允许在午餐之前就装入USB闪存驱动器,但是它不起作用。
当我插入USB闪存驱动器时,我可以看到:
clamscan.log
Wed Oct 3 14:12:49 CEST 2018
----------- START CLAMSCAN -----------
udevadm监控器
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
KERNEL[743.611572] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
KERNEL[743.611812] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
KERNEL[743.612522] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0 (scsi)
KERNEL[743.612794] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/scsi_host/host0 (scsi_host)
UDEV [743.640904] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
UDEV [743.648863] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
UDEV [743.654294] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0 (scsi)
UDEV [743.660319] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/scsi_host/host0 (scsi_host)
KERNEL[744.631052] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0 (scsi)
KERNEL[744.631237] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0 (scsi)
KERNEL[744.631352] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0 (scsi_disk)
KERNEL[744.631492] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/scsi_device/0:0:0:0 (scsi_device)
KERNEL[744.631896] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/scsi_generic/sg0 (scsi_generic)
KERNEL[744.633008] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/bsg/0:0:0:0 (bsg)
KERNEL[744.634633] add /devices/virtual/bdi/8:0 (bdi)
KERNEL[744.639142] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/block/sda (block)
UDEV [744.640478] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0 (scsi)
UDEV [744.642551] add /devices/virtual/bdi/8:0 (bdi)
UDEV [744.649485] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0 (scsi)
UDEV [744.658436] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/scsi_device/0:0:0:0 (scsi_device)
UDEV [744.659061] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0 (scsi_disk)
UDEV [744.663760] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/scsi_generic/sg0 (scsi_generic)
UDEV [744.665498] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/bsg/0:0:0:0 (bsg)
扫描完成后:
clamscan.log
Wed Oct 3 14:12:49 CEST 2018
----------- START CLAMSCAN -----------
----------- SCAN SUMMARY -----------
Known viruses: 6663263
Engine version: 0.100.0
Scanned directories: 1
Scanned files: 0
Infected files: 0
Data scanned: 0.00 MB
Data read: 0.00 MB (ratio 0.00:1)
Time: 78.549 sec (1 m 18 s)
----------- NO VIRUS FOUND -----------
PS:Clamscan应该找到病毒,因为我的USB闪存驱动器上有测试病毒,它根本找不到病毒,因为/ media / pi不包含该闪存驱动器的安装点。当clamscan运行时,我在/ media / pi中运行 ls ,并且该文件夹显然是空的。 clamscan完成后,将安装驱动器,并且/ media / pi包含我的闪存驱动器的内容。
然后我可以看到 udevadm监视器
中安装的磁盘UDEV [828.642230] add /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/host0/target0:0:0/0:0:0:0/block/sda (block)