Download Ubuntu Hardy Heron iso (or whatever version you want).

Download {{:howtos:unetbootin-linux-216.zip|unetbootin-linux-216.zip}} (find the newest version here: http://unetbootin.sourceforge.net/


Format your usb pen with fat32.

Make LiveUSB with unetbootin (just unpack unetbootin-linux-216.zip and execute unetbootin as root) or use the usb-builder tool within Ubuntu.

===== Version 1 =====

<code>
sudo mount -o loop -t squashfs /media/disk/casper/filesystem.squashfs /home/tdd/No-rsync-stuff/usb
sudo mkdir /tmp/ubuntu_usb_image
sudo cp -aR /home/tdd/No-rsync-stuff/usb/* /tmp/ubuntu_usb_image/
sudo mount -t proc none /tmp/ubuntu_usb_image/proc
sudo mount -o bind /dev /tmp/ubuntu_usb_image/dev
sudo chroot /tmp/ubuntu_usb_image /bin/bash
</code>

Inside chroot edit /etc/apt/sources.list to enable the repositories you need and insert a dns server into /etc/resolv.conf. If you don't do this you can't download anything through apt-get.

To choose a different keyboard layout:
<code>
apt-get install console-data
dpkg-reconfigure console-data
</code>


Make you apt-get install's and configurations.....

Start rebuilding the LiveUSB:
<code>
cd /tmp
sudo umount /tmp/ubuntu_usb_image/proc
sudo mksquashfs ubuntu_usb_image ubuntu_usb.squashfs -no-sparse
sudo cp ubuntu_usb.squashfs /media/disk/casper/filesystem.squashfs
</code>

===== Version 2 =====
This is more or less copied from https://wiki.ubuntu.com/MobileTeam/Mobile/HowTo/ImageModification. The difference is that my source is a build and ready usb stick and not an image file.

I also found it necessary to give tmpfs an upper size of 2GB, otherwise it ran out of space during updates.

===== The manual way =====

  * Preparation:
<code>
mkdir /tmp/squashfs
mkdir /tmp/tmpfs
mkdir /tmp/mergemount
</code>

  * Mount the filesystems:
<code>
sudo modprobe unionfs
sudo mount -o loop -t squashfs <mount point for usb>/casper/filesystem.squashfs /tmp/squashfs
sudo mount -o size=2G -t tmpfs tmpfs /tmp/tmpfs
sudo mount -t unionfs -o dirs=/tmp/tmpfs:/tmp/squashfs=ro none /tmp/mergemount
</code>

  * Make sure essential filesystems are mounted inside the image:
<code>
sudo chroot /tmp/mergemount mount -t proc proc /proc
sudo chroot /tmp/mergemount mount -t sysfs sysfs /sys
sudo chroot /tmp/mergemount mkdir -p /dev/pts
sudo chroot /tmp/mergemount mount -t devpts devpts -o noexec,nosuid,gid=5,mode=620 /dev/pts
</code>

Make sure packagelists are up to date and install the software you like:

<code>
LANG=C sudo chroot /tmp/mergemount apt-get update
LANG=C sudo chroot /tmp/mergemount apt-get install <your desired package>
</code>

  * Clean up:
<code>
sudo chroot /tmp/mergemount apt-get clean
sudo chroot /tmp/mergemount umount /proc
sudo chroot /tmp/mergemount umount /sys
sudo chroot /tmp/mergemount umount /dev/pts
sudo chroot /tmp/mergemount rm -rf /dev/pts
</code>

  * Build a new squashfs with your changes:
<code>
sudo mksquashfs /tmp/mergemount /tmp/filesystem.squashfs
</code>

  * Clean up the temporary mountpoints:
<code>
sudo umount /tmp/mergemount
sudo umount /tmp/tmpfs
sudo umount /tmp/squashfs
</code>

  * Copy the new squashfs in place:
<code>
sudo cp /tmp/filesystem.squashfs <usb mount point>/image/casper/
</code>

  * Clean up the rest:
<code>
sudo rm -rf /tmp/tmpfs /tmp/squashfs /tmp/mergemount /tmp/filesystem.squashfs
</code>

Have fun with your changed imagefile ....

===== The same as shellscript =====

The script below uses the same setup as above and spawns a rootshell inside the squashfs, you can then run "apt-get update" and install your package or make other changes, if you exit the shell with Ctrl-D or the 'exit' command it will offer you to re-roll the squashfs for you.

<file>
#!/bin/sh

if [ -z $1 ];then
    echo 'I need a path to the usb mount point as first argument'
    exit 0
fi

SQUASHFS=/home/tdd/tmp/squashfs
TMPFS=/home/tdd/tmp/tmpfs
MERGEMOUNT=/home/tdd/tmp/mergemount
SQUASHFSFILE=/home/tdd/tmp

mkdir $SQUASHFSFILE
mkdir $SQUASHFS
mkdir $TMPFS
mkdir $MERGEMOUNT

sudo modprobe unionfs

sudo mount -o loop -t squashfs $1/casper/filesystem.squashfs $SQUASHFS
sudo mount -o size=3G -t tmpfs tmpfs $TMPFS
sudo mount -t unionfs -o dirs=$TMPFS:$SQUASHFS=ro none $MERGEMOUNT

sudo chroot $MERGEMOUNT mount -t proc proc /proc
sudo chroot $MERGEMOUNT mount -t sysfs sysfs /sys
sudo chroot $MERGEMOUNT mkdir -p /dev/pts
sudo chroot $MERGEMOUNT mount -t devpts devpts -o noexec,nosuid,gid=5,mode=620 /dev/pts

sudo cp /etc/resolv.conf $MERGEMOUNT/etc/

LANG=C sudo chroot $MERGEMOUNT 

sudo chroot $MERGEMOUNT rm /etc/resolv.conf
sudo chroot $MERGEMOUNT umount /proc
sudo chroot $MERGEMOUNT umount /sys
sudo chroot $MERGEMOUNT umount /dev/pts
sudo chroot $MERGEMOUNT rm -rf /dev/pts

echo -n 'do you want to build a squashfs with the changes ? (y/n) '
read yesno
if [ -z $yesno ];then
    yesno='n'
fi

if [ $yesno = 'y' ];then
    sudo mksquashfs $MERGEMOUNT $SQUASHFSFILE/filesystem.squashfs -wildcards -e proc/*
fi

sudo umount -t unionfs $MERGEMOUNT
sudo umount -t tmpfs $TMPFS
sudo umount -t squashfs $SQUASHFS

if [ $yesno = 'y' ];then
    sudo cp $SQUASHFSFILE/filesystem.squashfs $1/casper/
fi

echo -n 'do you want to cleanup in /tmp ? (y/n) '
read yesno
if [ -z $yesno ];then
    yesno='n'
fi

if [ $yesno = 'y' ];then
sudo rm -rf $TMPFS $SQUASHFS $MERGEMOUNT $SQUASHFSFILE/filesystem.squashfs
fi

</file>

===== Important Notes =====
For unknown reasons the three mounted filesystems (tmpfs, squashfs and proc) are always lock by something, thus the cleanup phase will not work. I can only get it of by rebooting the computer (fairly annoying!). Because of the locks I was forced to include an exclude statement not digging into /proc.