通过ioctl发送INQUIRY来获取SCSI硬盘ID

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <unistd.h>

#ifndef MAX_RAID_SERIAL_LEN
#define MAX_RAID_SERIAL_LEN 36
#endif 

typedef unsigned char __u8;

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 argc, char* argv[])
{
	char devName[16] = {0};
	if (argc > 1)
		strncpy(devName, argv[1], sizeof(devName));
	else
		strncpy(devName, "/dev/sdc", sizeof(devName));

	int fd = open(devName, O_RDONLY);
	if(fd>=0)
	{
		__u8 serial[MAX_RAID_SERIAL_LEN];
 
 
		memset(serial, 0, MAX_RAID_SERIAL_LEN);
				
		int err = read_serial(fd, devName, serial);
		if(err==0)
		{
			printf("%s:[%s]\n", devName, (char*)serial);
		}
	}
	close(fd);
}

 

以上代码通过修改其它文章中的代码 ,在el5 x86下实测可编译。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值