You are not logged in.

#1 2021-11-27 05:51:17

PBS
Member
Registered: 2015-06-28
Posts: 44

Mounting btrfs subvolume via symlink

I recently found out by experimenting that you can mount a btrfs subvolume by making a symlink to it and attempting to mount the symlink instead. For example, if the btrfs filesystem contains

subvolume
link --> subvolume

then you can do

mount ... -t btrfs ... -o subvol=link

and you get 'subvolume' mounted instead.

Neither the man page BTRFS-SUBVOLUME(8) nor the btrfs faq seem to mention using symlinks in this way. Am I using an undocumented feature, which is hence liable to break in the future, or is it just obvious that this should work? What are the rules for how such symlinks are resolved - for example, if a symlink points to an absolute path, is that interpreted relative to the btrfs filesystem root or the system root?

Offline

#2 2021-11-27 06:50:20

willemw
Member
Registered: 2013-02-19
Posts: 84

Re: Mounting btrfs subvolume via symlink

Interesting. How exactly did you do this? In your example: how can "link" be a symlink file and a subvolume name?

Offline

#3 2021-11-27 07:36:19

PBS
Member
Registered: 2015-06-28
Posts: 44

Re: Mounting btrfs subvolume via symlink

Something like

cd /tmp
mkdir test
cd test
dd if=/dev/zero of=image.iso bs=1024 count=150000
losetup /dev/loop0 image.iso
mkfs.btrfs /dev/loop0
mkdir mnt
mount /dev/loop0 mnt
cd mnt
btrfs subvolume create subvolume
ln -sr subvolume link
cd ..
umount mnt
mount /dev/loop0 -t btrfs mnt -o subvol=link

although I pieced this together from my bash history, so it isn't actually tested.

Offline

#4 2021-11-28 11:43:37

PBS
Member
Registered: 2015-06-28
Posts: 44

Re: Mounting btrfs subvolume via symlink

It turns out you can even boot off one of these symlinks! (I.e. specifying it as the system root on the kernel commandline.)

I should also add for anyone wishing to try out the code sample above, don't forget to clean up by running

umount mnt
rmdir mnt
losetup -d /dev/loop0
rm image.iso
cd ..
rmdir test

and also remember to adjust if necessary if /dev/loop0 is not the first available loop device, or you'll end up wiping whatever was there.

Offline

#5 2021-11-28 12:05:25

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 6,232

Re: Mounting btrfs subvolume via symlink

I can confirm your findings but you don't need to use losetup or dd:

$ mkdir -p /tmp/test && cd $_
$ fallocate -l 10G image.iso
$ mkfs.btrfs image.iso
# mount image.iso /mnt && cd $_
# btrfs subvolume create subvolume
# ln -sr subvolume link && cd -
# umount /mnt
# mount -o subvol=link image.iso /mnt

And to test:

$ findmnt /mnt
TARGET SOURCE                 FSTYPE OPTIONS
/mnt   /dev/loop0[/subvolume] btrfs  rw,relatime,ssd,space_cache=v2,subvolid=256,subvol=/subvolume
$

To clean up:

# umount /mnt
$ cd
# rm -r /tmp/test

HTH

Offline

#6 2021-11-28 12:17:18

willemw
Member
Registered: 2013-02-19
Posts: 84

Re: Mounting btrfs subvolume via symlink

@Head_on_a_Stick: shouldn't the test check via the symlink "link" instead of "subvolume" directly?

Offline

#7 2021-11-28 12:32:57

Head_on_a_Stick
Member
From: London
Registered: 2014-02-20
Posts: 6,232

Re: Mounting btrfs subvolume via symlink

I'm sorry, I don't understand your question hmm

The "link" subvolume has been called from the mount command but findmnt shows the "subvolume" subvolume is actually mounted.

Offline

#8 2021-11-28 12:49:52

willemw
Member
Registered: 2013-02-19
Posts: 84

Re: Mounting btrfs subvolume via symlink

Nevermind. The mount is done via "subvol=link".

As far as I know the value in subvol=<value> is a path relative to the root folder of the subvolume. And <value> can optionally start with a /.

Offline

#9 2021-11-28 12:58:45

willemw
Member
Registered: 2013-02-19
Posts: 84

Re: Mounting btrfs subvolume via symlink

I guess any symlink, e.g. a symlink with an absolute path, in subvol=<symlink> would also be relative to the root folder of the subvolume mountpoint.

Last edited by willemw (2021-11-28 13:06:03)

Offline

Board footer

Powered by FluxBB