Wibbly Stuff

How to Customize a Ubuntu Live CD

Ubuntu has a very good documentation for customizing Live CDs. But for a novice, the information given can be a little overwhelming. Especially if you just want to create an Live CD just with all system updates and your custom software. Here i will describe how to create a updated Live CD image with all the updates and custom software. You can follow the procedure to customize the Ubuntu Live CD or other distros based on Ubuntu.

First, we need a Live ISO image which we will customize. Let's assume the file name of the ISO file is "ubuntu-11.04-desktop-i386.iso".

Then we need to install the required tools,
sudo apt-get install squashfs-tools genisoimage

Now let's get started. First, remember to keep a backup of the original ISO file. Now place a copy in your home folder and open a terminal. Now execute the following commands,
# Move or copy the ISO into an empty directory
# Use the name of the ISO file in place of ubuntu-11.04-desktop-i386.iso
mkdir ~/livecdtmp
mv ubuntu-11.04-desktop-i386.iso ~/livecdtmp
cd ~/livecdtmp
# Mount the ISO image
mkdir mnt
sudo mount -o loop ubuntu-11.04-desktop-i386.iso mnt
# Extract .iso contents into dir 'extract-cd'
mkdir extract-cd
rsync --exclude=/casper/filesystem.squashfs -a mnt/ extract-cd
# Extract the SquashFS filesystem
sudo unsquashfs mnt/casper/filesystem.squashfs
sudo mv squashfs-root edit
# Copy system APT cache, in case you want to update the system, to avoid unnecessary download
sudo cp /var/cache/apt/archives/*.deb edit/var/cache/apt/archives/
# Prepare and chroot
sudo cp /etc/resolv.conf edit/etc/
sudo cp /etc/hosts edit/etc/
sudo mount --bind /dev/ edit/dev
sudo chroot edit

Now you'll get a chroot environment, means the root system now will be the file system of the Live CD. Now when inside the chroot prompt, execute the following commands,
mount -t proc none /proc
mount -t sysfs none /sys
mount -t devpts none /dev/pts
export HOME=/root
export LC_ALL=C

Now you can install or remove packages and update the system using apt-get as you would do on a real system.
# Add Repositories
# Update APT sources
sudo apt-get update
# Remove Packages
sudo apt-get purge packagenames
# Update System. Don't use dist-upgrade as it will update kernel.
sudo apt-get upgrade
# Install Packages
sudo apt-get install packagenames

Avoid updating your kernel as you'll need some advanced skills to do it. Check Ubuntu's documentation page. If you update the kernel in the usual way, I'm afraid you won't be able to boot the Live CD.

Now after you are done, check if any user exists with an UID greater than 999. Otherwise your image won't boot because no initial user is available. Execute,
awk -F: '$3 > 999' /etc/passwd

If you find any output, execute,
usermod -u 500 $hit

Where $hit is the username (the first word) you get in the output.

Now after everything is finished and you want to rebuild the ISO, execute,
# Unmount Directories
umount /proc || umount -lf /proc
umount /sys
umount /dev/pts
# Exit chroot
exit
sudo umount -lf edit/dev

Now time to build the ISO, just execute the following,
# Regenerate manifest
chmod +w extract-cd/casper/filesystem.manifest
sudo chroot edit dpkg-query -W --showformat='${Package} ${Version}\n' > extract-cd/casper/filesystem.manifest
sudo cp extract-cd/casper/filesystem.manifest extract-cd/casper/filesystem.manifest-desktop
sudo sed -i '/ubiquity/d' extract-cd/casper/filesystem.manifest-desktop
sudo sed -i '/casper/d' extract-cd/casper/filesystem.manifest-desktop
# Compress filesystem
sudo rm extract-cd/casper/filesystem.squashfs
sudo mksquashfs edit extract-cd/casper/filesystem.squashfs
# Update the filesystem.size file, which is needed by the installer. The method given in Ubuntu's page didn't work for me.
cat <<eof | tee -a /tmp/filesystem.size
$(sudo du -sx --block-size=1 edit | cut -f1)
EOF
sudo mv /tmp/filesystem.size extract-cd/casper/
# Remove old md5sum.txt and calculate new md5 sums
cd extract-cd
sudo rm md5sum.txt
find -type f -print0 | sudo xargs -0 md5sum | grep -v isolinux/boot.cat | sudo tee md5sum.txt
# Create the ISO image
sudo mkisofs -D -r -V "$IMAGE_NAME" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o ../ubuntu-11.04-desktop-i386-custom.iso .


You can also use your preferred name in place of "ubuntu-11.04-desktop-i386-custom.iso".

To cleanup all the files generated during this, execute,
cd ~
sudo rm -rf livecdtmp

Now you will find your custom ISO file in your home folder. Test it in Virtual Box and do whatever you want with it. Enjoy!