User Tools

Site Tools


indexes:building_a_custom_liveusb

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

Download 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

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

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:

apt-get install console-data
dpkg-reconfigure console-data

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

Start rebuilding the LiveUSB:

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

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:
mkdir /tmp/squashfs
mkdir /tmp/tmpfs
mkdir /tmp/mergemount
  • Mount the filesystems:
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
  • Make sure essential filesystems are mounted inside the image:
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

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

LANG=C sudo chroot /tmp/mergemount apt-get update
LANG=C sudo chroot /tmp/mergemount apt-get install <your desired package>
  • Clean up:
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
  • Build a new squashfs with your changes:
sudo mksquashfs /tmp/mergemount /tmp/filesystem.squashfs
  • Clean up the temporary mountpoints:
sudo umount /tmp/mergemount
sudo umount /tmp/tmpfs
sudo umount /tmp/squashfs
  • Copy the new squashfs in place:
sudo cp /tmp/filesystem.squashfs <usb mount point>/image/casper/
  • Clean up the rest:
sudo rm -rf /tmp/tmpfs /tmp/squashfs /tmp/mergemount /tmp/filesystem.squashfs

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.

#!/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

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.

indexes/building_a_custom_liveusb.txt · Last modified: d/m/Y H:i by domingo