Posted by: kezhong | July 2, 2009

Converting EXT2 Filesystems to EXT3/EXT4

The ext2 or second extended filesystem is a file system for the Linux kernel, and the ext3 is a journaled file system. The ext3’s main advantage over ext2 is journaling which improves reliability and eliminates the need to check the file system after an unclean shutdown. The ext4 is a journaling file system developed as the successor to ext3.

In generally, changing Unix filesystems from one type to another need to the following things: backup the old filesystem, create a new filesystem, restore the contents of old to the new filesystem. However, Linux systems have a significant advantage in that it allows in-place upgrades from the ext2 file system.

I did the tests on my Fedora 11 system. I converted filesystem from ext2 to ext3, ext3 to ext4. I also converted from ext2 to ext4 directly. The following is my steps.

Convert from ext2 filesystem to ext3, and ext3 to ext4

1. Create an ext2 filesystem
Create a regular file (filled with zeros) which will be the container for our file system.
[root@f11 ~]# dd if=/dev/zero of=fakedisk bs=1k count=10000
Find the first unused loop device.
[root@f11 ~]# losetup –f
/dev/loop0
Use the losetup command to associate file fakedisk with /dev/loop0.
[root@f11 ~]#  losetup /dev/loop0 fakedisk
Create a ext2 file system in the container.
[root@f11 ~]#  mkfs -t ext2 /dev/loop0
Make a directory to be a mount point for the file system.
[root@f11 ~]#  mkdir /mnt1
Mount the file system.
[root@f11 ~]#  mount /dev/loop0 /mnt1
Check the filesystem disk space usage.
[root@f11 ~]#  df -Thi /dev/loop0
Filesystem       Type    Inodes  IUsed   IFree   IUse% Mounted on
/dev/loop0        ext2      25K     11        25K     1%       /mnt1
Copy something into it.
[root@f11 ~]# cp /etc/* /mnt1
Check the filesystem again.
[root@f11 ~]# df -Thi /dev/loop0
Filesystem       Type    Inodes  IUsed   IFree   IUse% Mounted on
/dev/loop0        ext2      25K     141      25K     1%       /mnt1

2. Convert the ext2 filesystem to ext3
Unmount the file system.
[root@f11 ~]# umount /mnt1
Convert ext2 filesystem to ext3.
[root@f11 ~]# tune2fs –j /dev/loop0
Tune2fs 1.41.4 (27-Jan-2009)
Creating journal inode: done
This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs –c or –i to override.
Mount the file system again.
[root@f11 ~]# mount /dev/loop0 /mnt1
Check the filesystem, I found the type column had changed from ext2 to ext3.
[root@f11 ~]#  df -Thi /dev/loop0
Filesystem       Type    Inodes  IUsed   IFree   IUse% Mounted on
/dev/loop0        ext3      25K     141      25K     1%       /mnt1

3. Convert the ext3 filesystem to ext4
Convert the ext3 filesystem to ext4, verify, and remove the loop device.
[root@f11 ~]# umount /mnt1
[root@f11 ~]# tune2fs –O dir_index,uninit_bg /dev/loop0
tune2fs 1.41.4 (27-Jan-2009)
Please run e2fsck on the filesystem.
[root@f11 ~]# e2fsck –pf /dev/loop0
/dev/loop0: Group descriptor 0 checksum is invalid.  FIXED.
       … …
/dev/loop0: Group descriptor 12 checksum is invalid.  FIXED.
/dev/loop0: Backing up journal inode block information.
/dev/loop0: Moving journal from /.journal to hidden inode.
/dev/loop0: 140/25064 files (1.4% non-contiguous), 10436/100000 blocks
[root@f11 ~]# mount /dev/loop0 /mnt1
[root@f11 ~]# df -Thi /dev/loop0
Filesystem       Type    Inodes  IUsed   IFree   IUse% Mounted on
/dev/loop0        ext4      25K     140      25K     1%       /mnt1
[root@f11 ~]# umount /mnt1
[root@f11 ~]# losetup –d /dev/loop0 

Convert from ext2 filesystem to ext4 directly.
In this part, I used another file container (RAM). Regarding the file container, there is a very good lesson in SYA710 that Professor John Selmys is teaching in Seneca College.
1. Create an ext2 filesystem
[root@f11 ~]# mke2fs –t ext2 /dev/ram0
[root@f11 ~]# mkdir /mnt2
[root@f11 ~]# mount /dev/ram0 /mnt2
[root@f11 ~]# df –Thi /mnt2
Filesystem       Type    Inodes  IUsed   IFree   IUse% Mounted on
/dev/ram0        ext2      4.0K    11        4.0K    1%       /mnt2
[root@f11 ~]# cp /etc/*  /mnt2
[root@f11 ~]# df –Thi /mnt2
Filesystem       Type    Inodes  IUsed   IFree   IUse% Mounted on
/dev/ram0        ext2      4.0K    140      3.9K    4%       /mnt2

2. Convert the ext2 filesystem to ext4
[root@f11 ~]# umount /mnt2
[root@f11 ~]# tune2fs –O dir_index,uninit_bg,has_journal /dev/ram0
tune2fs 1.41.4 (27-Jan-2009)
Please run e2fsck on the filesystem.
Creating journal inode: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs –c or –i to override.
[root@f11 ~]# e2fsck –pf /dev/ram0
/dev/ram0: Group descriptor 0 checksum is invalid.  FIXED.
/dev/ram0: 140/4096 files (0.7% non-contiguous), 3291/16384 blocks
[root@f11 ~]# mount /dev/ram0 /mnt2
[root@f11 ~]# df –Thi /mnt2
Filesystem       Type    Inodes  IUsed   IFree   IUse% Mounted on
/dev/ram0        ext4      4.0K    140      3.9K    4%       /mnt2 

The above is just converting common filesystems. As for how to convert the root filesystem, as well as how to convert back from ext3 to ext2, Mr. Steve Litt has a very detailed article.

 

References
Converting Ext2 Filesystems to Ext3 by Steve Litt
How to use a Ramdisk for Linux


Responses

  1. thanks google, thanks kezhong. you save my life!

  2. Thanks, it worked like a charm. After enough power outages, ext4 is a godsend.

  3. Converting ext2 to ext4 seem to require feature “extents”, so ‘–O dir_index,uninit_bg,has_journal’ is not quite correct, but ‘–O extents,dir_index,uninit_bg,has_journal’ is.


Leave a comment

Categories