0

Been working on the below sample code to retrieve the connected HDD detail using pyudev.

Devices that I am trying to probe:

  1. regular sata hard disk
  2. USB to SATA converter
  3. USB Sticks

With the below script, I have been able to retrieve the USB sticks information. But, when I connect the USB to SATA converter I get the serial number of the converter not the HDD. Also, for my regular SATA hard drives I get exception on "ID_VENDOR".

sample output:

    Device Connected Info:
    [Device(u'/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.0/host9/target9:0:0/9:0:0:0/block/sdc'), 
    Device(u'/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/host7/target7:0:0/7:0:0:0/block/sdb'), 
    Device(u'/sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda')]

Device Information:
SonyPendrive:
{'model': u'Storage Media', 'vendor': u'Sony', 'serial': u'CB071031B7E215C294'}
SATA IDE Converter
{'model': u'DE SATA Device', 'vendor': u'USB TO I', 'serial': u'000000000033'}
Regular HDD
Traceback (most recent call last):
  File "t.py", line 52, in <module>
    devInfo=decode_device_info(deviceRef)
  File "t.py", line 9, in decode_device_info
    vendor = device['ID_VENDOR'].replace('_',' ')
  File "/home/srivathsan/Desktop/pyudev-pyudev-3a26e05/pyudev/device.py", line 831, in __getitem__
    raise KeyError(property)
KeyError: 'ID_VENDOR'

Sample Code:

import glib
from pyudev import Context, Monitor

def decode_device_info(device):
    ''' Accept a device. Return a dict of attributes of given device.
    Clean up all strings in the process and make pretty.
    '''
    vendor = device['ID_VENDOR'].replace('_',' ')
    model = device['ID_MODEL'].replace('_',' ')
    try:
        serial = device['ID_SERIAL_SHORT']
    except:
        serial = device['ID_SERIAL']
    return({'vendor':vendor, 'model':model, 'serial':serial})

def getDevicelist(udevContext):
    devices=[]
    for device in udevContext.list_devices(subsystem='block', DEVTYPE='disk'):
        # Filter out cd drives, loop devices.
        if device.get('ID_TYPE', '') == 'cd':
            continue
        if device.get('UDISKS_PRESENTATION_NOPOLICY', '0') == '1':
            continue
        devices.append(device)
    return devices

try:
    from pyudev.glib import MonitorObserver

    def device_event(observer, device):
        print 'event {0} on device {1}'.format(device.action, device)
except:
    from pyudev.glib import GUDevMonitorObserver as MonitorObserver

    def device_event(observer, action, device):
        print 'event {0} on device {1}'.format(action, device)

context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='block')
observer = MonitorObserver(monitor)
observer.connect('device-event', device_event)
monitor.start()

devicesConnected=getDevicelist(context)
if (devicesConnected):
    print devicesConnected
    for deviceRef in devicesConnected:
        devInfo=decode_device_info(deviceRef)
        print devInfo 

glib.MainLoop().run()

Is there any parameters I am missing.Please give me some pointers.

emesday
  • 6,078
  • 3
  • 29
  • 46
Ragav
  • 942
  • 4
  • 19
  • 37

1 Answers1

0

For the KeyError you are getting, you can replace

vendor = device['ID_VENDOR'].replace('_',' ')

with one of:

try:
    vendor = device['ID_VENDOR'].replace('_',' ')
except KeyError:
    vendor = ""

or

vendor = device.get('ID_VENDOR', '').replace('_',' ')

or

if 'ID_VENDOR' in device:
    vendor = device['ID_VENDOR'].replace('_',' ')
else:
    vendor = ""

However, I'm afraid I don't know how/if you can use pyudev to get 'beyond' the USB/SATA converter to the device connected to it.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • This makes my system not to detect SATA drives's serial number.This breaks my whole intetntion. Also, is there any other mechanism otherthan pyudev to get hardware information – Ragav Apr 23 '14 at 10:39