Migrating your /home to Btrfs

     

There may be several reasons to switch to Btrfs, for me personally it’s because my KF2 server is eating up too much space, and Btrfs supports both compression and deduplication. Neither ext4 nor XFS have these so on CentOS I have no other option than Btrfs. Beware, RH deprecated it, so the 8.x series won’t have it, but until then, it’s pretty darn sweet. I’m pretty sure EPEL or some other repo will pick it up afterwards anyway.

Anyhow, to migrate your /home to Btrfs:

  1. Make sure you’ll be able to access your server as root after the operation, because remember, your /home won’t be accessible for a little while.
  2. Boot into a live CD with GParted included, e.g. SystemRescueCD or GParted Live.
  3. Start GParted and shrink your root (/) filesystem to a reasonable size, like 5G.
  4. Create a new, Btrfs formatted partition on the free space with GParted or cfdisk + mkfs.btrfs.
  5. Mount your root filesystem under the live environment, e.g.
mkdir /mnt/root
mount /dev/vda2 /mnt/root

Where /dev/vda2 is the identifier of your root partition, check with fdisk -l, YMMV.

  1. Rename your /home, e.g.
pushd /mnt/root
mv home home-old
popd
  1. Add the new partition to /mnt/root/etc/fstab, e.g.
UUID=487fd25d-cbcf-44e7-a835-4411ae5183d1 /home btrfs defaults,compress-force=zlib 0 0

You can check the partition’s UUID with blkid.

  1. Unmount your root, e.g. umount /dev/vda2.
  2. Reboot.
  3. Copy your files from /home-old to /home. There are several options for this, cp, rsync, etc.
  4. Fix file permissions, if needed (shouldn’t be with rsync).
  5. Fix SELinux contexts, e.g. restorecon -rv /home.

If you’re curious how much space you save with compression and stuff:

yum install gcc btrfs-progs-devel
git clone https://github.com/kilobyte/compsize.git
cd compsize
make
./compsize /home

To me it’s quite impressive:

# compsize /home
Processed 4126 files, 224846 regular extents (224847 refs), 1750 inline.
Type       Perc     Disk Usage   Uncompressed Referenced
Data        41%       12G          28G          28G
none       100%      3.3G         3.3G         3.2G
zlib        34%      8.7G          25G          25G

Enjoy.