Listing details of USB drives using python and udisk2

断了今生、忘了曾经 提交于 2020-01-12 08:38:07

问题


I have developed an application which uses udisks version 1 to find and list details of connected USB drives. The details include device (/dev/sdb1...etc), mount point, and free space. However, I found that modern distros has udisks2 installed by default. Here is the little code found on the other SO thread:-

#!/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)

It produces:-

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

The above result is fine but how can I connect org.freedesktop.UDisks2.Block and get properties of the devices?

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


回答1:


After lot of hit and trial, I could get what I wanted. Just posting it so that some one can benefit in the future. Here is the code:-

#!/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..."

The complete block device properties can be found here http://udisks.freedesktop.org/docs/latest/gdbus-org.freedesktop.UDisks2.Block.html




回答2:


Edit Note that the Block object does not have ConnectionBus or Removable properties. You will have to change the code to remove references to Drive object properties for the code to work. /Edit

If you want to connect to Block, not Drive, then instead of

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

try

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

Then you can iterate through drive_info and output it's properties. For example, to get the Id property, you could:

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

I'm sure that there is a nice pythonic way to iterate through all the property key/value pairs and display the values, but I'll leave that to you. Key being 'Id' and value being the string stored in drive_info['Id']. Good luck



来源:https://stackoverflow.com/questions/23244245/listing-details-of-usb-drives-using-python-and-udisk2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!