udev规则中的脚本未运行


8

我正在笔记本电脑上运行Ubuntu 9.10(Karmic Koala),并希望NumLock根据我的USB键盘是插入(数字锁定打开)还是拔出(数字锁定关闭)自动切换。

为此,我首先安装了“ numlockx”软件包。numlockx on并且numlockx off工作正常。

为了连接到设备系统,我想我会使用udev。我已经阅读了“编写udev规则”,但是我无法让udev规则正常工作。

首先,这是dmesg输出示例:

[20906.985102] USB 3-2:使用uhci_hcd和地址6的新低速USB设备
[20907.166403] USB 3-2:从1个选项中选择了配置#1
[20907.192904]输入:MicrosoftNatural®人体工学键盘4000作为/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.0/input/input20
[20907.193100] Microsoft 0003:045E:00DB.000B:input,hidraw1:usb-0000:00:1a.0-2 / input0上的USB HID v1.11键盘[MicrosoftNatural®Ergonomic Keyboard 4000]
[20907.217810]输入:MicrosoftNatural®人体工学键盘4000为/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.1/input/input21
[20907.217979] Microsoft 0003:045E:00DB.000C:input,hidraw2:usb-0000:00:1a.0-2 / input1上的USB HID v1.11设备[MicrosoftNatural®Ergonomic Keyboard 4000]

我曾经udevadm info收集设备信息:

> udevadm信息-a -p /devices/pci0000:00/0000:00:1a.0/usb3/3-2/2/3-2:1.1/input/input21

  查看设备'/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.1/input/input21':
    KERNEL ==“ input21”
    SUBSYSTEM ==“输入”
    驾驶员==“”
    ATTR {phys} ==“ usb-0000:00:1a.0-2 / input1”
    ATTR {uniq} ==“”
    ATTR {modalias} ==“ input:b0003v045Ep00DBe0111-e0,1,2,3,4,14,k71,72,73,74,75,77,79,7A,7B,7C,7D,7E,7F,80 ,81,82,83,84,85,86,87,88,89,8A,8B,8C,8E,90,96,98,9B,9C,9E,9F,A1,A3,A4,A5,A6 ,A7,A8,A9,AB,AC,AD,AE,B0,B1,B2,B3,B4,B5,B6,B8,B9,BA,BB,BC,BD,BE,BF,C0,C1,C2 ,CE,CF,D0,D1,D2,D5,D9,DB,DF,E2,E7,E8,E9,EA,EB,F0,100,162,166,16A,16E,178,179,17A,17B,17C,17D,17F ,180,181,182,185,18C,18D,192,193,195,1A0,1A1,1A2,1A3,1A4,1A5,1A6,1A7,1A8,1A9,1AA,1AB,1AC,1AD,1AE,1B0,1B1,1B7,r6,a20,m4 ,lsfw“

  查看父设备'/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.1':
    内核==“ 3-2:1.1”
    SUBSYSTEMS ==“ usb”
    DRIVERS ==“ usbhid”
    ATTRS {bInterfaceNumber} ==“ 01”
    ATTRS {bAlternateSetting} ==“ 0”
    ATTRS {bNumEndpoints} ==“ 01”
    ATTRS {bInterfaceClass} ==“ 03”
    ATTRS {bInterfaceSubClass} ==“ 00”
    ATTRS {bInterfaceProtocol} ==“ 00”
    ATTRS {modalias} ==“ usb:v045Ep00DBd0173dc00dsc00dp00ic03isc00ip00”
    ATTRS {supports_autosuspend} ==“ 1”

  查看父设备'/devices/pci0000:00/0000:00:1a.0/usb3/3-2':
    内核==“ 3-2”
    SUBSYSTEMS ==“ usb”
    DRIVERS ==“ usb”
    ATTRS {configuration} ==“”
    ATTRS {bNumInterfaces} ==“ 2”
    ATTRS {bConfigurationValue} ==“ 1”
    ATTRS {bmAttributes} ==“ a0”
    ATTRS {bMaxPower} ==“ 100mA”
    ATTRS {urbnum} ==“ 532”
    ATTRS {idVendor} ==“ 045e”
    ATTRS {idProduct} ==“ 00db”
    ATTRS {bcdDevice} ==“ 0173”
    ATTRS {bDeviceClass} ==“ 00”
    ATTRS {bDeviceSubClass} ==“ 00”
    ATTRS {bDeviceProtocol} ==“ 00”
    ATTRS {bNumConfigurations} ==“ 1”
    ATTRS {bMaxPacketSize0} ==“ 8”
    ATTRS {speed} ==“ 1.5”
    ATTRS {busnum} ==“ 3”
    ATTRS {devnum} ==“ 6”
    ATTRS {version} ==“ 2.00”
    ATTRS {maxchild} ==“ 0”
    ATTRS {quirks} ==“ 0x0”
    ATTRS {authorized} ==“ 1”
    ATTRS {manufacturer} ==“ Microsoft”

  查看父设备'/devices/pci0000:00/0000:00:1a.0/usb3':
    内核==“ usb3”
    SUBSYSTEMS ==“ usb”
    DRIVERS ==“ usb”
    ATTRS {configuration} ==“”
    ATTRS {bNumInterfaces} ==“ 1”
    ATTRS {bConfigurationValue} ==“ 1”
    ATTRS {bmAttributes} ==“ e0”
    ATTRS {bMaxPower} ==“ 0mA”
    ATTRS {urbnum} ==“ 127”
    ATTRS {idVendor} ==“ 1d6b”
    ATTRS {idProduct} ==“ 0001”
    ATTRS {bcdDevice} ==“ 0206”
    ATTRS {bDeviceClass} ==“ 09”
    ATTRS {bDeviceSubClass} ==“ 00”
    ATTRS {bDeviceProtocol} ==“ 00”
    ATTRS {bNumConfigurations} ==“ 1”
    ATTRS {bMaxPacketSize0} ==“ 64”
    ATTRS {speed} ==“ 12”
    ATTRS {busnum} ==“ 3”
    ATTRS {devnum} ==“ 1”
    ATTRS {version} ==“ 1.10”
    ATTRS {maxchild} ==“ 2”
    ATTRS {quirks} ==“ 0x0”
    ATTRS {authorized} ==“ 1”
    ATTRS {manufacturer} ==“ Linux 2.6.31-16-通用uhci_hcd”
    ATTRS {product} ==“ UHCI主机控制器”
    ATTRS {serial} ==“ 0000:00:1a.0”
    ATTRS {authorized_default} ==“ 1”

  查看父设备“ /devices/pci0000:00/0000:00:1a.0”:
    KERNELS ==“ 0000:00:1a.0”
    SUBSYSTEMS ==“ pci”
    DRIVERS ==“ uhci_hcd”
    ATTRS {vendor} ==“ 0x8086”
    ATTRS {device} ==“ 0x2937”
    ATTRS {subsystem_vendor} ==“ 0x1558”
    ATTRS {subsystem_device} ==“ 0x0860”
    ATTRS {class} ==“ 0x0c0300”
    ATTRS {irq} ==“ 16”
    ATTRS {local_cpus} ==“ ff”
    ATTRS {local_cpulist} ==“ 0-7”
    ATTRS {modalias} ==“ pci:v00008086d00002937sv00001558sd00000860bc0Csc03i00”
    ATTRS {broken_parity_status} ==“ 0”
    ATTRS {msi_bus} ==“”

  查看父设备“ / devices / pci0000:00”:
    内核==“ pci0000:00”
    SUBSYSTEMS ==“”
    DRIVERS ==“”

所以我创建了一个文件/etc/udev/rules.d/usb-keyboard.rules

#插入键盘后打开NumLock。
ACTION ==“ add”,ATTRS {manufacturer} ==“ Microsoft”,SUBSYSTEM ==“ input”,RUN + =“ / usr / bin / numlockx on”

#拔下键盘时,关闭NumLock。
ACTION ==“ remove”,ATTRS {manufacturer} ==“ Microsoft”,SUBSYSTEM ==“ input”,RUN + =“ / usr / bin / numlockx off”

我曾经用来udevadm test验证规则是否正确:

> udevadm test --action = add /devices/pci0000:00/0000:00:1a.0/usb3/3-2/2/3-2:1.1/input/input21
run_command:调用:测试
udevadm_test:147版

[...]
parse_file:读取“ /etc/udev/rules.d/usb-keyboard.rules”作为规则文件
udev_rules_new:规则使用180864个字节的令牌(15072 * 12个字节),31614个字节的缓冲区
udev_rules_new:使用的临时索引为49760字节(2488 * 20字节)
udev_device_new_from_syspath:设备0x28d7d80具有devpath'/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.1/input/input21'
udev_rules_apply_to_event:运行'/ sbin / modprobe -b $ env {MODALIAS}'/lib/udev/rules.d/80-drivers.rules:5
udev_rules_apply_to_event:运行'socket:@ / org / freedesktop / hal / udev_event'/lib/udev/rules.d/90-hal.rules:2
udev_device_new_from_syspath:设备0x28d8560具有devpath'/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.1'
udev_device_new_from_syspath:设备0x28d8708具有devpath'/devices/pci0000:00/0000:00:1a.0/usb3/3-2'
udev_rules_apply_to_event:运行'/ usr / bin / numlockx on'/etc/udev/rules.d/usb-keyboard.rules:7
udevadm_test:UDEV_LOG = 6
udevadm_test:DEVPATH = / devices / pci0000:00/0000:00:1a.0 / usb3 / 3-2 / 2 / 3-2:1.1 / input / input21
udevadm_test:PRODUCT = 3 / 45e / db / 111
udevadm_test:NAME =“ MicrosoftNatural®人体工学键盘4000”
udevadm_test:PHYS =“ usb-0000:00:1a.0-2 / input1”
udevadm_test:UNIQ =“”
udevadm_test:EV == 10001f
udevadm_test:KEY == 837fff 2c3027 bf004444 0 0 1 10f84 8a27c007 ff7f7bfa d9415fff febeffdf ffefffff ffffffff fffffffe
udevadm_test:REL == 40
udevadm_test:ABS == 1 0
udevadm_test:MSC == 10
udevadm_test:MODALIAS = input:b0003v045Ep00DBe0111-e0,1,2,3,4,14,k71,72,73,74,75,77,79,7A,7B,7C,7D,7E,7F,80,81, 82,83,84,85,86,87,88,89,8A,8B,8C,8E,90,96,98,9B,9C,9E,9F,A1,A3,A4,A5,A6,A7, A8,A9,AB,AC,AD,AE,B0,B1,B2,B3,B4,B5,B6,B8,B9,BA,BB,BC,BD,BE,BF,C0,C1,C2,CE, CF,D0,D1,D2,D5,D9,DB,DF,E2,E7,E8,E9,EA,EB,F0,100,162,166,16A,16E,178,179,17A,17B,17C,17D,17F,180,181,182,185, 18C,18D,192,193,195,1A0,1A1,1A2,1A3,1A4,1A5,1A6,1A7,1A8,1A9,1AA,1AB,1AC,1AD,1AE,1B0,1B1,1B7,r6,a20,m4,lsfw
udevadm_test:ACTION =添加
udevadm_test:SUBSYSTEM =输入
udevadm_test:运行:'/ sbin / modprobe -b输入:b0003v045Ep00DBe0111-e0,1,2,3,4,14,k71,72,73,74,75,77,79,7A,7B,7C,7D,7E ,7F,80,81,82,83,84,85,86,87,88,89,8A,8B,8C,8E,90,96,98,9B,9C,9E,9F,A1,A3,A4 ,A5,A6,A7,A8,A9,AB,AC,AD,AE,B0,B1,B2,B3,B4,B5,B6,B8,B9,BA,BB,BC,BD,BE,BF,C0 ,C1,C2,CE,CF,D0,D1,D2,D5,D9,DB,DF,E2,E7,E8,E9,EA,EB,F0,100,162,166,16A,16E,178,179,17A,17B,17C ,17D,17F,180,181,182,185,18C,18D,192,193,195,1A0,1A1,1A2,1A3,1A4,1A5,1A6,1A7,1A8,1A9,1AA,1AB,1AC,1AD,1AE,1B0,1B1,1B7,r6 ,a20,m4,lsfw'
udevadm_test:运行:'socket:@ / org / freedesktop / hal / udev_event'
udevadm_test:运行:'/ usr / bin / numlockx on'

这是“删除”测试:

> udevadm test --action =删除/devices/pci0000:00/0000:00:1a.0/usb3/3-2/2/3-2:1.1/input/input21

run_command:调用:测试
udevadm_test:147版

[...]
parse_file:读取“ /etc/udev/rules.d/usb-keyboard.rules”作为规则文件
udev_rules_new:规则使用180864个字节的令牌(15072 * 12个字节),31614个字节的缓冲区
udev_rules_new:使用的临时索引为49760字节(2488 * 20字节)
udev_device_new_from_syspath:设备0xd2fd80具有devpath'/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.1/input/input21'
udev_rules_apply_to_event:运行'socket:@ / org / freedesktop / hal / udev_event'/lib/udev/rules.d/90-hal.rules:2
udev_device_new_from_syspath:设备0xd2fff8具有devpath'/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.1'
udev_device_new_from_syspath:设备0xd30690具有devpath'/devices/pci0000:00/0000:00:1a.0/usb3/3-2'
udev_rules_apply_to_event:运行'/ usr / bin / numlockx off'/etc/udev/rules.d/usb-keyboard.rules:10
udevadm_test:UDEV_LOG = 6
udevadm_test:DEVPATH = / devices / pci0000:00/0000:00:1a.0 / usb3 / 3-2 / 2 / 3-2:1.1 / input / input21
udevadm_test:PRODUCT = 3 / 45e / db / 111
udevadm_test:NAME =“ MicrosoftNatural®人体工学键盘4000”
udevadm_test:PHYS =“ usb-0000:00:1a.0-2 / input1”
udevadm_test:UNIQ =“”
udevadm_test:EV == 10001f
udevadm_test:KEY == 837fff 2c3027 bf004444 0 0 1 10f84 8a27c007 ff7f7bfa d9415fff febeffdf ffefffff ffffffff fffffffe
udevadm_test:REL == 40
udevadm_test:ABS == 1 0
udevadm_test:MSC == 10
udevadm_test:MODALIAS = input:b0003v045Ep00DBe0111-e0,1,2,3,4,14,k71,72,73,74,75,77,79,7A,7B,7C,7D,7E,7F,80,81, 82,83,84,85,86,87,88,89,8A,8B,8C,8E,90,96,98,9B,9C,9E,9F,A1,A3,A4,A5,A6,A7, A8,A9,AB,AC,AD,AE,B0,B1,B2,B3,B4,B5,B6,B8,B9,BA,BB,BC,BD,BE,BF,C0,C1,C2,CE, CF,D0,D1,D2,D5,D9,DB,DF,E2,E7,E8,E9,EA,EB,F0,100,162,166,16A,16E,178,179,17A,17B,17C,17D,17F,180,181,182,185, 18C,18D,192,193,195,1A0,1A1,1A2,1A3,1A4,1A5,1A6,1A7,1A8,1A9,1AA,1AB,1AC,1AD,1AE,1B0,1B1,1B7,r6,a20,m4,lsfw
udevadm_test:ACTION =删除
udevadm_test:SUBSYSTEM =输入
udevadm_test:运行:'socket:@ / org / freedesktop / hal / udev_event'
udevadm_test:运行:'/ usr / bin / numlockx off'

问题是插入或拔出键盘后,NumLock状态不会更改。我尝试重启udev服务,但没有成功。我的udev规则有误吗?我会以错误的方式处理吗?


固定!感谢tony-p-lee和whitequark为我指出了正确的方向。
埃里克·海克斯

Answers:


5

问题是您可能已经在某些X环境中(如其名称所示)测试了numlockx。X客户端(连接到X服务器的GUI程序,例如Firefox或GEdit)需要知道要连接的服务器,并且还必须通过某种授权。无论是root用户还是普通用户,都尝试从普通控制台登录,然后启动一些GUI应用程序:由于不知道,它将显示一些与DISPLAY相关的错误。

要解决此问题,您需要设置环境变量DISPLAY;如果只有一台X服务器,则它几乎总是具有地址:0。

在普通控制台上尝试以下操作:键入numlockx on,它将显示“错误打开显示!”。键入DISPLAY=:0 numlockx on,它将起作用(至少对我有用)。

因此,您可以在udev中设置此环境变量,或者只是启动sh -c 'DISPLAY=:0 numlockx <state>'


你是对的!我没有考虑过numlockx需要一个X环境。不幸的是,尽管设置DISPLAY可以消除错误消息,但是无论是在控制台还是通过udev,它都无法正常工作。
埃里克·海克斯

发言时间过早。如果您使用完整路径运行shell,它将起作用:/bin/sh -c '...'
Eric Heikes 2010年

4

这两个命令可以用于调试udev问题很多:

 udevd --debug-trace --verbose
 strace -p pid_of_udevd -f -e trace=open,exec

请注意,args可能会在不同版本上发生变化...


3
出色的技巧-尽管我的udevd上没有“冗长”的选项。udevd --debug这是我追踪问题所需要的。
埃里克·海克斯

@EricHeikes:最终设法获得一些有用的输出,为什么udev shell脚本不起作用。谢谢!
Mikko Ohtamaa

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.