Linux环境下获取硬盘序列号、型号

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_14976351/article/details/75208543

获取硬盘序列号型号实例程序如下:

typedef struct _HDD_INFO
{
    char Model[64];
    char SN[64];
    char md5_hash[40];
} HDD_INFO, *PHDD_INFO;
void FillHDDStruct(IN PHDD_INFO hddinfo)
{
    struct hd_driveid id;
    char model[64]={0};
    char sn[64]={0};

    int fd = open("/dev/hda", O_RDONLY|O_NONBLOCK);
    if (fd < 0)
    {
        //try to open /dev/hda
        fd = open("/dev/sda", O_RDONLY|O_NONBLOCK);
        if(fd < 0)
        {
            perror("detecting harddisk");
            return;
        }
    }
    if(!ioctl(fd, HDIO_GET_IDENTITY, &id))
    {
        //Build HDD Model
        RTrim(id.model, 40, model);
        n = strlen(model);
        strcpy(hddinfo->Model, model);
        //Build SerialNumber
        LTrim(id.serial_no, sn);
        n = strlen(sn);
        strcpy(hddinfo->SN, sn);

    }
}

在上面的程序中,最关键的步骤就是ioctl(fd, HDIO_GET_IDENTITY, &id)。
1. 什么是ioctl
ioctl是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率、马达的转速等等。它的调用个数如下:
int ioctl(int fd, ind cmd, …);
其中fd是用户程序打开设备时使用open函数返回的文件标示符,cmd是用户程序对设备的控制命令,至于后面的省略号,那是一些补充参数,一般最多一个,这个参数的有无和cmd的意义相关。
ioctl函数是文件结构中的一个属性分量,就是说如果你的驱动程序提供了对ioctl的支持,用户就可以在用户程序中使用ioctl函数来控制设备的I/O通道。

猜你喜欢

转载自blog.csdn.net/qq_14976351/article/details/75208543
今日推荐