I'm looking to enumerate all hard disks on a computer using udev and specifically pyudev to enumerate everything:
import pyudev
context = pyudev.Context()
for device in context.list_devices(subsystem='block', DEVTYPE='disk'):
print "{}, ({})".format(device.device_node, device.device_type)
This prints out the following:
/dev/sdb (disk)
/dev/sdc (disk)
/dev/sda (disk)
/dev/sr0 (disk)
/dev/loop0 (disk)
/dev/loop1 (disk)
/dev/loop2 (disk)
/dev/loop3 (disk)
/dev/loop4 (disk)
/dev/loop5 (disk)
/dev/loop6 (disk)
/dev/loop7 (disk)
/dev/ram0 (disk)
/dev/ram1 (disk)
/dev/ram10 (disk)
/dev/ram11 (disk)
/dev/ram12 (disk)
/dev/ram13 (disk)
/dev/ram14 (disk)
/dev/ram15 (disk)
/dev/ram2 (disk)
/dev/ram3 (disk)
/dev/ram4 (disk)
/dev/ram5 (disk)
/dev/ram6 (disk)
/dev/ram7 (disk)
/dev/ram8 (disk)
/dev/ram9 (disk)
Since I'm mainly concerned with actual drives and not optical disk drives, loopback devices, or ram devices, how can I filter my results down to only get real physical media?
Since the device management under udev is very flexible, it may depend on the udev rules installed in
/lib/udev/rules.d
or/etc/udev/rules.d
that determine what keys you can filter on.You can use
udevadm
to get a list of devices and their associated udev keys.Similarly, your script could be modified to output this information as follows:
This will allow you to determine what keys are available for filtering. On my Debian system, some of the entries look like this:
It would be possible to filter on these key/value pairs to return just those disks which are real. The
UDISKS_PRESENTATION_NOPOLICY
entry for example, can be used to filter out loop devices. You can peruse the udev rules to determine how it tries to distinguish between real/virtual disks.Something like this will filter out cdroms and loop devices:
Since
udev
only runs on Linux kernel (at least as of now), you could filter out byMAJOR number 8
which represents all SCSI/SATA disk driver based devices.On my system, your code outputs the following:
After filtering by major number 8, I see the following output:
Note that you would also get USB hard drives and USB sticks in the list, since they also tend to use the same SCSI disk driver.
I'm not really sure if IDE hard drives are mapped as
sdX
orhdX
with the latest 2.6 or 3.x kernels. Do not have an IDE hard drive to verify and long since I have had one. :DUPDATE: The same device number page lists
/dev/hdX
to be the one used by IDE hard drives (and IDE cdroms too may be ?). If you want to filter these as well, I believe you could do something like this: