9

有什么方法可以使用 C++ 确定 linux 中 USB 驱动器的 s/n 吗?

如果不是 C++,还有其他与hwinfo -diskand不同的方式hdparm -i吗?

4

4 回答 4

22

我将尝试总结我在 linux 上检索存储驱动器序列号的经验。
我假设您想要存储设备标识的序列号(根据 SCSI 规范)而不是USB 设备的序列号(根据设备描述符下的 USB 规范),这两个是不同的实体。

注意!
大多数设备倾向于在 USB 控制器中实现序列号,而未实现内部 SCSI 磁盘的序列号。
因此,如果您想唯一标识 USB 设备,最好的方法是从设备描述符(USB 规范)创建一个字符串,如VendorId-ProductId-HardwareRevision-SerialNumber
在下面我将描述如何检索存储驱动器的 SN ,如所问。

驱动器分为 2 类(实际上更多,但让我们简化一下):类 ATA(hda、hdb ...)和类 SCSI(sda sdb ...)。USB 驱动器属于第二类,它们被称为SCSI 附加磁盘。在这两种情况下,ioctl调用都可以用来检索所需的信息(在我们的例子中是序列号)。

对于SCSI 设备(包括 USB 驱动器),Linux 通用驱动程序及其 API 记录在tldp中。
SCSI 设备上的序列号可在Vital Product Data(简称:VPD)中找到,并可使用SCSI 查询命令检索。linux 中可以获取此 VPD 的命令行实用程序是sdparm

> yum install sdparm
> sdparm --quiet --page=sn /dev/sda
    Unit serial number VPD page:
    3BT1ZQGR000081240XP7

请注意,并非所有设备都有此序列号,市场上充斥着廉价仿冒品,并且一些 USB 闪存盘返回奇怪的序列号(例如我的sandisk cruzer 只返回字母“u”)。为了克服这个问题,一些人选择通过混合来自 VPD 的不同字符串(如产品 ID、供应商 ID 和序列号)来创建唯一标识符。

c中的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>

int scsi_get_serial(int fd, void *buf, size_t buf_len) {
    // we shall retrieve page 0x80 as per http://en.wikipedia.org/wiki/SCSI_Inquiry_Command
    unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, buf_len, 0};
    unsigned char sense[32];
    struct sg_io_hdr io_hdr;
            int result;

    memset(&io_hdr, 0, sizeof (io_hdr));
    io_hdr.interface_id = 'S';
    io_hdr.cmdp = inq_cmd;
    io_hdr.cmd_len = sizeof (inq_cmd);
    io_hdr.dxferp = buf;
    io_hdr.dxfer_len = buf_len;
    io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
    io_hdr.sbp = sense;
    io_hdr.mx_sb_len = sizeof (sense);
    io_hdr.timeout = 5000;

    result = ioctl(fd, SG_IO, &io_hdr);
    if (result < 0)
        return result;

    if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
        return 1;

    return 0;
}

int main(int argc, char** argv) {
    char *dev = "/dev/sda";
    char scsi_serial[255];
    int rc;
    int fd;

    fd = open(dev, O_RDONLY | O_NONBLOCK);
    if (fd < 0) {
        perror(dev);
    }

    memset(scsi_serial, 0, sizeof (scsi_serial));
    rc = scsi_get_serial(fd, scsi_serial, 255);
    // scsi_serial[3] is the length of the serial number
    // scsi_serial[4] is serial number (raw, NOT null terminated)
    if (rc < 0) {
        printf("FAIL, rc=%d, errno=%d\n", rc, errno);
    } else
    if (rc == 1) {
        printf("FAIL, rc=%d, drive doesn't report serial number\n", rc);
    } else {
        if (!scsi_serial[3]) {
            printf("Failed to retrieve serial for %s\n", dev);
            return -1;
        }
        printf("Serial Number: %.*s\n", (size_t) scsi_serial[3], (char *) & scsi_serial[4]);
    }
    close(fd);

    return (EXIT_SUCCESS);
}

为了完整起见,我还将提供代码来检索ATA 设备(hda、hdb ...)的序列号。这不适用于 USB 设备。

#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <cctype>
#include <unistd.h>

int main(){
    struct hd_driveid *id;
    char *dev = "/dev/hda";
    int fd;

    fd = open(dev, O_RDONLY|O_NONBLOCK);
    if(fd < 0) {
        perror("cannot open");
    }
    if (ioctl(fd, HDIO_GET_IDENTITY, id) < 0) {
        close(fd);
        perror("ioctl error");
    } else {
        // if we want to retrieve only for removable drives use this branching
        if ((id->config & (1 << 7)) || (id->command_set_1 & 4)) {
            close(fd);
            printf("Serial Number: %s\n", id->serial_no);
        } else {
            perror("support not removable");
        }
        close(fd);
    }
}
于 2010-04-19T10:11:02.853 回答
2

这段代码会得到USB序列号……它在技术上没有clyfe的那么令人印象深刻,但它似乎每次都能成功。

#include <unistd.h>
#include <string.h>
#include <stdio.h>

int main(int arg, char **argv) {
    ssize_t len;
    char buf[256], *p;
    char buf2[256];
    int i;

    len = readlink("/sys/block/sdb", buf, 256);
    buf[len] = 0;
    // printf("%s\n", buf);
    sprintf(buf2, "%s/%s", "/sys/block/", buf);
    for (i=0; i<6; i++) {
        p = strrchr(buf2, '/');
        *p = 0;
    }
    // printf("%s\n", buf2);
    strcat(buf2, "/serial");
    // printf("opening %s\n", buf2);

    int f = open(buf2, 0);
    len = read(f, buf, 256);
    if (len <= 0) {
        perror("read()");
    }
    buf[len] = 0;
    printf("serial: %s\n", buf);


}
于 2011-08-23T10:23:14.767 回答
1

我发现了一些对你来说也很有趣的东西:

如何检测 /dev/* 是否为 USB 设备?

于 2011-02-19T09:12:51.477 回答
0

最好的方法可能是做命令行工具(同样,可能)做的事情:检查/proc或中的相关文件/sys,但来自 C++ 代码。

于 2010-03-12T12:55:02.293 回答