1.writing-udev-rules, the easy way
# lsusb
Bus 005 Device 003: ID 04fc:0c25 Sunplus Technology Co., Ltd
Bus 005 Device 001: ID 0000:0000
Bus 004 Device 001: ID 0000:0000
Bus 003 Device 001: ID 0000:0000
Bus 002 Device 001: ID 0000:0000
Bus 001 Device 004: ID 062a:0000 Creative Labs Optical Mouse
Bus 001 Device 001: ID 0000:0000
# cd /etc/udev/rules.d
As the device is a storage device we will be writing the rule in "60-persistent-storage.rules"
So open this file with your prefered editor (I recommend scite in gui and vim in console) and add the following line:
KERNEL=="sd*[0-9]",SYSFS{idVendor}=="04fc",SYSFS{idProduct}=="0c25", SYMLINK+="sata"
This informs udev-manager, that if any storage device with vendour-id, 04fc and product-id 0c25 is plugged into the system add a symlink in /dev with name, sata.
Note: This rule is specific to the device only. And also this is not the only rule that you can write.
2.writing-udev-rules, the other way
# tail -f /var/log/messages
Jul 13 16:47:20 workbench kernel: [ 5418.740803] sd 7:0:0:0: [sdb] 312581808 512-byte hardware sectors (160042 MB)
Jul 13 16:47:20 workbench kernel: [ 5418.743055] sd 7:0:0:0: [sdb] Write Protect is off
Jul 13 16:47:21 workbench kernel: [ 5418.743072] sdb: sdb1
Jul 13 16:47:21 workbench kernel: [ 5419.323103] sd 7:0:0:0: [sdb] Attached SCSI disk
Jul 13 16:47:21 workbench kernel: [ 5419.323163] sd 7:0:0:0: Attached scsi generic sg2 type 0
# udevinfo -q path -n /dev/sdb1
/block/sdb/sdb1
# udevinfo -a -p /block/sdb/sdb1
looking at device '/block/sdb/sdb1':
KERNEL=="sdb1"
SUBSYSTEM=="block"
DRIVER==""
ATTR{dev}=="8:17"
ATTR{start}=="63"
ATTR{size}=="312576642"
ATTR{stat}==" 11011 11620 0 0"
looking at parent device '/block/sdb':
KERNELS=="sdb"
SUBSYSTEMS=="block"
DRIVERS==""
ATTRS{dev}=="8:16"
ATTRS{range}=="16"
ATTRS{removable}=="0"
ATTRS{size}=="312581808"
ATTRS{stat}==" 206 10881 12228 1688 0 0 0 0 0 1272 1688"
ATTRS{capability}=="12"
...
looking at parent device '/devices/pci0000:00/0000:00:1d.7':
KERNELS=="0000:00:1d.7"
SUBSYSTEMS=="pci"
DRIVERS=="ehci_hcd"
ATTRS{vendor}=="0x8086"
ATTRS{device}=="0x24dd"
ATTRS{subsystem_vendor}=="0x8086"
ATTRS{subsystem_device}=="0x4246"
ATTRS{class}=="0x0c0320"
ATTRS{irq}=="19"
ATTRS{local_cpus}=="ff"
ATTRS{modalias}=="pci:v00008086d000024DDsv00008086sd00004246bc0Csc03i20"
ATTRS{broken_parity_status}=="0"
ATTRS{msi_bus}==""
looking at parent device '/devices/pci0000:00':
KERNELS=="pci0000:00"
SUBSYSTEMS==""
DRIVERS==""
So you can choose any block and write the rule for that.
Let me zoom into a block
looking at device '/block/sdb/sdb1':
KERNEL=="sdb1"
SUBSYSTEM=="block"
DRIVER==""
ATTR{dev}=="8:17"
ATTR{start}=="63"
ATTR{size}=="312576642"
ATTR{stat}==" 11011 11620 0 0"
KERNEL=="sdb1", SUBSYSTEM=="block", ATTR{dev}=="8:17", ATTR{start}=="63", ATTR{size}=="312576642", SYMLINK+="myDisk"
Now, this is also not the only rule. Go on! Generate your own rule. Mind with the spaces like in case of ATTR{stat}==" 11011 11620 0 0".
Wait: place the rule in /etc/udev/rules.d/60-persistent-storage.rules. Plug out the device and again plug-in to see the change.
调试lshal -m
可以查看一些插拔事件
/etc/init.d/udev stop
/etc/init.d/udev start
or udevstart
KERNEL=="sd[b-z]", NAME="%k", SYMLINK+="usbhd-%k", GROUP="users", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[b-z][0-9]", SYMLINK+="usbhd-%k", GROUP="users", NAME="%k"
ACTION=="add", KERNEL=="sd[b-z][0-9]", RUN+="/bin/mkdir -p /media/usbhd-%k"
我们如何来看是匹配规则还是动作,我们可以看KERNEL==这边是两个等于,那就是匹配,后面的都是一个等于,那就是动作
在 /etc/udev/rules.d 目录下创建文件 10_usbkey.rules, 其内容如下
KERNEL=="sda1", SUBSYSTEM=="block", RUN+="/root/usbmount.sh"
然后, 在 /root 目录中创建文件 usbmount.sh, 其内容为
#!/bin/bash
LOG=/var/log/usb-hotplug.log
lap=$(date --rfc-3339=ns)
echo "$lap: $DEVPATH requesting $ACTION" >> $LOG
if [ $ACTION == "add" ]
then
mount -t vfat -o umask=000,noatime,async,codepage=936,iocharset=gb2312 \
/dev/sda1 /media/usbkey
elif [ $ACTION == "remove" ]
then
umount -l /media/usbkey
fi
并把该文件属性设置为可执行, chmod a+x usbmount.sh
注意, 如果你的 linux 上 locale 是 zh_CN.utf-8, 需要把上面的 iocharset=gb2312 改成 iocharset=utf8
最后创建目录, mkdir /media/usbkey
这是一个非常简单但可用的例子. U盘插上后自动 mount 到 /media/usbkey , 拔出后自动 umount. 你可以查看 /var/log/usb-hotplug.log , 里面会有些简单的调用记录.
阅读(988) | 评论(0) | 转发(0) |