1. Overview
When working with ext4 partitions in Linux, we may sometimes find the need to format a partition quickly. By default, the mkfs.ext4 command in Linux performs a full format, which can be time-consuming for large partitions. However, there are ways to perform a quick format on an ext4 partition to save time. In this tutorial, we’ll explore different methods to quickly format an ext4 partition. First, we’ll learn the workings of the lazy_itable_init option offered by the mke2fs utility. Then, we’ll use the tune2fs utility to quickly convert existing ext4 and ext3 partitions. Finally, we’ll cover fallocate, which is a tool to quickly preallocate and deallocate disk space.
2. mke2fs
The mke2fs command is used to create an ext2, ext3, or ext4 filesystem on a storage device. It formats the device, initializes the necessary data structures, and prepares it for storing files and directories. By default, it’s installed on most major Linux distributions as part of the e2fsprogs package. Nonetheless, we can install it from the official package repositories using a package manager like apt:
$ sudo apt install e2fsprogs -y
2.1. The lazy_itable_init Option
mke2fs provides the -E lazy_itable_init option that delays the initialization of the inode tables during the formatting process. By delaying the initialization, the formatting process can be faster because it skips some of the time-consuming steps. As an example, let’s format the /dev/vda1 partition on the system:
$ sudo mke2fs -t ext4 -E lazy_itable_init /dev/vda1
Discarding device blocks: done
Creating filesystem with 4193792 4k blocks and 1048576 inodes
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
In addition, this solution only speeds up the formatting process. Later on, it can have an impact on the filesystem’s performance in the long run, as the inode tables would need to be initialized at some point. Alternatively, we can also use the mkfs.ext4 utility, which is essentially a symlink to mke2fs:
$ sudo mkfs.ext4 -E lazy_itable_init /dev/vda1
2.2. Alternate Approach
Alternatively, we can speed up the formatting process without using the lazy_itable_init option:
$ sudo mkfs.ext4 /dev/vda1 -O sparse_super,large_file -m 0 -T largefile4
Let’s break this down:
- -O specifies the features to be enabled for the filesystem
- sparse_super allows ext4 to store only one real copy of the superblock, which saves disk space and improves the performance of certain operations
- large_file enables support for large files
- -m 0 sets the reserved block percentage to 0, which means that the entire filesystem would be available for regular users, and no blocks would be reserved for privileged processes
- -T largefile4 optimizes the filesystem for large files by using a larger block size
The -m 0 option tells mkfs not to reserve the 5% space for root. The 5% space is a lot for larger partitions. For instance, it’s around 100GB for a 2TB partition. Therefore, we can use this option for data partitions without issues.
2.3. Speed Comparison
As an experiment, we’ll compare the timing of this command with the one that uses the -E lazy_itable_init option. Let’s time the latter first:
$ time sudo mkfs.ext4 -E lazy_itable_init /dev/vda1
...
real 0m0.117s
user 0m0.004s
sys 0m0.012s
Now, let’s time the second command:
$ time sudo mkfs.ext4 /dev/vda1 -O sparse_super,large_file -m 0 -T largefile4
...
real 0m0.073s
user 0m0.003s
sys 0m0.011s
Notably, the second command is slightly faster than the first one for a 16 GB partition. In addition, it’s faster for larger partitions greater than 2 TB as well.
3. Alternative: Converting an Existing ext2 or ext3 Partition with tunefs
The tune2fs utility lets us modify various parameters and settings of an ext2, ext3, or ext4 filesystem. It allows us to change features, adjust behaviors, and configure specific attributes of the filesystem. Therefore, it’s useful if we have an existing ext2 or ext3 partition and we need to quickly convert it to ext4 without formatting it. As an example, we’ll now convert an existing ext3 partition to ext4 using tune2fs. First, let’s check out the type of the /dev/vda1 partition:
$ lsblk -f /dev/vda1
...
vda1 ext3 1.0 7a664991-92ed-4823-a4f0-36dad54fa666
Now, let’s convert it to ext4:
$ sudo tune2fs -O extents,uninit_bg,dir_index /dev/vda1
Let’s break this down:
- -O specifies the filesystem features to be modified
- extents feature enables extents, which is a feature in ext4 that improves file allocation efficiency
- uninit_bg allows the use of uninitialized block groups that can improve filesystem creation speed
- dir_index enables the use of directory indexing that improves the performance of large directories
- /dev/vda1 is the partition we’re modifying
Moreover, it also retains our data in the partition. However, as an optional step, we can check and repair the filesystem to make sure everything is good so far:
$ sudo e2fsck -fDC0 /dev/vda1
e2fsck 1.47.0 (5-Feb-2023)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 3A: Optimizing directories
Pass 4: Checking reference counts
Pass 5: Checking group summary information
...
Mind that we need to backup our data because the conversion process can result in irreversible data loss.
4. Alternative: Using fallocate to Allocate Disk Space
fallocate allows us to preallocate space for a file, making it immediately available for use, or deallocate space, marking it as free and available for reuse by the filesystem. In some cases, we may already have a formatted ext4 partition but need to quickly clear the existing data that are filled with zeroes without going through a full format. This means that the space previously occupied by the file is now available for reuse by the filesystem. However, the file itself still exists on the filesystem with its metadata intact. By default, fallocate is available on most Linux distributions as part of the util-linux package. So, let’s go ahead and deallocate the space used by the /dev/vda**1 partition:
$ sudo fallocate -x -l <length> /dev/vda1
Let’s break this down:
- -x or –dig-holes is used to deallocate space by punching holes in the filesystem
- -l specifies the length of the hole to be punched
Here, we can replace
$ sudo fallocate -x -l $(sudo blockdev --getsize64 /dev/vda1) /dev/vda1
5. Conclusion
In this article, we learned how to convert and quickly format a partition as ext4. First, we covered -E lazy_itable_init options available in mke2fs, which allows faster format by delaying the initialization of inode tables. Additionally, we covered how to use the tunefs utility to quickly convert existing ext2 and ext3 partitions to ext4. Lastly, we used fallocate to deallocate space by punching holes in the filesystem, allowing for quickly removing file data.