4

I am trying to get the serial number (or any unique hardware based identifier) of a USB flash drive using Python on Linux. I started with the recipe I found in this SO question.

The code in that link work great for fixed hard drives. For example on my internal laptop SSD it returns:

$ sudo python clusterTool.py /dev/sda
Hard Disk Model: SAMSUNG SSD PM810 2.5" 7mm 256GB
  Serial Number: S0NUNYAB503012

Rocking!

However when I run it on a flash-stick I get a error from ioctl that is getting ungracefully coherced to the following python trace:

$ sudo python clusterTool.py /dev/sdb
Traceback (most recent call last):
  File "clusterTool.py", line 21, in 
    buf = fcntl.ioctl(fd, HDIO_GET_IDENTITY, " " * sizeof_hd_driveid)
IOError: [Errno 22] Invalid argument

File suggests they are the same sort of beast:

$ file /dev/sd{a,b}
/dev/sda: block special
/dev/sdb: block special

I am not sure how they differ, but they clearly diverge somewhere. What is the difference between these devices, and is there any way I uniformly get their identifiers?

Community
  • 1
  • 1
meawoppl
  • 2,714
  • 1
  • 23
  • 30
  • I have an interim solution that involves scraping the output of [this](http://stackoverflow.com/questions/8653505/read-usb-serial-code-in-bash-using-udevadm) It is hack-hack-ugly and depends on shell scripts etc. Looking for a real answer still. – meawoppl Sep 10 '13 at 20:36

1 Answers1

0

This might help http://www.roman10.net/linux-get-udev-device-information-in-pythonpyudev/ using pyudev and python setup tools with syspath.

Benjamin Barenblat
  • 1,311
  • 6
  • 19
linux_fanatic
  • 4,767
  • 3
  • 19
  • 20