Home page of this blog

Sunday, January 31, 2010

How to migrate ext4 partitions to ext3

Introduction

This article tries to help someone like me who want to migrate from one filesystem in linux to another filesystem. I converted my debian squeeze ext4 partition to ext3 and could not find a similar blog posted on net when I tried to convert.

I got my partition converted from ext4 to ext3 after multiple tries and found it rather simple than what I thought earlier. The article requires command line skill and a boldness to try out (and maynot work, so I disclaim any problems because of this, you are on your own to try out and may not worry if you backed up your data)

Missing Parts


Skip reading missing parts and jump to Requirements to directly start reading the core/gist of the article. But missing parts is my effort in trying to give the background information about why I wrote (writing) this article, what is required, so if you have time and patience, please read

Also migrating from ext3 to ext4 is straight forward and documented in Arch linux wiki and this article does not try to explain that. You can read that in Arch wiki, by clicking this link if you only wanted to convert from ext3 to ext4 http://wiki.archlinux.org/index.php/Ext4#Converting_ext3_Partitions_to_ext4

In the article I try to explain in steps of migration from ext4 to ext3. But you can use it for converting from any filesystem to any filesystem.

The article I am writing here is I feel licensed in GNU Free Document License. http://www.gnu.org/licenses/fdl-1.3.txt

Feel free to use it in your blog/article

I wanted to put many screenshots (not just from virtualbox, I wanted to put actual screenshots), proof read, format properly and make the article look more professional. But again, I am putting whatever I am drafting, I will try to refine it later

When I told my friend, how I converted my ext4 partition to ext3, he asked me to post it in my blog, as it may help someone though it is text only and feels vague with command line geekiness

I request readers to forgive my grammatical/technical mistakes. You can post your comments on mistakes and I will try to correct it. Again, I can only read your comments over weekend, as that is the only time I am a bit free. After all I am prone to mistakes as I am human

Now I will try to put whatever I tried as requirements and steps. These requirements are real as without them, the migration of ext4 to ext3 can never materialize.

For example, I used many live CD sessions to migrate (opensuse 11.2, Fedora 12, Ubuntu 9.10, Parted Magic 4.6, System Rescue CD, Arch Linux live USB and Linux Mint 8). All of the above listed live sessions crashed in the middle due to graphics driver or some weird memory overflow problem except Linux Mint 8 and Ubuntu 9.10. Especially, Fedora 12 was completely unusable for doing rescue work on systems with nvidia graphics card. Of the two, Linux Mint 8 and Ubuntu 9.10, I liked linux mint 8, as command line highlighted root in red color and grep selections were colorful. Linux Mint 8 felt very close to command line work, though it supported well,  modern graphics restricted drivers

I will tell you, first time I got amazed by the stability of Linux Mint 8, kudos to Linux Mint 8

Requirements

To migrate from a ext4 to ext3 is not straightforward and so requires the following.

If you are converting a partition of size 30 GB, ensure you have 31 GB of free space in another partition. Having free space in external hard disk is recommended.

When I tried migrating my home partition which was 7 GB size, I used a external hard disk formatted in ntfs (Seagate 320 GB portable hard disk) which had free size

You should require a live CD and I recommend using Linux Mint. I used Linux Mint 8 64 bit version to migrate both my home and root partitions from ext4 to ext3

If you have a NVIDIA or a ATI radeon graphics card or an intel graphics card whose drivers are restricted, then you should be having an internet connection to download the restricted drivers and install in linux mint live session. This is because, when I tried using parted magic live cd to migrate, it was way fast, but the graphics driver crashed due to too much of prints (I enabled verbose mode to see what is happening). Except linux mint 8 and Ubuntu 9.10 none of them worked with nvidia as they used by default nv driver (which crashes if lots of text prints in command line)

Linux Mint 8 is better with its command line than Ubuntu 9.10 as it shows root as red and way easy to handle. Kudos to linux mint. But you can try with Sabayon Linux live CD or Mandriva live CD as they all enable restricted graphics drivers while booting into live session. A word of caution for nvidia owners, Fedora 12 crashes with its nouveau and unfit for live CD work. opensuse 11.2 is also good, but again it crashed after stressing live cd for a while

To summarize the requirements, they are

1. Linux Mint 8 live CD (use 64 bit linux mint version if your processor is 64 bit) or Sabayon Live CD or Mandriva One Live CD. Again I recommend Linux Mint 8 for systems with nvidia graphics card, it felt like rock solid

2. External hard disk with enough space or more free space than the partition we want to migrate. External hard disk is recommended, but if there is a spare partition with enough free space in the same hard disk that is also fine

3. Internet connection for fetching/installing restricted graphics drivers in live session if you have nvidia/ati radeon graphics card

4. A UPS (Uninterruptible Power Supply), if your partition is large and if you fear, there  might be a power cut

Migration in Gist


Migration in gist is like a short summary or easy to remember points of how to convert from ext4 to ext3 (or any filesystem to any filesystem)

If you are geeky, you already knew it. You can easily migrate from one partition to another by just reading the following summary.

If you are not geeky, feel no fear, the later part of article, tries to explain each of the summarized table of contents, as much as possible by me. If you still feel, you can't migrate, take help from your geeky friend who is near and dear to you

Word of caution

1. Boot with live CD

2. Install restricted graphics drivers in live session if you have nvidia/ati graphics card

3. Mount the partition to be converted

4. Mount the backup partition, (better to have external hard disk as backup partition)

5. Become root

6. Use tar to recursively gzip the contents of the partition maintaining the permissions intact. (Do not copy the contents to external hard disk directly as it may not preserve permissions). This step creates a tarball of your home partition

7. This above tarball should be placed/copied inside your backup partition. Ensure it is written well, by calling sync, removing external hard disk and plugging it in to check if the tarball is intact. Further, tabulate tarball to check if all permissions are intact. This is simple, in Linux Mint 8, just mount the tar.gz to see if all the files are zipped properly



8. If you feel safe and take for a roller coaster ride, unmount the partition to be converted, format it to ext3 (or to whatever you wish, say XFS)


  8.1 Unmount the partition to be converted after you created a tarball of it and saved it in backup partition


  8.2 Use gparted to format the partition you intended to converted to the desired partition type

9. After formatting the partition to a different type, mount it again (in linux mint 8, just clicking partition in nautilus, mounts it). Don't forget to set the boot flag, if you are migrating your root partitions

10. Use backed up tarball and extract it in the mounted partition

11. Sync

12. Change /etc/fstab with the new uuid


Conclusion

Migration in Detail


Word of caution


Migrating a filesystem from one format to another requires tremendous patience. If you are impatient, please do not try it as it may damage your data. I tried a whole day after migrating my partition as grub was not working, when I just figured I missed boot flag after a whole day of trying various stuff with grub2


How the steps are presented here?


Each step, I tried to explain in detail. If there is any link which is quoted, please follow them for further information on that subject.

There is a fallback in each section, Fallback is in red color highlight signalling a warning. This is to ensure, if the step fails, what should be done to avoid problems or how to make the step work properly

I highlighted in green color, wherever I felt surprised when something worked, when I thought some more has to be done!


1. Boot with Live CD or Live USB


If you dont have a CD drive and only USB, you can use UNetBootin to boot any live session using USB pen drive, see http://www.webupd8.org/2009/04/4-ways-to-create-bootable-live-usb.html and http://www.hungry-hackers.com/2010/01/how-to-install-any-linux-on-a-usb.html


To boot from Linux Mint 8, you should have a live CD of Linux Mint 8, see http://www.linuxmint.com/download.php



Fallback



If you can't boot from your live CD or live USB, try a different linux live CD, Ubuntu, Fedora, Arch, openSUSE, PCLinuxOS, Sabayon, System Rescue CD, Parted Magic live CD, Tiny core, puppy, DSL ...


Check distrowatch.com to see the different distributions


If you can't boot with any of the live CD, you are probably not running Linux and this guide is not intended for you

2. Install restricted graphics drivers in live session if you have nvidia/ati graphics card

If you are already able to boot to your box using a linux live CD or a live USB, you are ready for this step. This step is required only if your graphics driver is restricted.

I have an nvidia graphics card and the free nv/nouveau drivers don't work properly (they hang the system and only hard rebooting/resetting works) if you print lots of text on command line

This step is also not required, if you had booted from Sabayon Linux or Mandriva One or PC Linux OS, as they would have enabled restricted graphics driver by default

Enable your restricted graphics driver https://help.ubuntu.com/8.04/hardware/C/restricted-manager.html

Enabling restricted driver, downloads extra packages from internet, so this step additionally requires an internet connection, preferably ethernet connection (but I did using wireless as that is enabled by default in Ubuntu 9.10/Linux Mint 8 by default)

After enabling the restricted graphics driver in Linux Mint 8 (and I love Linux Mint 8 here than Ubuntu 9.10), restart X server by giving Ctrl + Alt + Backspace (wont work by default on Ubuntu 9.10 and you need to goto System --> Preferences --> Keyboard --> Layouts --> Layout Options --> Key Sequence to Kill X server, tick the check box and then use Ctrl + Alt + Backspace, sorry Ubuntu, Linux Mint 8 betters Ubuntu 9.10 by enabling this key sequence here by default)

Fallback


If you can't use restricted graphics drivers, there is no guarantee, lots of printing in screen will not cause a hang. So note down, if you run with basic graphics drivers, not to enable verbose while doing a tar operation

3. Mount the partition to be converted

This step is straightforward in Linux Mint 8/Ubuntu 9.10. Just click the partition you want to mount in nautilus. To goto nautilus, double click Computer icon on the desktop.

The screenshot shows 4.1 GB home partition which I installed as ext4 inside virtualbox, just to demonstrate how the migration works from ext4 to ext3


The following is a spare partition which is 11 GB in size and no data inside it. I just mounted it by clicking 11 GB icon from left navigation bar of nautilus. To unmount you can see that red colored triangle next to the mounted partition. Isn't it superb?

Following screenshot shows, both partitions mounted on linux mint desktop




Fallback

Ensure you did not mount your ntfs partition or some other partition by mistake. It is simple, you know the files inside it. Open the partition and see some sample files/folders. If they are from a different partition, which you did not want to mount, no problem, press the triangular icon next to the partition you mounted to unmount it. Oh oh, Linux Mint 8/Ubuntu 9.10 rocks here by allowing us to mount/unmount with a mouse click

4. Mount the backup partition, (better to have external hard disk as backup partition)

Mount another partition which has enough free space. I recommend using an external hard disk or another hard disk. Why ? Because reading and writing on same hard disk makes your hard disk to spin more and requires more time. If you have external hard disk or another hard disk, use it as a temporary storage to help you migrate your partition file system type.

Again mounting your external hard disk is easy in Linux Mint 8/Ubuntu 9.10. It is a plug and a play. Just plug it and it gets mounted in Linux Mint 8/Ubuntu 9.10!! The mounted external hard disk pops up a new file browser window

Even if your spare partition is ntfs, feel no fear. Ubuntu 9.10 and Linux Mint 8 comes with a fuse driver for mounting your ntfs partitions writable. You don't need to do an extra step in Ubuntu 9.10/Linux Mint 8

How do you find, how much free space you require for migrating? Oh it is easy in Ubuntu 9.10/Linux Mint 8. Just run gnome-system-monitor and goto last tab (File Systems), to see how much space is occupied by each of your mounted partitions


The screenshot below shows, the 4.1 GB home filesystem as ext4 and the spare partition as ext3 along with size available and size used. Your spare partition size available should be greater than size used of the partition you intend to convert





In the above screenshot, the one highlighted in green color is ext4 (which I installed inside virtualbox using linux mint 8 to demonstrate how the ext4 to ext3 works)

Fallback

If you do not have a spare partition, or if your spare partition size is lesser than or equal to the size of data on your hard disk, do not try to  migrate or proceed further


5. Become root

Goto command line in live mode using, gnome-terminal


You can also launch terminal from Applications --> Accessories --> Terminal in Ubuntu 9.10

or just clicking Terminal after clicking Menu in the bottom right

To become root, use sudo su


In linux mint 8, after you become root, the root prompt will change into a cute red color




If you are using any other linux, use su command to become root, for this, you should have root account password

Fallback

If your live CD does not allow you to become root, it is not a right live CD you should use. Some live CD have passwords for root which you can learn from the site where you downloaded them. Use Linux Mint 8 or Ubuntu 9.10 instead as you can use sudo su




6. Use tar to recursively gzip the contents of the partition maintaining the permissions intact.

Do not copy the contents to external hard disk directly as it may not preserve permissions. This step creates a tarball of your home partition

After becoming root, we come here.

There are two partitions we already mounted in step 4, the partition which we want to convert and the backup partition.

Now we need to know the exact path of each partition before we operate.

click Computer icon on desktop, click the partition you wanted to convert and copy its path from location bar. The location bar can be activated by giving Ctrl + L inside the file browser


See the above screenshot, I copied the entire path of the home partition I mounted, by giving Ctrl + L and then right clicked my mouse and pressed copy

After copying the path, paste it after cd command in your root terminal. See the screenshot below


Now we should make a tarball of the mounted partition recursively

Before that we should know the path of the backup partition, where we wish to place the zipped partition. It is similar to copying the path like before, see below screenshot, how I copied the path of the backup partition


Whatever path you copied in the above step, paste after tar cvzfp in root terminal. After that give a name for your zipped partition, say zipped-home.tar.gz in this case (you can name however you like) followed by a space and a dot (this dot is very important, which means from current directory, the reason why we first changed directory to the partition which we want to backup

the following is a specific example of creating a backup

tar cvzfp /media/f8396516-feaa-4117-961b-e4315362a4d1/zipped-home.tar.gz .






See the below screenshot and read further to understand how it works


The above command is very powerful. The command zips all the files and folders under the current directory, see the last dot in the command do not miss that dot. The permissions of zipped files and folders will be preserved as we gave p option, the zipped will be compressed with gzip compression. The output file zipped-home.tar.gz will go inside the backup partition, whose path we copied. The extension should be tar.gz or tgz which allows us to uncompress the archive whenever we like easily


the following is the generic syntax to create a backup is, replace <backup path> with the path of the backup partition and <backup archive> with the name you like. for this to work, you should first cd to the path which you need to backup (read this step slowly from beginning to understand, how I did changed directory to the partition, which I wanted to backup)

tar cvzfp <backup path>/<backup archive>.tar.gz .


Fallback

option v should not be given when running with a basic video driver(it should be tar czfp instead of tar cvzfp if you have an nvidia card and you did not install a restricted nvidia driver in live session), as it may hang system

7. This above tarball should be placed/copied inside your backup partition

In the previous step tar command ensures, the backup archive is created directly inside your spare partition. But there is a catch, don't think it is written to disk.

Ensure it is written well, by calling sync.

sync

Remove external hard disk and plugg it in to check if the tarball is intact (or is there where you created). Further, tabulate tarball to check if all permissions are intact. This is simple, in Linux Mint 8, just mount the tar.gz to see if all the files are zipped properly




See the following screenshot to see how the archive manager tabulates archive, if you wish command line, you can use tar tvzfp .tar.gz which gives more control




Fallback


Please do not tabulate, if your RAM capacity is less or if you are running on a basic video driver on an nvidia card. Also do not double click the archive, if you have very less RAM








This is where the real migration starts. The partition which we wish to migrate to a different filesystem needs to be reformatted. To achieve this, we need to unmount it first.

  8.1 Unmount the partition to be converted after you created a tarball of it and saved it in backup partition

   The partition which we want to migrate to ext3 from ext4 needs to be unmounted.      This  can be done from command line or using nautilus file browser in Linux Mint 8. After the previous step, get outside of the directory which we archived by giving just 

cd

The above will ensure, no process is accessing the filesystem which is to be unmounted

Then goto filebrowser by double clicking computer icon on desktop and press the red triangle to unmount the ext4 partition which you archived in earlier 2 steps

See the below screenshot, I am right clicking and unmounting the partition, which can be done just by clicking the red triangle after the partition


You can also use umount command to unmount the partition (unless you are a geek you won't do this)

  8.2 Use gparted to format the partition you intended to converted to the desired partition type

run gparted from Menu or using Alt + F2 or through System --> Administration --> Partition Editor under ubuntu



Right click the ext4 partition you wanted to convert to ext3 from ext4 and select Format to --> ext3


Screenshot below shows, I formatted my ext4 to ext3 




Fallback

Do not by mistake format a different partition. Do not by mistake format a different drive. Do not forget to close gparted after doing your partitioning


9. Mount the reformatted partition


This is as simple as clicking in Linux Mint 8. Click the newly  formatted partition. 






Tuning your newly created partition


ext2/ext3/ext4 comes with an excellent tune2fs command, which can be used to tune filesystem further. Ensure partition is not mounted when you give tune2fs


You can tune your newly created partition reserved space by using the following trick. 


tune2fs -m 0 /dev/sda<N>


Change the <N>  to the correct partition number of your choice. -m 0 tells tune2fs to reserve zero percent. Suppose you want to reserve 1 percent it should be tune2fs -m 1 /dev/sda5 tells tune2fs to reserve 1 percent for sda5


The above tunes my newly created partition reserved space to zero percent. This will be helpful if the partition you created is large. Say you have a 400 GB partition, by default gparted reserves 5 percent of 400 GB which is huge and I use tune2fs to reduce the reserved space. I learned this trick from arch linux wiki


Fallback


Don't forget to set the boot flag, if you are migrating your root partitions. Goto gparted and set the boot flag if you are migrating your root partition. I struggled a whole day just forgetting to set boot flag and figured it after much trial and error




10. Use backed up tarball and extract it in the mounted partition


This is where we recreate and do the penultimate step in migrating to the new file system. This can be done step by step


1. change directory to newly reformatted partition


As earlier, you can copy the path of the newly mounted partition from file browser in linux mint 8/ubuntu 9.10


In Linux Mint 8 there is another trick, which allows opening terminal from the folder!!


See the below screenshot, I opened terminal from the newly mounted path and became root by giving sudo su





2. unzip the backed up archive


This step restores the entire file system content with permissions intact. Hurray, here the tar archiver works miracles, by allowing us migrate to the new filesystem type. To do this, we need to know the path of the archive we backed up. Open filebrowser and use Ctrl + L to copy the path of the backup partition, where we created a tarball of the partition earlier

tar xvzfp /media/f8396516-feaa-4117-961b-e4315362a4d1/zipped-home.tar.gz


The x option of tar command tells it to extract the archive


See the screenshot after extracting, which did our migration










the following is the generic syntax to extract a backup, replace <backup path> with the path of the backup partition and <backup archive> with the name you like. for this to work, you should first cd to the path where you need to restore 

tar xvzfp <backup path>/<backup archive>.tar.gz


Fallback


Please do not use v (verbose) option if you run from basic drivers on your nvidia card.









Do not think, just extracting wrote everything properly to your newly mounted drive. Force it to be written by giving

sync

Wait till sync finishes and comes to the command mode (next line)

Fallback

If sync is not working and red light is glowing always, do not panic. Try unmounting filesystem and remounting again. If that does not work, reboot your system in live mode and repeat steps 8 to 11. sync will take as much time more if you extracted a very big archive (say 100 GB and don't expect sync to just come to command line immediately, though the buffer is limited to RAM, linux will also use swap!, have patience for sync to finish)




This is the final step of the migration. You have converted your filesystem from ext4 to ext3, but there is a small problem. Its uuid has changed, its partition type has changed

change the uuid of reformatted partition in /etc/fstab


To get the uuid of newly formatted/restored partition, use nautilus file browser and take the 32 digit hexa decimal number from its path after /media/

see screenshot below, also you can see the new uuid in the titlebar of the file browser.


If you are a geek, use the following command as root

ls -l /dev/disk/by-uuid/

For this, you should know the partition number, in this example case it is sda5. see the following screenshot where the new uuid is highlighted,





Now mount your root partition, open terminal, become root, and replace the new uuid. Edit ./etc/fstab and update new uuid. I used sed command. You can use vi or gedit or nano, whichever you are familiar


After changing ext4 to ext3 in etc/fstab the migration step is complete with a sync

Fallback


If you migrated your root partition, mount your root partition, change its fstab with new uuid, new fs type. Apart from this you need to re create the grub. Follow reinstalling grub2 in grub2 community documentation in ubuntu from the following link


https://help.ubuntu.com/community/Grub2





Conclusion


If you run into disk errors while booting (in case of debian, wont allow if there is even a date/time change), do a fsck on the migrated partition. To do an fsck, the filesystem should not be mounted, otherwise it will damage the files


for doing fsck on the ext3 partition become root using root password and use


fsck /dev/sda<N>


Do not forget to sync and unmount. Here is the screenshot of mint running with migrated ext3 partition




Corrections


Please post in comments, if there is any error and I will try to correct the errors during weekends




Disclaimer

I put the disclaimer here to ensure that what you try out is at your own risk. 

THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.

9 comments:

  1. I hoped there is a way to change from ext4 to ext3 without copying the data (like there is from ext3 to ext2). Nice guide anyway.

    ReplyDelete
  2. Great stuff. This is very helpful because the current Acronis True Image does not understand ext4 but works fine with ext3.

    ReplyDelete
  3. Thanks for a great write-up. I just finished changing back to ext3 using your guide. I used Parted Magic 4.11, I am familiar with it.

    I found that you can skip the grub2 re-install. After you finish changing fstab, you can also modify the grub.cfg file to use the new UUID.

    Using your editor of choice, edit /boot/grub/grub.cfg. There is a note in it not to modify it; disregard that, in this case. I found 6 instances of the UUID I needed to change.

    Two are in section
    ### BEGIN /etc/grub.d/00_header ###
    with "--fs-uuid --set" just before the UUID

    The next four are in section
    ### BEGIN /etc/grub.d/10_linux ###
    two in each "menuentry" section.

    Note, if you have a multi-Linux environment, be careful to change only the UUIDs for the one that you converted ext4 -> ext3. Also, if you have Windows installed, it will have a short UUID - do not change that.

    Save the file, do a sync, unmount, and reboot.

    ReplyDelete
  4. Thanks a lot.
    Great tutorial.
    I just replaced the UUID of the new partition with the old one:
    sudo tune2fs -U OLD_UUID_HERE /YOUR/PARTITION

    Saved me the fstab and grub,cfg steps.

    ReplyDelete
  5. You can use fsarchiver to switch between file-systems if you have another drive to store the image. It will automatically format the file-system with the same UUID all you have to change in fstab is the filesystem type.

    fsarchiver restfs /mnt/backup/gentoo-rootfs.fsa id=0,dest=/dev/sda1,mkfs=reiserfs


    http://www.fsarchiver.org/QuickStart

    ReplyDelete
  6. Just what I need. Maybe the only clear advise on the internet. Ubuntu Forum doesn't elaborate the details -- which you do quite well.

    At this point, Ext4 is not well supported by disk imagine software inside or outside Linux. So many people may want to revert to Ext3 to make partition images.

    ReplyDelete
  7. Thanks for such appreciation. Such appreciation motivates me to blog more and more, :)

    ReplyDelete
  8. Hi, thank you for sharing this great info. Was just browsing through the net in my office and happened upon your blog. It is really very well written and quit comprehensive in explaining with a very simple language.
    about gadgets webstore

    ReplyDelete