I am trying to read a single specific sector from the disk directly. I've currently run out of ideas and any suggestions how to go about it would be great!
相关问题
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- how to get running process information in java?
- In what practical case bool(std::ifstream) != std:
- Error building gcc 4.8.3 from source: libstdc++.so
The other folks have pretty much covered it. You need to
access to the disk's device file (either be root or, better, change the permissions on it)
use the file IO functions to read sectors = chunks of (usually) 512 bytes from said disk.
Try something like this to do it from the CLI:
From
man 4 sd
:And if you want to do this from within a program, just use a combination of system calls from
man 2 ...
likeopen, lseek,
, andread
, with the parameters from thedd
example.I'm not sure what the best programmatic approach is, but from the Linux command-line you could use the dd command in combination with the raw device for your disk to directly read from the disk.
You need to sudo this command to get access to the raw disk device (e.g. /dev/rdisk0).
For example, the following will read a single 512-byte block from an offset of 900 blocks from the top of disk0 and output it to stdout.
See the dd man page to get additional information on the parameters to dd.
In C it is something like the following... It would require root permissions. I think you need to open the file with O_DIRECT if you want to read single sectors. Otherwise you'll get a page. I'm not sure if the aligned buffer is required for a read, but it is for a write.