简体   繁体   中英

Listing details of USB drives using python and udisk2

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM