【问题标题】:Listing details of USB drives using python and udisk2使用 python 和 udisk2 列出 USB 驱动器的详细信息
【发布时间】:2014-06-08 06:47:37
【问题描述】:

我开发了一个应用程序,它使用 udisks 版本 1 来查找和列出连接的 USB 驱动器的详细信息。详细信息包括设备(/dev/sdb1...等)、挂载点和可用空间。但是,我发现现代发行版默认安装了 udisks2。这是在另一个 SO 线程上找到的小代码:-

#!/usr/bin/python2.7
import dbus
bus = dbus.SystemBus()
ud_manager_obj = bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2')
om = dbus.Interface(ud_manager_obj, 'org.freedesktop.DBus.ObjectManager')
for k,v in om.GetManagedObjects().iteritems():
  drive_info = v.get('org.freedesktop.UDisks2.Drive', {})
  if drive_info.get('ConnectionBus') == 'usb' and drive_info.get('Removable'):
    if drive_info['MediaRemovable']:
      print("Device Path: %s" % k)

它产生:-

[sundar@arch ~]$ ./udisk2.py Device Path: /org/freedesktop/UDisks2/drives/JetFlash_Transcend_8GB_GLFK4LYSFG3HZZ48

上面的结果很好,但是如何连接org.freedesktop.UDisks2.Block并获取设备的属性?

http://udisks.freedesktop.org/docs/latest/gdbus-org.freedesktop.UDisks2.Block.html

【问题讨论】:

    标签: python dbus


    【解决方案1】:

    经过多次打击和尝试,我可以得到我想要的。只是发布它,以便将来有人可以受益。这是代码:-

    #!/usr/bin/python2.7
    # coding: utf-8
    import dbus
    
    
    def get_usb():
        devices = []
        bus = dbus.SystemBus()
        ud_manager_obj = bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2')
        om = dbus.Interface(ud_manager_obj, 'org.freedesktop.DBus.ObjectManager')
        try:
            for k,v in om.GetManagedObjects().iteritems():
                drive_info = v.get('org.freedesktop.UDisks2.Block', {})
                if drive_info.get('IdUsage') == "filesystem" and not drive_info.get('HintSystem') and not drive_info.get('ReadOnly'):
                    device = drive_info.get('Device')
                    device = bytearray(device).replace(b'\x00', b'').decode('utf-8')
                    devices.append(device)
        except:
            print "No device found..."
        return devices
    
    
    
    def usb_details(device):
        bus = dbus.SystemBus()
        bd = bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2/block_devices%s'%device[4:])
        try:
            device = bd.Get('org.freedesktop.UDisks2.Block', 'Device', dbus_interface='org.freedesktop.DBus.Properties')
            device = bytearray(device).replace(b'\x00', b'').decode('utf-8')
            print "printing " + device
            label = bd.Get('org.freedesktop.UDisks2.Block', 'IdLabel', dbus_interface='org.freedesktop.DBus.Properties')
            print 'Name od partition is %s'%label
            uuid = bd.Get('org.freedesktop.UDisks2.Block', 'IdUUID', dbus_interface='org.freedesktop.DBus.Properties')
            print 'UUID is %s'%uuid
            size = bd.Get('org.freedesktop.UDisks2.Block', 'Size', dbus_interface='org.freedesktop.DBus.Properties')
            print 'Size is %s'%uuid
            file_system =  bd.Get('org.freedesktop.UDisks2.Block', 'IdType', dbus_interface='org.freedesktop.DBus.Properties')
            print 'Filesystem is %s'%file_system
        except:
            print "Error detecting USB details..."
    

    完整的块设备属性可以在这里找到http://udisks.freedesktop.org/docs/latest/gdbus-org.freedesktop.UDisks2.Block.html

    【讨论】:

    • 如果有人想知道:对于 Python3,您必须更改以下内容:print 处缺少 (),将 iteritems() 替换为 items()
    【解决方案2】:

    编辑 请注意,Block 对象没有 ConnectionBusRemovable 属性。您必须更改代码以删除对 Drive 对象属性的引用,代码才能正常工作。 /编辑

    如果你想连接到Block,而不是Drive,那么代替

    drive_info = v.get('org.freedesktop.UDisks2.Drive', {})
    

    试试

    drive_info = v.get('org.freedesktop.UDisks2.Block', {})
    

    然后你可以遍历drive_info 并输出它的属性。例如,要获取Id 属性,您可以:

    print("Id: %s" % drive_info['Id'])
    

    我确信有一种很好的 Python 方法可以遍历所有属性键/值对并显示值,但我将把它留给你。键是'Id',值是存储在drive_info['Id'] 中的字符串。祝你好运

    【讨论】:

    • 您的答案几乎接近。我又遇到了一个小问题。我想获取详细信息,例如挂载点、uuid、标签、磁盘设备文件的类型 (/dev/sdb1) 等等。当我将drive_info 更改为文件系统等时,它只返回无。一点解释将非常有用。
    • 我不确定我是否理解您将drive_info 更改为文件系统的意思。您应该能够通过我描述的方法获得您在问题末尾链接的网页的属性部分中列出的任何属性。有一个名为IdUUID 的属性,所以我想drive_info['IdUUID'] 会访问该值。如果您正在寻找 Block 对象中未描述的属性,我不知道如何提供帮助(除非它们在 Drive 对象中)
    猜你喜欢
    • 1970-01-01
    • 2017-07-15
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    相关资源
    最近更新 更多