从linux读取和写入scsi设备文件,如sdb,sdc

时间:2017-03-29 02:25:50

标签: c linux usb scsi

我想在Linux上的设备文件(scsi文件sdb,sdc ...)上编写自己的标记。

我在open()文件上使用linux C read()write()/dev/sdb函数,在此文件中写入我的密钥。但是当usb磁盘设备从计算机拔出并重新插入时,在/dev/sdb的密钥中,有时它已经消失,或者不稳定。

我不知道为什么。

char readBuf[512] = { 0 }; 
char key[12] = "h%27dcd*()jd"; 
int fd = open("/dev/sdb",O_RDWR); 
lseek(fd,1024,SEEK_SET); 
read(fd,readBuf,512); 
for(int i=0; i<sizeof key; ++i) 
{ 
    readBuf[i] = ~key[i]; 
} 
lseek(fd,1024,SEEK_SET); 
write(fd,readBuf,512);
//In order to mark the Usb disk bear fruit... 

1 个答案:

答案 0 :(得分:0)

用于设置固定安装点:

您可以使用/dev/sdb规则和udev为USB设备分配静态挂载点(如symlink)。您可以通过供应商ID 产品ID 来识别udev规则中的设备,...

ACTION=="add", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", SYMLINK+="my_device"

https://unix.stackexchange.com/questions/66901/how-to-bind-usb-device-under-a-static-name

用于逐位写入棒上的特定内存位置:

以字节方式将数据写入块设备上的任意块,使用ddhttps://en.wikipedia.org/wiki/Dd_(Unix))之类的工具。文件io工具如read()write()不能用于按字节顺序写入而不使用文件......

相关问题