#!/bin/bash ## FORSTWOOF UBUNTU PRESEED :: BUILD SCRIPT # Quit on first error set -e # Temporary directory for the build TMP="/var/tmp/ubuntu-build" # Cleanup function cleanup () { echo "Cleaning up" umount "${TMP}/files" || true umount "${TMP}/mnt" || true rm -r "${TMP}" } # Clean up on SIGINT trap "cleanup" INT # Get the script's running directory SCRDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # Directory from where script was started CURDIR="$(pwd)" # Preseed file SEED="${SCRDIR}/preseed.cfg" # Source and destination files SRC="${1}" DST="${2}" echo "Performing file presence checks" # Check if the source file exists [ -f "${SRC}" ] || { echo "The source file does not exist!" exit 1 } # Check if the source file is of an appropriate type TYPE=$(file "${SRC}" | tail -c 16) [ "${TYPE}" != "x86 boot sector" ] && { echo "The source file is not a valid bootable ISO file!" exit 3 } # Check if the preseed file exists [ -f "${SEED}" ] || { echo "The preseed file does not exist!" exit 2 } # Check if the destination file exists and offer to delete it [ -e "${DST}" ] && { echo -n "The destination file already exists! Do you want to remove it? [y/N] " read ANS ([ "${ANS}" == "y" ] || [ "${ANS}" == "Y" ]) || { echo "Build cancelled." exit 4 } rm -r "${DST}" } # Create the temporary directory echo "Creating a temporary directory" [ -e "${TMP}" ] && cleanup mkdir "${TMP}" # Mount the source ISO image echo "Mounting the source image" mkdir "${TMP}/mnt" mount -o ro "${SRC}" "${TMP}/mnt" # Create an overlay file system echo "Creating an overlay file system" mkdir "${TMP}/files" mkdir "${TMP}/overlay" mkdir "${TMP}/work" mount -t overlayfs -o lowerdir="${TMP}/mnt",upperdir="${TMP}/overlay",workdir="${TMP}/work" overlayfs "${TMP}/files" # Enable serial console for EFI boot entries echo "Enabling serial console for EFI boot entries (grub)" cat <"${TMP}/files/boot/grub/grub.cfg" serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1 terminal_input console serial terminal_output console serial menuentry "Install Ubuntu Server (automated)" { linux /install/vmlinuz file=/cdrom/preseed/ubuntu-server.seed text console=ttyS0,115200n8 --- initrd /install/initrd.gz } menuentry "Check disc for defects" { linux /install/vmlinuz MENU=/bin/cdrom-checker-menu text console=ttyS0,115200n8 --- initrd /install/initrd.gz } menuentry "Rescue a broken system" { linux /install/vmlinuz rescue/enable=true text console=ttyS0,115200n8 --- initrd /install/initrd.gz } EOF # Enable serial console for legacy boot entries echo "Enabling serial console for legacy boot entries (syslinux)" cat <"${TMP}/isolinux.cfg" serial 0 115200 0x003 EOF cat "${TMP}/files/isolinux/isolinux.cfg" >>"${TMP}/isolinux.cfg" sed -i 's/default vesamenu.c32//g' "${TMP}/isolinux.cfg" sed -i 's/ui gfxboot bootlogo//g' "${TMP}/isolinux.cfg" mv "${TMP}/isolinux.cfg" "${TMP}/files/isolinux/isolinux.cfg" sed -i 's/---/console=ttyS0,115200n8 ---/g' "${TMP}/files/isolinux/txt.cfg" # Rebuilding initrd echo "Rebuilding initrd for CD booting" mkdir "${TMP}/initrd" cd "${TMP}/initrd" gzip -d < "${TMP}/files/install/initrd.gz" | cpio --extract --make-directories --no-absolute-filenames --quiet rm "${TMP}/files/install/initrd.gz" cp "${SEED}" "${TMP}/initrd" find . | cpio -H newc --create --quiet | gzip -9 > "${TMP}/files/install/initrd.gz" echo "Rebuilding initrd for network booting" rm -r * ARCH=$(ls "${TMP}/files/install/netboot/ubuntu-installer") gzip -d < "${TMP}/files/install/netboot/ubuntu-installer/${ARCH}/initrd.gz" | cpio --extract --make-directories --no-absolute-filenames --quiet rm "${TMP}/files/install/netboot/ubuntu-installer/${ARCH}/initrd.gz" cp "${SEED}" "${TMP}/initrd" find . | cpio -H newc --create --quiet | gzip -9 > "${TMP}/files/install/netboot/ubuntu-installer/${ARCH}/initrd.gz" rm -r "${TMP}/initrd" # Recreate the md5sum.txt file echo "Calculating MD5 checksums for the image" cd "${TMP}/files" md5sum -b $(find -type f) > md5sum.txt OPTS="" [ -f "${TMP}/files/boot/grub/efi.img" ] && OPTS="-eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot" # Build the ISO image echo "Building the new ISO image" cd "${CURDIR}" mkisofs -r -V "UBUNTUSEED" \ -cache-inodes \ -J -l -b isolinux/isolinux.bin \ -c isolinux/boot.cat -no-emul-boot \ -boot-load-size 4 -boot-info-table \ ${OPTS} \ -input-charset utf-8 -quiet \ -o "${DST}" "${TMP}/files" # Clean up the mess cleanup