HOWTO convert EXT2/3/4 partitions to Btrfs in CentOS 6 without losing data

ref: http://forums.fedoraforum.org/showthread.php?t=246520

/!\ This process has been tested by the author to convert root (/) partitions to btrfs using using the CentOS v6.5 LiveCD installer.

An emerging feature in CentOS 6 is better support for the next Linux file system btrfs.

However, the installer does not allow the creation of a btrfs filesystem, so it needs to be converted post-install. The Fedora version of Anaconda does seem to allow the creation of a btrfs partition by adding the linux btrfs parameter to the grub boot command during installation.

This HOWTO provides a workaround to the limitations above, which means you can still enjoy btrfs in CentOS no matter what installation media you use.

/!\ CAUTION: This is not and never meant to be a step-by-step guide because users who want to try btrfs probably have sufficient knowledge in relative areas.

STEP 1: Install CentOS

Get CentOS 6.5 installed on your hard drive using either ext3 or ext4 file system type. Be sure to layout the filesystem how you would like it to finish when converted to btrfs as this will only convert existing partitions. Due to btrfs sub-volume capability, it may be worthwhile just creating one big / filesystem, and avoid using LVM as btrfs has some volume management capabilities.

/!\ During the install ensure there is a separate /boot partition which will remain as ext3/4.

STEP 2: Install `btrfs-progs`

Boot into the freshly installed system and set it up to operate correctly when converted to btrfs. This requires installing btrfs-progs, which is not installed by default. System needs them when booting from a btrfs partition. Also, ensure the btrfs kernel modules can be loaded.

su -c 'yum install btrfs-progs'
modprobe btrfs

STEP 3: Boot into LiveCD

Boot from a CentOS LiveCD or LiveUSB (there are plenty of tutorials around on this topic).

STEP 4: Install `btrfs-progs` in LiveCD

Gain root privilege in Live environment. Then install btrfs-progs in the Live media using the identical command (as above). We need to use btfs-convert inside that Live environment.

STEP 5: Check LiveCD btrfs modules

Make sure you can mount btrfs partitions before you convert them by loading the btrfs modules.

modprobe btrfs

It may be necessary to copy them from the installed system. During one of my attempts the Live environment had corrupt kernel modules.

STEP 6: Convert the partitions

Now convert the partitions to btrfs as needed. Do NOT convert the /boot partition. Replace <your_partition_name> with your actual partition name in the following command:-

btrfs-convert  <your_partition_name>

It would take some time depending on the size of partition. A 400G ext4 partition can take almost 2 hours to be converted. So be patient.

Repeat these steps if you have several partitions to be converted.

/!\CAUTION: DO NOT ATTEMPT TO CONVERT /boot PARTITION !

STEP 7: Fix up `/etc/fstab`

Use blkid to see the new UUID for converted partition. Edit /etc/fstab (the one on the HARD DRIVE, NOT LIVE MEDIA) according to those UUIDs. Do not forget to change the partition type to btrfs.

/!\ You may want to add compress mount option, and ssd for SSD users.

mount <your_partition_name> /mnt
vi /mnt/etc/fstab
UUID=4d268d02-5aec-4362-a6de-99e292c3d690 /    btrfs    defaults,compress=lzo,ssd        1 1

STEP 8: Fix SELinux

Fix the SELinux context by create an empty file called .autorelabel in / partition of HARD DRIVE. 'touch' would do the job. It is the simplest way if you converted the / partition. The 'restorecon' command would work if the filesystem does not required auto-mount during booting (not the case for a / partition).

mount <your_partition_name> /mnt
touch /mnt/.autorelabel

STEP 9: Fix `grub.conf`

If you converted / partition, you probably need to edit /boot/grub/grub.conf (same,the one on the HARD DRIVE, NOT LIVE MEDIA) kernel boot line as well.

umount /mnt
mount <your_boot_partition_name> /mnt
vi /mnt/grub/grub.conf

title CentOS (2.6.32-431.1.2.0.1.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.1.2.0.1.el6.x86_64 ro root=UUID=4d268d02-5aec-4362-a6de-99e292c3d690 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD quiet SYSFONT=latarcyrheb-sun16 rhgb crashkernel=auto  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM
        initrd /initramfs-2.6.32-431.1.2.0.1.el6.x86_64.img

STEP 10: Reboot into btrfs system

Restart the computer and boot into CentOS on the HARD DRIVE. It might take some time if .autorelabel file was created. An alternate method to rebuild the SELinux contexts might be to run restorecon from the LiveCD.

CONGRATULATIONS! Btrfs is on your disk and your data is there, too.

Rollback

/!\ If there are any problems, it is possible to roll back to previous file system! The procedure is similar, except in STEP 5 using these command:

btrfs-convert  -r <your_partition_name>

If there is no need to rollback, to retrieve some disk space delete the snapshot of original file system:

btrfs subvolume delete <your_btrfs_partition_mount_point>/ext2_saved

That's all. Hope you have a good time with Btrfs!

PhilipJensen/CentOS6ConvertToBTRFS (last edited 2014-01-08 01:08:12 by PhilipJensen)