18.12 Converting an Ext2, Ext3, or Ext4 File System to a Btrfs File System

18.12.1 Converting a Non-root File System
18.12.2 Converting the root File System
18.12.3 Mounting the Image of the Original File System
18.12.4 Deleting the Snapshot of the Original File System
18.12.5 Recovering an Original Non-root File System

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.

18.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:

  1. Unmount the file system.

    # umount mountpoint
  2. 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
  3. Convert the file system to a btrfs file system.

    # btrfs-convert device
  4. 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
  5. Mount the converted file system on the old mount point.

    # mount device mountpoint

18.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:

  1. 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.

    # mount
    ...
    /dev/mapper/vg_hostol6-lv_root on / type ext4 (rw)
    ...
    # ls -l /dev/mapper/vg_hostol6-lv_root
    lrwxrwxrwx. 1 root root 7 Sep 14 14:00 /dev/mapper/vg_hostol6-lv_root -> ../dm-0
    # ls -l /dev/vg_hostol6/lv_root
    lrwxrwxrwx. 1 root root 7 Sep 14 14:00 /dev/vg_hostol6/lv_root -> ../dm-0

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

    # mount
    ...
    /dev/sda2 on / type ext4 (rw)
    ...
  2. Shut down the system.

  3. 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.

  4. 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.

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

  6. 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
  7. 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).

  8. Convert the file system to a btrfs file system.

    bash-4.1# btrfs-convert device
  9. 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
  10. 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
  11. 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.

  12. Unmount the converted root file system.

    bash-4.1# umount /mnt1
  13. Remove the boot CD, DVD, or ISO, and reboot the system.

18.12.3 Mounting the Image of the Original File System

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

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

    # mount -t btrfs -o subvol=ext2_saved device temp_mountpoint1 
  2. 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

18.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:

  1. 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
  2. Rebalance the btrfs file system.

    # btrfs filesystem balance device

18.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:

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

    # umount temp_mountpoint2
    # umount temp_mountpoint1/image
    # umount mountpoint
  2. Roll back the conversion.

    # btrfs-convert -r device
  3. Mount the original file system.

    # mount -t extN device mountpoint