linux c 获取硬盘的序列号

获取硬盘序列号的方法

找到指定硬盘的设备文件(如果是根目录的话,可以通过读取 /etc/mtab 文件获取),打开对应的设备文件。然后使用系统调用ioctl。

在这里 ioctl 的第二个参数为 HDIO_GET_IDENTITY (获得指定文件描述符的标志号)

第三个参数为 struct hd_driveid ,在 linux/hdreg.h 中。 struct hd_driveid 结构声明如下:

/*
 * Structure returned by HDIO_GET_IDENTITY, as per ANSI NCITS ATA6 rev.1b spec.
 *
 * If you change something here, please remember to update fix_driveid() in
 * ide/probe.c.
 */
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;  /* for 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 is 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), with IORDY */
    unsigned short  words69_70[2];  /* reserved words 69-70
                     * future command overlap and queuing
                     */
    unsigned short  words71_74[4];  /* reserved words 71-74
                     * for 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 be ZERO
                     * 14:  Shall be ONE
                     * 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 be ZERO
                     * 14:  Shall be ONE
                     * 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 be ZERO
                     * 14:  Shall be ONE
                     * 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 be ZERO
                     * 14:  Shall be ONE
                     * 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 for security erase */
    unsigned short  trsEuc;     /* time required for 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 be ZERO
                     * 14:  Shall be ONE
                     * 13:
                     * 12:
                     * 11:
                     * 10:
                     *  9:
                     *  8:
                     *  7:
                     *  6:
                     *  5:
                     *  4:
                     *  3:
                     *  2:
                     *  1:
                     *  0:  Shall be ONE
                     */
    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 for 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
                     */
};

其中 serial_no 问硬盘的序列号,如果此项为0,则为没有提供。

源码如下:

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/hdreg.h>
#include <sys/fcntl.h>

int main(){
    struct hd_driveid id;
    int fd = open("/dev/sda",O_RDONLY | O_NONBLOCK);
    if(fd < 0){
        perror("dev/sda");
        return 1;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-29 14:47:18

linux c 获取硬盘的序列号的相关文章

linux下获取硬盘使用情况

1.前言 在嵌入式设备中,硬盘空间非常有限,在涉及到经常写日志的进程时候,需要考虑日志的大小和删除,不然很快就硬盘写满,导致日志程序崩溃.为了捕获硬盘写满的异常场景,我们需要在写日志过程中判断硬盘空间的使用情况,根据硬盘的使用情况,就可以判断是否写满了.如果将要写满了,就给出警告.这样就可以避免程序崩溃.首先看一下linux获取硬盘和目录大小的命令,最后总结一下statfs结构和函数. 2.df命令 Linux下可以用df命令获取硬盘的使用情况,通过man可以获取df命令的详细情况.df命令经常

如何在Linux 中获取硬盘分区或文件系统的UUID?

作为一个 Linux 系统管理员,你应该知道如何去查看分区的 UUID 或文件系统的 UUID.因为现在大多数的 Linux 系统都使用 UUID 挂载分区.你可以在 /etc/fstab 文件中可以验证. 有许多可用的实用程序可以查看 UUID.本文我们将会向你展示多种查看 UUID 的方法,并且你可以选择一种适合于你的方法. 何为 UUID? UUID 意即通用唯一识别码Universally Unique Identifier,它可以帮助 Linux 系统识别一个磁盘分区而不是块设备文件.

java通过jni方式获取硬盘序列号(windows,linux)

linux系统java通过jni方式获取硬盘序列号 http://blog.csdn.net/starter110/article/details/8186788 使用jni在windows下读取硬盘序列号 http://www.iflym.com/index.php/code/201109070001.html 通常做java程序的开发人员都想过想要通过某种手段来保护自己的程序.一般的方式是通过授权,采取软件授权的方式.这个时间就需要读取客户机器上的一些关键信息,比如硬盘序列号,网卡,cpu信

c/c++获取硬盘序列号

最近在接触软件注册模块,需要获取硬盘序列号来生成注册码. 硬盘序列号,英文名:Hard Disk Serial Number,该号是硬盘厂家为区别产品而设置的,是唯一的.网上搜索一下,发现获取硬盘序列号的代码遍地都是,但很多是错误的.典型代表就是使用GetVolumeInformation函数获取序列号.这种方法获取的是卷的序列号,即Volume Serial Number.硬盘格式化后,这种序列号将发生变化.本文描述的是获取硬盘唯一的物理序列号的方法. 获取硬盘序列号的方法:https://w

vc 获取 硬盘序列号 和 cpu

vc 获取 硬盘序列号 和 cpu 唯一iD的方法?如题---------网上找来很多资料 也没找到, 要支持xp win7 32/64 系统下都能获取 硬盘序列号 和cpu ID 哪位朋友帮帮忙:------解决方案--------------------不是很确定 没有测试64位的 http://blog.sina.com.cn/s/blog_612b382d0100efdt.html------解决方案--------------------CString cpu_id(){ int s1

Linux系统管理之硬盘管理

硬盘是计算的重要组成部件之一,硬盘为操作系统提供持久话存储的功能,在Linux硬盘设备的性能和好坏可能关系到生成线的安全和用户体验等等.熟练的掌握硬盘管理相关的信息能让我们处理起这些问题来得心应手.#Linux中最为重要到的哲学就是一切皆文件,硬盘设备也不例外,在Linux系统中硬盘设备一般存放在/dev/下面SATA.SCSI等接口类型的硬盘使用sd表示,第一块sd的硬盘表示为/dev/sda,第二块sd的硬盘表示为/dev/sdb.IDE硬盘使用hd表示,第一块hd的硬盘表示为/dev/hd

用hdparm获取硬盘参数

hdparm是Linux下一款能够获取和设置SATA/IDE设备参数的工具. 1.获取硬盘参数 $ sudo hdparm -i /dev/sda$ sudo hdparam -i /dev/sda | grep SerialNo //获取硬盘序列号 2.测试硬盘读速率 #hdparm -t /dev/sda 原文地址:https://www.cnblogs.com/pandachen/p/10801571.html

KVM中Linux虚拟机的硬盘添加方法

[[email protected] ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root ext4 8.3G 937M 7.0G 12% / tmpfs tmpfs 119M 0 119M 0% /dev/shm /dev/vda1 ext4 477M 33M 419M 8% /boot [[email protected] ~]# 生成一块新的硬盘 [[email pro

linux中关于硬盘分区操作

不管是在真机还是在虚拟机上装入linux系统时,都会涉及到磁盘分区这一步骤,只有在磁盘规划好后,系统才会装入系统文件及相关程序文件.下面介绍关于linux系统中分区所涉及到的一些命令及注意事项. 首先我们要知道一块物理硬盘要被利用起来要经历哪些步骤: ①识别硬盘---只有在硬盘被电脑识别后我们才能进行下一步操作 ②分区规划---一般一块硬盘都是需要划分分区的,这样便于管理,系统重要与普通文件可以区分开来 ③格式化----将硬盘规划好后就要将各个分区转变成我们想要的文件系统类型 ④挂载使用---挂