Reading blocks from ext3 file system?

What is the easiest way to access block level ext3 file system? I don't need files or raw bytes, I just need to read FS one block at a time. Is there an easy way to do this (in C)? Or maybe a simple application, the source of which I could breathe inspiration? I did not find any useful lessons on the net, and I'm a little afraid to dive into the kernel source to find out how to do this.

+3
source share
3 answers

Yes, see e2fsprogs . This provides tools that you can use for anything (!) With ext2, ext3 and ext4 file systems. It also contains a library interface, so you can do something else.

See debugfs included, this may be enough to get started. Otherwise, check the headers and write the code.

+2
source

If you want a simple application, I suggest you take a look at " dd ". I came as part of the GNU Core Utility . Its source is available for download. Take a look at his homepage here .
If you want to achieve this from C code, refer to the 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;
}
+4

, , ( ), :

head -c 2048 /dev/sda1 > first_2048_bytes

, root.

+1
source

Source: https://habr.com/ru/post/1721883/


All Articles