1、在Linux系统中通过C语言获取硬盘序列号,可以借助于ioctl()函数,该函数原型如下:
int ioctl(int fd, unsigned long request, ...) ioctl的第一个参数是文件标识符,用open()函数打开设备时获取。ioctl第二个参数为用于获得指定文件描述符的标志号,获取硬盘序列号,一般指明为HDIO_GET_IDENTITY。
ioctl的第三个参数为一些辅助参数,要获取硬盘序列号,需要借助于struct hd_driveid结构体来保存硬盘信息 ,该结构体在Linux/hdreg.h中,struct hd_driveid的声明如下 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
*/
……
}
2、源代码如下
#include <stdio.h>//ioctl()的声明头文件
#include <sys/ioctl.h>
//硬盘参数头文件, hd_driveid结构声明头文件
#include <linux/hdreg.h>
//文件控制头文件
#include <sys/fcntl.h>
int main()
{
//用于保存系统返回的硬盘数据信息
struct hd_driveid id
//这里以第一块硬盘为例,用户可自行修改
//用open函数打开获取文件标识符,类似于windows下的句柄
int fd = open("/dev/sda", O_RDONLY|O_NONBLOCK)
//失败返回
if (fd < 0) {
perror("/dev/sda")
return 1 }
//调用ioctl()
if(!ioctl(fd, HDIO_GET_IDENTITY, &id))
{
printf("Serial Number=%s\n",id.serial_no)
}
return 0
}
编译完成后,执行效果如下:
root@localhost:~# fdisk -lWARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sda: 2000.4 GB, 2000398934016 bytes
255 heads, 63 sectors/track, 243201 cylinders, total 3907029168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 1 3907029167 1953514583+ ee GPT
root@localhost:~# hdparm -i /dev/sda1
/dev/sda1:
Model=WDC WD2003FYYS-02W0B1, FwRev=01.01D02, SerialNo=WD-WMAY04598673
Config={ HardSect NotMFM HdSw>15uSec SpinMotCtl Fixed DTR>5Mbs FmtGapReq }
RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=50
BuffType=unknown, BuffSize=unknown, MaxMultSect=16, MultSect=16
CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=3907029168
IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
PIO modes: pio0 pio3 pio4
DMA modes: mdma0 mdma1 mdma2
UDMA modes: udma0 udma1 udma2 udma3 udma4 udma5 *udma6
AdvancedPM=yes: unknown setting WriteCache=enabled
Drive conforms to: Unspecified: ATA/ATAPI-1,2,3,4,5,6,7
* signifies the current active mode
LINUX查询工作站多硬盘的硬盘序列号方法:1、确认安装yaourt hdparm
2、运行下面命令查看共有多少硬盘
[root@myhost detect]# fdisk -l
Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 * 63 385559 192748+ 83 Linux
/dev/sda2 385560 4385744 2000092+ 82 Linux swap / Solaris
/dev/sda3 438574514378174 4996215 83 Linux
/dev/sda414378175 625137344 3053795855 Extended
/dev/sda5143782383437909910000431 83 Linux
/dev/sda63437916344371529 4996183+ 83 Linux
/dev/sda7443715937561795415623181 83 Linux
/dev/sda875618018 10686437915623181 83 Linux
/dev/sda9 106864443 12686530410000431 83 Linux
/dev/sda10 126865368 625137344 249135988+ 83 Linux
由上可见,只有一个硬盘sda,其中,sda1到sda10是说该硬盘共有十个分区。
多个硬盘的话会看到sdb、sdc等;
3、查看sda硬盘的序列号,
[root@myhost detect]# hdparm -i /dev/sda
/dev/sda:
Model=ST3320418AS, FwRev=CC66, SerialNo=6VMDEMJM
Config={ HardSect NotMFM HdSw>15uSec Fixed DTR>10Mbs RotSpdTol>.5% }
RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=4
BuffType=unknown, BuffSize=16384kB, MaxMultSect=16, MultSect=16
CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=625142448
IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
PIO modes: pio0 pio1 pio2 pio3 pio4
DMA modes: mdma0 mdma1 mdma2
UDMA modes: udma0 udma1 udma2 udma3 udma4 *udma5
AdvancedPM=no WriteCache=enabled
Drive conforms to: unknown: ATA/ATAPI-4,5,6,7
由上可见,sda硬盘的序列号为6VMDEMJM。
同理查看其它硬盘序列号。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)