1,167 瀏覽數

Linux – udev(4) Rule 語法(syntax)

剛剛我們看到 rules 的撰寫但是其中寫了很多的規則,系統是依據什麼做比對呢!!現在我們直接用 udev 提供的程式 udevinfo 來看 /sys/block/hdc 的光碟裝置(這是我實體的光碟裝置位置,可能會跟你的不盡相同).

[root@benjr ~]# udevinfo -ap /sys/block/hdc
Udevinfo starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
  looking at device '/block/hdc':
    KERNEL=="hdc"
    SUBSYSTEM=="block"
    SYSFS{stat}=="       0        0        0        0        0        0        0        0        0        0        0"
    SYSFS{size}=="8388604"
    SYSFS{removable}=="1"
    SYSFS{range}=="1"
    SYSFS{dev}=="22:0"

  looking at parent device '/devices/ide1/1.0':
    ID=="1.0"
    BUS=="ide"
    DRIVER=="ide-cdrom"
    SYSFS{modalias}=="ide:m-cdrom"
    SYSFS{drivename}=="hdc"
    SYSFS{media}=="cdrom"

  looking at parent device '/devices/ide1':
    ID=="ide1"
    BUS==""
    DRIVER==""

筆對一下  /etc/udev/rules.d/50-udev.rules 哪一些是符合上面條件的
KERNEL=="hd[a-z]", BUS=="ide", SYSFS{removable}=="1", SYSFS{device/media}=="cdrom", SYMLINK+="cdrom cdrom-%k"
KERNEL=="hd[a-z]", BUS=="ide", SYSFS{removable}=="1", PROGRAM=="check-cdrom.sh %k DVD", SYMLINK+="dvd dvd-%k"
KERNEL=="hd[a-z]", BUS=="ide", SYSFS{removable}=="1", PROGRAM=="check-cdrom.sh %k CD-R", SYMLINK+="cdwriter cdwriter-%k cdrw cdrw-%k"
KERNEL=="hd[a-z]", BUS=="ide", SYSFS{removable}=="1", PROGRAM="check-cdrom.sh %k DVD-R", SYMLINK+="dvdwriter dvdwriter-%k dvdrw dvdrw-%k"

目前只有第一個符合,因為其他的會去執行程式 check-cdrom.sh 檢查是否符合條件.
check-cdrom.sh 會去檢查檔案 /proc/sys/dev/cdrom/info ,有興趣的可以看這個 bash file(/lib/udev/check-cdrom.sh)

他既不是 DVD / Cd-R / DVD-R 等裝置.

[root@benjr mnt]# cat /proc/sys/dev/cdrom/info 
CD-ROM information, Id: cdrom.c 3.20 2003/12/17
drive name:             hdc
drive speed:            48
drive # of slots:       1
Can close tray:         1
Can open tray:          1
Can lock tray:          1
Can change speed:       1
Can select disk:        0
Can read multisession:  1
Can read MCN:           1
Reports media changed:  1
Can play audio:         1
Can write CD-R:         0
Can write CD-RW:        0
Can read DVD:           1
Can write DVD-R:        0
Can write DVD-RAM:      0
Can read MRW:           1
Can write MRW:          1
Can write RAM:          0

所以系統只會幫我們產生 /dev/cdrom 和 /dev/cdrom-hdc

[root@benjr ~]# ll /dev/cdrom*
lrwxrwxrwx 1 root root 3 Mar 22 03:17 /dev/cdrom -> hdc
lrwxrwxrwx 1 root root 3 Mar 22 03:17 /dev/cdrom-hdc -> hdc

網友的想法

  1. 自動參照通知: Linux – udev | Benjr.tw

發表迴響