Skip to content

Instantly share code, notes, and snippets.

@jeffscrum
Forked from ravecat/debootstrap.md
Last active January 14, 2024 15:44
Show Gist options
  • Save jeffscrum/f854d45ea10e0c3f14839fd1c00f919f to your computer and use it in GitHub Desktop.
Save jeffscrum/f854d45ea10e0c3f14839fd1c00f919f to your computer and use it in GitHub Desktop.
debootstrap, livecd

A live CD or live DVD is a complete bootable Linux operating system loaded from a CD or DVD. Although there are a lots of live Linux CDs, for seemingly every taste and purpose, it might still be useful on occasion to build your own. This guide details the steps to build a bootable live CD/DVD based on Debian "bullseye".

Step 1 – Installing the necessary software

These are the software packages you need to install on your Debian system:

apt-get install xorriso live-build syslinux squashfs-tools isolinux

Step 2 – Create a basic filesystem

Start by creating a new work directory, and bring in a basic Debian filesystem using debootstrap. Depending on your network connection, it will take some time downloading all the necessary packages:

# mkdir ~/livework && cd ~/livework
# debootstrap --arch=amd64 bullseye chroot http://deb.debian.org/debian

The new filesystem was created in ~/livework/chroot directory. It is time to chroot into the new filesystem and finish the installation.

Step 3 – chroot

# cd ~/livework
# LANG=C.UTF-8 chroot chroot /bin/bash
# mount none -t proc /proc && \
  mount none -t sysfs /sys && \
  mount none -t devpts /dev/pts
# export LC_ALL=C
# export PS1="\e[01;31m(live):\W \$ \e[00m"

In chroot you need to bring in a Linux kernel and the necessary livecd packages. You can also set up a root password:

(live):/ $ apt-get install dialog dbus
(live):/ $ dbus-uuidgen > /var/lib/dbus/machine-id
(live):/ $ apt-get install linux-image-amd64 live-boot
(live):/ $ apt-get install vim ssh wget curl net-tools iputils-ping isc-dhcp-client less binutils parted fdisk
(live):/ $ echo "liveCD" > /etc/hostname
(live):/ $ passwd

Add hostname to hosts file

(live):/ $ cat > /etc/hosts << EOF
127.0.0.1 localhost
127.0.1.1 $(cat /etc/hostname)

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF

This is a very basic Debian system. On top of it you can install packages such as desktop environment (apt-get install lxde), a web browser (apt-get install iceweasel) etc. When you are done, cleanup apt caches and exit chroot.

(live):/ $ apt-get clean && apt-get autoclean && apt-get autoremove
(live):/ $ rm /var/lib/dbus/machine-id && rm -rf /tmp/*
(live):/ $ umount /proc /sys /dev/pts
(live):/ $ exit

Step 4 – ISOLINUX

The CD/DVD image is set up using ISOLINUX. Start by creating a new directory, binary, containing the Linux kernel, a compressed copy of chroot, and isolinux executables:

# cd ~/livework
# mkdir -p binary/live && mkdir -p binary/isolinux
# cp chroot/boot/vmlinuz-*-amd64 binary/live/vmlinuz
# cp chroot/boot/initrd.img-*-amd64 binary/live/initrd
# mksquashfs chroot binary/live/filesystem.squashfs -comp xz -e boot
# cp /usr/lib/ISOLINUX/isolinux.bin binary/isolinux/.
# cp /usr/lib/syslinux/modules/bios/* binary/isolinux/.
# touch binary/isolinux/DEBIAN_IMAGE

Next, an isolinux config file is created:

# cat <<'EOF' > binary/isolinux/isolinux.cfg

ui menu.c32
prompt 0
menu title Boot Menu
timeout 300
label live-amd64
	menu label ^Live (amd64)
	menu default
	linux /live/vmlinuz
 	append initrd=/live/initrd boot=live persistence quiet

label live-amd64-failsafe
	menu label ^Live (amd64 failsafe)
	linux /live/vmlinuz
	append initrd=/live/initrd boot=live persistence config memtest noapic noapm nodma nomce nolapic nomodeset nosmp nosplash vga=normal

endtext
EOF

Step 5 – Building the iso image

I use GNU xorriso to build the final iso image. It creates an isohybrid image that can be transferred to a USB stick using dd command.

# cd ~/livework
# xorriso -as mkisofs -r -J -joliet-long -l -cache-inodes \
-isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin -partition_offset 16 \
-A "Debian Live"  -b isolinux/isolinux.bin -c \
isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
-boot-info-table -o bullseye-livecd.iso binary

Conclusion

The iso image in this example has a size of 203MB. It is a basic Debian system as created by debootstrap, with only the necessary livecd executables. From here it will grow as more packages are added and the image is personalized.

I have decided to document my steps in case anyone might find them useful. Please let me know if you run into problems, or if you have any questions or suggestions. I use these steps to build small network appliances, servers, and rescue disks, nothing important. I’ve never went as far as to build a full distribution.

If you are considering it for a more serious project, better try live-build. Debian team uses live-build to build the official Debian CDs. The tool is very powerful and highly configurable, and it goes well beyond what I’ve covered in this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment