You can use the btrfs-convert utility to convert an
ext2, ext3, or ext4 file system to
btrfs. The utility preserves an image of the original file system in a
snapshot named ext2_saved. This snapshot allows you to roll back the
conversion, even if you have made changes to the btrfs file system.

If you convert the root file system to btrfs, you can use snapshots to roll back changes
such as upgrades that you have made to the file system.

Note

You cannot convert a bootable partition, such as /boot, to a btrfs
file system.

19.12.1 Converting a Non-root File System

Caution

Before performing a file system conversion, make a backup of the file system from
which you can restore its state.

To convert an ext2, ext3, or
ext4 file system other than the root file system to
btrfs:

Unmount the file system.

# umount mountpoint

Run the correct version of fsck (for example,
fsck.ext4) on the underlying device to check and correct the
integrity of file system.

# fsck.extN -f device

Convert the file system to a btrfs file system.

# btrfs-convert device

Edit the file /etc/fstab, and change the file system type of
the file system to btrfs, for example:

/dev/sdb /myfs btrfs defaults 0 0

Mount the converted file system on the old mount point.

# mount devicemountpoint

19.12.2 Converting the root File System

Caution

Before performing a root file system conversion, make a full
system backup from which you can restore its state.

To convert an ext2, ext3, or
ext4
root file system to btrfs:

Run the mount command to determine the device that is
currently mounted as the root file system, and the type of the file
system.

In the following example, the root file system is configured as an LVM logical
volume lv_root in the volume group vg_hostol6,
and the file system type is ext4. Using the ls
-l command confirms that the mapped device corresponds to
/dev/vg_hostol6/lv_root.

In the next example, the root file system corresponds to the disk partition
/dev/sda2:

# mount
...
/dev/sda2 on / type ext4 (rw)
...

Shut down the system.

Boot the system from an Oracle Linux 6 Update 3 or later UEK Boot ISO (which you
can burn to CD or DVD if necessary). You can download the UEK Boot ISO from https://edelivery.oracle.com/linux.

Note

You must use the UEK Boot ISO. You cannot use the RHCK Boot ISO to perform the
conversion.

From the installation menu, select Rescue Installed
System. When prompted, choose a language and keyboard, select Local CD/DVD as the installation media, select No to bypass starting the network interface, and select
Skip to bypass selecting a rescue environment.

Select Start shell to obtain a
bash shell prompt (bash-4.1#) at the bottom of
the screen.

If the existing root file system is configured as an LVM
volume, use the following command to start the volume group (for example,
vg_hostol6):

bash-4.1# lvchange -ay vg_hostol6

Run the correct version of fsck (for example,
fsck.ext3 or fsck.ext4) to check and correct
the integrity of the file system.

bash-4.1# fsck.extN -f device

where device is the root file system device (for
example, /dev/vg_hostol6/lv_root or /dev/sda2).

Convert the file system to a btrfs file system.

bash-4.1# btrfs-convert device

Create a mount point (/mnt1) and mount the converted
root file system on it.

bash-4.1# mkdir /mnt1
bash-4.1# mount -t btrfs device /mnt1

Use the vi command to edit the file
/mnt1/etc/fstab, and change the file system type of the
root file system to btrfs, for example:

/dev/mapper/vg_hostol6-lv_root / btrfs defaults 1 1

Create the file .autorelabel in the root of the mounted file
system.

bash-4.1# touch /mnt1/.autorelabel

The presence of the .autorelabel file in /
instructs SELinux to recreate the security attributes of all files on the file system.

Note

If you do not create the .autorelabel file, you might not be
able to boot the system successfully. If you forget to create the file and the
reboot fails, either disable SELinux temporarily by specifying
selinux=0 to the kernel boot parameters, or run SELinux in
permissive mode by specifying enforcing=0.

Unmount the converted root file system.

bash-4.1# umount /mnt1

Remove the boot CD, DVD, or ISO, and reboot the system.

19.12.3 Mounting the Image of the Original File System

To mount the image of the original file system read-only:

Mount the snapshot of the original file system on a temporary mount point.

# mount -t btrfs -o subvol=ext2_saved devicetemp_mountpoint1

Mount the image of the original file system read-only on another temporary mount
point, specifying the correct file system type (ext2,
ext3, or ext4) to the -t
option.

# mount -t extN -o loop,ro temp_mountpoint1/image temp_mountpoint2

19.12.4 Deleting the Snapshot of the Original File System

Caution

If you delete the snapshot of the original file system to save storage space, you
will no longer be able to recover the original file system.

To delete the snapshot of the original file system and recover the space that it uses:

Delete the ext2_saved subvolume.

# btrfs subvolume delete mountpoint/ext2_saved

For example, if you converted the root file system (/) file
system, you would enter:

# btrfs subvolume delete //ext2_saved

For another file system, such as /usr, you would enter:

# btrfs subvolume delete /usr/ext2_saved

Rebalance the btrfs file system.

# btrfs filesystem balance device

19.12.5 Recovering an Original Non-root File System

Caution

If you roll back a conversion, you will lose any changes that you have made to the
btrfs file system. Make a back up of the changes that you want to reapply to the restored
file system.

To roll back the conversion of the file system and recover the original file system:

Unmount the btrfs file system and all of its snapshots and images in the reverse
order from which you originally mounted them.