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 snapshots or subvolumes to be created inside the root filesystem, which may be undesirable. Another possible layout would be to have the default subvolume contain a snapshots directory, and a rootfs subvolume, which is the root filesystem. How can I change the distro-default btrfs installation to use this subvolume layout without booting from a livecd?

Firstly, we'll create the layout we want in the default subvolume:

btrfs subvolume snapshot / /rootfs
mkdir /snapshots

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

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

mount our new root filesystem somewhere, mount the relevant fileystems (dev, sys, proc, boot if you have it), chroot to it and update grub:

mount -o subvol=rootfs /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
chroot .
update-grub
exit

That's it. Reboot, and your root filesystem should be the rootfs subvolume. If this succeeded, there shouldn't be any /snapshots directory. You can now delete the contents of the old root filesystem in the default subvolume.

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 and create your snapshots/subvolumes.

  • 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
  • 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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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