Avoid Dependency Issues and Safely Cut Down Your Linux Boot Time With e4rat

If you have been performance tuning your Ubuntu rig you may have run across a nifty tool called e4rat. This tool allows you to optimize the location of files your system accesses during its boot process, and pre-load them in memory in order to speed up boot times. This idea is definitely not new, as there have been similar tools to do this in the past such as ureadahead or sreadahead. However, e4rat is a new tool which uses some recent Linux kernel functionality introduced alongside the ext4 filesystem. On the e4rat homepage, the author goes into more detail:

e4rat (“Ext4 – Reducing Access Times”) is a toolset to accelerate the boot process as well as application startups. Through physical file realloction e4rat eliminates both seek times and rotational delays. This leads to a high disk transfer rate.
Placing files on disk in a sequentially ordered way allows to efficiently read-ahead files in parallel to the program startup. The combination of sequentially reading and a high cache hit rate may reduce the boot time by a factor of three, as the example below shows.

e4rat is based on the online defragmentation ioctl EXT4_IOC_MOVE_EXT from the Ext4 filesystem, which was introduced in Linux Kernel 2.6.31. Other filesystem types and/or earlier versions of extended filesystems are not supported.

 

This tool may be for you if you:

  • Use Linux
  • Use ext4 filesystem
  • Have a non-SSD Hard Drive
  • Want to speed up your boot time!

However, there is a rather unfortunate dependency issue that might have bugged some people (including myself). Currently in Ubuntu 12.04 e4rat conflicts with ureadahead, while ureadahead is a part of the ubuntu-minimal metapackage[1]!

This caused the problem of having to uninstall ubuntu-minimal if you wanted to use the e4rat package, potentially opening the door to unexpected problems for your next dist-upgrade. On top of this, e4rat requires that you modify your init kernel option in grub.cfg, and may not clean itself up when you remove the package. This could potentially leave your system in a non-bootable state if e4rat is removed until you fix your grub config.

To address this problem and make things safer, I’ve created a PPA containing a rebuilt ubuntu-minimal package without the ureadahead dependency. It also includes a rebuilt e4rat package containing a postrm cleanup script to remove any init=e4rat-* kernel options you may have added to your /etc/default/grub file.

Here’s what will happen:

  1. Upon dist-upgrade, this PPA will be disabled in /etc/apt/sources.list.d/trinitronx-minimal-no-ureadahead-precise.list.distUpgrade
  2. The dist-upgrade will continue because this ubuntu-minimal package is still installed
  3. ubuntu-minimal will be upgraded to the latest version, *with* dependency of ureadahead
  4. apt will detect a conflict with e4rat, and remove it when installing the new ureadahead package
  5. If you have my e4rat package installed, the postrm script will take care of your /etc/default/grub entry, and re-run update-grub to update your /boot/grub/grub.cfg
  6. Your system should now reboot fine!

How to install & use e4rat

To install e4rat from my PPA (and avoid potential issues later down the line), follow these simple steps:

Install my PPA, Remove stock ubuntu-minimal and ureadahead & install re-packaged ubuntu-minimal & e4rat

    sudo apt-add-repository ppa:trinitronx/minimal-no-ureadahead
    sudo dpkg --purge ureadahead ubuntu-minimal
    sudo apt-get update
    sudo apt-get install ubuntu-minimal e4rat

Setup e4rat to Optimize Your Boot Time

After you’ve got it installed, you’ll follow the same steps helpfully outlined in this article.

1. Collect

First e4rat needs to analyze the files accessed within the first 120 seconds of your system boot process. If you wish to override the default timeout of 120 seconds, you’ll need to edit your /etc/e4rat.conf before you reboot, and uncomment the line that looks like this:

; timeout 120

And change it to however long in seconds you want. Here’s an example for 4 minutes:

timeout 240

To collect the list of files, you’ll need to reboot and tell grub to run e4rat-collect first thing (the init process is the first process started when booting). To do this, follow these steps:

  1. Reboot your system & wait for the GRUB menu to pop up. When it does, highlight your Linux kernel and hit the “e” key to edit the configuration. Search for the kernel line, which varies a bit on everyone’s system, but should look something like this:
    linux   /boot/vmlinuz-3.2.0-36-generic root=UUID=badcafe0-f00d-babe-feed-facef00d0000 ro   quiet splash
  2. Add init=/sbin/e4rat-collect to the end like so:
    linux   /boot/vmlinuz-3.2.0-36-generic root=UUID=badcafe0-f00d-babe-feed-facef00d0000 ro   quiet splash init=/sbin/e4rat-collect
  3. Hit Ctrl+X or F10 to continue booting. e4rat will collect a list of files accessed within the first 120 seconds (or whatever timeout you chose)
  4. After 2 minutes (or your timeout) is up, check that /var/lib/d4rat/startup.log exists.
    ls -l /var/lib/e4rat/startup.log 
    -rw-r--r-- 1 root root 307549 Nov 26 11:50 /var/lib/e4rat/startup.log

    If so, you can proceed. (Feel free to check what files it found too 😉

Note: After rebooting for this step, you may want to follow this helpful tip from LifeHacker:

For the purposes of collecting the right files, act as you normally would when you boot. E.g., if the first thing you do when you boot up your computer is open your browser and mail client, do that after you finish booting here. It’ll move those files too so those programs launch extra quickly. Once your two minutes are up, you can stop “acting”.

 

2. Reallocate

Next, reboot your computer again and edit your Grub line as before. This time, add the word single to the end of the kernel line like so:

linux   /boot/vmlinuz-3.2.0-36-generic root=UUID=badcafe0-f00d-babe-feed-facef00d0000 ro   quiet splash single

This should reboot you into a terminal. (if you get a blue screen, pick the option to boot as normal) You can log in using your username and password. Once there, execute this command:

sudo e4rat-realloc /var/lib/e4rat/startup.log

3. Preload

Now you just need to set e4rat-preload to run first thing on boot. This time to make the change persistent, we will edit the /etc/default/grub config file, and change the GRUB_CMDLINE_LINUX_DEFAULT boot line. First open your favorite editor (feel free to use vi instead of nano if you wish).

sudo nano /etc/default/grub

Add “init=/sbin/e4rat-preload” to the end of the variable like so:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash init=/sbin/e4rat-preload"

Save the file and close it. Then run:

sudo update-grub

Congratulations, you’re done!  On your next reboot, you’ll enjoy a much faster boot time!


DISCLAIMER: Although I have unit-tested the various pieces that will happen during the dist-upgrade process, I do not guarantee that you won’t run into any problems, so if you are in doubt, are paranoid, or are just extremely unlucky please remove e4rat & check your /etc/default/grub and /boot/grub/grub.cfg manually.

If something goes terribly wrong, you’ll probably need to remove the “init=/sbin/e4rat-preload” grub parameter from your /etc/default/grub file and run sudo update-grub manually.

If you are stuck with a kernel panic on booting, just reboot, press ‘e‘ to edit your grub command line, and remove any ‘init=/sbin/e4rat-*‘ you see there, then press F10 to boot.

[1]: https://help.ubuntu.com/community/MetaPackages