下划线后就是磁盘的序列号了。他们实际都是一个连接符号而已,如下:
linux-b460:/dev/disk/by-id # ll
total 0
lrwxrwxrwx 1 root root 9 2012-07-09 18:28 ata-ST31000525SV_6VP17SN7 -> ../../sdl
lrwxrwxrwx 1 root root 9 2012-07-11 18:57 ata-WDC_WD10EVDS-63N5B1_WD-WCAU49474761 -> ../../sde
lrwxrwxrwx 1 root root 9 2012-06-19 18:58 ata-WDC_WD10EVDS-63N5B1_WD-WCAU49481174 -> ../../sda
lrwxrwxrwx 1 root root 10 2012-06-19 18:58 ata-WDC_WD10EVDS-63N5B1_WD-WCAU49481174-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 2012-06-19 18:58 ata-WDC_WD10EVDS-63N5B1_WD-WCAU49481174-part2 -> ../../sda2
lrwxrwxrwx 1 root root 10 2012-06-19 18:59 ata-WDC_WD10EVDS-63N5B1_WD-WCAU49481174-part3 -> ../../sda3
lrwxrwxrwx 1 root root 9 2012-07-11 18:46 ata-WDC_WD15EVDS-63V9B0_WD-WCAVU0300086 -> ../../sdf
lrwxrwxrwx 1 root root 9 2012-07-11 18:46 ata-WDC_WD15EVDS-63V9B0_WD-WCAVU0300095 -> ../../sdj
lrwxrwxrwx 1 root root 10 2012-07-06 14:13 ata-WDC_WD15EVDS-63V9B0_WD-WCAVU0300095-part1 -> ../../sdj1
lrwxrwxrwx 1 root root 10 2012-07-06 14:13 ata-WDC_WD15EVDS-63V9B0_WD-WCAVU0300095-part2 -> ../../sdj2
lrwxrwxrwx 1 root root 9 2012-07-11 18:46 ata-WDC_WD2002FYPS-01U1B0_WD-WCAVY0881976 -> ../../sdg
lrwxrwxrwx 1 root root 9 2012-07-11 18:46 ata-WDC_WD2002FYPS-01U1B0_WD-WCAVY0951385 -> ../../sdh
lrwxrwxrwx 1 root root 9 2012-06-27 12:53 ata-WDC_WD2002FYPS-01U1B0_WD-WCAVY0965407 -> ../../sdb
lrwxrwxrwx 1 root root 9 2012-07-11 18:46 ata-WDC_WD20EVDS-63T3B0_WD-WCAVY0946590 -> ../../sdi
lrwxrwxrwx 1 root root 9 2012-07-09 18:28 scsi-SATA_ST31000525SV_6VP17SN7 -> ../../sdl
lrwxrwxrwx 1 root root 9 2012-07-11 18:57 scsi-SATA_WDC_WD10EVDS-63_WD-WCAU49474761 -> ../../sde
lrwxrwxrwx 1 root root 9 2012-06-19 18:58 scsi-SATA_WDC_WD10EVDS-63_WD-WCAU49481174 -> ../../sda
...
这样,和磁盘的对应关系也得到了。
我们也可以通过ioctl函数来获取磁盘的序列号,代码如下:
int get_serial(int fd, void *buf, size_t buf_len)
{
unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, buf_len, 0};
unsigned char sense[32];
struct sg_io_hdr io_hdr;
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;
return ioctl(fd, SG_IO, &io_hdr);
}
int read_serial(int fd, char *devname,
__u8 serial[MAX_RAID_SERIAL_LEN])
{
unsigned char scsi_serial[255];
int rv;
int rsp_len;
int len;
char *dest;
char *src;
char *rsp_buf;
int i;
memset(scsi_serial, 0, sizeof(scsi_serial));
rv = get_serial(fd, scsi_serial, sizeof(scsi_serial));
if (rv != 0) {
if (devname)
fprintf(stderr,
": Failed to retrieve serial for %s\n",
devname);
return rv;
}
rsp_len = scsi_serial[3];
if (!rsp_len) {
if (devname)
fprintf(stderr,
": Failed to retrieve serial for %s\n",
devname);
return 2;
}
rsp_buf = (char *) &scsi_serial[4];
/* trim all whitespace and non-printable characters and convert
* ':' to ';'
*/
for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
printf("%c", *src);
src = &rsp_buf[i];
if (*src > 0x20) {
/* ':' is reserved for use in placeholder serial
* numbers for missing disks
*/
if (*src == ':')
*dest++ = ';';
else
*dest++ = *src;
}
}
len = dest - rsp_buf;
dest = rsp_buf;
/* truncate leading characters */
if (len > MAX_RAID_SERIAL_LEN) {
dest += len - MAX_RAID_SERIAL_LEN;
len = MAX_RAID_SERIAL_LEN;
}
memset(serial, 0, MAX_RAID_SERIAL_LEN);
memcpy(serial, dest, len);
return 0;
}
int main()
{
int fd = open("/dev/sdb", O_RDONLY);
if(fd>=0)
{
__u8 serial[MAX_RAID_SERIAL_LEN];
memset(serial, 0, MAX_RAID_SERIAL_LEN);
int err = read_serial(fd, "/dev/sdb", serial);
if(err==0)
{
printf("%s, %s\n", "/dev/sdb\n", (char*)serial);
}
}
close(fd);
}
scsi提供了若干的命令让我们能同其进行互交,命令为6个字节长,我么使用的查询命令格式如下:
bit→ ↓byte |
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|
0 | Operation code = 12h | |||||||
1 | LUN | Reserved | EVPD | |||||
2 | Page code | |||||||
3 | Allocation length (MSB) | |||||||
4 | Allocation length (LSB) | |||||||
5 | Control |
EVPD设置的为1,按说明返回的应是Vital Product Data(If the EVPD parameter bit is one then the target will return Vital Product Data (VPD).)
Page code为0x80表示的意思为Unit serial number,我们要获取的就是这个了
根据文档spc4r34.pdf第7.8.18节(Unit serial number VPD Page,在7.8节下介绍了各类Vital Product Data Page Code)的说明
返回的内容格式如下
从第5个字节开始就是我们要得到的序列号了