Linux: direct access to the hard-disk in C

Lopoc picture Lopoc · Jun 10, 2009 · Viewed 10.6k times · Source

How can I obtain a raw access to the HD and know if that location is used or is a free space? Just a piece of example, I can obtain a direct access simply with an open and a read on a disk device, the goal is knowing whether the, for example, 10.000 byte is used or not.

Answer

Mehrdad Afshari picture Mehrdad Afshari · Jun 10, 2009

You can just open the block device (for example, /dev/sda) and read the sectors manually. However, that won't tell tell you if it's empty or not directly. Being empty or not is a something defined at the filesystem abstraction level. File system data structures store that kind of data and you should have a deep understanding of the specific filesystem to do so from the raw blocks (without using any filesystem provided function).