0

For some reasons, I need to read and write a sector of hard disk in my module.

I want to read and write a sector by programming in the kernel level

My linux kernel version is 3.13.0.

Is there anyone can help me ?

1 Answers1

1

I'm not sure what you can do in your module. If you are working on the shell, you may be able to use the dd command.

dd if=/dev/sda of=outfile

If you cannot use user-space commands, you may have to work on the disk block device directly. You need to open the device using open(2) system call (see man 2 open for details).

int fd;
fd = open("/dev/sda", FLAGS);

After that you can perform read commands (man 2 read) to read from the device.

ssize_t read(int fd, void *buf, size_t count);

You can also send commands to the device using IOCTLs:

int ioctl(int d, int request, ...);

Using these calls you should be flexible enough to do whatever you want to do with your disk.

olfek
  • 3,210
  • 4
  • 33
  • 49
TomS
  • 467
  • 9
  • 25