Sunday, February 8, 2015

Coverting a ext4 filesystem into btrfs


  I was working with ASM at my work , ASM is high performant Storage manager , Volume manager, filesystem all in one solution atop which Oracle runs its enterprise database clusters but its proprietary .I found it very elegant .

In my personal linux box the latest setup i had was a lvm (lvm2 is the  version, lvm - linux volume manager) volume group with multiple lvm volumes on top of which i had laid out ext4 filesystem (Fedora 21) and xfs filesystem (Centos 7 ) and  2 separate ntfs partitions for windows of course.

On LVM

 The advantage with LVM is we can dynamically add disks and increase size by extending the volume group over the new disk online ,extend the logical volume over the space added to volume group and resize the filesystem to utilize the added  space in the logical volume .
LVM also supports mirroring and stripping , i have not tested it much as i have been using a single disk (Its a lap) 
LVM approach is  elegant but the only inconvenience is we need to do modifications at volume manager level and filesystem level unlike asm

Btrfs Overview .

B- tree filesystem is the latest filesystem available in linux with cow, snapshots and many other new functionalities in addition to existing functionalities like journaling in ext4 filesystem .To me , it looked like the next generation filesystem i was searching for and had a design philosophy similar to ASM :)

Incidentally , Btrfs itself is initially developed by oracle which was later open sourced and merged into the linux kernel .
Btrfs has direct support for block devices (hard disks and ssds ) at filesystem level and eliminates the need for a separate volume manager like LVM (However we can still run btrfs as a filesystem above lvm using only filesystem level features , this is the example i l be covering today )

Moreover btrfs had been marked stable for more than a year and half and many distros have good enough support (/boot support may be limited based on distro ) for btrfs filesystem , so i wanted to immediately give it a try.

I did not want to reformat or backup my data and btrfs has tools for online filesystem conversion from ext3/4 to btrfs and i used it .

Below is the process to convert a non-root filesystem from ext4 to btrfs .(Please check as you may be running all your linux filesystems on a single physical partition , i had a separate /home logical volume with ext4 filer , this should not be an issue if you are using a linux distro like fedora,ubuntu,cent release in last 12 months since they by default create a lvm layout with separate mounts )

1)Install btrfs-progs if it is not present in your existing linux system,

yum install btrfs-progs
(or)
apt-get install btrfs-tools

The package manager and package name can vary based on your distro.

you can find the version of btrfs in your system using ,

mkfs.btrfs -V


2)Check the disk and filesystem lay out of your system .

You need to run fdisk command as root with the hard disk containing the linux filesystem.

eg) fdisk -l /dev/sda 
(If it is the second hard disk ,it has to be /dev/sdb )
 
 In my case it shows that i have 2 different linux LVM volume groups (fedora, cent OS ), two non-lvm linux boot partitions and some windows partitions .

Please note that when we use LVM , the ext4 /ext3 filesystems do not show up in fdisk -l output .

The lvs ,lvscan and lvdisplay commands help us view the volume groups and logical volumes in side the LVM .
 


Here cl and fedora are the volume groups created when i had installed fedora 21 and cent OS 7.

The cl volume group has 3 logical volumes home ,root and swap , similarly the fedora volume group also has 3 logical volumes.

LVSCAN gives the status of the logical volumes and the path in /dev filesystem which is a virtual system filesystem that helps us easily identify and manage devices in linux .

Lvdisplay  gives detailed information about the logical volumes .I looked up info on the /dev/fedora/home logical volume which i am gonna convert to btrfs

3) Check the integrity of the filesystems .(This is similar to when windows checks the disks during abnormal power offs )

fsck.ext4 -f /dev/fedora/home

4) Login to a non-graphical shell preferably in single user root mode and run the conversion

 btrfs-convert /dev/fedora/home 

5)Mount the converted filesystem in a separate mount point with type as btrfs

 mkdir -p /btrfs
 
 mount -t btrfs /dev/fedora/home /btrfs

Optional steps : (6 -8 )
6)During this conversion, btrfs preserves the existing ext4 metadata by creating a ext4 snapshot, we can mount it to see .

mount -t btrfs -o subvol=ext2_saved /dev/fedora/home /ext2_saved/

7) Mount the filesystem as ext4 in a separate mount point if you want to test ext4 still works

mount -t ext4 -o loop,ro /ext2_saved/image /ext4

8) In case you want to roll back/revert , unmount btrfs,ext4 snapshot and loopback image (/ext2_saved,/ext4,/btrfs) and then do it as below , (Pls note that i did not test this , I did not run revert )
 
btrfs-convert -r /dev/fedora/home


mount -t ext4 /dev/fedora/home /ext4

9)Remove the ext4 snapshots and use only the btrfs .

 btrfs subvol delete ext2_saved 

Check if any sub directories exist ( snapshots in btrfs are nothing but directories which can be mounted directly , you can go through btrfs architecture to have a taste of advanced features )

 btrfs subvol list -d /btrfs

 Btrfs recommends a full defragmentation and rebalance operation after a manual conversion .It will take only a few minutes.

 btrfs fi defrag -r /btrfs/
 btrfs balance start /btrfs/ 

10) Now remember that we have change the filer properties outside OS installation , so we need to update the /etc/fstab file to make the OS aware of these changes so that it mounts the /home filesystem with correct options when it boots .

If we dont update in fstab file ,while booting ,your linux distro will search for ext4 metadata in the logical volume , it will not detect btrfs metadata and throw error.

 
 

Modify the filesystem type for the filesystem you converted as btrfs , in my case i changed it from ext4 to btrfs for the /home logical volume.

Now we have a btrfs filesystem ready and usable with all existing data.Play with btrfs and share  your experiences.:)

 


No comments:

Post a Comment