Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a file with a specific inode number?

Tags:

ext3

How can I create a file in ext3 filesystem with a specific inode number? (ex: I want to create a file with inode-number = 12253)

like image 969
montini Avatar asked Apr 22 '11 06:04

montini


People also ask

How inode is allocated to a new file?

Inode Assignment to a New File The kernel reads the inode list on disk, block by block, and fills the super block list of inode numbers to capacity, remembering the highest-numbered inode that it finds ("remembered" inode). It is the last one saved in the super block.

How can you find a file using only its inode number?

Just use find / -inum <inode> . It is much more portable than debugfs and also works much more reliably (it can find paths that are not belonging to files on the hard drive, like devices, for instance).

Which option can be used to specify the inode number?

The maximum number of files that can be created can be specified by using the --inode-limit option on the mmcrfs command and the mmchfs command. You can determine the inode size (-i) and subblock size (value of the -B parameter / 32) of a file system by running the mmlsfs command.

Can two files have same inode number?

Two different files can have the same inode number if they reside on different file systems (different discs). In Unix, the inode represents what would be called a file in other Operating Systems. You can have different directory entries (file names) that actually point to the same inode.


2 Answers

I don't think there's any programmatic way to request a specific inode number when creating a file from userspace. Other than being visible in stat() results, inode numbers have no significance in userspace; they're part of the filesystem's internal bookkeeping data, just like the block numbers where the file contents are allocated.

You could probably use debugfs to "change" an existing file's inode number, by copying the contents of one inode to another, then updating any directory entries to point to the new inode and deallocating the old one. So you could create your file with any inode number, then "change" it to the desired one. This would have to be done with extreme care, however, since mistakes are likely to result in filesystem corruption and data loss. You'd also have to account for the possibility that your desired inode number is already in use by another file.

like image 97
Wyzard Avatar answered Oct 11 '22 09:10

Wyzard


That's a pretty low number, so chances are it's already in use; if not, you could run a Bash script to create a few thousand files: something like for i in $(seq 1 12000); do touch $i.txt; done. Then find the one you want: find / -inum 12253, and rename it to whatever you want, and put in it what you want. If you don't overwrite the allocated space, in which case a new inode will most likely be created, that should do it. It's a sloppy solution, though, and there must be a better way.

like image 37
jcomeau_ictx Avatar answered Oct 11 '22 11:10

jcomeau_ictx