#!/bin/bash set -Eeuo pipefail function cleanup() { trap - SIGINT SIGTERM ERR EXIT if [ -n "${tmpdir+x}" ]; then rm -rf "$tmpdir" log "🚽 Deleted temporary working directory $tmpdir" fi } trap cleanup SIGINT SIGTERM ERR EXIT script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) [[ ! -x "$(command -v date)" ]] && echo "💥 date command not found." && exit 1 today=$(date +"%Y-%m-%d") function log() { echo >&2 -e "[$(date +"%Y-%m-%d %H:%M:%S")] ${1-}" } function die() { local msg=$1 local code=${2-1} # Bash parameter expansion - default exit status 1. See https://wiki.bash-hackers.org/syntax/pe#use_a_default_value log "$msg" exit "$code" } usage() { cat </dev/null if [ $? -ne 0 ]; then rm -f "${script_dir}/${ubuntu_gpg_key_id}.keyring~" die "👿 Verification of SHA256SUMS signature failed." fi rm -f "${script_dir}/${ubuntu_gpg_key_id}.keyring~" digest=$(sha256sum "${source_iso}" | cut -f1 -d ' ') set +e grep -Fq "$digest" "${script_dir}/SHA256SUMS-${today}" if [ $? -eq 0 ]; then log "👍 Verification succeeded." set -e else die "👿 Verification of ISO digest failed." fi else log "🤞 Skipping verification of source ISO." fi log "🔧 Extracting ISO image..." xorriso -osirrox on -indev "${source_iso}" -extract / "$tmpdir" &>/dev/null chmod -R u+w "$tmpdir" rm -rf "$tmpdir/"'[BOOT]' log "👍 Extracted to $tmpdir" log "🧩 Adding preseed parameters to kernel command line..." # These are for UEFI mode sed -i -e 's,file=/cdrom/preseed/ubuntu.seed maybe-ubiquity quiet splash,file=/cdrom/preseed/custom.seed auto=true priority=critical boot=casper automatic-ubiquity quiet splash noprompt noshell,g' "$tmpdir/boot/grub/grub.cfg" sed -i -e 's,file=/cdrom/preseed/ubuntu.seed maybe-ubiquity iso-scan/filename=${iso_path} quiet splash,file=/cdrom/preseed/custom.seed auto=true priority=critical boot=casper automatic-ubiquity quiet splash noprompt noshell,g' "$tmpdir/boot/grub/loopback.cfg" # This one is used for BIOS mode cat < "$tmpdir/isolinux/txt.cfg" default live-install label live-install menu label ^Install Ubuntu kernel /casper/vmlinuz append file=/cdrom/preseed/custom.seed auto=true priority=critical boot=casper automatic-ubiquity initrd=/casper/initrd quiet splash noprompt noshell --- EOF log "👍 Added parameters to UEFI and BIOS kernel command lines." log "🧩 Adding preseed configuration file..." cp "$preseed_file" "$tmpdir/preseed/custom.seed" log "👍 Added preseed file" log "👷 Updating $tmpdir/md5sum.txt with hashes of modified files..." # Using the full list of hashes causes long delays at boot. # For now, just include a couple of the files we changed. md5=$(md5sum "$tmpdir/boot/grub/grub.cfg" | cut -f1 -d ' ') echo "$md5 ./boot/grub/grub.cfg" > "$tmpdir/md5sum.txt" md5=$(md5sum "$tmpdir/boot/grub/loopback.cfg" | cut -f1 -d ' ') echo "$md5 ./boot/grub/loopback.cfg" >> "$tmpdir/md5sum.txt" log "👍 Updated hashes." log "📦 Repackaging extracted files into an ISO image..." cd "$tmpdir" xorriso -as mkisofs -r -V "ubuntu-preseed-$today" -J -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin -boot-info-table -input-charset utf-8 -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot -isohybrid-gpt-basdat -o "${destination_iso}" . &>/dev/null cd "$OLDPWD" log "👍 Repackaged into ${destination_iso}" die "✅ Completed." 0