How do I manually modify an inode?

5,062

That depends on the filesystem. For ext4, you can do this with debugfs as follows:

dennis@lightning:/tmp$ dd if=/dev/zero of=ext4.img bs=1M count=100
104857600 bytes (105 MB) copied, 0.645009 s, 163 MB/s
dennis@lightning:/tmp$ mkfs.ext4 ext4.img 
mke2fs 1.42.5 (29-Jul-2012)
ext4.img is not a block special device.
Proceed anyway? (y,n) y
...
Writing superblocks and filesystem accounting information: done 
dennis@lightning:/tmp$ mkdir ext4
dennis@lightning:/tmp$ sudo mount ext4.img ext4
dennis@lightning:/tmp$ mkdir -p ext4/test/sub/
dennis@lightning:/tmp$ sudo umount ext4
dennis@lightning:/tmp$ debugfs -w ext4.img 
debugfs 1.42.5 (29-Jul-2012)
debugfs:  link test test/sub/loop
^D
dennis@lightning:/tmp$ ls ext4/test/sub/loop/sub/loop/sub/loop/sub/loop/sub/loop/
total 1
drwxrwxr-x 2 dennis dennis 1024 mrt 26 12:15 sub

Notes:

  • you cannot link directly to the parent, so foo/bar can't be a link to foo, hence the extra directory.
  • You should not run debugfs on mounted filesystems. If you do, you will need to unmount/mount after making changes.

Tools like find and ls still won't loop:

dennis@lightning:/tmp$ find ext4
ext4
ext4/lost+found
find: `ext4/lost+found': Permission denied
ext4/test
ext4/test/sub
find: File system loop detected; `ext4/test/sub/loop' is part of the same file system loop as `ext4/test'.
Share:
5,062

Related videos on Youtube

Benubird
Author by

Benubird

Updated on September 18, 2022

Comments

  • Benubird
    Benubird almost 2 years

    I am using Ubuntu Linux and, just for fun, I want to create a hardlink to a directory (as seen here). Because I'm just doing this for fun, I'm not looking for any sort of pre-developed directory-hardlinking software that someone else wrote, I want to know how to do it myself. So, how do I directly, manually, modify an inode?

    Ideally I would like the answer as a Linux command that I can run from the Bash command line, but if there is no way to do it from there I would also accept information on how to do it in C or (as a last resort) assembly.

    • vonbrand
      vonbrand over 11 years
      Want to break the filesystem? There certainly are simpler ways...