A simple command to request the hard drive serial number from a device.
authorBrandon King <kingb@caltech.edu>
Thu, 4 Jun 2009 22:55:37 +0000 (22:55 +0000)
committerBrandon King <kingb@caltech.edu>
Thu, 4 Jun 2009 22:55:37 +0000 (22:55 +0000)
 * Used py_sg (easy_install py_sg)

htsworkflow/util/hdquery.py [new file with mode: 0644]

diff --git a/htsworkflow/util/hdquery.py b/htsworkflow/util/hdquery.py
new file mode 100644 (file)
index 0000000..ff16387
--- /dev/null
@@ -0,0 +1,25 @@
+import os
+
+#try:
+import py_sg
+#except:
+#    print 'ERROR: Please install py_sg (easy_install py_sg)'
+    
+    
+def get_hd_serial_num(device):
+    """
+    device = '/dev/sdX'
+    
+    returns hard drive serial number for a device; requires read permissions.
+    """
+    fd = os.open(device, os.O_RDONLY)
+    
+    # fd: device object
+    # \x12: INQUIRY CMD; \x01: EVPD bit set to 1; \x80: Unit Serial Number page
+    #  See http://en.wikipedia.org/wiki/SCSI_Inquiry_Command for helpful chart
+    # ##: # byte buffer for returned data
+    data = py_sg.read(fd, "\x12\x01\x80", 32)
+    
+    # Remove extra \x00's, and split remaining data into two chunks,
+    #  the 2nd of which is the serial number
+    return data.strip('\x00').split()[1]