USB drive listing listing using python and udisk2

I developed an application that uses udisks version 1 to search and list the details of connected USB drives. Details include the device (/dev/sdb1...etc), mount point, and free space. However, I found that modern distributions have udisks2 installed by default. Here is a little code found on another 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 good, but how can I connect org.freedesktop.UDisks2.Blockand get device properties?

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

+4
source share
2 answers

, . , - . : -

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

+8

Edit , Block ConnectionBus Removable. , Drive . /Edit

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']. .

0

Source: https://habr.com/ru/post/1537702/


All Articles