#!/bin/bash # Build a Debian installation ISO that runs this preseed unattended. # # This is the Debian equivalent of putting ks.cfg on the Fedora ISO: the preseed # is baked into the image and the boot menu is edited to load it, so the machine # installs with no questions and no extra medium. # # Usage: # ./make-iso.sh /path/to/debian-13.x.x-amd64-netinst.iso [output.iso] # # Requirements: xorriso only. # apt-get install xorriso # # The ISO is rebuilt, not modified in place: the source ISO is never touched. # # WHY THE FILES ARE MAPPED ONTO THE ORIGINAL IMAGE INSTEAD OF EXTRACTING IT # # The obvious implementation - extract the whole tree, edit, rebuild with # `xorriso -as mkisofs` - works but grows the image by ~186 MB on a netinst # (792 MB -> 988 MB), because Debian's ISO shares identical file extents between # paths and extracting to a normal filesystem duplicates all of them. # # `xorriso -indev SRC -outdev DST -boot_image any replay -map ...` instead copies # the original session and overwrites only the files named in -map, so the # deduplication and the boot record survive untouched. Verified on # debian-13.7.0-amd64-netinst.iso: output 792002560 bytes vs source 792723456, # all 1010 source files present, the four payload files byte-identical except # for the boot-info table, and both El Torito images (BIOS and UEFI) intact. set -euo pipefail SRC_ISO=${1:-} OUT_ISO=${2:-} HERE=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) PRESEED="$HERE/preseed.cfg" die() { printf 'make-iso: %s\n' "$*" >&2; exit 1; } note() { printf ' %s\n' "$*"; } [ -n "$SRC_ISO" ] || die "usage: $0 [output.iso] the ISO is the one you would otherwise boot by hand, e.g. debian-13.7.0-amd64-netinst.iso" [ -f "$SRC_ISO" ] || die "no such ISO: $SRC_ISO" [ -f "$PRESEED" ] || die "preseed.cfg not found next to this script: $PRESEED" command -v xorriso >/dev/null 2>&1 || die "xorriso is required (apt-get install xorriso)" if [ -z "$OUT_ISO" ]; then OUT_ISO="${SRC_ISO%.iso}-vdi.iso" fi [ "$OUT_ISO" != "$SRC_ISO" ] || die "refusing to overwrite the source ISO" if [ -e "$OUT_ISO" ]; then # Overwriting an existing image is destructive and never what is wanted by # accident; the caller can remove it deliberately. die "output already exists: $OUT_ISO (remove it first)" fi WORK=$(mktemp -d "${TMPDIR:-/tmp}/make-iso.XXXXXX") # shellcheck disable=SC2064 trap "rm -rf '$WORK'" EXIT # --------------------------------------------------------------------------- # 1. Find every boot menu file on the ISO. # # Discovered rather than hardcoded, because a fixed list gets this wrong in a way # that is easy to miss: on Debian's netinst the BIOS menu is isolinux.cfg, which # is only "include menu.cfg" and "default vesamenu.c32"; the actual default entry # is defined in gtk.cfg (label installgui, "menu default"). Patching only the # obvious files leaves the default BIOS entry unpatched, so the machine still # stops and prompts - exactly the failure this script exists to prevent. # So: every .cfg under isolinux/ and boot/grub/ that contains a kernel line gets # the parameters. # --------------------------------------------------------------------------- MENUS=$( for dir in /isolinux /boot/grub; do xorriso -indev "$SRC_ISO" -find "$dir" -name '*.cfg' 2>/dev/null done | tr -d "'" | grep -v '^$' | sort -u ) [ -n "$MENUS" ] || die "no .cfg files found under /isolinux or /boot/grub in $SRC_ISO - is this a Debian netinst ISO?" # --------------------------------------------------------------------------- # 2. Patch each menu. # # The parameters, per the Debian installation guide (appendix B.2.1): # auto=true alias for auto-install/enable; delays the locale and # keyboard questions so the preseed can answer them # priority=critical alias for debconf/priority; stops anything below # critical from being asked # preseed/file= read the preseed from the ISO itself # # The checksum parameter is deliberately omitted: it must be an md5 and must # match exactly, so it breaks the moment the preseed is edited. # --------------------------------------------------------------------------- PARAMS='auto=true priority=critical preseed/file=/cdrom/preseed.cfg' MAP_ARGS=() for menu in $MENUS; do local_name="$WORK/$(echo "$menu" | tr '/' '_')" xorriso -osirrox on -indev "$SRC_ISO" -extract "/$menu" "$local_name" 2>/dev/null \ || die "could not extract /$menu from the ISO" chmod u+w "$local_name" before=$(grep -cF 'preseed/file=' "$local_name" || true) # Some menus are stubs: Debian's /boot/grub/x86_64-efi/grub.cfg is a single # line, "source /boot/grub/grub.cfg", with no kernel entries of its own. It # inherits the patched entries through the source, so there is nothing to # edit and requiring a change there would be wrong. Count the real kernel # lines first and skip a menu that has none. kernels=$(grep -cE '^[[:space:]]*(append|linux)[[:space:]]' "$local_name" || true) if [ "$kernels" -eq 0 ]; then note "/$menu: no kernel entries of its own (stub), left alone" continue fi # Two quoting traps, both found by running this rather than by reading it: # * '@' is the delimiter because the pattern uses '|' for the # append/linux alternation, so '|' cannot also delimit the s/// command; # * the end-of-pattern '$' must be written '\$'. Inside double quotes a # bare '$@' expands to the script's positional parameters, which turns # the expression into garbage and makes sed report "unterminated s". # The /^[[:space:]]*#/! guard keeps comment lines out of the rewrite. sed -i -E \ -e "/^[[:space:]]*#/! s@^([[:space:]]*(append|linux)[[:space:]].*)\$@\1 ${PARAMS}@" \ "$local_name" after=$(grep -cF 'preseed/file=' "$local_name" || true) if [ "$after" -le "$before" ]; then die "patching /$menu changed nothing - the menu layout is not what this script expects" fi note "/$menu: $after installer entr(y|ies) now pass the preseed" MAP_ARGS+=(-map "$local_name" "/$menu") done # --------------------------------------------------------------------------- # 3. Rebuild by replaying the original session and overwriting only our files. # --------------------------------------------------------------------------- note "building $OUT_ISO" xorriso -indev "$SRC_ISO" -outdev "$OUT_ISO" \ -boot_image any replay \ -map "$PRESEED" /preseed.cfg \ "${MAP_ARGS[@]}" \ 2>&1 | grep -iE "xorriso : FAILURE|libisofs: ERROR|completed successfully" || true [ -s "$OUT_ISO" ] || die "xorriso did not produce $OUT_ISO" # --------------------------------------------------------------------------- # 4. Verify rather than trust the exit status. # # xorriso can report success having silently skipped a -map, and an ISO that # boots to an interactive prompt is worse than no ISO at all. These checks are # the ones that would actually catch that. # --------------------------------------------------------------------------- note "verifying the result" # 4a. The preseed must be on the image, byte-identical to the source. xorriso -osirrox on -indev "$OUT_ISO" -extract /preseed.cfg "$WORK/check.cfg" 2>/dev/null \ || die "the preseed is not on the output ISO" cmp -s "$WORK/check.cfg" "$PRESEED" \ || die "the preseed on the ISO differs from $PRESEED" note "preseed present and byte-identical" # 4b. Every menu that has kernel entries must carry the parameter. A stub # menu has none and inherits them through its source line. for menu in $MENUS; do local_name="$WORK/verify_$(echo "$menu" | tr '/' '_')" xorriso -osirrox on -indev "$OUT_ISO" -extract "/$menu" "$local_name" 2>/dev/null \ || die "cannot read /$menu back from the output ISO" kernels=$(grep -cE '^[[:space:]]*(append|linux)[[:space:]]' "$local_name" || true) [ "$kernels" -eq 0 ] && continue grep -qF 'preseed/file=/cdrom/preseed.cfg' "$local_name" \ || die "/$menu on the output ISO does not pass the preseed parameter" done note "boot menus on the output ISO pass: $PARAMS" # 4c. Both El Torito images must still be there, or the stick will not boot on # one of the two firmware types. TORITO=$(xorriso -indev "$OUT_ISO" -report_el_torito plain 2>&1) printf '%s' "$TORITO" | grep -q 'El Torito' || die "the output ISO has no El Torito boot record" BIOS=0; UEFI=0 printf '%s' "$TORITO" | grep -q 'BIOS' && BIOS=1 printf '%s' "$TORITO" | grep -q 'UEFI' && UEFI=1 if [ "$BIOS" -eq 1 ] && [ "$UEFI" -eq 1 ]; then note "boot record intact (BIOS and UEFI images present)" else # Not fatal if the source never had one of them, but say so plainly. note "WARNING: only one firmware type is bootable (BIOS=$BIOS UEFI=$UEFI);" note " check the source ISO - this is inherited, not caused here" fi # 4d. Nothing may be missing relative to the source. xorriso -indev "$SRC_ISO" -find / -type f -exec lsdl 2>/dev/null | awk '{print $NF}' | sort > "$WORK/src.list" xorriso -indev "$OUT_ISO" -find / -type f -exec lsdl 2>/dev/null | awk '{print $NF}' | sort > "$WORK/out.list" MISSING=$(comm -23 "$WORK/src.list" "$WORK/out.list" | wc -l) [ "$MISSING" -eq 0 ] || die "$MISSING file(s) from the source ISO are missing in the output" note "all $(wc -l < "$WORK/src.list") source files present" note "wrote $OUT_ISO ($(du -h "$OUT_ISO" | cut -f1))" cat <