Question

What's the easiest way to access an ext3 file system at the block level? I don't care for the files, or raw bytes, I just have to read the FS one block at a time. Is there a simple way to do this (in C)? Or maybe a simple app whose source I could look into for inspiration? I found no usable tutorials on the net, and I'm a bit scared to dive into the kernel source to find out how to do it.

Was it helpful?

Solution

Yes, see e2fsprogs. This provides tools you can use to do anything(!) with ext2, ext3, and ext4 filesystems. It also contains a library interface so you can do anything else.

See the included debugfs, it might be enough for you to start. Otherwise, check out the headers and write some code.

OTHER TIPS

If you want a simple app then I suggest you can take a look at "dd" utility. I comes as part of GNU Core Utility. Its source is available for download. Take a look at its home page, here.
If you want to achieve same from a C code, then please refer to following code. Hope this helps you. :)

#include <stdio.h>
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define SECTOR_NO 10 /*read 10th sector*/

int main()
{
        int sector_size;
        char *buf;
        int n = SECTOR_NO;

        int fd = open("/dev/sda1", O_RDONLY|O_NONBLOCK);
        ioctl(fd, BLKSSZGET, &sector_size);
        printf("%d\n", sector_size);
        lseek(fd, n*sector_size, SEEK_SET);

        buf = malloc(sector_size);
        read(fd, buf, sector_size);

        return 0;
}

Disk devices, and partitions within them, behave just like regular files that you can read from (and write to), e.g.:

head -c 2048 /dev/sda1 > first_2048_bytes

You'll need to be root of course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top