1

I want to write my own tag on the device file (scsi file sdb, sdc...) on Linux.

I use linux C open(), read(), write() functions on /dev/sdb file, write my key in this file. But when usb disk device Unplug from computer and plug again, in /dev/sdb's key, sometimes it's gone, or is unstable.

I don't know why.

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... 
Mathieu
  • 8,840
  • 7
  • 32
  • 45
张优传
  • 11
  • 1
  • 2
    You should show us exactly what you did and exactly what happened. Formula: 1) What did you do? 2) What did you expect to happen? 3) What actually happened? – user253751 Mar 29 '17 at 02:28
  • sorry.......... i did Probably so... 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 – 张优传 Mar 29 '17 at 03:04
  • You should put it in the question so it's easier to read. Also show how you checked whether the key was still there. – user253751 Mar 29 '17 at 03:30
  • 1
    Also did you try running `sync` before unplugging the USB drive? – user253751 Mar 29 '17 at 03:31

1 Answers1

0

for setting a fixed mount point:

you can assign a static mount point (like /dev/sdb) for USB devices using udev rules and a symlink. you can identify the device in the udev rule by i.e. vendor id and product id,...

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

for writing bit-wise to specific memory location on the stick:

to write data bytewise to arbitrary blocks on a block device use tools like dd ( https://en.wikipedia.org/wiki/Dd_(Unix) ). file io tools like read(), write() can not be used for writing bytewise WITHOUT using a file...

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • (idVendor,idProduct) these I all get, I just want to write something in /dev/sdb file ,But is unstable when i use open(), read(), write() functions,like Data missed......... – 张优传 Mar 30 '17 at 03:45
  • if you have a second device that is USB mass storage connect this and check if it is also on this device. maybe this is a hardware error (flash chips broken, or USB port physical connection,...). you can use `libusb` for this see http://stackoverflow.com/questions/14772152/read-write-on-a-pen-drive-using-libusb-libusb-bulk-transfer normally the kernel module (`usb_storage`) is responsible for this – ralf htp Mar 30 '17 at 06:18
  • your mean is my way(open,read(),write()) is no wrong, maybe this is a hardware error ? I always thought (open(),read(),write) is wrong... – 张优传 Mar 30 '17 at 06:33
  • the kernel module (driver) `usb_storage` enables the os to see the usb device as part of its file system (`/dev/sdbx`) by mounting it. so you should be able to use standard file system operations like `open()`, `read()`, `write()`,... so if you want to write something with file io commands you have to create a file on the drive you can not write `h%27dcd*()jd` anywhere on the usb stick you want because there is an internal structuring of the memory in block devices (block size, file system table,...) – ralf htp Mar 30 '17 at 09:01
  • see http://www.stackoverflow.com/questions/28532570/how-do-i-write-directly-to-a-usb-drive-with-over-4gb-data to write data bytewise to arbitrary blocks on the device use tools like `dd` ( https://en.wikipedia.org/wiki/Dd_(Unix) ) – ralf htp Mar 30 '17 at 09:10