1

I want to get to a specifIC inode (using its number), within an ext2 image, using the C language. I'm traying to do this by opening the ext2 image with the open() syscall, and then traverse it using lseek() with the right offsets, until I get to the inode. Is this correct? Or am I doing something wrong? I'm a little confused whether using open() is correct, or there is more appropriate functions to do this.

int fd = open("ext2fs.img", O_RDONLY);
assert(fd != -1);


off_t startPosition = lseek(fd, 0, SEEK_CUR);
assert(startPosition != -1);

Should I just add offsets to startPosition until I get to the inodes? But how can I search for a specific inode?

UPDATED (to be more specific)

I already have the layout of the ext2 file system (http://www.nongnu.org/ext2-doc/ext2.html), this gives me everything (all the offsets) important I need. And I need to create a C program to manipulate the metadata and data. Like removing and copying files, for example.

I know what to do, but I'm having trouble implementing it.

For example: To test if I know what I'm doing, I'm trying to read the number of free inodes in an ext2 disk image my professor provided, doing this:

#define SUPER_BLOCK 1024

int main()
{
    int freeInodes;

    int fd = open("path.img", O_RDONLY);
    off_t startPosition = lseek(fd, 0, SEEK_CUR);

    lseek(fd, startPosition + SUPER_BLOCK + 16, SEEK_CUR);
    read(fd, freeInodes, 4);
    printf("Number of free inodes: %d", freeInodes);
}

The output I receive is: "Number of free inodes: 32767"

Am I interpreting the data I got from read() correctly? I have no idea if this value received is correct.

Community
  • 1
  • 1
Miguel Péres
  • 630
  • 1
  • 10
  • 19

2 Answers2

1

There is no real use case for accessing by inode, except to circumvent file system security. In Linux, debugfs allows you to do that. If you think it is an absolute requirement try the source code for debugfs.

Consider using the stat() call from file tree walk (ftw()/nftw() ) as a callback, or use stat() + scandir() directly for faster coding. Either will allow you to get all inodes of files you have permissions to stat. And open or whatever.

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
1

You can try doing a raw read on the file system image, something similar to what is done in fsck.ext{2,3,4}. However, this would require one to extensive knowledge the ext{2,3,4} layout, on-disk structures and a understanding of the magic numbers (offset on the disk which point to bitmaps / other metadata) to determine where the inode bitmaps start on the disk.

askb
  • 6,501
  • 30
  • 43
  • That's exactly what I'm trying to do! I already have the layout of the ext2 file system (http://www.nongnu.org/ext2-doc/ext2.html), this gives me everything (all the offsets) important I need. And I need to create a C program to manipulate the metadata and data. I already know what to do, but I'm having trouble implementing it. I'm going to update the original post. – Miguel Péres Nov 25 '14 at 00:15
  • hello @MiguelPéres I am trying to do the same thing... did you make any progress? – Zibri Mar 22 '18 at 17:08
  • Sorry @Zibri, I ended using Python, never figured it out. And it's been a long time, so I don't remember how I did it. If you make any progress please come back to share you result! – Miguel Péres Apr 02 '18 at 14:00
  • No problem, I figured it out and I made a small C program to edit a single inode :D – Zibri Apr 03 '18 at 15:11