Using an external journal with ext4

To speed things up I’ve decided to give an external journal on a SSD a try. Here’s how.

Before I even start explaining stuff, you should seriously understand what ^ does when dealing with tune2fs. For example this thread shows the following commands:

# tune2fs -o journal_data_writeback /dev/sdxN
# tune2fs -O ^has_journal /dev/sdxN
# e2fsck -f /dev/sdxN

nobody at that thread took the time to actually tell what ^has_journal would do. ^ in tune2fs is used for clearing a filesystem flag or feature flag. So the above reads like this:

  1. Set the journaling mode to writeback
  2. Disable the journal completly
  3. Check the filesystem for errors

Actually you really don’t want to disable the journal.

-O [^]feature[,…]
Set or clear the indicated filesystem features (options) in the
filesystem. More than one filesystem feature can be cleared or
set by separating features with commas. Filesystem features
prefixed with a caret character (‚^‘) will be cleared in the
filesystem’s superblock; filesystem features without a prefix
character or prefixed with a plus character (‚+‘) will be added
to the filesystem. For a detailed description of the file sys‐
tem features, please see the man page ext4(5).


Use a journal to ensure filesystem consistency even
across unclean shutdowns. Setting the filesystem
feature is equivalent to using the -j option.

Hopefully that user noticed that this was a bad idea. Anyway, how to place the journal to an external device like a fast SSD? For that I’ve created a 1 GB partition on the SSD (the manpage states that for 4K blocks the maximum is 400 MB, even if thats the case when using a device for the journal, it doesn’t hurt (at least not me) to waste 600 MB).

Then I did create the journal on that partition:

mke2fs -O journal_dev -L root_journal /dev/sda3

Now we need to remove the journal of the filesystem we want to add the external journal (here we actually need ^). Please note that this won’t work if the filesystem is mounted. If you’re doing that for the rootfs just like I did boot with init=/bin/bash or pick some recent rescue system/image.

tune2fs -O ^has_journal /dev/md0

Now add the journal dev (using the label root_journal) to /dev/md0

tune2fs -j -J device=LABEL=root_journal /dev/md0

Another word of caution here. I’ve seen some guides which do use -o journal_data here. That’ll mean it does use data=journal when mounting the filesystem. If you manually change that in your fstab to data=ordered for example, you’ll get a read only filesystem on the next boot and notice the following message in your syslog:

Feb 26 00:35:32 nyota kernel: [    1.581324] EXT4-fs: Warning:
    mounting with data=journal disables delayed allocation and O_DIRECT support!

Remounting the filesystem with data=ordered won’t work (dmesg tells you, too. Switching the data= mode is not possible with remount). Your only fix for that will be

tune2fs -o ^journal_data /dev/md0
tune2fs -o journal_data_ordered /dev/md0

If you want to run with data=ordered. The data= options allow a good compromise between data consistency and speed:

When the filesystem is mounted with journalling
enabled, all data (not just metadata) is committed
into the journal prior to being written into the
main filesystem.

I’d say this would be extremly good for data-consistency and most likely slow even if a fast SSD is used. Update: I’ve just checked the performance and when using an SSD as external device for the journal, this mode improves performance a lot. Just check: Benchmarks using a SSD to speed up ext4 with external journal and enhanceio.

When the filesystem is mounted with journalling
enabled, all data is forced directly out to the main
file system prior to its metadata being committed to
the journal.

This is the default

When the filesystem is mounted with journalling
enabled, data may be written into the main filesys‐
tem after its metadata has been committed to the
journal. This may increase throughput, however, it
may allow old data to appear in files after a crash
and journal recovery.

may may may. I’ll stick to the default 🙂

No Comments

Post a Comment