#!/bin/bash # Taken from https://github.com/covertsh/ubuntu-autoinstall-generator # modified Aug 23, 2024 # # MIT License # # Copyright (c) 2020 covertsh # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. 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-${sha_suffix}" 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" if [ ${use_hwe_kernel} -eq 1 ]; then if grep -q "hwe-vmlinuz" "$tmpdir/boot/grub/grub.cfg"; then log "☑️ Destination ISO will use HWE kernel." sed -i -e 's|/casper/vmlinuz|/casper/hwe-vmlinuz|g' "$tmpdir/boot/grub/grub.cfg" sed -i -e 's|/casper/initrd|/casper/hwe-initrd|g' "$tmpdir/boot/grub/grub.cfg" sed -i -e 's|/casper/vmlinuz|/casper/hwe-vmlinuz|g' "$tmpdir/boot/grub/loopback.cfg" sed -i -e 's|/casper/initrd|/casper/hwe-initrd|g' "$tmpdir/boot/grub/loopback.cfg" else log "⚠️ This source ISO does not support the HWE kernel. Proceeding with the regular kernel." fi fi log "🧩 Adding autoinstall parameter to kernel command line..." sed -i -e 's/---/ autoinstall ---/g' "$tmpdir/boot/grub/grub.cfg" sed -i -e 's/---/ autoinstall ---/g' "$tmpdir/boot/grub/loopback.cfg" log "👍 Added parameter to UEFI and BIOS kernel command lines." if [ ${all_in_one} -eq 1 ]; then log "🧩 Adding user-data and meta-data files..." mkdir "$tmpdir/nocloud" cp "$user_data_file" "$tmpdir/nocloud/user-data" if [ -n "${meta_data_file}" ]; then cp "$meta_data_file" "$tmpdir/nocloud/meta-data" else touch "$tmpdir/nocloud/meta-data" fi sed -i -e 's,timeout=30,timeout=1,g' "$tmpdir/boot/grub/grub.cfg" sed -i -e 's,---, ds=nocloud\\\;s=/cdrom/nocloud/ ---,g' "$tmpdir/boot/grub/grub.cfg" sed -i -e 's,---, ds=nocloud\\\;s=/cdrom/nocloud/ ---,g' "$tmpdir/boot/grub/loopback.cfg" log "👍 Added data and configured kernel command line." fi if [ ${md5_checksum} -eq 1 ]; then log "👷 Updating $tmpdir/md5sum.txt with hashes of modified files..." md5=$(md5sum "$tmpdir/boot/grub/grub.cfg" | cut -f1 -d ' ') sed -i -e 's,^.*[[:space:]] ./boot/grub/grub.cfg,'"$md5"' ./boot/grub/grub.cfg,' "$tmpdir/md5sum.txt" md5=$(md5sum "$tmpdir/boot/grub/loopback.cfg" | cut -f1 -d ' ') sed -i -e 's,^.*[[:space:]] ./boot/grub/loopback.cfg,'"$md5"' ./boot/grub/loopback.cfg,' "$tmpdir/md5sum.txt" log "👍 Updated hashes." else log "🗑️ Clearing MD5 hashes..." echo > "$tmpdir/md5sum.txt" log "👍 Cleared hashes." fi log "📦 Repackaging extracted files into an ISO image..." cd "$tmpdir" xorriso -as mkisofs -r -V 'Ubuntu-Server 24.04.1 LTS amd64' --modification-date='2024083109475700' --grub2-mbr --interval:local_fs:0s-15s:zero_mbrpt,zero_gpt:'/home/dtookey/build/ubuntu-original-2024-09-02.iso' --protective-msdos-label -partition_cyl_align off -partition_offset 16 --mbr-force-bootable -append_partition 2 28732ac11ff8d211ba4b00a0c93ec93b --interval:local_fs:5577512d-5587655d::'/home/dtookey/build/ubuntu-original-2024-09-02.iso' -appended_part_as_gpt -iso_mbr_part_type a2a0d0ebe5b9334487c068b6b72699c7 -c '/boot.catalog' -b '/boot/grub/i386-pc/eltorito.img' -no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info -eltorito-alt-boot -e '--interval:appended_partition_2_start_1394378s_size_10144d:all::' -no-emul-boot -boot-load-size 10144 -o "${destination_iso}" . #&>/dev/null cd "$OLDPWD" log "👍 Repackaged into ${destination_iso}" die "✅ Completed." 0