Finding only disk drives using pyudev

前端 未结 2 2108
不知归路
不知归路 2021-01-20 16:33

I\'m looking to enumerate all hard disks on a computer using udev and specifically pyudev to enumerate everything:

import pyudev

context = pyudev.Context()
         


        
2条回答
  •  长发绾君心
    2021-01-20 17:01

    Since udev only runs on Linux kernel (at least as of now), you could filter out by MAJOR number 8 which represents all SCSI/SATA disk driver based devices.

    for device in context.list_devices(MAJOR='8'):
        if (device.device_type == 'disk'):
            print "{}, ({})".format(device.device_node, device.device_type)
    

    On my system, your code outputs the following:

    /dev/sda, (disk)
    /dev/sdf, (disk)
    /dev/sdb, (disk)
    /dev/sdc, (disk)
    /dev/sdd, (disk)
    /dev/sde, (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)
    

    After filtering by major number 8, I see the following output:

    /dev/sda, (disk)
    /dev/sdf, (disk)
    /dev/sdb, (disk)
    /dev/sdc, (disk)
    /dev/sdd, (disk)
    /dev/sde, (disk)
    

    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 or hdX 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. :D

    UPDATE: 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:

    for device in context.list_devices(DEVTYPE='disk'):
        major = device['MAJOR']
        if major == '8' or major == '3':
            print "{}, ({})".format(device.device_node, device.device_type)
    

提交回复
热议问题