Build a persistent Debian USB drive

The Debian Live Project has made some nice tools to build a Debian Live image, this howto describes the usage of these tools to build a persistent Live USB drive with custom packages.

Note for Debian testing users: I had the following error while building the image with the 3.0 package from the testing repositories:

cp: cannot stat `/usr/share/syslinux/themes/debian-squeeze/isolinux-live': No such file or directory

After some research i found that it is recommended to use the 2.0 package from the stable repositories, so lets start with that, the package debootstrap is a dependency, so we will install that one to:

apt-get install -t stable live-build debootstrap

Next we will have to install the packages necessary to build the image:

apt-get install dosfstools genisoimage mtools parted syslinux uuid-runtime squashfs-tools genext2-fs mtd-tools

Now we can create a folder for the project, for example:

mkdir live
cd live

In this folder we run the following commands to setup the configuration for the build:

First we set the architecture for the live image, if you don’t do this it will default to the architecture of the host system:

lb config --architecture i386

(the architectures i386,AMD64,PowerPC and SPARC are available)

To set the output format as a suitable image for the USB drive (if you don’t use this, it will produce a CD ISO):

lb config -b usb-hdd

This will set the “persistent” boot parameter:

lb config --bootappend-live "persistent"

Note: If this option is set, the live OS will search for a partition labeled “live-rw” and use it for persistence.

To select the packages for the live system from the stable repositories(you can also select testing or unstable if you like):

lb config --distribution stable

To select the package types to use (you could also add non-free):

lb config --archive-areas "main contrib"

The next commands will set the mirrors for the packages used by the building process:

lb config --mirror-bootstrap
lb config --mirror-chroot-security

These commands will set the mirrors used for packages within the live system:

lb config --mirror-binary
lb config --mirror-binary-security

Note: these are Dutch mirrors, for a mirror closer to your location have a look at:

Now we are ready to setup the packages we wish to include in the live system:

lb config --packages "xserver-xorg-core xorg xfce4 iceweasel gparted partimage"

The basic configuration is done, if you wish to learn about more settings, see this manual:

Finally to start the build:

lb build

If everything goes well, you will have a binary.img file in your root folder.

Note: I received the following error a few times:

"loop: can't delete device /dev/loop2: Device or resource busy"

Apparently this is a bug that has something to do with an invalid symlink, to remove the symlink:

rm binary/dists/squeeze/squeeze

and run the build again, it should run fine the second time.

To copy the image to your USB drive:

dd if=binary.img of=/dev/your-usb-device

WARNING: Do not select a different disk, it will destroy all data without asking for confirmation.

Last but not least, we will add a partition for the persistence:

fdisk /dev/your-usb-device

Press ‘p’ to list the partition table, look for the end cylinder of the first partition.

Then press ‘n’ to create a new partition, select a primary partition and enter the first free cylinder as the starting cylinder. Now you have to enter the size of the new partition.

Press ‘w’ to write the changes to the disk.

Now all you have to do is create a filesystem in your new partition, use the label ‘live-rw’:

mkfs.ext3 -L live-rw /dev/your-new-partition

Congratulations! you have build your own persistent Debian Live USB drive, all changes you make in the live system will be saved on the live-rw partition.