Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a single sector from a disk

Tags:

linux

io

kernel

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!

like image 905
svp Avatar asked Nov 18 '09 01:11

svp


People also ask

How much time does it take to read one sector?

Since there are on average 500 sectors per track, we need 1/500th of a revolution of the disk to read the entire sector. We can work this out as (time for one revolution of disk) / 500 = 6ms / 500 = 0.012ms.

What is a sector on a disk?

A sector is the smallest physical storage unit on the disk, and on most file systems it is fixed at 512 bytes in size. A cluster can consist of one or more consecutive sectors – commonly, a cluster will have four or eight sectors.

What is the size of one sector in a hard drive?

For over 30 years, data stored on hard drives has been formatted into small logical blocks called sectors, with a legacy sector size of 512 bytes.

What is the difference between a sector and a block on a HDD?

A sector is the smallest individual reference-able regions on a disk. The block size refers to the allocation size the file system uses.


2 Answers

Try something like this to do it from the CLI:

# df -h .
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              27G   24G  1.6G  94% /
# dd bs=512 if=/dev/sda2 of=/tmp/sector200 skip=200 count=1
1+0 records in
1+0 records out

From man 4 sd:

FILES
   /dev/sd[a-h]: the whole device
   /dev/sd[a-h][0-8]: individual block partitions

And if you want to do this from within a program, just use a combination of system calls from man 2 ... like open, lseek,, and read, with the parameters from the dd example.

like image 118
DigitalRoss Avatar answered Sep 18 '22 12:09

DigitalRoss


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.

sudo dd if=/dev/rdisk0 bs=512 skip=900 count=1

See the dd man page to get additional information on the parameters to dd.

like image 45
Gene Goykhman Avatar answered Sep 18 '22 12:09

Gene Goykhman