harrythomas
harrythomas

Reputation: 1467

C function to modify inode?

I was learning how linux file system works and I came across the concept of inodes. I have written a C program to read a particular inode and print its contents.

Now I wan't to modify the contents of inode from my C code. I know this could break the filesystem if something goes wrong but still I want to try it.

How can I achieve this?

Upvotes: 2

Views: 1666

Answers (1)

John Burger
John Burger

Reputation: 3672

You need to access what is called the "meta" information of the drive - the information about the information on the drive - not the normal information. To do that, you need to open the drive itself rather than any file or directory inside the drive.

If you're talking i-nodes, then you're on Linux and the ext filesystem, so the drive name will be something like /dev/sdb. Be careful: this is the whole disk, NOT one partition/volume/slice within it. That might be called /dev/sdb2 or something - different types of Linux call them different things.

Once you have the partition open, you can treat it just like a (very large!) file: a succession of bytes that coincidentally happen to be arranged as sectors on the hard disk. You can seek to any position and read the data there. If you want to overwrite it, you can - but as you say:

You may completely destroy the data on your hard disk!

Perhaps mount a USB stick (with nothing important on it) and experiment on that? And make VERY sure that you open ITS name and not your main disk's name!

Upvotes: 2

Related Questions