linux - How to get hard disk serial number using Python -


how can serial number of hard disk drive using python on linux?

i use python module instead of running external program such hdparm. perhaps using fcntl module?

linux

as suggested, fcntl way on linux. c code want translate looks this:

static struct hd_driveid hd; int fd;  if ((fd = open("/dev/hda", o_rdonly | o_nonblock)) < 0) {     printf("error opening /dev/hda\n");     exit(1); }  if (!ioctl(fd, hdio_get_identity, &hd)) {     printf("%.20s\n", hd.serial_no); } else if (errno == -enomsg) {     printf("no serial number available\n"); } else {     perror("error: hdio_get_identity");     exit(1); } 

translated python on ubuntu 9.10, goes little this:

import sys, os, fcntl, struct  if os.geteuid() >  0:     print("error: must root use")     sys.exit(1)  open(sys.argv[1], "rb") fd:     # tediously derived monster struct defined in <hdreg.h>     # see comment @ end of file verify     hd_driveid_format_str = "@ 10h 20s 3h 8s 40s 2b h 2b h 4b 6h 2b 36h q 152h"     # <hdreg.h>     hdio_get_identity = 0x030d     # how big buffer need?     sizeof_hd_driveid = struct.calcsize(hd_driveid_format_str)      # ensure our format string correct size     # 512 extracted using sizeof(struct hd_id) in c code     assert sizeof_hd_driveid == 512       # call native function     buf = fcntl.ioctl(fd, hdio_get_identity, " " * sizeof_hd_driveid)     fields = struct.unpack(hd_driveid_format_str, buf)     serial_no = fields[10].strip()     model = fields[15].strip()     print("hard disk model: %s" % model)     print("  serial number: %s" % serial_no)   ## documentation purposes, struct copied <hdreg.h> # struct hd_driveid { #   unsigned short  config;     /* lots of obsolete bit flags */ #   unsigned short  cyls;       /* obsolete, "physical" cyls */ #   unsigned short  reserved2;  /* reserved (word 2) */ #   unsigned short  heads;      /* obsolete, "physical" heads */ #   unsigned short  track_bytes;    /* unformatted bytes per track */ #   unsigned short  sector_bytes;   /* unformatted bytes per sector */ #   unsigned short  sectors;    /* obsolete, "physical" sectors per track */ #   unsigned short  vendor0;    /* vendor unique */ #   unsigned short  vendor1;    /* vendor unique */ #   unsigned short  vendor2;    /* retired vendor unique */ #   unsigned char   serial_no[20];  /* 0 = not_specified */ #   unsigned short  buf_type;   /* retired */ #   unsigned short  buf_size;   /* retired, 512 byte increments #                    * 0 = not_specified #                    */ #   unsigned short  ecc_bytes;  /* r/w long cmds; 0 = not_specified */ #   unsigned char   fw_rev[8];  /* 0 = not_specified */ #   unsigned char   model[40];  /* 0 = not_specified */ #   unsigned char   max_multsect;   /* 0=not_implemented */ #   unsigned char   vendor3;    /* vendor unique */ #   unsigned short  dword_io;   /* 0=not_implemented; 1=implemented */ #   unsigned char   vendor4;    /* vendor unique */ #   unsigned char   capability; /* (upper byte of word 49) #                    *  3:  iordysup #                    *  2:  iordysw #                    *  1:  lba #                    *  0:  dma #                    */ #   unsigned short  reserved50; /* reserved (word 50) */ #   unsigned char   vendor5;    /* obsolete, vendor unique */ #   unsigned char   tpio;       /* obsolete, 0=slow, 1=medium, 2=fast */ #   unsigned char   vendor6;    /* obsolete, vendor unique */ #   unsigned char   tdma;       /* obsolete, 0=slow, 1=medium, 2=fast */ #   unsigned short  field_valid;    /* (word 53) #                    *  2:  ultra_ok    word  88 #                    *  1:  eide_ok     words 64-70 #                    *  0:  cur_ok      words 54-58 #                    */ #   unsigned short  cur_cyls;   /* obsolete, logical cylinders */ #   unsigned short  cur_heads;  /* obsolete, l heads */ #   unsigned short  cur_sectors;    /* obsolete, l sectors per track */ #   unsigned short  cur_capacity0;  /* obsolete, l total sectors on drive */ #   unsigned short  cur_capacity1;  /* obsolete, (2 words, misaligned int)     */ #   unsigned char   multsect;   /* current multiple sector count */ #   unsigned char   multsect_valid; /* when (bit0==1) multsect ok */ #   unsigned int    lba_capacity;   /* obsolete, total number of sectors */ #   unsigned short  dma_1word;  /* obsolete, single-word dma info */ #   unsigned short  dma_mword;  /* multiple-word dma info */ #   unsigned short  eide_pio_modes; /* bits 0:mode3 1:mode4 */ #   unsigned short  eide_dma_min;   /* min mword dma cycle time (ns) */ #   unsigned short  eide_dma_time;  /* recommended mword dma cycle time (ns) */ #   unsigned short  eide_pio;       /* min cycle time (ns), no iordy  */ #   unsigned short  eide_pio_iordy; /* min cycle time (ns), iordy */ #   unsigned short  words69_70[2];  /* reserved words 69-70 #                    * future command overlap , queuing #                    */ #   unsigned short  words71_74[4];  /* reserved words 71-74 #                    * identify packet device command #                    */ #   unsigned short  queue_depth;    /* (word 75) #                    * 15:5 reserved #                    *  4:0 maximum queue depth -1 #                    */ #   unsigned short  words76_79[4];  /* reserved words 76-79 */ #   unsigned short  major_rev_num;  /* (word 80) */ #   unsigned short  minor_rev_num;  /* (word 81) */ #   unsigned short  command_set_1;  /* (word 82) supported #                    * 15:  obsolete #                    * 14:  nop command #                    * 13:  read_buffer #                    * 12:  write_buffer #                    * 11:  obsolete #                    * 10:  host protected area #                    *  9:  device reset #                    *  8:  service interrupt #                    *  7:  release interrupt #                    *  6:  look-ahead #                    *  5:  write cache #                    *  4:  packet command #                    *  3:  power management feature set #                    *  2:  removable feature set #                    *  1:  security feature set #                    *  0:  smart feature set #                    */ #   unsigned short  command_set_2;  /* (word 83) #                    * 15:  shall 0 #                    * 14:  shall 1 #                    * 13:  flush cache ext #                    * 12:  flush cache #                    * 11:  device configuration overlay #                    * 10:  48-bit address feature set #                    *  9:  automatic acoustic management #                    *  8:  set max security #                    *  7:  reserved 1407dt parties #                    *  6:  setf sub-command power-up #                    *  5:  power-up in standby feature set #                    *  4:  removable media notification #                    *  3:  apm feature set #                    *  2:  cfa feature set #                    *  1:  read/write dma queued #                    *  0:  download microcode #                    */ #   unsigned short  cfsse;      /* (word 84) #                    * cmd set-feature supported extensions #                    * 15:  shall 0 #                    * 14:  shall 1 #                    * 13:6 reserved #                    *  5:  general purpose logging #                    *  4:  streaming feature set #                    *  3:  media card pass through #                    *  2:  media serial number valid #                    *  1:  smart selt-test supported #                    *  0:  smart error logging #                    */ #   unsigned short  cfs_enable_1;   /* (word 85) #                    * command set-feature enabled #                    * 15:  obsolete #                    * 14:  nop command #                    * 13:  read_buffer #                    * 12:  write_buffer #                    * 11:  obsolete #                    * 10:  host protected area #                    *  9:  device reset #                    *  8:  service interrupt #                    *  7:  release interrupt #                    *  6:  look-ahead #                    *  5:  write cache #                    *  4:  packet command #                    *  3:  power management feature set #                    *  2:  removable feature set #                    *  1:  security feature set #                    *  0:  smart feature set #                    */ #   unsigned short  cfs_enable_2;   /* (word 86) #                    * command set-feature enabled #                    * 15:  shall 0 #                    * 14:  shall 1 #                    * 13:  flush cache ext #                    * 12:  flush cache #                    * 11:  device configuration overlay #                    * 10:  48-bit address feature set #                    *  9:  automatic acoustic management #                    *  8:  set max security #                    *  7:  reserved 1407dt parties #                    *  6:  setf sub-command power-up #                    *  5:  power-up in standby feature set #                    *  4:  removable media notification #                    *  3:  apm feature set #                    *  2:  cfa feature set #                    *  1:  read/write dma queued #                    *  0:  download microcode #                    */ #   unsigned short  csf_default;    /* (word 87) #                    * command set-feature default #                    * 15:  shall 0 #                    * 14:  shall 1 #                    * 13:6 reserved #                    *  5:  general purpose logging enabled #                    *  4:  valid configure stream executed #                    *  3:  media card pass through enabled #                    *  2:  media serial number valid #                    *  1:  smart selt-test supported #                    *  0:  smart error logging #                    */ #   unsigned short  dma_ultra;  /* (word 88) */ #   unsigned short  trseuc;     /* time required security erase */ #   unsigned short  trseuc;     /* time required enhanced erase */ #   unsigned short  curapmvalues;   /* current apm values */ #   unsigned short  mprc;       /* master password revision code */ #   unsigned short  hw_config;  /* hardware config (word 93) #                    * 15:  shall 0 #                    * 14:  shall 1 #                    * 13: #                    * 12: #                    * 11: #                    * 10: #                    *  9: #                    *  8: #                    *  7: #                    *  6: #                    *  5: #                    *  4: #                    *  3: #                    *  2: #                    *  1: #                    *  0:  shall 1 #                    */ #   unsigned short  acoustic;   /* (word 94) #                    * 15:8 vendor's recommended value #                    *  7:0 current value #                    */ #   unsigned short  msrqs;      /* min stream request size */ #   unsigned short  sxfert;     /* stream transfer time */ #   unsigned short  sal;        /* stream access latency */ #   unsigned int    spg;        /* stream performance granularity */ #   unsigned long long lba_capacity_2;/* 48-bit total number of sectors */ #   unsigned short  words104_125[22];/* reserved words 104-125 */ #   unsigned short  last_lun;   /* (word 126) */ #   unsigned short  word127;    /* (word 127) feature set #                    * removable media notification #                    * 15:2 reserved #                    *  1:0 00 = not supported #                    *  01 = supported #                    *  10 = reserved #                    *  11 = reserved #                    */ #   unsigned short  dlf;        /* (word 128) #                    * device lock function #                    * 15:9 reserved #                    *  8   security level 1:max 0:high #                    *  7:6 reserved #                    *  5   enhanced erase #                    *  4   expire #                    *  3   frozen #                    *  2   locked #                    *  1   en/disabled #                    *  0   capability #                    */ #   unsigned short  csfo;       /*  (word 129) #                    * current set features options #                    * 15:4 reserved #                    *  3:  auto reassign #                    *  2:  reverting #                    *  1:  read-look-ahead #                    *  0:  write cache #                    */ #   unsigned short  words130_155[26];/* reserved vendor words 130-155 */ #   unsigned short  word156;    /* reserved vendor word 156 */ #   unsigned short  words157_159[3];/* reserved vendor words 157-159 */ #   unsigned short  cfa_power;  /* (word 160) cfa power mode #                    * 15 word 160 supported #                    * 14 reserved #                    * 13 #                    * 12 #                    * 11:0 #                    */ #   unsigned short  words161_175[15];/* reserved cfa */ #   unsigned short  words176_205[30];/* current media serial number */ #   unsigned short  words206_254[49];/* reserved words 206-254 */ #   unsigned short  integrity_word; /* (word 255) #                    * 15:8 checksum #                    *  7:0 signature #                    */ # }; 

apologies length, thought useful include original c struct comment. also, i'm pretty new both fcntl , struct modules, may doing unidomatic. in anycase, run command-line (with root privelidges) looks (i've redacted exact serial privacy):

fmark@fmark-laptop:~/desktop/hdserial$ sudo python hd.py "/dev/sda" hard disk model: fujitsu mhv2080bh pl   serial number: nw-------- 

what's going on here?

in order able understand going on here, need @ #include <linux/hdreg.h> in original c program. include imports hdio_get_identity constant , struct hd_driveid. i've copied struct comment python sourcecode above, won't repeat here. find out going on hdio_get_identity, grep source-code (on ubuntu @ /usr/include/linux/hdreg.h. should find this:

 #define hdio_get_identity       0x030d  /* ide identification info */ 

thus, find hdio_get_identity constant tells fcntl interested in getting hd info. notice, same value (0x030d integer in hexadecimal notation) assigned variable in python code.

windows

i realise interested in linux, i'll keep here posterity. following hdd serial number on windows (you need install wmi package):

import wmi c = wmi.wmi() item in c.win32_physicalmedia():     print item 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -