1
votes

I'm trying to detect all the USB pen drives on my raspberry pi in python. And I want all their mount locations. I've checked the APi, but I can't find how I can select the mount point for the devices.

bus = dbus.SystemBus()
  ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
  ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')
  output=[]
  for dev in ud_manager.EnumerateDevices():
    device_obj = bus.get_object("org.freedesktop.UDisks", dev)
    device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)

    if device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsRemovable"):
      temp = []
      **## HOW DO I GET THE MOUNTED ON I SEE IN df -h ?**
      output.append(temp)
  return output

This is what I need:

Filesystem      Size  Used Avail Use% Mounted on
rootfs          2.6G  2.2G  313M  88% /
/dev/root       2.6G  2.2G  313M  88% /
devtmpfs        112M     0  112M   0% /dev
tmpfs            24M  228K   23M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/sdb1        15G  581M   15G   4% **/media/usb0**  <--- I need these
tmpfs            47M     0   47M   0% /run/shm
/dev/mmcblk0p1   56M  9.5M   47M  17% /boot
/dev/sda        3.8G  1.1M  3.8G   1% **/media/usb1**  <--- I need these

For every external USB device I want an array with following information: [manufacturer,total size Human readable (GB),mount point first partition, total size in KB,available size in KB]

EDIT:

I changed my code to this:

def USBDEVICES():
  bus = dbus.SystemBus()
  ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
  ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')
  output=[]
  for dev in ud_manager.EnumerateDevices():
    device_obj = bus.get_object("org.freedesktop.UDisks", dev)
    device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)

    if device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsRemovable"):
      temp = []
      temp.append(str(device_props.Get('org.freedesktop.UDisks.Device', "DriveVendor")))
      temp.append(str(math.ceil(device_props.Get('org.freedesktop.UDisks.Device', "PartitionSize")/1048576.0)/1000)+' GB')
      temp.append(device_props.Get('org.freedesktop.UDisks.Device', "DeviceIsMounted"))
      temp.append(device_props.Get('org.freedesktop.UDisks.Device','DeviceMountPaths'))
      output.append(temp)
  print output

I was hoping to get this output:

[['TDKMedia', '15.255 GB',True,'/media/usb0'],['Sony', '3.812 GB',True,'/media/usb1']

But instead I got this:

[['TDKMedia', '15.255 GB', dbus.Boolean(False, variant_level=1), dbus.Array([], signature=dbus.Signature('s'), variant_level=1)], ['USB2.0', '3.812 GB', dbus.Boolean(True, variant_level=1), dbus.Array([dbus.String(u'/media/usb1')], signature=dbus.Signature('s'), variant_level=1)]]

No mount path for the TDKMedia USB Drive and something strange for the other USB drive. But when I look at df, it says the TDK is mounted... :/

Since the main question has been solved, I've opened a new topic for the not mounted problem: https://stackguides.com/questions/24928984/python-dbus-acting-weird

1
Is parsing the output of mount or df an option?Salem
I think so, but I only need external usb devices and I have absolutely no idea how to do that (new to linux and python) and I also want for every external usb device an array with following inofrmation: [manufacturer,total size Human readable (GB),mount point first partition, total size in KB,available size in KB]Bosiwow

1 Answers

1
votes

According to the org.freedesktop.UDisks.Device documentation, there are the following properties for a Device:

The DeviceIsMounted property

TRUE if the device is mounted.

The DeviceMountPaths property

A list of paths in the root namespace where the root of the device is mounted. This property is only valid if DeviceIsMounted is TRUE.

Alas I am unable to verify, but something as follows ought to work then:

if (device_props.Get('org.freedesktop.UDisks.Device', 'DeviceIsRemovable') and
        device_props.Get('org.freedesktop.UDisks.Device', 'DeviceIsMounted')):
    output.extend(device_props.Get('org.freedesktop.UDisks.Device',
                                   'DeviceMountPaths'))