简体   繁体   English

从ext3文件系统读取块?

[英]Read blocks from an ext3 filesystem?

What's the easiest way to access an ext3 file system at the block level? 在块级别访问ext3文件系统的最简单方法是什么? I don't care for the files, or raw bytes, I just have to read the FS one block at a time. 我不在乎文件或原始字节,我只需要一次读取FS一个块。 Is there a simple way to do this (in C)? 有没有简单的方法可以做到这一点(在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. 我在网上找不到可用的教程,而且我有点害怕深入内核源代码中查找如何做。

If you want a simple app then I suggest you can take a look at " dd " utility. 如果您想要一个简单的应用程序,那么我建议您看看“ dd ”实用程序。 I comes as part of GNU Core Utility . 我是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. 如果要通过C代码实现相同目的,请参考以下代码。 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;
}

Yes, see e2fsprogs . 是的,请参阅e2fsprogs This provides tools you can use to do anything(!) with ext2, ext3, and ext4 filesystems. 这提供了可用于对ext2,ext3和ext4文件系统执行任何操作(!)的工具。 It also contains a library interface so you can do anything else. 它还包含一个库接口,因此您可以执行其他任何操作。

See the included debugfs, it might be enough for you to start. 请参阅随附的debugfs,可能足以让您开始。 Otherwise, check out the headers and write some code. 否则,请检查标题并编写一些代码。

Disk devices, and partitions within them, behave just like regular files that you can read from (and write to), eg: 磁盘设备及其中的分区的行为就像可以从中读取(或写入)的常规文件一样,例如:

head -c 2048 /dev/sda1 > first_2048_bytes

You'll need to be root of course. 当然,您需要成为根。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM