UDEV - Run program on USB flash drive insert [closed]

笑着哭i 提交于 2019-12-03 02:37:19
SpliFF

First you need your rule to detect usb storage devices

/etc/udev/rules.d/10-usbmount.rules:

KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/usr/bin/usbdevinserted"

This runs our custom executable shell script /usr/bin/usbdevinserted:

#!/bin/bash

set 2>&1 >> /tmp/usbdevinfo

This sample script dumps the environment variables which you will need to know which device was found, eg:

DEVLINKS='/dev/disk/by-id/usb-Generic_USB_Flash_Disk-0:0 /dev/disk/by-path/pci-0000:00:13.2-usb-0:2:1.0-scsi-0:0:0:0'
DEVNAME=/dev/sdk
DEVPATH=/devices/pci0000:00/0000:00:13.2/usb2/2-2/2-2:1.0/host29/target29:0:0/29:0:0:0/block/sdk
DEVTYPE=disk
ID_BUS=usb
ID_FS_TYPE=
ID_INSTANCE=0:0
ID_MODEL=USB_Flash_Disk
ID_MODEL_ENC='USB\x20Flash\x20Disk\x20\x20'
ID_MODEL_ID=9380
ID_PART_TABLE_TYPE=dos
ID_PART_TABLE_UUID=61d1df0b
ID_PATH=pci-0000:00:13.2-usb-0:2:1.0-scsi-0:0:0:0
ID_PATH_TAG=pci-0000_00_13_2-usb-0_2_1_0-scsi-0_0_0_0
ID_REVISION=7.76
ID_SERIAL=Generic_USB_Flash_Disk-0:0
ID_TYPE=disk
ID_USB_DRIVER=usb-storage
ID_USB_INTERFACES=:080650:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=Generic
ID_VENDOR_ENC='Generic\x20'
ID_VENDOR_ID=058f
MAJOR=8
MINOR=160
SUBSYSTEM=block
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!