17

When doing a installation to a root btrfs filesystem, many Linux distributions install to the default subvolume. If left unmodified, this layout will force any new snapshots or subvolumes to be created inside the root filesystem, which is quite confusing, as the snapshots contain themselves:

/ 
│─dev
│─home
│─var
│─usr
│─...
└─snapshots
  └─snap1 

An easier to understand default subvolume layout would be:

/ 
├─subvolumes
│ └─root
│   ├─dev
│   ├─home
│   ├─var
│   ├─usr
│   └─...
└─snapshots
  └─snap1 

How can I change the distro-default btrfs installation to use this subvolume layout without booting from a livecd?

18

While not strictly necessary, you might want to do these steps in single user ("recovery") mode to avoid accidental data loss.

We'll create the layout we want in the default subvolume:

mkdir /subvolumes
btrfs subvolume snapshot / /subvolumes/root
mkdir /snapshots

/subvolumes/root will be our new root filesystem, so don't make any changes to the filesystem one after this step.

Edit /subvolumes/root/etc/fstab to make the system use the new root subvolume as root filesystem. For that, you'll need to modify it to include the subvol=/subvolumes/root option.

Now we need to mount our new root filesystem somewhere in order to fix grub to point to the new subvolume:

mkdir /media/temporary
mount -o subvol=/subvolumes/root /dev/sdXX /media/temporary
cd /media/temporary
mount -o bind /dev  dev
mount -o bind /sys  sys
mount -o bind /proc proc
mount -o bind /boot boot # only necessary if you have a separate boot partition
chroot .
update-grub
exit

That's it. Reboot, and your root filesystem should be the new subvolume. If this succeeded, there shouldn't be any /snapshots directory.

If you want, you may make a permanent mount point for the default subvolume:

mkdir /media/btrfs/root

then you can mount -o subvolid=0 /dev/sdXX /media/btrfs/root to mount the default subvolume.

You can now safely delete the contents of the old root filesystem in the default subvolume.

cd /media/btrfs/root
rm -rf {dev,home,var,...}
4
  • 1
    Also, note that instead of mounting, chrooting and updating grub, you can simply set the default subvolume to be rootfs with btrfs subvolume set-default ID /. ID can be found using `btrfs subvolume list / – goncalopp Nov 2 '13 at 23:52
  • I added subvol=/rootfs (note the /) to fstab as that seems to be the 'proper' way as described in the (btrfs wiki)[btrfs.wiki.kernel.org/index.php/Mount_options]. – go2null Sep 2 '17 at 2:47
  • @go2null The problem of using subvol=/rootfs is that any program that is not aware of your layout (distro installers, for example) will mount the default subvolume instead. If the program expects the filesystem to look like a linux filesystem, if will fail, or worse - fill your default subvolume with things you don't want there – goncalopp Sep 21 '17 at 23:26
  • 1
    hmm, would have thought that progs that mess around with filesystems would at least check fstab or bootloader config. – go2null Oct 26 '17 at 15:17
0

the proposed layout contains a 'tiny' problem: (regardless of that, is quite nice - I'm using it right now)

GURB - is unable to evaluate the 'set-default' path. It will always expect the grub.cfg in (hd0,gpt2)/boot/grub2/grub.cfg (or whatever you spec'ed as "--boot-directory=/path" in grub2-install)

So - the idea of having /subvolumes/{root,home,swap} DOES WORK! But unfortunately - you can't boot any longer if there's no /boot :-( (strictly, you CAN - grub loads - but won't find the grub.cfg (the menu) )

I workarounded this, by creating a subvolume '/boot', which I bind-mount in the running-OS (/subvolumes/root -> /) to /boot. Then a 'grub2-install /dev/sda --boot-directory=/boot' installs the required files. When 'grub' boots: "ls (hd0,gpt2)/boot/grub/grub.cfg" is in the expected place, and things work. The OS also 'sees' this as /boot (subvol-mount) - and can update kernels etc.

What this setup of course kinda 'kills' - I had the idea to have several independent root-installs - and just switch between them via 'btrfs set-default'. Unfortunately that's not working - due to grub.

So my /etc/fstab looks then like this:

UUID=blah /   btrfs auto,compress=no,subvol=/subvolumes/fedora_33_root 0 0
UUID=blah /boot     btrfs auto,compress=no,subvol=/boot       0 0
UUID=blah /home     btrfs auto,subvol=/subvolumes/home        0 0
UUID=blah /mnt/swap btrfs auto,subvol=/subvolumes/swap_subvol 0 0
/mnt/swap/swapfile  none          swap   0 0
New contributor
user2974635 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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