14

I installed Ubuntu 12.10, and I don't know why, I have an ext2 partition.

  • How can I convert this ext2 file system to an ext4?
  • Will I lose all my data?
Lucio
  • 19,199

5 Answers5

5

A lot has changed in ext4 since the article linked to in the accepted answer was written. There are a lot more ext4 features that can be enabled now. To convert an ext2 filesystem to a full-featured ext4 (at the time of this writing), The command is:

tune2fs -O extents,uninit_bg,dir_index,has_journal,flex_bg,huge_file,extra_isize,dir_nlink,uninit_bg /dev/EXT2DEVICE

There really isn't a set "ext2", "ext3", or "ext4" any more. There is a whole spectrum of features. To see the features enabled for your extX filesystem, use the command:

dumpe2fs -h /dev/DEVICE

The core set of features for ext2 are: ext_attr,resize_inode,filetype,sparse_super,large_file. Generally, ext3 is considered to be ext2 plus a journal. And ext4 is basically ext3 plus any combination of the rest of the available features. In the future they may name it ext5, 6 or 7. Really it's all one filesystem family with a variable set of features.

To find out what the most current set of extWHATEVER features are at the time you read this, the best thing you can do is just create a small filesystem and look at its feature list. For ext4 it is:

dd if=/dev/zero of=ext4test bs=1M count=256
mkfs.ext4 ext4test
dumpe2fs -h ext4test

Look at the line that says "Filesystem features:" and it will tell you what features have been included on that filesystem. At the time of this writing it is:

Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize

Once you do that, you can create your own method for updating your extX to the extLATEST. Do a dumpe2fs on the filesystem you want to upgrade and compare the features on it to the features on your test ext filesystem. For any or all of the features that are missing on the old filesystem you:

tune2fs -O <comma-delimited list of missing features>

Once you are done adding the features, force a filesystem check with:

e2fsck -f /dev/DEVICE

And you have just now ensured your extX is now the extLATEST with all the available features.

Kurt Fitzner
  • 452
  • 4
  • 9
  • 1
    small typo: s/tunee2fs/tune2fs/ – josch Feb 24 '22 at 21:11
  • This answer makes little sense - many of these flags have nothing to do with ext4 per se. For example, dir_index is not required for ext4, and is an ext3 feature. It doesn't make an ext4 fs more full-featured. mke2fs defines an ext4 fs as "has_journal,extent,huge_file,flex_bg,metadata_csum,64bit,dir_nlink,extra_isize" by default, which is a completely different set of features (and uses "sparse_super,large_file,filetype,resize_inode,dir_index,ext_attr" for any ext version). – Remember Monica May 15 '24 at 18:44
5

Follow the instructions detailed in this post: Convert ext2/3 to ext4

dobey
  • 41,690
  • Will I lose all my data? How do I know if my boot partition is SEPARATE? Should I do the Step 3 or not? – Lucio Nov 02 '12 at 19:21
  • 3
    You won't lose you're data, unless you do something incorrectly. If you're worried about data loss, you should always back it up somewhere else first. You can use mount to show what partitions are mounted where. You can also run sudo parted list to print a list of partitions. – dobey Nov 02 '12 at 20:14
  • The command sudo parted list doesn't work, but sudo parted -l does. – Lucio Jan 13 '13 at 22:23
  • 1
    Note that this only enables some of the features of ext4. Some can not be enabled without reformatting. – psusi Aug 26 '13 at 02:21
  • 2
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – jrg Jun 15 '17 at 17:11
  • 1
    This has changed a lot in the 11 years since the article you posted was made. There are a lot more ext4 features than the article's command enabled. See answer below. – Kurt Fitzner Apr 10 '21 at 03:00
2

The short version for the impatient:

dev=/dev/sdXn # <-- Adjust this

then

umount $dev && tune2fs -O extents,uninit_bg,dir_index,has_journal $dev && e2fsck -v -pf $dev

If you cannot unmount because it's your system partition, then of course you will need to first boot from another system like a Live CD.

And no, you will not lose data. Unless something very weird happens, in which case you will have to reach to your backup. (You do have backups, of course. Right?)

mivk
  • 5,821
  • Some things have changed since 2015, for one, extents is now extent (without the s at the end). Also, what does e2fsck -v -pf do exactly? As far as I can see this is nothing out of the ordinary: verbose, automatic repair ("preen"), force check even when marked as clean. Is this the whole story? I doubt that... – luttztfz Jul 20 '25 at 16:36
1

First of all, it is recommended to backup all the files, create a completely new filesystem, then restore all the files. It is not recommended to simply add features, especially if the filesystem is quite old and full... That said: make a backup first!

First, run dumpe2fs on the filesystem in question. Replace /dev/DEV with your actual device, e.g. /dev/sdx5. If you've just connected the device (for example when it is an external HDD) you can see the device recognized by the kernel with the dmesg command as such: dmesg | tail, or you can mount it, then unmount it. Again, dmesg should show which device it was. Or, mount it, then run mount without a command line argument and it will print out all mounted volumes, including the one you just mounted. If it is the only ext2 or ext3 filesystem, you can use grep to get only the specific line, e.g. mount | grep ext2.

dumpe2fs -h /dev/DEV

Check the line that says "Inode size". If it is less than 256, you cannot continue. (Very, very old ext2 filesystems used a 128 byte inode size, and this simply is not enough space for modern features!)

So, according to UpgradeToExt4 in the kernel.org wiki archive a "Less Effective In-Place Upgrade" is possible. Be aware that on a full filesystem with lots of files the procedure will take a very long time! The following steps are important:

  1. Make sure you use the latest version of e2fsprogs!
  2. Unmount the filesystem, e.g. umount /dev/DEV
  3. Run a full filesystem check: e2fsck -fy /dev/DEV (will take time!)
  4. If it's an ext2 filesystem, create the journal first: tune2fs -j /dev/DEV (will take time!)
  5. Enable the ext4 filesystem features: tune2fs -O extent,huge_file,flex_bg,metadata_csum,metadata_csum_seed,64bit,dir_nlink,extra_isize,orphan_file /dev/DEV will most likely not work, so you may want to play with adding feature by feature, one at a time!
  6. Follow the instructions given by tune2fs!
  7. Run a mandatory filesytem check: e2fsck -fDC0 /dev/DEV

To see the default ext4 filesystem features currently used on your system, look them up in /etc/mke2fs.conf, under [fs_types], ext4 = { features = .... On my system, mid 2025, these default features were:

has_journal,extent,huge_file,flex_bg,metadata_csum,metadata_csum_seed,64bit,dir_nlink,extra_isize,orphan_file

Running the full tune2fs -O extent,huge_file,flex_bg,metadata_csum,metadata_csum_seed,64bit,dir_nlink,extra_isize,orphan_file /dev/DEV didn't work for me, it returned an error. (Since I added the journal as an extra step before anyway, I omitted has_journal.)

My suggestion is to add feature by feature, one at a time. E.g.

  • tune2fs -O extra_isize /dev/DEV
  • tune2fs -O extent /dev/DEV
  • ... and so on ... (run dume2fs -h /dev/DEV in-between to see what you already have...)

Not all features may work, but most importantly some feature require additional steps, e.g. after tune2fs -O 64bit /dev/DEV you will be instructed to run resize2fs -b /dev/DEV, which you must do!

Note: if you've inadvertently activated a feature, it can be disabled the same way by adding a leading ^ to it, e.g. tune2fs -O ^uninit_bg /dev/DEV will disable uninit_bg.

Also, take a closer look at the e2fsck options using man. You will see that -f forces a filesystem check, even when a filesystem is marked as clean. To rebuild all directory structures, -D is mandatory. For a more informative output, -C0 will display a progress bar. This command will take a very long time to complete on a big drive with lots of directories and files! Newer ext4 features like first uninit_bg and now metadata_csum have reduced the time needed to check an Extended Filesystem considerably, but for ext2 and ext3, it will take as long as it takes, and this means much much... much longer than one coffee break!

But that's not all. Since we've just enabled extents, we would like to use them for our files too! Only newly created files will use extents, the already existing files will use standard block groups just as before. In order to convert all existing files to use extents as well, an additional e2fsck run is required (amongst other possibilities):

  • e2fsck -E bmap2extent -fy /dev/DEV

Another possibility is to manually set the extend attribute on each file.

Before you start, please read this:

  • UpgradeToExt4 in the kernel.org wiki archive; note that this page is obsolete! For example: extentsextent and uninit_bg has been superseded by metadata_csum)
  • ext4(5) Linux manual page
  • tune2fs(8) Linux manual page
  • e2fsck(8) Linux manual page

Bear in mind that a lot has changed since extents were introduced to the ext4 filesystem. For example, the uninit_bg feature mentioned in the kernel wiki archive has been superseded by metadata_csum.

Important: if this convertion is intended for a system partition, e.g. the root partition /, I would highly recommend to use a live-bootable system to convert it. I don't even know if it would be possible otherwise. I don't think so, because if / is in use, it cannot be converted. Also, configuration files such as /etc/fstab have to be corrected (ext2 to ext4)—and quite possibly more. For example, there may be a statement covering the root filesystem on the kernel command-line, like rootfstype=ext2 or =ext3 which have to be corrected to =ext4 as well, and the procedure will be different depending on the bootloader in use (i.e. GRUB2, Dracut, rEFInd, systemd-boot, ...).

Good luck! I sincerely hope you did make a backup first!

luttztfz
  • 291
-3
  • Reformat the drive
  • Yes

As an alternative, you can enable some of the features of ext4 by running the following command, and rebooting:

sudo tune2fs -O has_journal,uninit_bg,extents /dev/sda1
psusi
  • 38,171
  • 1
    The partition may not be /dev/sda1 ... ? – Clive van Hilten Nov 02 '12 at 18:39
  • @user30275, obviously.... – psusi Nov 02 '12 at 18:56
  • 1
    to psusi - in which case you should have pointed this out to the OP – Clive van Hilten Nov 03 '12 at 09:29
  • While the posting is too short, it is also concise and, to the point, correct—except that extents is now extent. For a longer answer see https://askubuntu.com/a/1553225/1105946 – 1.) it is highly recommended to backup, reformat and restore. 2) yes, conversion ext2/ext3 → ext4 is possible, albeit not recommended. – luttztfz Jul 24 '25 at 06:32