How to identify devices with udev

Prismatic picture Prismatic · Dec 8, 2011 · Viewed 8.4k times · Source

I'd like to use libudev to watch for certain devices. Specifically, I want to monitor for removable storage: USB Hard Drives, USB Keys, SD cards, etc. The libudev API lets you find a device if you know that device's parent's 'subsystem' and 'devtype'. I tried the devices out on my computer and used udevadm to find that all the storage types had device subsystem of 'block'->'scsi', but I have no idea what devtype these devices have. Is there a list of devtypes and subsystems I can use as a reference somewhere, or a better method to look up devtype?

Answer

ILYA Khlopotov picture ILYA Khlopotov · Dec 8, 2011

You can get list of subsystems with ls /sys/class/ I'm not sure about device types though. I guess you can get this using:

ls -l  /sys/class/scsi_disk/
total 0
lrwxrwxrwx 1 root root 0 2011-12-07 21:20 0:0:0:0 -> ../../devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0
cat /sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0/device/vendor
ATA     
cat /sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0/device/model
ST9500325AS

You can try other files in device directory.

Actually I think you need:

cat /sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0/device/type
0

cat /usr/include/scsi/scsi.h | grep TYPE_
#define TYPE_DISK           0x00
#define TYPE_TAPE           0x01
#define TYPE_PROCESSOR      0x03    /* HP scanners use this */
#define TYPE_WORM           0x04    /* Treated as ROM by our system */
#define TYPE_ROM            0x05
#define TYPE_SCANNER        0x06
#define TYPE_MOD            0x07    /* Magneto-optical disk -
#define TYPE_MEDIUM_CHANGER 0x08
#define TYPE_ENCLOSURE  0x0d    /* Enclosure Services Device */
#define TYPE_NO_LUN         0x7f