Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read blocks from an ext3 filesystem?

Tags:

linux

ext3

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.

like image 475
klozovin Avatar asked Dec 31 '25 08:12

klozovin


2 Answers

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;
}
like image 184
Vinit Dhatrak Avatar answered Jan 02 '26 01:01

Vinit Dhatrak


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.

like image 21
Adam Goode Avatar answered Jan 02 '26 00:01

Adam Goode