peteris.rocks

Customize Ubuntu Desktop Live CD/USB

Customize Ubuntu Desktop LiveCD ISO file and write it to a bootable USB flash drive

Last updated on

The other day at work I needed to create a prototype of a machine translation system we were developing. That prototype was going to be used by end users and they would mainly interact with the browser and also try out various command line tools.

One requirement was that it should just work, so that no installation or configuration was necessary, and it was possible the users would have little to no knowledge of Linux and how to use the command line, but still they needed to verify that the tools included in the prototype work.

I ended up creating a custom Ubuntu Live USB that the user can plug into his computer. Firefox, a terminal window and a file browser would then automatically start and the user could read the documentation in the browser, copy & paste commands in the terminal window and browse the included files in a file browser.

The tool I used was Ubuntu Customization Kit (UCK) and here is how to use it.

Ubuntu Customization Kit (UCK)

We're going to use the official Ubuntu 14.04.3 LTS (Trusty Tahr) desktop installation ISO image ubuntu-14.04.3-desktop-amd64.iso. You can download it from the official Ubuntu website. Its size is about 1 GB but you can reduce it by removing LibreOffice, Firefox and other applications.

If you later discover that you need to make additional customizations, you can also use your own ISO image that you customized previously. You can of course also use Kubuntu.

First, let's install the required tools:

sudo apt-get -y install uck syslinux

UCK has a very simple GUI wizard but it is very awkward to use, so we are going to use the command line tools to customize the image.

An ISO image is like a tar archive so we need to extract it first. What you'll find is a bunch of files for init and booting, but not the actual filesystem. That is because all the files and directories are compressed into one large file rootfs.squashfs that we also need to unpack.

UCK needs a working directory. By default, it's ~/tmp.

All UCK tools require root privileges for mounting and unmounting.

sudo uck-remaster-unpack-iso ubuntu-14.04.3-desktop-amd64.iso
sudo uck-remaster-unpack-rootfs

When that's done, the root filesystem will be located in ~/tmp/remaster-root and take up 3.1 GB of disk space. The size of the ~/tmp directory together with the unpacked ISO file will be 4.1 GB.

You can add, move and delete files from that directory.

To install packages, restart services and test if your applications are working properly, you can change the root to the image filesystem.

You can do this with the following command. It will also mount /tmp and /var/apt/cache to ~/tmp so that you don't have to worry about cleaning up.

sudo uck-remaster-chroot-rootfs

Now you can install packages with apt-get and perform all kinds of customizations.

When that's done, we need to pack it all up.

First of all, pack the filesystem into one compressed SquashFS file. This will take a few minutes and it will be faster if you have more cores available, so if you're doing this in a virtual machine, you should add more CPUs to it. If your filesystem was about 4 GB in size then the compressed image could be about 1 GB.

After that, you can create an ISO image.

sudo uck-remaster-pack-rootfs -c
sudo uck-remaster-pack-iso my.iso -h -g -d "My Live Ubuntu"

That's it. You'll find your my.iso and its md5sum in ~/tmp/remaster-new-files/ if you used a relative path.

You can test in VirtualBox.

On Windows, you can create a bootable USB with Rufus.

Useful commands

Only the Main and Restricted repositories are enabled by default for apt-get in the image. If you are also running Ubuntu on your host computer, you can fix that by copying the hosts sources.list to the image root before running uck-remaster-chroot-rootfs:

sudo cp /etc/apt/sources.list ~/tmp/remaster-root/etc/apt/sources.list

When you change the root to the image filesystem you'll keep your hosts hostname. But when you boot into the Live CD the hostname will be ubuntu. You can change the hostname like this:

echo 127.0.0.1 ubuntu > /etc/hosts
echo ubuntu > /etc/hostname
hostname ubuntu
bash

The user ubuntu that the Live CD session will use does not exist yet exist, neither does its home directory. However, /etc/skel will be used as a template (or skeleton, hence skel) for the user so you can use it temporarily as $HOME. You can also load .bashrc so that you get colors for ls and other default aliases like ll when you're in the shell.

export HOME=/root
. /etc/skel/.bashrc
cd

You can install additional packages with apt-get as you would normally:

apt-get update
apt-get -y install vim

Once booted, you will be given a choice to try or install Ubuntu. You can get rid of this window by removing the ubiquity package.

apt-get remove ubiquity -y --purge

Remove software that you don't need and that takes up lots of precious megabytes. For instance, you could remove LibreOffice, Firefox and Thunderbird. Simply uninstalling LibreOffice will free up 226 MB of disk space.

apt-get remove libreoffice-core libreoffice-common thunderbird --purge -y
apt-get remove unity-lens-shopping unity-lens-applications --purge -y
apt-get autoremove --purge -y

You can see the installed packages with dpkg:

dpkg --list

And the ones that take up the most space:

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n

ncdu is a handy tool for displaying directory disk usage:

apt-get install -y ncdu
ncdu /

Many applications and libraries have debugging and other unneeded symbols built in. You can strip all these debug symbols from all system files like this. This will free up 100 MB but may free even more if you compiled and installed something from scratch and forgot to turn off debugging symbols.

Keep in mind that if you try to compile something after you've stripped system libraries, you may get linking errors since none of the required symbols will exist.

This is very destructive so you may only want to use it on programs in /usr/bin and not libraries.

find / -type f | xargs strip -s 2> /dev/null

Change file and folder ownership to the live session user (that will be ubuntu) which does not exist yet. Files and folders in /etc/skel will belong to ubuntu no matter what ownership they have right now.

chown -R 999:999 /usr/local

You can add your own startup scripts in /etc/rc.local.

Once thing you may want to do is change the hosts file which will be regenerated at startup. By default, localhost will be 127.0.0.1 and ubuntu (the hostname) will be 127.0.1.1. That caused problems for me so I changed them like this:

$ vim /etc/rc.local
echo 127.0.0.1 ubuntu > /etc/hosts
echo 127.0.1.1 localhost >> /etc/hosts

You can install some additional fonts.

apt-get install -y fonts-crosextra-carlito fonts-crosextra-caladea

echo deb http://lv.archive.ubuntu.com/ubuntu trusty multiverse >> /etc/apt/sources.list
apt-get update
echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | debconf-set-selections
apt-get install -y ttf-mscorefonts-installer

Fix ugly fonts:

echo '<match target="font"><edit mode="assign" name="embeddedbitmap"><bool>false</bool></edit></match>' > /etc/skel/.fonts.conf

Disable keyboard shortcut overlay:

mkdir -p /etc/skel/.cache/unity
touch /etc/skel/.cache/unity/first_run.stamp

You can have applications launch at startup by copying their .desktop (e.g. find /usr | grep firefox.desktop) files or creating your own .desktop (spec here) files in /etc/skel/.config/autostart.

This will launch Firefox at startup and open peteris.rocks. If you need to modify the Firefox profile, do it on the host and then copy the .mozilla folder to /etc/skel.

mkdir -p /etc/skel/.config/autostart
cp /usr/share/applications/firefox.desktop /etc/skel/.config/autostart
sed -i 's@Exec=firefox %u@Exec=firefox https://peteris.rocks@' /etc/skel/.config/autostart/firefox.desktop

You'll notice that some directories will be created in the user's home directory such as Documents, Music and Pictures. You can remove them by editing the /etc/xdg/user-dirs.defaults file.

echo DESKTOP=Desktop > /etc/xdg/user-dirs.defaults

Vagrant

Here is a convenient Vagrantfile for you.

Make sure you let the virtual machine use all of your cores so that SquashFS packing is faster.

Vagrant.configure("2") do |config|
  # Ubuntu 14.04 x64 official cloud image
  config.vm.box = "ubuntu/trusty64"
  config.vm.box_check_update = false

  # VirtualBox
  config.vm.provider "virtualbox" do |vb|
      vb.name = "LiveCD Creator" # friendly name that shows up in Oracle VM VirtualBox Manager
      vb.memory = 1024 # memory in megabytes
      vb.cpus = 4 # cpu cores, can't be more than the host actually has!
      vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] # fixes slow dns lookups
      # vb.gui = true
  end

  # use local ubuntu mirror
  #config.vm.provision :shell, inline: "sed -i 's/archive.ubuntu.com/lv.archive.ubuntu.com/g' /etc/apt/sources.list"
  # install tools
  config.vm.provision :shell, inline: "apt-get -qq update && apt-get -qq install uck syslinux"

  # enable logging in via ssh with a password
  config.ssh.username = "vagrant"
  config.ssh.password = "vagrant"
end

Then just do

vagrant up
vagrant ssh

You can mount /vagrant into the image root as well (do this before chroot):

sudo mkdir -p ~/tmp/remaster-root/vagrant
sudo mount --bind /vagrant ~/tmp/remaster-root/vagrant

And you will be able to access /vagrant inside the image shell. So you can for instance copy a file to the vagrant directory using Windows Explorer and then just copy it to the home folder (e.g. cp /vagrant/file.txt /etc/skel/file.txt).

~/tmp/remaster-root/vagrant will be unmounted automatically when you exit the shell.

Relinux

One advantage of Relinux is that you can convert an existing Ubuntu installation to a Live CD. This is great because you can set up the UI just as you would like it and then with a simple command generate a Live CD from it.

Unfortunately, while the ISO will work in VirtualBox it froze when I tried to boot on to real hardware. So I guess this does not work reliably, but you can still try it.

Version 0.3a3 is the stable version. There's a newer version which the author rewrote in Python, but it does not work reliably.

Install by downloading the archive and extracting it to the root directory and relinux (it's a shell script) will be installed in /usr.

wget https://launchpad.net/relinux/0.3/0.3a3/+download/relinux-0.3a3.tar.gz
sudo tar xf relinux-0.3a3.tar.gz -C /

Create an ISO of your running system like this:

sudo relinux iso

Let me know if this works and if you think Relinux is better than UCK.