강좌 & 팁
글 수 2,412
2015.05.22 14:54:43 (*.134.169.166)
40224
Ubuntu 기준으로 설명합니다. 이 설명에서 수행하는 명령들은 모두 root 권한을 필요로 합니다. sudo를 사용하거나 root 계정으로 로그인해서 실행해야합니다.
/etc/udev/rules.d 에서 udev로 인식된 장치들에 대한 설정을 할 수 있습니다.
우선 sdcard를 먼저 꽂고 나서 dmesg를 통해서 어떤 device로 인식되었는지를 확인합니다.
/dev/uba로 인식되었다고 가정하고 다음 명령을 수행합니다.
root@localhost # udevadm info -a -n /dev/uba Udevadm info 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 '/devices/platform/fsl-ehci.1/usb2/2-1/2-1:1.0/block/uba': KERNEL=="uba" SUBSYSTEM=="block" DRIVER=="" ATTR{range}=="8" ATTR{ext_range}=="8" ATTR{removable}=="1" ATTR{ro}=="0" ATTR{size}=="7569408" ATTR{alignment_offset}=="0" ATTR{discard_alignment}=="0" ATTR{capability}=="11" ATTR{stat}==" 163 0 1304 270 0 0 0 0 0 180 270" ATTR{inflight}==" 0 0" ATTR{events}=="" ATTR{events_async}=="" ATTR{events_poll_msecs}=="2000" looking at parent device '/devices/platform/fsl-ehci.1/usb2/2-1/2-1:1.0': KERNELS=="2-1:1.0" SUBSYSTEMS=="usb" DRIVERS=="ub" ATTRS{bInterfaceNumber}=="00" ATTRS{bAlternateSetting}==" 0" ATTRS{bNumEndpoints}=="02" ATTRS{bInterfaceClass}=="08" ATTRS{bInterfaceSubClass}=="06" ATTRS{bInterfaceProtocol}=="50" ATTRS{supports_autosuspend}=="0" looking at parent device '/devices/platform/fsl-ehci.1/usb2/2-1': KERNELS=="2-1" SUBSYSTEMS=="usb" DRIVERS=="usb" ATTRS{configuration}=="" ATTRS{bNumInterfaces}==" 1" ATTRS{bConfigurationValue}=="1" ATTRS{bmAttributes}=="80" ATTRS{bMaxPower}=="500mA" ATTRS{urbnum}=="695" ATTRS{idVendor}=="05e3" ATTRS{idProduct}=="0723" ATTRS{bcdDevice}=="9451" ATTRS{bDeviceClass}=="00" ATTRS{bDeviceSubClass}=="00" ATTRS{bDeviceProtocol}=="00" ATTRS{bNumConfigurations}=="1" ATTRS{bMaxPacketSize0}=="64" ATTRS{speed}=="480" ATTRS{busnum}=="2" ATTRS{devnum}=="2" ATTRS{devpath}=="1" ATTRS{version}==" 2.00" ATTRS{maxchild}=="0" ATTRS{quirks}=="0x0" ATTRS{avoid_reset_quirk}=="0" ATTRS{authorized}=="1" ATTRS{manufacturer}=="Generic " ATTRS{product}=="USB Storage" ATTRS{serial}=="000000009451" looking at parent device '/devices/platform/fsl-ehci.1/usb2': KERNELS=="usb2" SUBSYSTEMS=="usb" DRIVERS=="usb" ATTRS{configuration}=="" ATTRS{bNumInterfaces}==" 1" ATTRS{bConfigurationValue}=="1" ATTRS{bmAttributes}=="e0" ATTRS{bMaxPower}==" 0mA" ATTRS{urbnum}=="27" ATTRS{idVendor}=="1d6b" ATTRS{idProduct}=="0002" ATTRS{bcdDevice}=="0300" ATTRS{bDeviceClass}=="09" ATTRS{bDeviceSubClass}=="00" ATTRS{bDeviceProtocol}=="01" ATTRS{bNumConfigurations}=="1" ATTRS{bMaxPacketSize0}=="64" ATTRS{speed}=="480" ATTRS{busnum}=="2" ATTRS{devnum}=="1" ATTRS{devpath}=="0" ATTRS{version}==" 2.00" ATTRS{maxchild}=="1" ATTRS{quirks}=="0x0" ATTRS{avoid_reset_quirk}=="0" ATTRS{authorized}=="1" ATTRS{manufacturer}=="Linux 3.0.35 ehci_hcd" ATTRS{product}=="Freescale On-Chip EHCI Host Controller" ATTRS{serial}=="fsl-ehci.1" ATTRS{authorized_default}=="1" looking at parent device '/devices/platform/fsl-ehci.1': KERNELS=="fsl-ehci.1" SUBSYSTEMS=="platform" DRIVERS=="fsl-ehci" looking at parent device '/devices/platform': KERNELS=="platform" SUBSYSTEMS=="" DRIVERS==""
우선 몇가지 키워드들을 잡아봅시다.
KERNEL=="uba"
DRIVERS=="usb"
그리고 /etc/udev/rules.d에 50-sdcard-detector.rules이라는 파일을 다음과 같이 생성합니다.
ACTION=="add", KERNEL=="ub[a-z]", DRIVERS=="usb", RUN+="/bin/sh /root/inserted.sh %k"
여기서 %k 는 kernel으로 들어온 값을 의미합니다. 즉 "uba"라는 값이 들어갑니다.
/root/inserted.sh를 다음과 같이 생성합니다.
#!/bin/sh echo $1 is inserted >> /root/sdcard.log
이렇게 설정하고 sdcard를 뽑았다가 다시 꽂으면 /root/sdcard.log는 다음과 같이 확인이 됩니다.
root@imja-armhf:~# cat sdcard.log uba is inserted
스크립트 및 설정파일들을 조금씩 더 개선하면 sdcard가 꽂혔을 때 특정 동작을 하는 환경을 만들 수 있습니다.