This post contains notes on this topic in the form of a concise guide.


The first step – create a virtual machine

Create a new virtual machine using VirtualBox and install minimal Ubuntu OS.

The second step – install required packages

Install squashfs-tools package so you can create squashfs image later:

$ sudo apt-get install squashfs-tools

Install the live-boot package with dependencies (live-boot-initramfs-tools), so you can use the boot to ram:

$ sudo apt-get install live-boot

The third step – prepare image contents

Create a new directory and copy root file system contents:

$ sudo mkdir /squashfs

You need to exclude the contents of the /live directory and the one created a moment ago.

You can also exclude contents of directories like /boot/*, /tmp/*, …

$ sudo rsync -a --delete --one-file-system / /squashfs \
    --exclude=/live --exclude=/squashfs

It’s a good idea to remove the root file system from the /squashfs/etc/fstab file.

The fourth step – create squashfs image

Create /live directory (squashfs image will be stored here):

$ sudo mkdir /live

Create squashfs image:

$ sudo mksquashfs /squashfs /live/livefs.squashfs -noappend -always-use-fragments

The fifth step – configure grub2

Change GRUB_TIMEOUT to -1 in /etc/defaults/grub so it will be waiting forever in the boot menu.

Update grub configuration:

$ sudo update-grub

Check your kernel release:

$ uname -r

Edit /etc/grub.d/40_custom file to add a new entry in the grub2 menu and take into account your kernel release:

menuentry "Live minimal OS" {
 set root='(hd0,1)'
 linux /boot/vmlinuz-3.2.0-24-generic boot=live toram=livefs.squashfs
 initrd /boot/initrd.img-3.2.0-24-generic

Update grub configuration again:

$ sudo update-grub

The sixth step – check it out!

Reboot the system and check it out:

$ sudo reboot

A couple of notes

Compression is very effective as it can compress a 2 GB file system (KDE + Libre Office + a couple of smaller applications) to around 700 MB.

The system becomes blazingly fast and you can easily create live USB this way.

Make your changes persistent, create partitions labeled accordingly live-rw for root, home-rw for the home filesystem, and add a persistent parameter to kernel boot parameters. Create files in the the root directory (not inside live fs) if you can’t create new partitions. For example, to create a persistent home (200MB, without reserved blocks) use commands:

$ sudo dd if=/dev/zero of=/home-rw bs=1M count=200
$ sudo mkfs.ext4 /home-rw
$ sudo tune2fs -m 0 /home-rw
$ sudo tune2fs -L home-rw /home-rw

Apparmor doesn’t work very well so you need to remove it.

Go to /usr/share/initramfs-tools/scripts directory and start reading the live file to see how it works.

Don’t forget to update initramfs after applying modifications:

$ sudo update-initramfs -u

Don’t forget to read the live-boot manual page.

A couple of common errors

only one RO file system supported with exposedroot

Just remove livefs.squashfs file from /squashfs/live/ directory and create squashfs image again.

a wrong rootfs was mounted

This error means that the live-boot package was not installed on the system used to create squashfs image, so install it and create an image again.