Make it Btr(fs)
How to switch the filesystem of your Linux home partition from ext2 / ext4 to Btrfs without any data loss in Debian Buster.
A couple weeks ago I reactivated my old laptop and installed the latest Debian on it. I wanted to have a machine for fun which I can use to play with and trying out things that I’m afraid to do with my regular Linux box. One of my first ideas were to play around with the Btrfs filesystem and his cool features. In this article I want to share the experience I collected during the filesystem switch of my home partition from ext4 to Btrfs.
Step 0: Setup
It’s not much you need to switch one filesystem to another. In my case I needed an external drive to backup my data, a live distro that supports Btrfs and my /home directory has to be mounted on a separate partition. Luckily the last point was fulfilled during my Debian installation because I already thought earlier, separating my home directory from the rest of the system would be a good idea.
Step 1: Check your kernel
Btrfs must be supported by your kernel because it’s deeply integrated in it. On the offical Btrfs site they recommend to always use the latest kernel because Btrfs is a “fast-moving-target”, they say. I ignored this recommendation because last time I used the latest kernel on my old box, I messed up the whole system. For this tutorial I used the standard 4.19 kernel which’s shipped with Debian Buster. So if you’re using the same kernel or something above, I’m quite sure, it will also work on your machine.
If you want to ensure that your kernel supports Btrfs you can run the following command which lists all the filesystems your kernel supports:
$> cat /proc/filesystems
Step 2: Backup your home partition
The upcoming steps were all done from a live distribution. This happened because I wanted to be 100% sure that no background activity within my home directory interrupts the backup or any other process during the switch. In my case I used a Xubuntu 20.04 Focal Fossa because it works quite nice as a live distro and also has build in support for Btrfs.
From the live distro I mounted my external hard drive to store the content of my home partition. In my case the external drive was labeled with sdb1 and my home partition with sda6. To transfer all my files I chose rsync, because I wanted to keep the ownership and the privileges of the files in my home directory during the transfer. In case of a regular copy, the files would belong to root and can not be used from my host system without further interactions of me.
$> sudo mkdir /mnt/backup-disk
$> sudo mkdir /mnt/debian-home$> sudo mount /dev/sdb1 /mnt/backup-disk
$> sudo mount /dev/sda6 /mnt/debian-home$> sudo rsync -av /mnt/debian-home/ /mnt/backup-disk
Step 3: Create the new filesystem and subvolumes
After the backup was created and I verified that all data were transferred to the backup disk I wiped my old ext4 filesystem and created the new Btrfs filesystem.
$> sudo umount /mnt/debian-home
$> sudo mkfs.btrfs -f -L home /dev/sda6
Once the basic filesystem was created I remounted the partition and created two subvolumes. Subvolumes are one of the power features in Btrfs. A subvolume is a part of filesystem with its own independent file/directory hierarchy. Subvolumes can be mounted separate in the system and interact as a kind of “virtual” volume without knowledge about the surrounding subvolumes. From a subvolume you can also simply create snapshots which can be used to restore data instantly, another power feature of Btrfs. My intent was to create a subvolume for my home directory and one for my snapshots. If I wanted to do some risky stuff with the data inside my home directory I can create a snapshot in advance and restore it, if it becomes necessary.
$> sudo mkdir /mnt/btrfs-home
$> sudo mount /dev/sda6 /mnt/btrfs-home$> sudo btrfs subvolume create /mnt/btrfs-home/@home
$> sudo btrfs subvolume create /mnt/btrfs-home/@.snapshots
From now, you can list the subvolumes of your filesystem using the following command. Keep track of the ‘@home’ subvolume id (256) , it will be used in further steps:
$> sudo btrfs subvolume list /mnt/btrfs-home
Step 4: Restore the data
To restore my data I had to mount the subvolume of the fresh created partition. To mount the subvolume I had to use the id of the subvolume and pass it to the mount command. You can also use the name of the subvolume but I prefer the id. I also added the compression option to ensure that all data will be written compressed to the disk.
$> sudo mkdir /mnt/subvolume_home
$> sudo mount -o subvolid=256,compression=lzo /dev/sda6 /mnt/subvolume_home
After the new partition was mounted I restored my data from the external hard disk back to the new Btrfs subvolume.
$> sudo rsync -av /mnt/backup-disk /mnt/subvolume_home
Step 5: Edit the filesystem table (fstab)
To make the new partition persistent in my debian system I had to edit the fstab file and provide all information for mounting the partition during the boot process. But before I had to find the new uuid of the created Btrfs partition because with changing the filesystem of the /dev/sda6 partition the uuid of the device also had changed. If you’re not using the correct uuid the system will not boot because the home partition can not be found. The new uuid can be found using the Btrfs filesystem show command.
$> sudo btrfs filesystem show /mnt/btrfs-home
$> sudo mkdir /mnt/debian-root
$> sudo mount /dev/sda1 /mnt/debian-root
$> sudo nano /mnt/debian-root/etc/fstab
File content excerpt:
UUID=6f019b4f-8b3e-4337–9f4c-4a56ccc81206 /home btrfs defaults,subvolid=256,compress=lzo 0 2
Step 6: Restart the hostsystem
That’s it! After shutting down the live distro I started my Debian system and Voilà, Debian runs Btrfs for my home partition.
Step 7: Install btrfs tools (Debian specific)
To create snapshots and use all the other cool Btrfs features I needed the “btrfs tools” which are not pre installed on Debian. To fix that I simply installed them using apt.
Final thoughts:
This was a funny first project with my new old legacy machine. I learned a lot about the Btrfs filesystem and also discovered that the “btrfs tools” provide a migration tool which can be used to migrate ext2 / ext3 / ext4 partitions to Btrfs. In my case that would simplify the steps 2–4 because the tool would handle these. But to understand what’s going on during that migration process I would also have chosen the way I walked through it even if I have known that tool before.