3

我从块子系统中检索第一个设备信息。例如,将 USB 记忆棒分配给 /dev/sdb。这是我可以通过块子系统检索的所有信息:

[(u'DEVLINKS', u'/dev/disk/by-id/usb-Generic_Flash_Disk_63F2B6EC-0:0 /dev/disk/by-label/Fedora-Live-Desktop-x86_64-20-1 /dev/disk/by-path/pci-0000:00:1d.0-usb-0:1.2:1.0-scsi-0:0:0:0 /dev/disk/by-uu
id/2013-12-12-00-56-55-00'),
 (u'DEVNAME', u'/dev/sdb'),
 (u'DEVPATH', u'/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host74/target74:0:0/74:0:0:0/block/sdb'),
 (u'DEVTYPE', u'disk'), (u'ID_BUS', u'usb'), 
 (u'ID_FS_APPLICATION_ID', u'GENISOIMAGE\\x20ISO\\x209660\\x2fHFS\\x20FILESYSTEM\\x20CREATOR\\x20\\x28C\\x29\\x201993\\x20E.YOUNGDALE\\x20\\x28C\\x29\\x201997-2006\\x20J.PEARSON\\x2fJ.SCHILLING\\x20\\x28C\\x29\\x202006-2007\\x20CDRKIT\\x20TEAM'), 
 (u'ID_FS_BOOT_SYSTEM_ID', u'EL\\x20TORITO\\x20SPECIFICATION'), 
 (u'ID_FS_LABEL', u'Fedora-Live-Desktop-x86_64-20-1'),  
 (u'ID_FS_LABEL_ENC', u'Fedora-Live-Desktop-x86_64-20-1'), 
 (u'ID_FS_SYSTEM_ID', u'LINUX'), 
 (u'ID_FS_TYPE', u'iso9660'), 
 (u'ID_FS_USAGE', u'filesystem'), 
 (u'ID_FS_UUID', u'2013-12-12-00-56-55-00'), 
 (u'ID_FS_UUID_ENC', u'2013-12-12-00-56-55-00'), 
 (u'ID_FS_VERSION', u'Joliet Extension'), 
 (u'ID_INSTANCE', u'0:0'), 
 (u'ID_MODEL', u'Flash_Disk'), 
 (u'ID_MODEL_ENC', u'Flash\\x20Disk\\x20\\x20\\x20\\x20\\x20\\x20'), 
 (u'ID_MODEL_ID', u'6387'), 
 (u'ID_PART_TABLE_TYPE', u'dos'), 
 (u'ID_PART_TABLE_UUID', u'5513338d'), 
 (u'ID_PATH', u'pci-0000:00:1d.0-usb-0:1.2:1.0-scsi-0:0:0:0'), 
 (u'ID_PATH_TAG', u'pci-0000_00_1d_0-usb-0_1_2_1_0-scsi-0_0_0_0'), 
 (u'ID_REVISION', u'8.07'), 
 (u'ID_SERIAL', u'Generic_Flash_Disk_63F2B6EC-0:0'),
 (u'ID_SERIAL_SHORT', u'63F2B6EC'), 
 (u'ID_TYPE', u'disk'), 
 (u'ID_USB_DRIVER', u'usb-storage'), 
 (u'ID_USB_INTERFACES', u':080650:'), 
 (u'ID_USB_INTERFACE_NUM', u'00'), 
 (u'ID_VENDOR', u'Generic'), 
 (u'ID_VENDOR_ENC', u'Generic\\x20'), 
 (u'ID_VENDOR_ID', u'058f'), 
 (u'MAJOR', u'8'),
 (u'MINOR', u'16'), 
 (u'MPATH_SBIN_PATH', u'/sbin'), 
 (u'SUBSYSTEM', u'block'), 
 (u'TAGS', u':systemd:'), 
 (u'UDISKS_PRESENTATION_NOPOLICY', u'0'), 
 (u'USEC_INITIALIZED', u'7197321174')]

我想从 USB 子系统获取一些详细信息。但是,如何使用从块子系统中检索到的信息找到我的 USB 设备?

4

1 回答 1

8

PyUSB 确实是获取详细 USB 信息的好地址。但是我使用pyudev来监控USB可移动设备的插入。所以我试图用一个库来做我需要的一切。这是一个有效但很难看的代码(如你所见,我可以使用 pyudev 提取我想要的所有信息,除了 USB 大小/容量):

import glib

from pyudev import Context, Monitor
import pyudev
import subprocess 

def get_block_infos(dev_name):
    dev = pyudev.Device.from_device_file(context, dev_name)


    try:
        objProc = subprocess.Popen('lsblk --nodeps %s | grep -v SIZE  | awk \'{ print $4 }\'' % dev.get('DEVNAME'), shell=True, bufsize=0, executable="/bin/bash", stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except OSError, e:
        print(e)

    #  stdOut.communicate() --> dimension [0]: stdout, dimenstion [1]: stderr
    stdOut = objProc.communicate()

    print('<BLOCK INFORMATION>')
    print('Device name: %s' % dev.get('DEVNAME'))
    print('Device type: %s' % dev.get('DEVTYPE'))
    print('Bus system: %s' % dev.get('ID_BUS'))
    print('Partition label: %s' % dev.get('ID_FS_LABEL'))
    print('FS: %s' % dev.get('ID_FS_SYSTEM_ID'))
    print('FS type: %s' % dev.get('ID_FS_TYPE'))
    print('Device usage: %s' % dev.get('ID_FS_USAGE'))
    print('Device model: %s' % dev.get('ID_MODEL'))
    print('Partition type: %s' % dev.get('ID_PART_TABLE_TYPE'))
    print('USB driver: %s' % dev.get('ID_USB_DRIVER'))
    print('Path id: %s' % dev.get('ID_PATH'))
    print('Capacity: %s' % stdOut[0].strip())
    print('</BLOCK INFORMATION>')

def get_usb_infos(dev_path):
    print('<USB INFORMATION>')

    usb_removable_device = None

    # deprecated and removed from dbus
    # print(pyudev.Device.from_path(context, id_path))  
    # because I found no other documented way, I iterate
    # over all  devices and match my pci path..
    for device in context.list_devices(subsystem='usb'):
        usb_dev_path = device.get('DEVPATH')

        if dev_path.startswith(usb_dev_path):
            # this matches the usb hub,
            # the usb controller and
            # in third place the usb stick
            # so lets watch out for the
            # longest/most precise match

        try:
            if len(usb_dev_path) > len(usb_removable_device.get('DEVPATH')):
                usb_removable_device = device
        except AttributeError:
            # ignore because in first loop
            # usb_removable_device is None and
            # there is no usb_removable_device.get() attrib
            usb_removable_device = device

        # get more information with usb_removable_device.items()
        print('Vendor: %s' % usb_removable_device.get('ID_VENDOR_FROM_DATABASE'))
        print('</USB INFORMATION>')

try:
    from pyudev.glib import MonitorObserver

    def device_event(observer, device):
        get_block_infos(device.get('DEVNAME'))
        get_usb_infos(device.get('DEVPATH'))
except:
    from pyudev.glib import GUDevMonitorObserver as MonitorObserver

    def device_event(observer, action, device):
        get_block_infos(device.get('DEVNAME'))
        get_usb_infos(device.get('DEVPATH'))


context = Context()
monitor = Monitor.from_netlink(context)

monitor.filter_by(subsystem='block')
observer = MonitorObserver(monitor)

observer.connect('device-event', device_event)
monitor.start()

glib.MainLoop().run()

此脚本的示例输出为:

<BLOCK INFORMATION>
Device name: /dev/sdb
Device type: disk
Bus system: usb
Partition label: CentOS-6.5-x86_64-LiveCD
FS: LINUX
FS type: iso9660
Device usage: filesystem
Device model: Patriot_Memory
Partition type: dos
USB driver: usb-storage
Path id: pci-0000:00:1d.0-usb-0:1.2:1.0-scsi-0:0:0:0
Capacity: 14.5G
</BLOCK INFORMATION>
<USB INFORMATION>
Vendor: Kingston Technology Company Inc.
</USB INFORMATION>
<BLOCK INFORMATION>
Device name: /dev/sdb1
Device type: partition
Bus system: usb
Partition label: CentOS-6.5-x86_64-LiveCD
FS: LINUX
FS type: ext4
Device usage: filesystem
Device model: Patriot_Memory
Partition type: dos
USB driver: usb-storage
Path id: pci-0000:00:1d.0-usb-0:1.2:1.0-scsi-0:0:0:0
Capacity: 14.4G
</BLOCK INFORMATION>
<USB INFORMATION>
Vendor: Kingston Technology Company Inc.
</USB INFORMATION>
于 2014-08-26T11:19:46.273 回答