Tuesday, August 28, 2007

Making Ubuntu boot in 19 seconds

I started to take it upon myself to speed up the boot process of my secondary workstation, but I decided I needed a way to quantitatively measure the boot process. I wanted to document my results in a way that could be reproduced, and I wanted to be very methodical in the process.

My secondary workstation is a Dell GX270 with a 3.0GHz P4 processor, 1GB of RAM and a 40GB Seagate ST340014A hard drive. I blew away the existing install, reinstalled Feisty with the default settings, specified my static IP, and updated via apt-get. The kernel is 2.6.20-16-generic #2 SMP i686.
To measure the boot times, I turned Automatic Login on for my user account. Then I made a file called imup.sh in my home directory, with the following contents, and put it in my session startup:
nc mymaindesktop 89 -z

This meant that when GNOME was up and running, my secondary desktop would attempt a TCP connection to my primary desktop. This I can quantify. First, I got a root shell by executing 'sudo -s,' and then I typed the following command into my console:
time nc -l -p 89 -vvn

I don't execute it just yet. I hit the power button on my test machine and the Enter key on my primary desktop simultaneously. When the test machine hits the desktop, I'm presented with output similar to the following on my main desktop:

listening on [any] 89 ... connect to [] from (UNKNOWN) [] 30958 sent 0, rcvd 0 real 0m46.919s
user 0m0.000s

sys 0m0.004s

So at this point, I know it took about 47 seconds to boot all the way to the GNOME desktop from a cold start. I repeat all my tests 3 times to make sure that the number is accurate. In the descriptions of each boot, save the Base Install, I'll try to link to a page detailing how to replicate my process.

Some Results
Base Install (Fresh install, updated via apt-get):
47 seconds
Prelinking installed and run: 49 seconds
Disabling hplip: 47 seconds
Disabling various services: 49 seconds
(I also disabled brltty in the above)
Running prelink again after the above: 47 seconds

At this point, I installed bootchart via apt-get ( sudo apt-get install bootchart) and rebooted.
Then I began to examine just what was using up the most time during my boot process.
It looks like under S40Networking, dhclient is being run and then executing a sleep. Funny because I have specified a STATIC IP. So I went into /etc/network , and edited out all of the extra interfaces from /etc/network/interfaces. Only eth0 and lo remained. I also went into all of the directories inside /etc/network and moved all of those files to backup folders elsewhere. I don't use avahi and I don't use WPA, so those things shouldn't run, right? So I removed those and went along my way.

But wait, bootchart tells you how long it took to boot! Which, after removing those files, appears to have decreased from 30 seconds to 24 seconds. Now we're getting somewhere, and this article is going to take a different turn.

Next, remember how I removed the avahi bits from /etc/network/? Avahi is still running. I don't care to discover what's on my network, and I want it to go. I just have to edit /etc/defaults/avahi and set that =1 to =0. If all goes well, it won't run next boot.

25 seconds.
Oh well.

More Results
Next, I enabled concurrent init shell scripts.: 24 seconds.
I disabled usplash: 24 seconds
I reprofiled the bootup and restarted: 20 seconds
I went into /etc/readahead/ and edited every line containing hlip, blue, or avahi from the files boot and desktop: 20 seconds
I disabled readahead altogether: 25 seconds
Turned readahead back on, reprofiled: 20 seconds

Trying to optimize readahead
Next, I sorted through the /etc/readahead/boot file and sorted out the list by time last accessed.
Which did nothing, because readahead had accessed them all in the order that they were in /etc/readahead/boot.
So I disabled readahead, rebooted, and went back through and sorted the list again. Some of the files had not been accessed in the past 24hrs, or in the last boot, so I omitted those from the list.
This made the boot time 22 seconds. Oops. Maybe I shouldn't have omitted those files that haven't been accessed in the last 24hrs.
Out of the blue, I removed ipv6 from the modules list: 21 seconds
So I sorted the original list again, and didn't prune anything from the list: 21 seconds
I put the original list back: 20 seconds
I disabled the kinit resume function: 20 seconds
I blacklisted xpad, the Xbox controller module: 20 seconds
Next, I went into the BIOS and disabled all of the stuff I didn't need, like the floppy controller, serial ports, parallel port, etc: 20 seconds
I disabled the wacom lines in xorg.conf, and turned on boot logging: 20 seconds
I noticed that something to do with Wacom was still being loaded, so I went through all of the /etc/rcX.d directories and disabled them: still at 20 seconds
I don't have a printer connected to this computer, nor will I ever, so I disabled cups: 19 seconds
I found that /etc/inittab does nothing now. The extra ttys are spawned in /etc/event.d, so I removed tty3, 4, 5, and 6, then rebooted: 19 seconds
Next, since I have a static IP and a completely static connection, I removed the packages for dhcdbd, network-manager, and network-manager-gnome, then rebooted: 19 seconds.
I generated another initramfs: 19 seconds.

I could go further, since I have no need for sound, or anacron, but at this point I'm severely I/O limited, according to the bootchart. Maybe 90% of the boot process is spent in I/O wait. I doubt disabling those will even shave a half second off of my boot times.

Just for kicks, I removed bootchart and tried out my old method three times: It takes 40 seconds to get to a desktop from the moment I push the power button. Not too shabby for older hardware.

All in all, it looks like the best speedups to get are from:
1. Reprofiling with readahead
2. Setting a static IP for your network (if you're a desktop user like me)

Everything else was just a waste of time. At a later date I may reinstall, then simply try the reprofiling and disabling DHCP, and see how long that takes. If I had a dual-core CPU, I may have seen a benefit from concurrency. At this point it seems like the easiest way to speed up boot even more would be to use a faster/dual-core CPU or get faster disks. Maybe I'll bring in my Seagate X15 from home and give that a shot.

Friday, August 24, 2007

Getting Evolution mail into Gmail

Recently, I got into the Gmail kick and decided to get all of my POP e-mail accounts pointing to Gmail. The search functionality just really made it worthwhile.

However, I was posed with a problem: I had almost 2 years worth of e-mail in Evolution. How do I get my Evolution Inbox to the land of Gmail? (Oddly enough, a lot of this e-mail went from Outlook 2003 -> Thunderbird for Windows -> Evolution)

Fortunately, Evolution makes this easy for sysadmin types. Evolution will export e-mail in mbox format.

In Evolution, you'll want to dump all of your e-mail back into one folder, if you had filters that sorted it to hell and back. Then, you'll want to hit Select All, and then File->Save Message. Name it something meaningful, and add .mbox on the end if you wish. I compressed the little booger into a tarball and then gzipped it.

Now on my e-mail server, which runs Qpopper and Exim, I made a new e-mail account. I uploaded the mbox archive to my home directory on my mail server, extracted it, and shoved it over to the /var/spool/mail directory in place of the original spool file for the new account I made.

Then I just pointed Gmail at the new account. Presto! I have all of my old e-mail in Gmail.

I really can't believe I didn't think of it before.

I'm considering offering a free service where you can upload an mbox file into a webapp, and then a temporary POP account from that mbox will be made just so you can pull the e-mail in to Gmail or etc.

Leave me your thoughts in the comments!

Wednesday, August 22, 2007

The 10 most useful applications in Ubuntu

Many of these applications are the reason I switched to Ubuntu at home, and I have made them a staple of my technology lifestyle. If you're someone who has just gotten started tinkering with Ubuntu (or GNU/Linux in general), you should give these a shot.

10. vim / gvim
Vim is the editor of choice for everything I do (well, except excessive copying and pasting from Firefox.) The capabilities of vim will far exceed anything I'll ever do with it. Periodically, though, I have to do something ridiculous to a file, and vim saves the day. Want to run a macro that will trim the first three characters off of every line in a file, add a semicolon at the end of every line, and replace every instance of FOO with BAR? Vim can do it. To install, simply the following a command prompt:

sudo apt-get install vim

Now you can execute vi (or vim) from the command prompt and get to editing away. Here's a cheatsheet for how to get around. There are hundreds of pages about the vim editor, search the net to find more!
Even if you run Windows, you can still enjoy some of the vim goodness. Just to go www.vim.org and grab the Win32 Installer. You won't get the command-line vim, but you will get the always-handy gVim.

9. The GNU find command
The GNU find program is part of the Ubuntu default installation, so there is no need to install it.
A lot of people rely on the locate command to find files, and that does serve its purpose very well. Where the GNU find command is useful is finding files of a specific nature, or acting upon those files in particular. For instance, if you want to get the MD5 checksum of every file in a directory, just CD to that directory and execute the following at the command prompt:

find ./ -type f -exec md5sum {} \;

You can even use the GNU find command to delete files that haven't been updated in a given timeframe (for instance, log files older than 90 days.) First, you should open a terminal and cd to the directory with the log files, usually /var/log. Then, running the following will print those files to the command line:

find ./ -type f -mtime +90

Adding -delete to the end of that line, and executing it again, will delete all files older than 90 days in that folder and every folder underneath it, as long as your user account has file permissions to do so!

8. Midnight Commander
If you remember Doubletree from the DOS days, then Midnight Commander will probably be your new best friend. If you haven't enabled the additional apt-get repositories yet, please visit the Ubuntu Guide and complete that before taking the next step.
To install Midnight Commander, type the following at a terminal prompt:

sudo apt-get install mc

Midnight Commander is great for traversing the filesystem quickly, and even moreso when you're doing the same remotely via SSH. You can edit files from within MC, and even browse a remote filesystem via an SSH connection with it (and without installing SSHFS.)
To run, simply open a terminal window and execute 'mc'

7. Mplayer
I know you can get mplayer for Windows, but it's just not the same. Again, you'll want to visit the Ubuntu Guide to enable the additional apt-get repositories if you haven't already. To install mplayer, open a terminal window and execute the following:

sudo apt-get install mplayer

Mplayer has a special place in my heart as the media player that would play just about everything, even in the days when other players wouldn't. Other media players have made some headway, but mplayer still has tricks up its sleeves. It will even play VCDs directly from a .bin file!

6. Rsync
Looking back, this was probably one of the main reasons I went to Ubuntu at home. There is just not a suitable Rsync application for Windows. Period. I have a directory that follows me around wherever I go, it resides in my home directory as 'sync'. When I log in at work or on my laptop, it is copied down from my main desktop at home. When I log out, the directory is synchronized back with my main desktop at home. Here is a great article from Linux.com detailing how to use rsync in some practical ways. To install rsync, pull up that terminal prompt and type:

sudo apt-get install rsync

5. Alltray
Alltray allows you to take any window and minimize it to an icon. If you like the way that GAIM/Pidgin stashes away, and want other windows to do the same, try out Alltray!

sudo apt-get install alltray

After you're done installing, go to Applications->Accessories->Alltray, then just click on the window you want to turn into an icon. To bring the window back, just click on the icon.

4. TightVNC Server
Oh, I know what you're thinking: "TightVNC is available for Windows, LC! Why is this worthy of mention?!" The Windows TightVNC implementation allows you to remote in to your existing desktop. The Linux implementation, however, allows you to remote in to a discrete desktop environment. You can even run multiple discrete VNC servers on the same Ubuntu box. I use this to give an Xorg environment to items I want to leave running all the time. Azureus, among other applications, stays running in the VNC session and I can check up on my seeding efforts from afar. To install, simply open a terminal window and execute the following:

sudo apt-get install tightvncserver

To run a VNC server, simply execute 'vncserver' from any terminal. You can set a password for the VNC server by executing 'vncpasswd' from a terminal. You can automate the startup of vncserver, but I usually just launch it when I need it and then tromp around from there.

3. Fuse SSHFS
Got SSH access to another computer, but want to view the files on that computer as if they were on your own filesystem? SSHFS has you covered. The Ubuntu Blog has a better HOWTO on this than I could ever write, so go pay them a visit. I find SSHFS very handy when I want to listen to my home music collection from another computer on another network, but don't want to carry multiple copies of everything around with me. Editing PHP files on my remote webserver got a lot easier, too.

2. SABnzbd
If you download anything at all from Newsgroups, you'll appreciate some of the things that SABnzbd offers. SABnzbd will download, PAR check, unRAR and clean up any NZB that you throw at it. It offers a web interface and will monitor a directory for automatic pickup. I installed SABnzbd manually, but you can always check out this thread on the Ubuntu Forums to pick up a script that will install everything for you. There are no packages in apt-get repositories to install SABnzbd, so everything must be done the less convenient way. The results are well worth the effort, though.

1. Yakuake
If you've ever played any of the Quake series games (or games derived from iD software game engines,) you probably remember hitting the tilde (~) key to bring down a console where you could mess with game settings. Yakuake stashes a terminal window in the same manner, tied to the F12 key. You can reassign the key to any keystroke or combination that you like. I have mine set to CTRL + * at work (since they're right next to each other on a Northgate keyboard), and Windows Key + Open Dialog Menu Key on my personal machines.
To install, execute the following at a terminal prompt:

sudo apt-get install yakuake

To run, type 'yakuake &' at a terminal, and you should see a message that Yakuake started! Now you have a stowable terminal at your fingertips. Just press F12 to make it drop down.

Tuesday, August 21, 2007

Taking over the MBEQWA project

I really like MBEQWA and I've used it successfully in my small e-mail systems in the past. MBEQWA basically gives you a Web console where you can add/remove/modify e-mail users, and even hosted e-mail domains. I had to modify it to work with my database layout, but it has worked wonderfully for 2+ years.

Unfortunately, it's fallen out of development, and changes in PHP require modification to the package to make it function today.

I've e-mailed the author asking if I could help contribute. We'll see if he's interested.

Wish me luck!

Greylisting in Exim

My personal e-mail server hosts a handful of domains. Being listed as the technical contact on these, I tend to get a lot of spambots swarming my mail server.

Recently, I stumbled across this site at theinternetco detailing how to set up greylisting in an exim/mysql setup.

I took the plunge.

From Friday 1:28PM Central to Saturday 7:30AM Central

840 connection attempts greylisted
585 individual ip/domain pairs tried
505 unique ip connections

All of this is after the following two iptables rules:
/sbin/iptables -I INPUT -p tcp --dport 25 -i eth0 -m state --state NEW -m recent --set
/sbin/iptables -I INPUT -p tcp --dport 25 -i eth0 -m state --state NEW -m recent --update --seconds 60 --hitcount 1 -j DROP

(Basically, any given IP can only make one new connection per minute. Had a problem where spam bots would get greylisted and then would try to connect about 100 times in a row)
Nothing really needs to connect to my mail server more than once a minute, and any large transaction of e-mail would be ETRN'ed anyway.

I upgraded the Amavisd-new package, which also broke some of my config files. Basically, Amavis now comes out of the box with SpamAssassin and ClamD support disabled. I didn't realize this until Monday when I noticed the logs didn't have startup entries from Amavis for these two. Doh!

But all told, it looks like my spam count has dropped noticeably. I've been putting some manual entries in for google's mail servers, but it's a little harder to catch them when you have 3000 entries in the greylist tables, like I do right now. Maybe I'll write a cron job to prune old entries later on. Greylist entries are only good for a week anyway.

I'm toying with the idea of automatically blacklisting certain senders based on a set of circumstances. A few years ago, I used an alias of my main mail account to make 2-3 posts to a mailing list. Even today I see spam mail come in for that mailbox. I'm looking at how to check for mail intended for that address and just blacklist the ip that sent it (again, via MySQL,) but I'm not sure if it would be worth the effort at this time.
If I could get a good system in place to blacklist country codes, I'm sure that would cut down on the spam significantly. I have a good blacklist file from work that could probably do the trick.

I suppose I could write a script to manually walk through my logs and spit out a list of people who attempted to e-mail the non-existant address. I could automate that as well and have it insert into a MySQL table. I might as well blacklist microsoft.com while I'm at it.