我开发了一个应用程序,它使用udsks版本1查找并列出已连接USB驱动器的详细信息。详细信息包括设备(/dev/sdb1...etc),安装点和可用空间。但是,我发现现代发行版默认安装了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
答案 0 :(得分:8)
经过大量的打击和试验,我可以得到我想要的东西。只是发布它,以便将来有人可以受益。这是代码: -
#!/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
答案 1 :(得分:0)
修改强>
请注意,Block
对象没有ConnectionBus
或Removable
属性。您将不得不更改代码以删除对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'])
我确信有一种很好的pythonic方法来遍历所有属性键/值对并显示值,但我会留给你。键为'Id'
,值为drive_info['Id']
中存储的字符串。祝你好运