Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify devices with udev

Tags:

linux

usb

udev

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?

like image 252
Prismatic Avatar asked Dec 08 '11 04:12

Prismatic


People also ask

What is a udev device?

udev is a generic device manager running as a daemon on a Linux system and listening (via a netlink socket) to uevents the kernel sends out if a new device is initialized or a device is removed from the system.

How do I check my udev?

To see the actions udevd is taking, you can run it in verbose debug mode: sudo pkill udevd. sudo udevd --debug-trace --verbose --suppress-syslog.

What are udev rules used for?

Udev uses rules files that determine how it identifies devices and creates device names. The udev daemon ( udevd ) reads the rules files at system startup and stores the rules in memory.

What is udev folder?

udev supplies the system software with device events, manages permissions of device nodes and may create additional symlinks in the /dev/ directory, or renames network interfaces.


1 Answers

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
like image 145
ILYA Khlopotov Avatar answered Oct 11 '22 10:10

ILYA Khlopotov