O'Reilly logo

Ubuntu Hacks by Bill Childers, Kyle Rankin, Jonathan Oxer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #4. Customize the Ubuntu Live CD

Rip, burn, and boot to create a personalized version of the Ubuntu Live CD with your choice of software and documents.

The Ubuntu Live CD [Hack #1] contains a complete Ubuntu installation that can run directly from the CD itself, without needing to be installed onto a hard disk. It's ideal for demonstrating Linux on computers with another operating system installed because after you take the CD out and reboot the computer, it returns to the exact state it was in originally. It's a totally painless way to take Linux for a test run with no risk.

The Live CD is also extremely useful for recovering an unbootable machine: just pop in the Live CD and reboot, and you will have a fully running Linux system from which you can access the internal hard disk, copy files across the network, or do whatever else you need to do to fix the system. And you can even use a memory stick [Hack #3] to store changes made inside the Live CD environment.

The Ubuntu Live CD starts up a full desktop environment that's functionally identical to a standard Ubuntu installation, but perhaps you want a Live CD that contains specific software or documents to suit your environment. For example, you may want to create a Live CD that boots up a machine as a fully configured router and firewall with no hard disk. Or maybe you want a forensics disk preloaded with virus-scanning and network-analysis tools plus the checksums of important files.

No problem. You can create a customized version of the Ubuntu Live CD configured exactly the way you want it.

Basic Requirements

Building the disk image for the Live CD takes a huge amount of storage, so you'll need up to 5 GB of swap plus at least another 3 GB of disk space for storing the image. You'll also need tools for creating and mounting disk images.

Add extra swap

While the disk image is being compressed, two copies of it are held entirely in memory, so without a huge amount of swap, you won't be able to do the compression necessary to generate the ISO.

Don't worry if you don't already have a 5 GB swap partition. You can set up a temporary swapfile inside one of your existing partitions without having to reformat. Assuming you have at least 5 GB of free space inside /tmp (usually in your root partition), you can create the extra swapfile with dd:

$ sudo dd if=/dev/zero of=/tmp/swap bs=1M count=5000

It can take a very long time to create the swapfile, so you'll need to be patient. Once the file itself has been created, you can set up a swap filesystem on it and activate your new swap:

$ sudo mkswap /tmp/swap
$ sudo swapon /tmp/swap

You don't need to disable your existing swap first. Linux is smart enough to handle multiple swap partitions simultaneously, so your system should now have a total swap space comprising the new 5 GB swapfile plus your existing swap.

Install the tools

To mount a disk image as a loopback device and generate the ISO for your custom Live CD, you will need the cloop-utils and mkisofs packages, and to work with the squashfs compressed image on the Live CD, you'll need the squashfs-tools package:

$ sudo apt-get install cloop-utils mkisofs squashfs-tools

Standard Live CD

While it's possible to build a Live CD from scratch, it's much easier to start by modifying the standard Ubuntu Live CD. You can download the Live CD ISO disk image from http://cdimage.ubuntu.com/ or use one of the CDs available from Canonical through the ShipIt program (https://shipit.ubuntu.com/).

Prepare Original Image

Make sure your locale is set to C to prevent Unicode problems with the build process:

$ export LC_ALL=C

Mount the original Ubuntu Live CD ISO image as a loopback device:

$ mkdir ~/mnt
$ sudo mount dapper-live-i386.iso ~/mnt -o loop

This will mount the CD image inside your home directory at ~/mnt. You can use an alternative location or mount the actual Live CD in your CD-ROM drive if you prefer.

Copy everything from the mounted image into a working directory, but make sure you skip the filesystem.squashfs compressed filesystem because you'll need to extract that separately. You can use rsync to make it easy:

$ rsync --exclude=/casper/filesystem.squashfs -a ~/mnt/ ~/extracted_cd

Next, extract the compressed filesystem. The Dapper Live CD uses the squashfs read-only filesystem, unlike previous Ubuntu Live CDs, which just used cloop filesystems. To work with squashfs, you will need to load the squashfs kernel module:

$ sudo modprobe squashfs

Now you can mount it and copy it onto your local hard disk:

$ mkdir squash
$ sudo mount -o loop ~/mnt/casper/filesystem.squashfs squash
$ sudo cp -a squash extracted_fs

Be prepared to wait quite a while for this to run. Once it's finished, you will have a complete, extracted copy of the Live CD image, so you can unmount the original:

$ sudo umount ~/mnt

Set Up the Target Filesystem

Mount the proc and sys virtual filesystems into your target:

$ sudo mount -t proc proc ~/extracted_fs/proc
$ sudo mount -t sysfs sysfs ~/extracted_fs/sys

In a moment, you'll be chrooting into the CD image, so if there are files you will need on your customized CD, the easiest thing to do is mount /home into it:

$ sudo mount -o bind /home ~/extracted_fs/home

Then, once you are in the chroot, you will have full access to any files stored in your home directory.

Apply Customizations

Use chroot to enter the filesystem image:

$ sudo chroot ~/extracted_fs/ /bin/sh

Now, as far as you're concerned, you're running on a read/write installation of the Live CD. From there, you can use the usual package tools to update programs installed on the Live CD.

Delete unnecessary packages

The default Live CD is fairly full, so if you want to install extra packages, you will probably need to make some room first. If you want some ideas about which packages to remove, you can create a list of installed packages sorted by size using this command:

$ dpkg-query -W --showformat='${Installed-Size;10} ${Package}\\n' | \\
                    sort -gr | less

Be very careful, though, because some packages are essential for the system to work at all. The GNOME Live CD is based on Ubuntu, so if you're looking for inspiration for which packages you can safely remove, you can start by looking at its configuration file, available at http://cvs.gnome.org/viewcvs/livecd-project/livecd.conf?view=markup.

Once you've settled on some packages to remove, you can uninstall them using dpkg:

$ sudo dpkg -r --purge 

Install additional packages

The regular network-based package tools won't work inside the chroot, so unfortunately it's not as simple as apt-get install foo to add packages. There are a number of ways around it, such as copying in a hosts file with the addresses of repository servers pre-resolved, because you can't do DNS lookups inside the chroot.

The simplest way, though, is probably just to predownload some packages into your home directory and use dpkg to install them after entering the chroot.


One very cool trick to simplify this process is to run the Synaptic package manager on your host system, find and mark the packages you want to install on your Live CD, and then select File→"Generate package download script." You will then have a script that you can execute to fetch and save the packages locally, storing them in your home directory for access from the chroot.

Customize the home directory

When the Live CD boots, it creates the user's home directory from scratch each time, using the files in /etc/skel. If you have specific files you want to include in the home directory, you can put them in skel.

Unmount Customized Image

Now that all your changes have been applied, exit the chroot and then unmount the various filesystems:

$ exit
$ sudo umount ~/extracted_fs/home
$ sudo umount ~/extracted_fs/sys
$ sudo umount ~/extracted_fs/proc

Your customized filesystem is now ready to recompress, but first you need to generate a new manifest file that reflects the changes you have made to the list of installed packages. If you didn't actually install or remove any software, you can skip this step.

You can't perform this step using sudo (you have to really be running as root), so get a root shell with sudo -s:

$ sudo -s

Now generate the new manifest:

# chroot extracted_fs dpkg-query -W \\
                 --showformat='${Package} ${Version}\\n' \\
                 > extracted_cd/casper/filesystem.manifest

You can exit the root shell now.

Repack the Filesystem

The new squashfs filesystem that will go inside the CD needs to be created:

$ sudo mksquashfs extracted_fs extracted_cd/casper/filesystem.squash

Once again, this stage can take a really long time.

The Live CD also needs to contain a checksum file that can be used to verify the integrity of the compressed filesystem. The checksum needs to be calculated from inside the CD image:

$ cd ~/extracted_cd
$ find . -type f -print0 | xargs -0 md5sum > md5sum.txt

Build the ISO

Everything up to this point has been architecture-independent, but the final stage of building the ISO itself depends on what type of system you are running:

x86 (i386) and x86_64 (amd64)

Use the following command:

$ sudo mkisofs -r -V "Custom Ubuntu 6.04 Live CD" \\
                          -cache-inodes \\
                          -J -l -b isolinux/isolinux.bin \\
                          -c isolinux/boot.cat -no-emul-boot \\
                          -boot-load-size 4 -boot-info-table \\
                          -o custom-dapper-live-i386.iso extracted_cd

For PowerPC, it's necessary to download hfs.map:

$ wget http://people.ubuntu.com/~cjwatson/hfs.map

Then build the actual ISO:

$ sudo mkisofs -o new_image.iso -chrp-boot \\
                          -U -part -hfs -T -r -l -J -A "application_id" \\
                          -sysid PPC -V "volid" -volset 4 -volset-size 1 \\
                          -volset-seqno 1 -hfs-volid "volume_name_hfs" \\
                          -hfs-bless extracted_cd/install \\
                          -map hfs.map -no-desktop -allow-multidot extracted_ppc_cd

Use the following command:

$ sudo mkisofs -r -V "Custom Ubuntu 6.04 Live CD ia64" \\
                          -o custom-dapper-live-ia64.iso -no-emul-boot \\
                          -J -b boot/boot.img -c boot/boot.catalog extracted_cd

Burn and Boot

You now have an ISO image of your customized Live CD, so burn it to a disc [Hack #33] and give it a try.

More Information and Scripts

The process of creating a customized Live CD is quite manual and laborious, but some of the steps above can be simplified using the live_cd_tools scripts that you can find online at http://wiki.ubuntu.com/LiveCDCustomizationHowTo. Note, however, that the process for building the Dapper Live CD is a bit different from the older process used by previous releases, such as Breezy, that used a compressed loopback filesystem instead of squashfs, so make sure you don't use scripts intended for the older process.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required