A delightful question that gets to the heart of how filesystems and links work in Unix-like systems (including Linux and macOS)!
Hardlinks 101
Before we dive into the mounting part, let's quickly recap what a hardlink is:
A hardlink is a directory entry (a filename) that points directly to the inode (a data structure on disk) of a file.
An inode contains metadata (permissions, ownership, timestamps, etc.) and points to the actual data blocks on disk where the file's contents are stored.
When you create a hardlink to a file, you're essentially creating a new filename that points to the same inode as the original file.
Both filenames (the original and the hardlink) are equal; there's no "primary" or "secondary" link – they're just two names for the same file.
Deleting one hardlink doesn't affect the other; the file's data remains on disk until all hardlinks to that inode are deleted (i.e., the link count reaches 0).
Mounting a hardlink to another drive
Now, let's get to the interesting part. Suppose you have:
A file original_file on Drive A (e.g., /dev/sda1, mounted at /mnt/drive_a).
You create a hardlink hardlink_file on Drive A, pointing to the same inode as original_file. Both files are on the same filesystem.
You mount the hardlink hardlink_file to Drive B (e.g., /dev/sdb1, mounted at /mnt/drive_b).
Here's what happens:
The short answer:It doesn't work as you might expect. Mounting a hardlink to another drive doesn't "copy" or "move" the file; it doesn't make the file's data magically appear on Drive B. Instead...
The long answer:
When you try to mount hardlink_file (on Drive A) to /mnt/drive_b (on Drive B), the mount command will follow the hardlink and attempt to mount the underlying filesystem ( Drive A, /dev/sda1 ) to /mnt/drive_b.
In other words:
The mount command resolves the hardlink hardlink_file to its underlying inode (on Drive A).
It then identifies the filesystem that inode belongs to ( Drive A's filesystem, /dev/sda1 ).
The entire filesystem (Drive A, /dev/sda1) is then mounted (again) to /mnt/drive_b on Drive B!
This means:
You'll now have two mount points for the same filesystem (Drive A):
The original mount point: /mnt/drive_a
The new mount point: /mnt/drive_b
Both /mnt/drive_a and /mnt/drive_b will show the entire contents of Drive A (not just the hardlink_file or original_file).
Any changes made through either mount point will affect the same underlying data on Drive A.
Example
Suppose the filesystem layout is:
bash
# Drive A ( /dev/sda1, mounted at /mnt/drive_a )
/mnt/drive_a/
├── original_file
├── hardlink_file -> original_file (hardlink)
└── other_file
# Drive B ( /dev/sdb1, empty, mounted at /mnt/drive_b )
/mnt/drive_b/
Now, run:
bash
sudo mount hardlink_file /mnt/drive_b
After mounting, the layout becomes:
bash
# Drive A ( /dev/sda1, still mounted at /mnt/drive_a )
/mnt/drive_a/
├── original_file
├── hardlink_file -> original_file (hardlink)
└── other_file
# Drive B ( /dev/sdb1, now "contains" Drive A's filesystem )
/mnt/drive_b/
├── original_file
├── hardlink_file -> original_file (hardlink)
└── other_file # Surprise! other_file is now accessible here too!
As you can see, /mnt/drive_b now mirrors the entire contents of Drive A, not just the hardlink_file.
Key takeaways
Hardlinks are not "moved" or "copied" when mounted to another drive.
The underlying filesystem is mounted, making all its contents accessible through the new mount point.
Changes are reflected across both mount points, as they point to the same data on disk.
To achieve what you might have intended (making a file available on another drive), consider these alternatives:
Symbolic links (softlinks): Create a softlink on Drive B pointing to the original file on Drive A: ln -s /mnt/drive_a/original_file /mnt/drive_b/link_file
Bind mounts: Use mount --bind to mount a specific directory (not a file) from Drive A to a mount point on Drive B: sudo mount --bind /mnt/drive_a/specific_dir /mnt/drive_b/specific_dir
File copies: Simply copy the file to Drive B: cp /mnt/drive_a/original_file /mnt/drive_b/
I hope this clarifies the fascinating (or confusing?) world of hardlinks and mounting!