Hard Links Cannot Span Partitions

 

1. Inodes and File System Structure

  • Every file in Linux has an inode, which stores metadata (permissions, timestamps, location on disk, etc.)

  • A filename is just a reference (directory entry) pointing to an inode.

  • Multiple filenames can point to the same inode—this is what a hard link is.


🔹 2. Hard Links vs Symbolic Links

Feature

Hard Link

Symbolic (Soft) Link

Points to

Inode (actual file)

Path of the target file

Cross-filesystem

❌ Not allowed

✅ Allowed

If target deleted

File still accessible

Broken (dangling) link

Use inode sharing

✅ Yes

❌ No


🔹 3. Partitions and Mount Points

  • A partition is a separate logical division of a disk.

  • Each partition has its own filesystem with a separate inode table.

  • Hard links require pointing to the same inode, so they must be on the same filesystem (i.e., same partition).


🔹 4. System Calls: link() vs symlink()

  • link() is used for hard links

    • Fails across filesystems because the kernel can’t reference an inode from another partition.

  • symlink() is used for symbolic links

    • Works by storing the path, so cross-partition is OK.


🔹 5. Filesystem Types and Link Support

  • Not all filesystems support hard links. For example:

    • ext4, xfs: ✅ Support

    • FAT32, NTFS (on Linux): ❌ Partial or no support


❌ Why Hard Links Cannot Span Partitions

  • Hard links must reference the same inode number, and inodes are local to each filesystem.

  • Since different partitions have independent inode tables, it's technically impossible for a hard link to reference an inode outside its partition.


🔧 Example:

# Assume /home and /mnt are on different partitions


ln /home/user/file.txt /mnt/file_hardlink.txt

# Output:

# ln: failed to create hard link '/mnt/file_hardlink.txt': Invalid cross-device link



✅ What You Can Do Instead

Use a symbolic link if you need cross-partition linking:

ln -s /home/user/file.txt /mnt/file_symlink.txt


This creates a shortcut pointing to the path (not the inode).


🧠 FAANG-Level Related Concepts and Interview Questions

Here are key related questions you may get:

🔸 Conceptual

  1. What is an inode? How does Linux manage file metadata?

  2. What is the difference between a hard link and a soft link?

  3. Why are hard links restricted to a single filesystem?

  4. How do link() and symlink() system calls behave?

  5. What happens to hard/symbolic links when the original file is deleted?

  6. What is a dangling symlink?

🔸 Practical / Debugging

  1. You tried to create a hard link across partitions and it failed. Why?

  2. How do you find how many hard links a file has?
    → Use: ls -l (second column) or stat filename

How to locate all hard links pointing to a file?
→ Find by inode:

find / -inum <inode_number>


🔸 Edge Cases

  1. What are the limitations of symbolic links in terms of permissions?

  2. How can symlinks introduce security vulnerabilities (e.g., symlink race)?

  3. How does cp -a handle hard links?

  4. Can you create a hard link to a directory? Why not?


🧪 Bonus: Hands-On Commands

🔹 Check Filesystem Mount Points

df -h


🔹 View Inode Number

ls -li filename


🔹 Count Hard Links

stat filename


🔹 Find Files by Inode

find . -inum $(stat -c %i filename)



Summary

  • ❌ Hard links cannot cross partitions due to inode isolation per filesystem.

  • ✅ Use symbolic links for cross-partition references.

  • 🧠 Understanding inodes, filesystems, and link types is crucial for system-level interviews.



Distributed by Gooyaabi Templates | Designed by OddThemes