linux下获取scsi上的磁盘设备的序列号

最简单的方法是直接到/dev/disk/by-id目录下查看,如下是一台机器上的结果
linux-b572:/dev/disk/by-id # ls
ata-ST31000525SV_6VP17SN7                      scsi-SATA_ST31000525SV_6VP17SN7                  wwn-0x5000c5001863e582
ata-WDC_WD10EVDS-63N5B1_WD-WCAU49474761        scsi-SATA_WDC_WD10EVDS-63_WD-WCAU49474761        wwn-0x50014ee202b7c499
ata-WDC_WD10EVDS-63N5B1_WD-WCAU49481174        scsi-SATA_WDC_WD10EVDS-63_WD-WCAU49481174        wwn-0x50014ee202b7c499-part1
ata-WDC_WD10EVDS-63N5B1_WD-WCAU49481174-part1  scsi-SATA_WDC_WD10EVDS-63_WD-WCAU49481174-part1  wwn-0x50014ee202b7c499-part2
ata-WDC_WD10EVDS-63N5B1_WD-WCAU49481174-part2  scsi-SATA_WDC_WD10EVDS-63_WD-WCAU49481174-part2  wwn-0x50014ee202b7c499-part3
ata-WDC_WD10EVDS-63N5B1_WD-WCAU49481174-part3  scsi-SATA_WDC_WD10EVDS-63_WD-WCAU49481174-part3  wwn-0x50014ee203752eff
ata-WDC_WD15EVDS-63V9B0_WD-WCAVU0300086        scsi-SATA_WDC_WD15EVDS-63_WD-WCAVU0300086        wwn-0x50014ee203752eff-part1
ata-WDC_WD15EVDS-63V9B0_WD-WCAVU0300095        scsi-SATA_WDC_WD15EVDS-63_WD-WCAVU0300095        wwn-0x50014ee203752eff-part2
ata-WDC_WD15EVDS-63V9B0_WD-WCAVU0300095-part1  scsi-SATA_WDC_WD15EVDS-63_WD-WCAVU0300095-part1  wwn-0x50014ee20375d94c
ata-WDC_WD15EVDS-63V9B0_WD-WCAVU0300095-part2  scsi-SATA_WDC_WD15EVDS-63_WD-WCAVU0300095-part2  wwn-0x50014ee203766323
ata-WDC_WD2002FYPS-01U1B0_WD-WCAVY0881976      scsi-SATA_WDC_WD2002FYPS-_WD-WCAVY0881976        wwn-0x50014ee258cbe480
ata-WDC_WD2002FYPS-01U1B0_WD-WCAVY0951385      scsi-SATA_WDC_WD2002FYPS-_WD-WCAVY0951385        wwn-0x50014ee2ad62dc61
ata-WDC_WD2002FYPS-01U1B0_WD-WCAVY0965407      scsi-SATA_WDC_WD2002FYPS-_WD-WCAVY0965407        wwn-0x50014ee2ae201ed4
ata-WDC_WD20EVDS-63T3B0_WD-WCAVY0946590        scsi-SATA_WDC_WD20EVDS-63_WD-WCAVY0946590        wwn-0x50014ee2ae21da40

下划线后就是磁盘的序列号了。他们实际都是一个连接符号而已,如下:

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);
}


输出结果为
/dev/sdb, WD-WCAVY0965407

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
这里操作码INQUIRY= 12h,表示是查询操作

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个字节开始就是我们要得到的序列号了

你可能感兴趣的:(linux下获取scsi上的磁盘设备的序列号)