Hard-links and mounts

If I create two mounts within a privileged container, I cannot create a hard link from one to another.

E.g.

lxc init ubuntu: c
lxc config set c security.privileged true
sudo mkdir a
sudo mkdir b
lxc config device add c ad disk source=$(realpath -e a) path=/a 
lxc config device add c bd disk source=$(realpath -e b) path=/b
lxc start c
lxc exec c bash
cd /a
echo a > afile
cd ..
cp --link a/afile b/afile

The cp command gets me the error “cp: cannot create hard link ‘b/afile’ to ‘a/afile’: Invalid cross-device link”

If I check device IDs with “stat -c %D” I get 801 for both /a and /b.

My storage driver is ‘dir’.

How do I create this hard-link, or, if this is not possible, how do I let my script check if hard-links are possible?

Thanks

Mike

The commands you use to create and manage the container indicate that you’re using LXD, not LXC. You may want to change the category of this thread to LXD where it might attract more attention.

That being said, I just tried this same thing in a regular Linux install running in a Xen VM, so fully virtualised, and it doesn’t work there either:

I had the same device mounted at /home and /mnt and tried the following:

user@main:~$ echo "bar" > foo

user@main:~$ ls -l foo
-rw-r--r-- 1 user user 4 Feb  3 01:11 foo

user@main:~$ ls -l /mnt/home/user/foo 
-rw-r--r-- 1 user user 4 Feb  3 01:11 /mnt/home/user/foo

user@main:~$ cp --link foo /mnt/home/user/bar
cp: cannot create hard link '/mnt/home/user/bar' to 'foo': Invalid cross-device link

user@main:~$ cp --link foo bar

user@main:~$ ls -l foo bar
-rw-r--r-- 2 user user 4 Feb  3 01:11 bar
-rw-r--r-- 2 user user 4 Feb  3 01:11 foo

It seems you just cannot create hard links that cross mount points, even when it’s the same device (bind) mounted to different places in the FS hierarchy. Doesn’t seem to be the container’s fault.

1 Like

I changed the category of the thread to LXD.