We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – How To Convert Ext4 filesystem to Btrfs

Learn how to – How To Convert Ext4 filesystem to Btrfs.

Btrfs Is a modern filesystem that combines a file system based on the COW(copy-on-write) principle for Linux systems. This file system aims to implement advanced features such as repair, fault tolerance, and simplified administration to Linux file systems. In its design, Btrfs provides high performance and supports large storage servers.

The COW principle implemented here means that when data is modified on a file system, it is copied, modified, and written on a different free location of the filesystem. This helps avoid data corruption or partial update errors during power losses.

Btrfs offers the following features:

  • Huge file size support
  • Extent-based file storage
  • Space-efficient packaging of small files
  • Writable snapshots and read-only snapshots
  • Subvolumes support
  • Subvolume aware quota support
  • Integrated multiple device support with LVM support, configure RAID arrays e.t.c
  • Checksums on data and metadata
  • Space-efficient indexed directories

The main aim of this guide is to provide an overview of the Btrfs filesystem as well as a systematic walk-through of how you can convert the Ext4 filesystem to Btrfs. Converting a non-root file system can be done easily without a reboot. To convert a root file system, you need to boot into a Live CD. In this guide, we will be learning how to convert the root file system and roll it back to Ext4 if need to.

Getting Started.

It is highly recommended that you take a backup of your home folder and any important data on your system such as /etc.

Once a backup is made, proceed and update your system packages to their latest available versions.

##On Debian/ubuntu
sudo apt update && sudo apt upgrade

##On Fedora
sudo dnf update

Step 1 – Free up disk space

The btrfs-convert command recreated the filesystem data into the partition’s free space while retaining the existing EXT4 data in its current location.

The amount of the required free space cannot be known, if you do not have enough space, the conversion will fail but doesn’t harm your data. To get more free space:

  • Clean up unused packages and flatpaks: dnf autoremove, flatpak remove –unused,
  • Clean up package caches: pkcon refresh force -c -1, dnf clean all
  • Clean up unused virtual machine images inside e.g. GNOME Boxes
  • Clean up old system journals: journalctl –vacuum-size=100M
  • If you are using Docker, carefully use tools like docker volume prune, docker image prune -a
  • Use baobab to identify large files & folders to remove. Don’t manually delete files outside of your home folder if possible.

Once this has been done and enough free space is available, proceed as below.

Step 2 – Create a live image

Since it is not possible to convert a mounted file system, we need to use a Live image. I will demonstrate this using an Ubuntu 22.04 system with a live image downloaded as shown:

# Download with wget - Desktop Edition (Intel 64-bit)
wget https://cdimage.ubuntu.com/daily-live/current/jammy-desktop-amd64.iso

# Download with wget - Desktop Edition (ARM 64-bit)
wget https://cdimage.ubuntu.com/daily-live/current/jammy-desktop-arm64.iso

You can also use another image for Live booting that is suitable for the system. For example the Fedora 35 image:

wget https://download.fedoraproject.org/pub/fedora/linux/releases/35/Workstation/x86_64/iso/Fedora-Workstation-Live-x86_64-35-1.2.iso

With the ISO file, create a bootable USB stick as below.

# Identify USB device
$ sudo lsblk

# Flash OS to USB drive
$ sudo dd bs=4M if=/jammy-desktop-amd64.iso of=/dev/sdx status=progress oflag=sync

Ensure you replace /dev/sdx with the USB disk path and jammy-desktop-amd64.iso with the correct ISO file path.

Step 3 – Boot Into Live CD

Once the installation media has been created, boot into it. You may need to modify your boot options to boot from the Live CD.

Proceed to Live mode as shown. For Ubuntu 22.04:

Switch to the root user:

sudo su -

Identify your device using gnome-disks or alternatively using Fdisk as below.

$ fdisk -l
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 50B1DEBD-1E51-47C6-A980-272DCF1B9287

Device     Start      End  Sectors Size Type
/dev/sda1      2048 79903539 79901492 38.1G Linux filesystem
/dev/sda2  79904768 79908863     4096    2M BIOS boot
.......



Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk model: QEMU HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 89C5C503-8E64-4CC2-8E0C-B54634768D73

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  1054719  1050624  513M EFI System
/dev/sda3  1054720 83884031 82829312 39.5G Linux filesystem
...

Here, my partition is /dev/sda3. Verify that it is an EXT4 filesystem.

$ file -sL /dev/sda3
/dev/sda3: Linux rev 1.0 ext4 filesystem data, UUID=0c84a840-1dfb-4ea3-b387-57d6182f7672 (extents) (64bit) (large files) (huge files)

Step 4 – Convert the Ext4 Filesystem to Btrfs.

Once the appropriate disk has been identified, you can now run the commands below to convert to Btrfs:

Umount the disk and check its integrity.

sudo umount /dev/sda3
sudo fsck.ext4 -fyv /dev/sda3

Sample Output:

e2fsck 1.46.5 (30-Dec-2021)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information

      216559 inodes used (8.37%, out of 2588672)
          89 non-contiguous files (0.0%)
         240 non-contiguous directories (0.1%)
             # of inodes with ind/dind/tind blocks: 0/0/0
             Extent depth histogram: 181707/22
     2701443 blocks used (26.09%, out of 10353664)
           0 bad blocks
           1 large file

      158883 regular files
       20244 directories
           8 character device files
           0 block device files
           0 fifos
           6 links
       37408 symbolic links (34807 fast symbolic links)
           7 sockets
------------
      216556 files

Once the integrity is checked, proceed as below

sudo btrfs-convert /dev/sda3

Remember to replace sda3 with your partition. In case of any trouble, get help using the man command: man btrfs-convert

Sample execution output:

create btrfs filesystem:
	blocksize: 4096
	nodesize:  16384
	features:  extref, skinny-metadata (default)
	checksum:  crc32c
free space report:
	total:     41874882560
	free:      40559443968 (96.86%)
creating ext2 image file
creating btrfs metadata
copy inodes [.] [     83935/     80608]
conversion complete

This process takes quite a while depending on your partition size and whether solid-state or rotational.

In case you find errors, you need to create more free space and run btrfs-convert -n

Step 5 – Mount the disk

Now that you have a new Btrfs filesystem, you are required to mount it as below.

sudo mount /dev/sda3 /mnt

List subvolumes in the Path.

$ btrfs subvolume list /mnt
ID 256 gen 3 top level 5 path ext2_saved

In the above output, we have several subvolumes such as toplevel and ext2_saved created by default.

Step 6 – Create subvolumes.

If you want to have a flat subvolume layout where you can have snapshots for the /home directory, and different mount options for each volume proceed as below.

cd /mnt
btrfs subvolume snapshot ./ ./root2
btrfs subvolume create home2
cp -a home/* home2/

Now we have to volumes created

  • root2 – a full snapshot of the partition
  • home2 – created empty volume and copied contents of home/* to it.

Now with the two subvolumes made, delete everything in /mnt except root2, home2, and ext2_saved.

sudo rm -rf bin dev boot cdrom etc home lib* lost+found media mnt opt proc root run sbin snap srv swap.img tmp usr sys var usr
  • Rename root2 and home2 subvolumes to root and home.
mv root2 root
mv home2 home
  • Inside the root subvolume, empty out the home folder so that we can mount the home subvolume there later.
cd root/home
rm -rf *

Step 7 – Modify the Fstab

We need to make the volumes persistent after boot so we have to modify the fstab by replacing the old ext4 files with Btrfs as below.

First, identify the UUID of the partition.

# blkid
.....
/dev/sda3: UUID="a8c320fa-a9d6-42c4-8fa2-95c10c6b202f" UUID_SUB="93562e7f-f631-45dc-a49c-34be5b393b9a" BLOCK_SIZE="4096" TYPE="btrfs" PARTUUID="8d3d5e37-635b-4ce5-a52e-34adb5a18da1"
.......

Open the FSTAB file

vim /mnt/root/etc/fstab

Add the lines below.

###Comment out the existing EXT4 filesystem and add this##
UUID=xx / btrfs subvol=root 0 0
UUID=xx /home btrfs subvol=home 0 0

Replace XX with the UUID of the BTRS partition. The UUID is the same and remember to comment out the previous ext4 partition.

For example:

Save the file and remount the directories.

cd
umount /mnt
mount -o subvol=root /dev/sda3 /mnt
mount /dev/sda3 /mnt/boot
mkdir /mnt/boot/efi
mount /dev/sda3 /mnt/boot/efi

Proceed and mount system devices as below.

mount -t proc /proc /mnt/proc
mount --rbind /dev /mnt/dev
mount --make-rslave /mnt/dev
mount --rbind /sys /mnt/sys
mount --make-rslave /mnt/sys
cp /mnt/etc/resolv.conf /mnt/etc/resolv.conf.chroot
cp -L /etc/resolv.conf /mnt/etc

Now Chroot into your system

chroot /mnt /bin/bash

Verify if you can access the internet by pinging the below URL.

# ping google.com
PING google.com (172.217.18.14) 56(84) bytes of data.
64 bytes from fra15s28-in-f14.1e100.net (172.217.18.14): icmp_seq=1 ttl=118 time=5.21 ms
64 bytes from fra24s22-in-f14.1e100.net (172.217.18.14): icmp_seq=2 ttl=118 time=5.19 ms
^Z
[1]+  Stopped                 ping google.com

If it is not resolvable, create a resolv.conf file as below.

rm -rf etc/resolv.conf
vi etc/resolv.conf

Add the below lines and retry pinging,

nameserver 8.8.8.8
nameserver 8.8.4.4

Step 8 – Reinstall GRUB & Kernel

Still inside chroot, reinstall the Kernel and GRUB as below:

On Debian/Ubuntu:

Edit the file below and add the line:

# vim etc/default/grub
.....
GRUB_DISABLE_OS_PROBER=false

Now reinstall grub with the commands:

mount /boot/efi
apt reinstall grub-efi shim*
grub-mkconfig -o /boot/efi/efi/grub.cfg
grub-install /dev/sda

Sample output:

List the Linux images

# dpkg -l | grep linux-image-.*-generic
rc  linux-image-5.15.0-25-generic              5.15.0-25.25                            amd64        Signed kernel image generic
ii  linux-image-5.15.0-33-generic              5.15.0-33.34                            amd64        Signed kernel image generic
ii  linux-image-5.15.0-40-generic              5.15.0-40.43                            amd64        Signed kernel image generic

Reinstall the preferred image:

apt-get install --reinstall linux-image-5.15.0-40-generic 
update-grub

On Fedora, use the below commands to reinstall grub

mount /boot/efi
dnf reinstall grub2-efi shim 
dnf reinstall kernel-core
grub-install /dev/sda
grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg

Once everything has been accomplished, shut down the system, eject the installation media, and boot from your disk.

Step 9 – Post Conversion Activities

If the system boots correctly, and you have the new Btrfs system working as desired, you can now reclaim the volume occupied by the old ext4 snapshot. This snapshot can be used to roll back to the old EXT4 file system. You can only lose the ability to roll back if you defragment and balance the subvolumes.

At this point, you can delete the snapshot if you are comfortable with the Btrfs file system.

Mount the top-level subvolume:

sudo su - 
mount -o subvol=/  /dev/sda3 /mnt
cd /mnt

You may need to install the tool below before you proceed to delete the snapshot.

apt install btrfs-progs

Delete the snapshot:

btrfs subvolume delete ext2_saved

Finally, verify the changes:

mount | grep "^/dev"

Sample Output:

Update grub:

update-grub

That is it!

Wrapping up

This guide has provided you with the required knowledge on how to convert an Ext4 filesystem to the Btrfs filesystem. I hope this guide was significant to you.

See more:

Understanding and Working With BtrFS Filesystem in Linux

How To Resize XFS / Btrfs file systems on Linux

How To Install Arch Linux using Arch Linux installer

Cart

Your Cart is Empty

Back To Shop