Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
1  
Want to break the filesystem? There certainly are simpler ways... –  vonbrand Mar 26 '13 at 15:54
add comment

1 Answer 1

up vote 5 down vote accepted

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|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.