Custom debian based livecd - cesetxeberria/pxeserver GitHub Wiki

Prepare the server

First of all we have to prepare our PXE server using one of these bootloaders

We also need to configure it as a nfs server.

Now we need to get 3 files: a kernel (vmlinuz), an initramfs (initrd.img) and a squashed installation (filesystem.squashfs).

Prepare the files

Install debootstrap and squashfs-tools

apt install debootstrap squashfs-tools

Create a new folder for the custom system, with a subfolder named "live" inside. Go to the newly created directory (not the subfolder).

mkdir -p /home/tftp/livecds/customdebian/live
cd /home/tftp/livecds/customdebian

Use debootstrap to create a new debian installation.

debootstrap --arch=amd64 --include=locales,keyboard-configuration,console-setup \
--components=main,contrib,non-free,non-free-firmware --merged-usr \
bookworm live/squashfs-root http://deb.debian.org/debian

Keep in mid that this is a one-line command, it's written like this for readability. It will create a debian buster installation, for amd64 architecture inside "live/squashfs-root" directory. Rest of the options are pretty self-explanatory, read the manual if you have doubts.

After finishing with debootstrap, enter the new system with chroot

LANG=C.UTF-8 chroot ./live/squashfs-root

First part of the command (LANG=C.UTF-8) isn't mandatory but can help if you want to create a system with different locale settings than yours.

Inside a chroot is preferable to mount some special filesystems

mount -t proc none /proc
mount -t sysfs none /sys
mount -t devpts none dev/pts

Configure locale, keyboard, console settings and local time.

dpkg-reconfigure locales
dpkg-reconfigure keyboard-configuration
dpkg-reconfigure console-setup
dpkg-reconfigure tzdata

Customize some systemd options.

mkdir -p /etc/systemd/system/networking.service.d/
mkdir -p /etc/systemd/system/[email protected]/
echo "[Service]" > /etc/systemd/system/[email protected]/override.conf
echo "ExecStop=" >> /etc/systemd/system/[email protected]/override.conf
echo "[Service]" > /etc/systemd/system/networking.service.d/override.conf
echo "ExecStop=" >> /etc/systemd/system/networking.service.d/override.conf
sed -i.bak 's/#Storage=auto/Storage=volatile/g' /etc/systemd/journald.conf

First four commands create some override configuration for ifup and networking services. An empty "ExecStop=" attribute means that network won't stop working during the shutdown process.

Last command will prevent systemd from storing journal in hard disk.

Now install minimum packages and execute tasksel, to select the rest of packages to install.

apt install linux-image-amd64 live-boot sudo
tasksel --new-install

Clean all the temporary files created by apt

apt clean
apt autoclean
apt autoremove
rm -rf /var/cache/apt/archives/*.deb
rm -rf /var/cache/apt/archives/partial/*
rm -rf /var/cache/debconf/*-old
rm -rf /var/lib/apt/lists/*

Umount the previously mounted special filesystems

umount -df /dev/pts
umount -df /sys
umount -df /proc

Create a new user and add it to the "sudo" group, so it can become root. Then exit the chroot

adduser username
adduser username sudo
exit

Remember to replace "username" with your own user name.

Get initrd.img and vmlinuz files from the new system. Compress the filesystem using mksquash and delete the uncompressed folder.

cp live/squashfs-root/vmlinuz .
cp live/squashfs-root/initrd.img .
mksquashfs live/squashfs-root live/filesystem.squashfs
rm -R live/squashfs-root

Add the livecd entry to your boot menu.

Using pxelinux

nano /home/tftp/syslinux/bios/pxelinux.cfg/default

label Custom Debian live
 menu clear
 kernel /livecds/customdebian/vmlinuz
 initrd /livecds/customdebian/initrd.img
 append panic=10 ro boot=live root=/dev/nfs nfsroot=192.168.1.54:/home/tftp/livecds/customdebian swap
 ipappend 2 

Remember that 192.168.1.54 is just an example. You must use the ip of your own nfs server. Last line (ipappend 2) is very important. When Debian boots, it tries to get an ip address using dhcp, but as we are booting from pxe he already has one. With this line we are telling the system to keep it.

Using grub

nano /home/tftp/grub/grub.cfg

menuentry "Custom Debian live" {
 linux /livecds/customdebian/vmlinuz panic=10 ro boot=live root=/dev/nfs nfsroot=192.168.1.54:/home/tftp/livecds/customdebian ksdevice=bootif
 initrd /livecds/customdebian/initrd.img
}

Remember that 192.168.1.54 is just an example. You must use the ip of your own nfs server. Last part of the first command (ksdevice=bootif) is very important. When Debian boots, it tries to get an ip address using dhcp, but as we are booting from pxe he already has one. With this line we are telling the system to keep it.

Using ipxe

nano /home/tftp/default.ipxe

:customdebianlive
kernel /livecds/customdebian/vmlinuz
initrd /livecds/customdebian/initrd.img
imgargs vmlinuz initrd=initrd.img panic=10 ro boot=live root=/dev/nfs nfsroot=192.168.1.54:/home/tftp/livecds/customdebian ksdevice=bootif      
boot ||
goto MENU

Remember that 192.168.1.54 is just an example. You must use the ip of your own nfs server. Last part of the first command (ksdevice=bootif) is very important. When Debian boots, it tries to get an ip address using dhcp, but as we are booting from pxe he already has one. With this line we are telling the system to keep it.