Table of Contents

Create custom debian live usb-stick (uefi / bios dual boot)

Install necessary packages on the build system

root@host:~# apt-get install debootstrap squashfs-tools rsync xorriso grub-pc-bin grub-efi-amd64-bin mtools
[ ... ]

Create directory structure & install base image

root@host:~# mkdir /root/shtf-stick && cd /root/shtf-stick
 
root@host:~/shtf-stick# debootstrap --arch=i386 --variant=minbase stretch chroot http://http.at.debian.org/debian/
[ ... ]

Perform basic configuration

root@host:~/shtf-stick# chroot ./chroot
root@host:/# echo shtf > /etc/hostname
root@host:/# passwd root
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@host:/# mount none -t proc /proc && \
mount none -t sysfs /sys && \
mount none -t devpts /dev/pts && \
export HOME=/root && \
export LC_ALL=C && \
apt-get update && \
apt-get install dialog dbus --yes && \
dbus-uuidgen > /var/lib/dbus/machine-id

root@host:/# apt-get install --no-install-recommends --yes linux-image-686 firmware-linux-free systemd-sysv live-boot kbd console-data
[ ... ]

Install additional packages

root@host:/# apt-get install --no-install-recommends --yes \
network-manager net-tools wireless-tools wpagui tcpdump wget openssh-client \
blackbox xserver-xorg-core xserver-xorg xinit xterm \
pciutils usbutils gparted ntfs-3g hfsprogs rsync less curl tcpdump dosfstools syslinux partclone vim pv \
firefox-esr chntpw lvm2 mdadm  keepassx gdisk gnupg duplicity s3cmd bzip2 lsof screen mc htop iproute2 inetutils-ping inetutils-traceroute  netcat-openbsd extundelete
 
[ ... ]

Cleanup & leave chroot environment

root@host:/# rm -f /vmlinuz* /initrd.img* /var/lib/dbus/machine-id && \
apt-get clean && \
rm /etc/resolv.conf && \
umount -lf /proc /sys /dev/pts
 
root@host:/# exit
exit
root@debian:~/shtf-stick# umount -lf $(mount | awk '/shtf-stick/ { print $3 }')
root@host:~/shtf-stick# rm chroot/root/.bash_history

Create filesystem & boot config

root@host:~/shtf-stick# mkdir -p ./{scratch,image/live} && \
mksquashfs chroot image/live/filesystem.squashfs -e boot
Parallel mksquashfs: Using 1 processor
Creating 4.0 filesystem on image/live/filesystem.squashfs, block size 131072.
[=========================================================================================================================/] 25507/25507 100%

Exportable Squashfs 4.0 filesystem, gzip compressed, data block size 131072
	compressed data, compressed metadata, compressed fragments, compressed xattrs
	duplicates are removed
Filesystem size 305495.00 Kbytes (298.33 Mbytes)
	39.71% of uncompressed filesystem size (769303.94 Kbytes)
Inode table size 311570 bytes (304.27 Kbytes)
	29.08% of uncompressed inode table size (1071399 bytes)
Directory table size 300335 bytes (293.30 Kbytes)
	42.66% of uncompressed directory table size (703968 bytes)
Number of duplicate files found 809
Number of inodes 30836
Number of files 21710
Number of fragments 2182
Number of symbolic links  6003
Number of device nodes 8
Number of fifo nodes 0
Number of socket nodes 0
Number of directories 3115
Number of ids (unique uids + gids) 10
Number of uids 2
	root (0)
	systemd-timesync (100)
Number of gids 9
	root (0)
	shadow (42)
	utmp (43)
	rtkit (109)
	tty (5)
	systemd-journal (101)
	staff (50)
	adm (4)
	mail (8)
root@host:~/shtf-stick# 
root@host:~/shtf-stick# cp chroot/boot/vmlinuz-* image/vmlinuz && \
cp chroot/boot/initrd.img-* image/initrd
root@host:~/shtf-stick# cat <<'EOF' > ./scratch/grub.cfg

search --set=root --file /DEBIAN_CUSTOM

insmod all_video

set default="0"
set timeout=30

menuentry "SHTF Live Stick" {
    linux /vmlinuz boot=live noquiet nomodeset
    initrd /initrd
}
EOF
root@host:~/shtf-stick#
root@host:~/shtf-stick# touch ./image/DEBIAN_CUSTOM

Prepare USB stick

root@host:~/shtf-stick# export DEVICE=/dev/sdc
root@host:~/shtf-stick# mkdir -p /mnt/{usb,efi}

Create GPT & MBR partition tables (skip for updating)

root@host:~/shtf-stick# dd if=/dev/zero of=${DEVICE} bs=1k count=100
100+0 records in
100+0 records out
102400 bytes (102 kB) copied, 1.5086 s, 67.9 kB/s
root@host:~/shtf-stick# parted --script ${DEVICE} \
    mklabel gpt \
    mkpart primary fat32 2048s 4095s \
        name 1 BIOS \
        set 1 bios_grub on \
    mkpart ESP fat32 4096s 413695s \
        name 2 EFI \
        set 2 esp on \
    mkpart primary fat32 413696s 100% \
        name 3 LINUX \
        set 3 msftdata on

root@host:~/shtf-stick# gdisk ${DEVICE} << EOF
r     # recovery and transformation options
h     # make hybrid MBR
1 2 3 # partition numbers for hybrid MBR
N     # do not place EFI GPT (0xEE) partition first in MBR
EF    # MBR hex code
N     # do not set bootable flag
EF    # MBR hex code
N     # do not set bootable flag
83    # MBR hex code
Y     # set the bootable flag
x     # extra functionality menu
h     # recompute CHS values in protective/hybrid MBR
w     # write table to disk and exit
Y     # confirm changes
EOF

Create filesystems (skip for updating)

root@host:~/shtf-stick# mkfs.vfat -F32 ${DEVICE}2 && mkfs.vfat -F32 ${DEVICE}3

Create & mount filesystems

root@host:~/shtf-stick# mount ${DEVICE}2 /mnt/efi && mount ${DEVICE}3 /mnt/usb

Install grub for UEFI and BIOS

root@host:~/shtf-stick# grub-install \
    --target=x86_64-efi \
    --efi-directory=/mnt/efi \
    --boot-directory=/mnt/usb/boot \
    --removable \
    --recheck

root@host:~/shtf-stick# grub-install \
    --target=i386-pc \
    --boot-directory=/mnt/usb/boot \
    --recheck \
    ${DEVICE}
 
root@host:~/shtf-stick#

Create USB Stick

root@host:~/shtf-stick# mkdir -p /mnt/usb/{boot/grub,live} && \
cp -ar ./image/* /mnt/usb/ && \
cp -a ./scratch/grub.cfg /mnt/usb/boot/grub/grub.cfg && \
umount /mnt/{usb,efi}

Source: https://willhaley.com/blog/custom-debian-live-environment/