e4rat – decreasing bootup time on HDD drives

This time I will describe how to set up e4rat in order to speed your Ubuntu’s boot time. Let’s begin with some motivation: my netbook used to boot-up in ~40 seconds. Using e4rat, it takes ~10-15 seconds. Impressive, isn’t it? Let’s see how does this trick work, and I’ll teach you how to enable it on your machine.


Prerequisites

Note: e4rat will work only on HDD drives. If you installed your system on a SSD drive, it won’t make any chance. (I will explain this later on, but you may be already uninterested in this article). In case of SSD drives, ureadahead, which is installed with Ubuntu by default, already does it’s best to improve the boot time. Physical HDD drives however, can benefit a lot from e4rat.

Note 2: You need to have your system installed on a ext4 partition in order to use e4rat (which is default in most cases). Furthermore, a kernel not older than 2.6.31 is required. No worries – Ubuntu ships with a suitable kernel since 10.04! Also, e4rat is confirmed to work great with all Ubuntu releases since 11.04 Natty Narwhal.

How does it work?

First, let’s think why does your machine take so much time to boot up. Investigating the boot process, one can learn that if you use a physical HDD drive, most time during startup is spent waiting for your drive to access data (if you wish to investigate it on your own, bootchart is the utility that will help you). This makes a lot of sense, it needs to accelerate the plates, spin them as needed, move other mechanical parts to read information… Because there is lots of physical movement related, the time needed to read a file gets longer. And there can be thousands of files that are required on boot! Things are even worse: if the files are located throughout the whole disk space, much much more seeking for files has to be done (This also explains why the drive can be very noisy on startup!). Luckily, there is no (or almost none) file fragmentation on ext4 filesystems, so at least once the file is found, reading it requires not that much mechanisms movement. But “moving to files” is enough to make your boot take a long time.

First observation that has to be done, is that every time you bootup your system almost the same files are required. This is kind of intuitive, starting Ubuntu on the same machine should require similar stuff every time. What if we could found out what these files are, and somehow move them on the drive close to each other, so that accessing them requires less there-and-back drive movement? Yeah… so this is basically what e4rat does for you.

First, we’ll let e4rat inspect your boot-up, so that it can learn which files are needed to start your system. Then, we’ll use it’s file reallocation tool to move these files in a pattern as optimal as possible. Finally, we’ll let it start before your Ubuntu boots – it will load all the required files at once to RAM (which should take much much less time, because they will be read as a large block of concatenated data), and Ubuntu will continue the boot using the data in RAM – which, because RAM is insanely fast, in case of my Ubuntu 12.04 on ASUS 1225c, takes a total of 2 seconds. Therefore, the boot process should be significantly faster.

Pleas note, that you should re-do this process every time you upgrade your Ubuntu to a new release. This is because a lot of core system files are substituted during upgrade, and many different files will be used to start your system, so for best effects they should be relocated again. This is also the reason why using e4rat on Ubuntu Development version is not advised, as in such case lots of system files are changed with updates on a daily basis… which kills the idea of remembering what has to be preloaded for bootup.

Do you like this trick? If so, let me show you how to install and configure e4rat.

How to enable it?


1. Installing e4rat. First, you need to get e4rat on your system. Unfortunately, it is not available in the Software Center. Therefore, begin by looking at e4rat downloads page. Choose the latest release, and download the .deb file suitable for your system (amd64 for 64-a bit system / i386 for 32 bits). Don’t install the file yet!

The default boot-up aid in Ubuntu is ureadahead. It does a similar job, but it never relocates files. Therefore while it is helpful in case of SSD drives, it does not improve much if a HDD is involved. Because ureadahead conflicts with e4rat, we’ll need to uninstall ureadahead first. The easiest way to do it is running this command:

sudo apt-get purge ureadahead

Note: This will warn you that you are about to remove ubuntu-minimal too. Don’t worry, this will not destroy your Ubuntu. Ubuntu-minimal is just an empty package that ensures all other required packages default for Ubuntu (like ureadahead) are present on your system. Therefore it’s safe to continue.

The next step is to install the e4rat .deb file we downloaded. You can double-click it and the Software Center will help. I prefer running:

sudo dpkg -i e4rat_file_name.deb

Once this is done, we can let it learn about your boot process!


2. Collecting startup files data. This step is about getting e4rat to know what files your system needs to boot up. This is fairly simple, and requires little work.

Start by restarting your system. Wait for the GRUB boot menu to appear (it is possible that you may need to hold the Shift key pressed in order to access this menu). When you will be presented with system selection menu, do not boot your Ubuntu. Instead, use arrow keys to highlight the entry you would normally use (most likely it’s called “Ubuntu” or “Ubuntu, with Linux version-version-version”). Press e to enter edit mode.

Do not be afraid of editing this data! Changes done here are not persistent! Therefore the worst thing that can happen while messing up here (unless you intentionally enter malicious commands) is that your Ubuntu will fail to start – but it will be back to normal next time you start your computer as usually.

The exact contents of what you will see depend on your system version. However, what we are going to edit is common to all of them. Look for a line that starts with:   linux /boot/vmlinuz-…   Use arrow keys to reach the end of this line, and add the following at it’s end:

init=/sbin/e4rat-collect

(Note: This line can be longer than your screen, and it will wrap around – if you are unsure where to add this text, go to one line below the one we would like to edit, and press left arrow key, which should take you to the end of the previous like – right where you need to add the above text).

Then press Ctrl+X, which will start the system using this new argument.

For the next 120 seconds e4rat will be looking which files are loaded during boot. Pro tip: if you open your browser (or any other application you use frequently) within these 2 minutes, e4rat will think the browser’s files are essential to boot, and will pre-load them everytime you start your system – this way your frequently used apps will also start faster!

Once your system is up, make sure everything went right, by testing if file /var/lib/e4rat/startup.log is present. I do it by running:

file /var/lib/e4rat/startup.log

If it says it’s a UTF-8 text file, everything’s fine. If it says there is no such file, you need to redo this step carefully – you must have somehow not launched e4rat-collect.


3. Relocating files.

Now we’ll need to boot up in low-level text-mode. This is because file relocation won’t work, if whole system is running. No worries – again, it sounds scarier than it really is.

To enter it, we’ll do something similar to the previous step. Restart your system, select your OS in GRUB boot menu, and press e to edit it. Look for the same line as previously (the one that starts with   linux /boot/vmlinuz-…  ), but this time we need to add some other text. Type:

single

and press Ctrl+X to boot your system. After few seconds you will see command prompt (if not, press Ctrl+Alt+F1). The following command will start file relocation, according to data e4rat collected:

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

It can take a long time. Do not worry if it does not finish within several minutes. It needs to move lots of data on your hard drive, and because it’s a slow one, it will take time. If you have little free disk space, this can be even longer. Just be patient, it will finish eventually.

Once it finishes, it will tell you some more or less interesting data about how well was it able to move your files.

It is recommended to run this command multiple times, until it clearly says that No further improvements are possible. Every time you run it, it should take less time, and really, it’s worth to wait – the better the files are located, the faster your system will boot once we finish.

When it says that there is are no further improvements possible, we are done with this step. Do not shut down your computer.


4. Enabling e4rat to preload files every time you start your system.

We are almost done. The last thing that has to be done is modifying the way your Ubuntu starts, so that it can benefit from e4rat. Still in text mode, run:

nano /etc/default/grub

A full-screen text-editor will appear, with a config file open. It’s very intuitive to use. Find a line that starts with  

GRUB_CMDLINE_LINUX_DEFAULT="..."

Leave whatever is between quotemarks, and add  init=/sbin/e4rat-preload . For example, on my system this line looked like that:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

so I changed it into:

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

I hope that’s clear. Press Ctrl+O and then Enter to save the file, then press Ctrl+X to close the editor.

To apply the changes we’ve just made, run the following command:

update-grub

Once if finishes, restart your machine using this command:

reboot

Now your Ubuntu should start normally. Well… almost. If everything went right, it should start faster. If you are lucky, it will start lighting fast (and it will be just as fast on every reboot since now)!

I hope you found this guide useful, and that your system boot time shocked you. If so, thank the people who develop e4rat!


Final note: uninstalling e4rat. If for some reason you want to revert the changes you did with installing e4rat, here are the instructions. First, revert the changes to /etc/default/grub we introduced in step 4. Run sudo update-grub to apply this change. Run sudo apt-get purge e4rat to uninstall e4rat, and sudo apt-get install ubuntu-minimal ureadahead to restore ureadahead. Note that file relocation is not revertable, but you will not suffer from it.

About these ads

11 Responses to “e4rat – decreasing bootup time on HDD drives”

  1. Ледков Дмитрий Says:

    Why not integrate this into ubuntu/ureadahead proper?

    • Rafał Cieślak Says:

      Good question. The point is that the amount of work needed to integrate it with Ubuntu by default is much greater than the value of resulting improvements. I remember that few years ago people who worked on ureadahead have considered to implement such trick, but it never happened. The main problem is that relocation would need to be done in an elegant way. As you see, the system cannot be fully running when relocation is peformed. If this was done automatically, then once in a while the system would need to ask the user to restart their machine, wait 15 minutes (or more) for it to move the files, and then proceed back to normal. It would be difficult to handle that in an elegant, polite way, without surprisingly not booting up the system for a longer time, which would be widely considered as an annoying feature. Another point is that this procedure is never guaranteed to cause a significant difference. Also, these times SDD drives are becoming more and more popular, while HDD drives are on their way to extinction. Without denying that many people still have such drives, I hope it’s clear that finding a volunteer to work on implementing such a difficult to organize feature, was indeed difficult, and eventually this idea got dropped – or at least it’s has a very low priority.

  2. Volkan Says:

    Very cool! I will try it out!

  3. chilicuil Says:

    I’ll definitely check this out, thanks for the detailed guide!

  4. Stefano Says:

    Hi, on 12.04 I had to substitute the word “single” with “text” in order to boot in low-level mode. As for improvements, on my main machine I didn’t notice any, probably because it was already fast before…I’ll try on my notebook wich has a slower drive, I’m sure it will speed up boot times noticeably.

  5. OTR Links 03/20/2013 | doug --- off the record Says:

    [...] e4rat – decreasing bootup time on HDD drives | Rafał Cieślak’s blog [...]

  6. Jack Says:

    preload side of this tool worked for me (browser starts up much faster now), but I can’t really say that it improved my boot time. Relocation didn’t take that long, by the way. Only one minute or so.

  7. George Gourgiotis (@gourgi) Says:

    it didn’t reduced my boot time in my 4 year old laptop (EEE PC)
    7seconds more than ureadahead !
    wondering if i did something wrong both times i tried ? :(

    • Rafał Cieślak Says:

      There is not much you can do wrong with these instructions. Most likely you are just unlucky – it may happen that the gain from using e4rat is little. If you have very little free disk space, start tons of custom services on boot, have a severely worn-out drive, or try pre-loading many applications by running them when collect still gathers data, you may get an unfavorable result.

  8. Alexander Riccio Says:

    (Great post!)
    Something that I found tremendously helpful was to create a grub submenu (‘e4rat’, doing so is even easier with grub-customizer) and new entries for e4rat-collect & single user mode – now I can easily reprofile/relocate any time I want!

    Also, I like a little reminder – so I put one in:

    echo ‘boot single-user mode!’
    echo ‘Loading initial ramdisk …’
    initrd /boot/initrd.img-3.8.0-24-generic
    echo ‘to begin e4rat file ops, enter: e4rat-realloc /var/lib/e4rat/startup.log’


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: