ff163874e40675f0b5964743d5fe08e0bacd3216
[htsworkflow.git] / htsworkflow / util / hdquery.py
1 import os
2
3 #try:
4 import py_sg
5 #except:
6 #    print 'ERROR: Please install py_sg (easy_install py_sg)'
7     
8     
9 def get_hd_serial_num(device):
10     """
11     device = '/dev/sdX'
12     
13     returns hard drive serial number for a device; requires read permissions.
14     """
15     fd = os.open(device, os.O_RDONLY)
16     
17     # fd: device object
18     # \x12: INQUIRY CMD; \x01: EVPD bit set to 1; \x80: Unit Serial Number page
19     #  See http://en.wikipedia.org/wiki/SCSI_Inquiry_Command for helpful chart
20     # ##: # byte buffer for returned data
21     data = py_sg.read(fd, "\x12\x01\x80", 32)
22     
23     # Remove extra \x00's, and split remaining data into two chunks,
24     #  the 2nd of which is the serial number
25     return data.strip('\x00').split()[1]