Booting Many Compressed Environments on a Laptop

by Scotty Fitzgerald

Disclaimers:  All standard disclaimers apply, especially ones about reading manuals before trying, backing up data and using a box without important info on it before trying anything in this article.  Mileage may vary.

This article is about how to use a few Linux/UNIX command lines to be able to carry several operating systems in a compressed form on a laptop with limited disk space.  Before getting into the "meat" of the article, | will explain what inspired me to figure out this way of doing things, so the reader can see when it might be useful.  After learning the techniques presented here, the laptop user will be able to do the following:

  • Carry a laptop set up for dual-booting, but have more than two complete OSes actually stored on the laptop.
  • Be able to uncompress and activate a compressed image of an OS instance in about ten minutes.
  • Be able to compress and deactivate an uncompressed OS instance in about ten minutes, or, alternately, to revert any changes made during a usage session, simply by choosing not to compress the session.

Here is why | figured out this technique: I recently had a trip to visit a relative in another state.  At the same time that I was preparing for this trip, which included my planning to take my laptop for offline computing needs as my relative does not have net access, I realized that Verizon might need me to have a Windows partition set up for a distant but upcoming fiber conversion.

I really am not fond of Windows and don't use it, but apparently Verizon has something against non-proprietary OSes.  I really resent it, as I would rather use that disk space for something useful, such as backups or mirrors, instead of wasting it with an OS I don't use.

This led me to begin toying with the idea of compressing it somehow while not in use.  I also teach at a local computer club, and wanted to boot into other distributions of Linux and FreeBSD so I could test things on multiple distributions to see if they work before giving my lectures.  All this forced me to learn a way of compressing and "swapping out" a whole operating system to "swap in" and use another.

The technique works, and I have personally field tested it.

It only requires six standard Linux/UNIX commands: dd, cfdisk, gzip, df, rm, and Vim.  Theoretically, parts of this technique can be used just for backing up whole systems, or a Linux "Live CD" such as Knoppix could be used to run the commands, and thus a Windows user could switch between different versions of Windows for whatever reason.

For the sake of brevity, I will make a few assumptions.  One is that the reader is tech savvy enough to have set up the GNU GRUB boot loader in the Master Boot Record (MBR) and the "alternate" OS (whatever that might be) in the partition /dev/hda1.  I will call this partition the "swing partition," as different OSes will swing into uncompressed activity in this disk space.

Most users will come to this article with a Windows installation in the swing partition, and a Linux one in the first extended partition, which is usually /dev/hda5.  Again, after understanding this technique, it is easily modifiable to suit different customized situations.

Let's start with a look at the GRUB bootloader in a typical dual-boot situation.

On a typical installation, GRUB resides in the computer's MBR and uses a file called /boot/grub/menu.lst, which resides in the Linux partition.  The menu.lst file is a simple text file which presents the boot menu to the user.

This menu looks like a couple of boot options for the Linux system, then a separator that says "other operating systems" and a few automatically-generated lines for your Windows installation.

From a Linux command line prompt, open up the /boot/grub/menu.lst file with your favorite text editor.  You will notice that the Windows entry has the command chainloader +1 at the bottom.

This is the first key to understanding how to complete our project.  This command tells GRUB to go into that partition and load whatever boot loader is in that partition, which in this case is Windows'.

Therefore, whatever you have in that first partition will load and run as long as it has a boot loader in the partition with it, rather than having its boot loader in the MBR.  This is why on my system I changed the menu from Windows Whatever to Chainload Partition #1.

So after making any desired changes to GRUB's menu, the first step is to copy the whole partition to a compressed file.

The beautiful thing about the command line is its power: with a pipe and two commands we can copy that whole partition, byte-for-byte, including its format (FAT or NTFS) to a data file on Linux.

This needs to be done as root, because only root can access a whole unmounted partition as a device under Linux or UNIX.  I first set up a directory called /backup/images to hold my images of the swing partition.

Here is the command to make the image:

# dd if=/dev/hda1 | gzip > /backup/images/windows.hda1.gz

Let me explain.  The first part of the command is dd, which is the disk-to-disk copy command.  When given the parameter if=/dev/hda1, it makes a byte-for-byte copy of the partition, including the format, garbage, data, and everything else, and sends that to standard output.  (If you had used a second parameter, of=somefile, you would get an uncompressed image of the disk.)

You need to pipe this to a compression program in order to save disk space, so pipe it right into gzip.  gzip makes a compressed file from standard input to standard output, so direct the output (the > directs output) to a file.  Later on, I'll touch on how to optimize the compression before running this command, but let's jump right into how to restore a partition.

As you have probably guessed, the command to restore the partition is pretty much the opposite of the imaging command.  The command is:

# gzip -cd /backup/images/windows.hdal.gz | dd of=/dev/hda1

Here, the gzip command is called with the options -cd which tells gzip to decompress and throw the result onto standard output.  That standard output is piped to the dd command with the output file set to the partition to which we want to write.

As we stand now, we have a nifty way of backing up a whole partition using only a few standard commands.  But let's see how we can extend this to put an alternate system into the swing partition.

First, swap out whatever is in the swing partition.

Then, use a disk partition editing utility to delete the partition /dev/hda1.  I like to use cfdisk, which is a text mode graphical partition editor, but anything that can delete the partition will do the job.  The important thing is not to change the size of any of the partitions, because that will cause the image files you create with dd to be either too big or too little.  So delete the partition without modifying the other partitions.

This will clear the way for you to install another system into the swing partition.

All you need to do is to put your installation CD into the CD drive and reboot.  The installer of the new OS will then see an empty slot in the partition table.  After installing and setting up the new OS, you use the same commands to copy off and compress the swing partition, and then use the restore command to bring in whichever system you want into the swing partition.

Just make sure that when the installer of the new system asks where you want to put the boot loader, you don't overwrite the MBR!

For this to work, that MBR needs to remain untouched, so place the boot loader into the partition with the system.

The astute reader will see that this whole things raises an important question, which is that the partition label will be inconsistent.

For example, let's say that the last install you did was FreeBSD.  Now the partition table entry is marked as a FreeBSD partition, but when you load a Windows partition into the swing partition, you'll have a Windows format partition with a FreeBSD type label in the partition table.

Well, the beauty of GRUB is that it ignores the partition label, so as long as the kernel it boots under can read that partition it will boot it.  In my field testing, the only oddness I had was that Windows XP would check its file system because of what it termed an "inconsistent flag."  But the check would come back as O.K. and the machine would reboot into Windows XP.

Now, a word on the compression.

Remember where I said that image that dd produces contains even the garbage on the disk?  Well, that garbage, be it old chunks of now-deleted files or other whatnot, can cause a hiccup.  The easiest and most efficient thing to compress, is a long string of the same character, so it would be helpful to write out the free spaces on the disk with something and then delete it.

How?  We once again turn to the dd command.  This command goes like this:

# dd if=/dev/zero of=/mnt/hda1/zeros.dat bs=1000000 count=10

This command produces a file of 10 megabytes of zeros.

Note that, for this command, you must mount the swing partition because you want to create a file within the partition, not to work on the whole partition.  Here I mount the partition to /mnt/hda1.  Then, the input file is /dev/zero, which is a pseudo-device in Linux which just gives unlimited zeros when accessed.

The blocksize (bs) is one megabyte, and dd is asked to do this for ten such blocks, giving a 10 megabyte file of zeros.  You need to see the free space on the swing partition and adjust this command accordingly (try df -h after mounting).

Then, run the command, follow it up with a rm /mnt/hda1/zeroes.dat to remove the file.

This is an easy way of zeroing out unused areas of the disk.  After doing this, I managed to get 9 GB partitions with Windows XP, Windows 2000, and FreeBSD compressed down to 1.4 GB files!

As we've seen, standard Linux/UNIX commands totally rock with their power!

Now you can travel with several different OSes crunched down into small files while traveling with limited disk space.  But the benefits of learning these commands does not end there; you can also back up a whole partition before applying an update or installing a program.

Then, if you don't like what that update or installation did, you can roll back to your previous state.  By keeping several of these previous states copied up to a larger drive such as a USB disk or another computer, you can go back to whatever state you want.

Also, if you set up computers for others, you can set up everything in a fixed-sized partition of, for example, 10 GB.

Now, you could actually roll out and install really quickly if the hardware is similar enough by just copying out a standardized 10 GB partition.

We've also seen how to quickly zero out unused space in a partition, which can have security applications.

Return to $2600 Index