May 20 2010

CentOS: Migrating root (and any other) filesystem from ext3 to ext4

Category: Articles,Linux administrationFractalizeR @ 4:52 pm

With the release of CentOS 5.5 ext4 is considered stable in this distribution so I decided to migrate to it. Luckily I started from migrating fresh server with CentOS 5.5 using some instruction I found on the internet. I think I shouldn’t say, that I screwed the whole thing up 😉 After about 6 hours cursing, kicking, and crying I solved the task and figured the correct sequence of actions. The small problem with migrating root partition is that you can’t unmount it BTW.

During migration task, I found, that CentOS 5.5 rescue mode is somewhat broken a little in terms of ext4 support. It can mount ext4 partitions successfully. But it’s e2fsprogs package (tune2fs, e2fsck etc.) doesn’t see ext4 partitions and say, that superblock is corrupted on a partition once it’s converted to ext4 (at least it did it for me, may be I did something wrong here and -t ext4 switch will make it working ok?). Keep in mind, that if you screw your system up too badly, you will not be able to run tune2fs and e2fsck on it from rescue mode. But you will still able to mount it if it is not corrupted badly. In all below examples, I’m converting /dev/md2 partition. Replace with yours. If you are using default LVM names… then… happy typing! 🙂

Ok, let’s start.

Boot your system normally and login as root. Upgrade kernel if you wish (I usually use yum upgrade to upgrade all on new machines). Then upgrade/install some other packages

yum upgrade mkinitrd e2fsprogs
yum install e4fsprogs

If you upgraded kernel – reboot to ensure no problems occured and it’s active now. It’s important for mkinitrd command format I used to have kernel you will use with ext4 active at the time of command execution.

Edit your fstab

nano -w /etc/fstab

I use -w switch to prevent lines wrapping. You should use the same with your favourite editor or you may run into some trouble. Now change the partition filesystem type from ext3 to ext4 of the partition you wish to migrate. Save fstab and exit editor. Don’t reboot!

Now we need to prepare new initrd image with ext4 support. While it looks activated by default and should support ext4 filesystem in it’s default incarnation it is not quite so.

First backup your old initrd:

mv /boot/initrd-`uname -r`.img /boot/initrd-`uname -r`.img.old

Watch the backtick quotes.  Check that all ok by ls -l /boot/initrd*

Now let’s generate new initrd with ext4 support:

mkinitrd -v --with=ext4 /boot/initrd-`uname -r`.img `uname -r`

Pay attentition to –with switch and again backtick quotes. You may add -f switch to overwrite existing initrd in case you did something wrong and want to repeat.

Now reboot your system. Watch while it boots and ensure ext4 driver is active on the selected partition. I saw that after dm-raid driver initialization. But you may login and issue mount command to see what is mounted and how. If your partition is still detected as ext3, DON’T PROCEED! Proceeding with the process will make your system unbootable if your partition is still detected as ext3. Return and check last steps. You did something wrong.

Now filesystem works under ext4 driver, but it’s still ext3 with some additions. If you are converting a non-root partition, you should now unmount it now. If you are converting root partition, this is not necessary (and is impossible). Did all that? Ok, let’s make a converstion:

tune4fs -O extents,uninit_bg,dir_index,flex_bg -m 1 /dev/md2

Pay attention, that I call tune4fs, not tune2fs. They seem to be different versions for now.

I used here filesystem features I need. Feel free to add some you need from the below list:

  • extents – enable extents to improve file allocation effeciency. Should present for ext4.
  • dir_index – enable indexing filenames in directories to speed up file search in large directories. Should present for ext4.
  • flex_bg – optimized storage of filesystem metadata like bitmaps and inode table
  • extra_isize – nanosecond inode timestamps
  • uninit_bg – enable group checksums so that fsck work faster. Should present for ext4.
  • dir_nlink – enable more than 64000 files per directory
  • huge_file – enable files more than 2Tb in size

“-m 1” option decreases reservered system space for root to 1% of partition space (it is done for stablity so that you could login to system even if partition is full of some garbage. But 5% used by default is too much on modern hard drives). If you don’t need that, you can omit it.

tune4fs should tell you to run fsck on your filesystem. If it hasn’t, you did something wrong.

Ok. Now fsck. If you are converting non-root partition, just start fsck like this:

fsck -pvfDC0 /dev/md2

The system will probably ask you to remove -p switch. If so – do that. After fscking, all is done and you are good to go even without reboot. Just mount partition.

If you are converting root partition, you will need to reboot your system. You will see many errors from ext4 driver on boot. It’s fine. Ignore. CentOS will tell you that it found an unexpected problem and is starting recovery mode (you will need to provide your root password for that). It is also fine by now. Enter your root password and start fsck like this:

fsck -vDC0 /dev/md2

fsck will tell you, that it doesn’t like -p option anyway and -f is not needed since repair mode is active. Check will take some time. After it is completed, type exit into console and CentOS will reboot. After reboot all should be fine.

When e4defrag is available, use it to convert files, existed before conversion to using extents (without it, extents will be used only by files created after conversion).

Tags: ,

4 Responses to “CentOS: Migrating root (and any other) filesystem from ext3 to ext4”

  1. awong_xen says:


    Thanks for the nice tutorial. Do you have a recommended solution to do the initial install of CentOS directly to EXT4 filesystem instead of converting / from EXT3 to EXT4?

    Thank you

  2. FractalizeR says:

    Latest CentOS version will include this possibility in it’s installer. Until this is not done, I’m afraid, it will be impossible. You might try to format partition in etx4 first with gparted for example. But I am not sure installer will recognize it.

  3. Chris says:


    This is post is an amazing resource! One question; are you aware of any way to automate the fsck that runs in recovery mode? I’m trying to automate this process, and have been able to script everything except for that.


  4. cecilyen says:


    I didn’t find your post about converting etx3 to ext4 on my first search. So, I converted the root of my Centos 5 from ext3 to ext4 without mkinitrd. Now, I was stuck at kernel panic with following message “ext3-fs: dm-0: couldn’t mount because of unsupported optional features” any suggestions about how to fix it now?

Leave a Reply

You must be logged in to post a comment. Login now.