Reduce login time in Ubuntu/Linux

It has been well-documented that Ubuntu’s readahead system during boot significantly cuts down on boot time by reducing hard disk seeking via precaching commonly-used files during bootup. This feature, however, only lasts up to the GDM screen.

Recently I found out that this use of readahead can be further extended to improve the login time - the time it takes to reach the desktop after the GDM login screen.

!!! You probably shouldn’t try this unless you have more than 512MB RAM.!!!

Part 1 : Monitor login sequence

First, we need to ask readahead to monitor a login sequence and make note of all the files read during this period. The idea obviously is to begin reading these files as soon as you reach the GDM screen.

Let's store the readahead list somewhere.
Code:
mkdir ~/.readahead

Now, log out(CTRL+ALT+BACKSPACE), and press CTRL+ALT+F1 to log into a terminal.(Do not login from the GDM screen) Start the profiler now.

Code:
sudo readahead-watch -o ~/.readahead/gnome.root /

This will grind the disk for a while (up to a few minutes, so have patience and dont think you are screwed and try to abort it :) then it will return you to a command prompt. The profiler is now in the background watching all actions. Now, without logging out, go to your GDM prompt (CTRL+ALT+F7 ) and log in normally.

after you are fully logged in, press CTRL+ALT+F1 to go back to your terminal, then run:

Code:
sudo killall readahead-watch
sudo chown neo:neo ~/.readahead -R

Replace neo:neo with your user and group name. Now, we need to go prune this list a bit. Especially if you have large files on your desktop (like a 1GB AVI), the login sequence may touch it, causing readahead to think it should load the whole thing into memory! In a terminal, run:

Code:
cat ~/.readahead/gnome.root | xargs -i ls -lk {} | sort -rn -k +5 | less > readahead_list

This will output all the files readahead wants to cache, sorted by largest file first into the readahead_list file. Open the readahead_list file using your favorite editor. The 5th column (before date) is the size of the file in KB. Make sure there’s nothing glaringly large. If it’s bigger than 10,000KB or so, it’s probably not worth preloading. Remove such unwanted files from the preload list by opening ~/.readahead/gnome.root and deleting its line.

Code:
gedit ~/.readahead/gnome.root

Part 2 : Hooking this into the login sequence

Next, we need to tell Ubuntu to do this readahead on every login. There are many ways one can do this. Let's use /etc/X11/Xsession.d.

Create a file called /etc/X11/Xsession.d/00readahead.

Code:
sudo gedit /etc/X11/Xsession.d/00readahead

Put the following in the file :

for list in ~/.readahead/*; do
readahead-list $list &
done

wait

Save the file. Now you can reboot to test if this works.

Part 3 : Hook into boot sequence

Often times, we don’t log in immediately when the prompt comes up. It makes sense to cache a login as a part of bootup. Open up /etc/rc.local in your favorite text editor, and before the exit 0
statement, add:

Note : Replace neo in the first line by your username.

for list in /home/neo/.readahead/*; do
readahead-list $list
done

Now, reboot, and wait for all disk activity to stop before logging in. You should feel really nice :)

Undoing This :

Not much to do! Just delete ~./readahead directory, /etc/X11/Xsession.d/00readahead and remove the added lines from /etc/rc.local.

This improved my login time drastically. Hopefully it will improve your's as well.

1 comments:

chicky (visit their site)

What was the bootup time from pressing enter on grub/lilo and being able to open firefox ?