Ext2文件系统转换为Ext3的方法

Ext2文件系统在操作系统崩溃或者非法关机的情况下重启之后,要开始漫长的自检过程。自检的时间大致和硬盘的大小有关。因此,现在大部分LINUX系统在安装的时候都使用了Ext3。但如果使用了Ext2文件系统,又不想重装系统的话,可以采用下面提供的方法将Ext2转换为Ext3。

Converting Ext2 Filesystems to Ext3

Copyright (C) 2003 by Steve Litt

[ Linux Library | Troubleshooters.Com | Email Steve Litt | Copyright Notice ]

Contents:

Executive Summary

Until recently, the Ext2 filesystem has been the Linux default. Ext2 is a technological miracle. Low fragmentation, redundant enough to be reliably regenerated on error yet diskspace efficient, fast, and adaptable. But when the computer is rebooted or powered off without correctly shutting down, Ext2 filesystems are placed in an error state. When the computer comes back up, the user is confronted with some mildly confusing, and very intimidating, messages and choices. Should he let the filesystem correct itself? Warning, this can lose data!

Journalized filesystems are made to eliminate such error messages. The Ext3 filesystem is an Ext2 filesystem with a journal file and some filesystem driver additions making the filesystem journalized.

My research and those of others, as well as limited experimentation by myself, indicate that the tune2fs-j command, which is the primary command for converting from Ext2 to Ext3, is safe to run even on writeable mounted partitions. However, when possible, I run the command on unmounted or read-only mounted partitions. It might be superstitious, but I feel that is playing it safe. Nevertheless, when confronted with situations making unmounting difficult, I run the command on writeable mounted partitions.

Converting the root directory from Ext2 to Ext3 is a little more difficult. Converting back from Ext3 to Ext2 is a hairy and dangerous procedure. Instructions for both are given in this document, but think long and hard before converting the root directory.

Disclaimer

Obviously, you use this document at your own risk. I am not responsible for any damage or injury caused by your use of this document, or caused by errors and/or omissions in this document. If that’s not acceptable to you, you may not use this document. By using this document you are accepting this disclaimer.

Converting from Ext2 to Ext3

The conversion procedure is simple enough. Imagine /dev/hda10 mounted as /test – the procedure would be as follows:

  • Log in as root
  • Make sure /etc/fstab has /dev/hda10 mounted to /test as ext2, read write
  • umount /dev/hda10
    • If you can’t unmount it, then remount it read only (mount -o remount,ro /dev/hda10)
  • tune2fs -j /dev/hda10
  • Edit /etc/fstab, and for /dev/hda10, change ext2 to ext3
  • mount /dev/hda10
  • /sbin/shutdown -h now
  • mount | grep /dev/hda10
    • If it’s not shown as ext3, reboot, if still not, troubleshoot
    • Otherwise, you’re done.

A few explanations are in order. The tunefs command creates the journal file, which is kept in a special inode on the device (by default). You then must change the /etc/fstab entry to reflect it’s a journalling filesystem, and then mount it.

Converting the /usr directory

This applies only to those systems in which the /usr tree has its own partition. Converting the /usr directory presents a challenge because commands like tune2fs are located in the /usr tree, so it must be mounted. Mount it read only with the following command:

mount -o remount,ro /usr

Then run tune2fs -j, edit /etc/fstab, and then unmount and remount /usr.

Converting the / directory

First, think long and hard before deciding to convert the root directory. Ext3’s primary purpose is shorter recovery from disaster rather than data loss prevention. Converting the root directory from Ext2 to Ext3 isn’t difficult, but converting it back from Ext3 to Ext2 is a treacherous process fraught with problems. But, if you really must perform the Ext2 to Ext3 conversion on the root directory, here’s how, assuming /dev/hda2 is mounted as the root directory and /dev/hda1 is mounted as /boot:

  • Log in as root
  • Edit /etc/fstab and change ext2 to  ext3 on the line referencing the root directory.
  • tune2fs -j /dev/hda2
  • cd /boot
  • mv initrd-2.4.18-26.8.0.img initrd-2.4.18-26.8.0.img.ext2
  • mkinitrd initrd-2.4.18-26.8.0.img 2.4.18-26.8.0
  • reboot

In the preceding, you MUST perform all the steps, including the mkinitrd, before rebooting. Failing to perform all the steps before rebooting produces a “buried shovel” where if only you could boot the machine, you could run the mkinitrd command, and if only you could run the mkinitrd command, you could boot the machine.

Converting from Ext3 back to Ext2Summary

There may come a time when you want to convert back to Ext2. For directories other than the root directory or /usr, it’s pretty easy. The following once again uses the example of /dev/hda10 mounted to directory /test:

  • umount /dev/hda10
  • tune2fs -O ^has_journal /dev/hda10
  • e2fsck /dev/hda10
  • Edit /etc/fstab to change /dev/hda10 to mount type ext2
  • mount /dev/hda10

The tune2fs command removes the journal inode, and the e2fsck command completes that removal.

Back-Converting the root directory

The root directory is a challenge for a number of reasons. First, it must be mounted for the system to run, but it must be unmounted to run the e2fsck command. Also, different distros behave different ways. The mkinitrd command varies widely between distros. The preceding works on a Red Hat 8.0 machine, but other machines might require other solutions. We’ll assume that /dev/hda1 is /boot, while /dev/hda2 is the root directory (/).

  • Log in as root
  • Edit /etc/fstab to change the /dev/hda2 line from ext3 to ext2
  • reboot
  • Log in as root
  • mv initrd-2.4.18-26.8.0.img initrd-2.4.18-26.8.0.img.ext3
  • mkinitrd initrd-2.4.18-26.8.0.img 2.4.18-26.8.0
  • Place your Knoppix CD in the CD drive
  • reboot
  • Notice you are now booted to Knoppix
  • Ctrl+Alt+F2 to access a root prompt
  • umount /dev/hda1
  • umount /dev/hda2
  • tune2fs -O ^has_journal /dev/hda2
  • e2fsck /dev/hda2
  • mount -t ext3 /dev/hda2 /mnt/hda2
    • This should fail. If it does, it proves that the partition no longer has a journaling inode.
  • reboot
  • Remove the Knoppix CD and press Enter as prompted
  • Notice you are now booted to the original operating system (hopefully)
  • Log in as root
  • Execute a mount command to verify an Ext2 root partition.

Back Converting the /usr directory

Assume that /usr is mounted by /dev/hda4

  • Place your Knoppix CD in the CD drive
  • reboot
  • Notice you are now booted to Knoppix
  • Ctrl+Alt+F2 to access a root prompt
  • umount /dev/hda4
  • tune2fs -O ^has_journal /dev/hda4
  • e2fsck /dev/hda4
  • mount -t ext3 /dev/hda4 /mnt/hda4
    • This should fail. If it does, it proves that the partition no longer has a journaling inode.
  • reboot
  • Remove the Knoppix CD and press Enter as prompted
  • Notice you are now booted to the original operating system (hopefully)
  • Log in as root
  • Execute a mount command to verify an Ext2 /usr partition.

Summary

Converting from Ext2 to Ext3 is usually easy, although converting the root partition is usually undesirable. From what I hear, and from my limited testing, the tune2fs -j command is so safe that it can be performed on a partition mounted read-write. However, it’s usually easy to unmount the partition or mount it read-only, and when that’s easy, I’ve done it that way.

Changing the filesystem type of the root directory requires rebuilding the initrd image. Those procedures are explained in this document.

发表评论