Automation of a local dev environment to learn, self-document, save time and avoid to reinvent the wheel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
debian-unattended/regeniso-sid.sh

236 lines
6.8 KiB

#!/bin/bash
set -eu
if [ "$#" -lt 1 ]; then
echo 'Need at least 1 parameter'
echo '-h for help'
exit 1
fi
if [ "$1" = "-h" ]; then
echo '<preseedFilePath> [dist] [arch] [mode] [boot]'
echo ''
echo 'Parameters:'
echo 'seed preseed file.'
echo 'img preferred image.'
echo 'dist preferred distribution.'
echo 'arch preferred architecture.'
echo 'mode whitch mode you want to automatically launch the installer.'
echo 'boot kernel boot options'
echo ''
echo 'List of supported values for each parameter and their defaults (*):'
echo 'seed must be a valid path of an existsing file.'
echo 'img netinst, netinst-mini*.'
echo 'dist sid, stable*, unstable, stretch, testing.'
echo 'arch amd64*, arm64, armhf, i386.'
echo 'mode none*, auto, expert.'
echo 'boot must be a set of valid kernel boot options'
exit 0
fi
if [ -z "$1" ]; then
echo 'Parameter 1 should not be empty or not set!'
echo 'Issue a valid path of a preseed file. Aborting...'
exit 1
else
PRESEED_PATH=$1
fi
if [ -z "$2" ]; then
IMAGE="netinst-mini"
fi
if [ -z "$3" ]; then
DIST="stable"
fi
if [ -z "$4" ]; then
ARCH="amd64"
fi
if [ -z "$5" ]; then
MODE="none"
fi
if [ -f "$PRESEED_PATH" ]
then
echo "Preseed file '${PRESEED_PATH}' found."
else
echo "Preseed file '${PRESEED_PATH}' not found. Aborting..."
exit 1
fi
SUPPORTED_IMAGES="netinst netinst-mini"
IMAGE=$2
if echo "${SUPPORTED_IMAGES}" | grep -w "${IMAGE}" > /dev/null; then
echo "Image '${IMAGE}' supported"
else
echo "Image '${IMAGE}' not supported! Aborting..."
exit 1
fi
SUPPORTED_DIST="sid stable unstable stretch testing"
DIST=$3
if echo "${SUPPORTED_DIST}" | grep -w "${DIST}" > /dev/null; then
echo "Dist '${DIST}' supported"
else
echo "Dist '${DIST}' not supported! Aborting..."
exit 1
fi
SUPPORTED_ARCH="amd64 armhf arm64"
ARCH=$4
if echo "${SUPPORTED_ARCH}" | grep -w "${ARCH}" > /dev/null; then
echo "Architecture '${ARCH}' supported"
else
echo "Architecture '${ARCH}' not supported! Aborting..."
exit 1
fi
MIRROR="http://debian.mirror.garr.it"
if [ "$IMAGE" == "netinst-mini" ]; then
IMG_URL="${MIRROR}/debian/dists/${DIST}/main/installer-${ARCH}/current/images/netboot/mini.iso"
else
if [ "$IMAGE" == "netinst" ]; then
IMG_URL="${MIRROR}/debian-cd/current/${ARCH}/iso-cd/debian-11.1.0-${ARCH}-${IMAGE}.iso"
else
echo "An error occurred in selecting the proper image. Aborting..."
exit 1
fi
fi
FILE_ISO="debian-${DIST}-${ARCH}-${IMAGE}.iso"
if [ -f "${FILE_ISO}" ]
then
echo "${FILE_ISO} found."
else
echo "${FILE_ISO} not found."
echo "Downloading..."
wget -O "${FILE_ISO}" "${IMG_URL}"
fi
SUPPORTED_MODE="none auto expert"
MODE=$5
if echo "${SUPPORTED_MODE}" | grep -w "${MODE}" > /dev/null; then
echo "Mode '${MODE}' supported"
else
echo "Mode '${MODE}' not supported! Aborting..."
exit 1
fi
echo "Extracting..."
7z x -oisofiles "${FILE_ISO}"
# Adding a Preseed File to the Initrd:
# You now have the directory isofiles with all the ISO's files in it. Make
# initrd.gz writable by the user, uncompress it and append a preseed file to
# the initrd. Recompress the initrd and return initrd.gz to its original
# read-only state.
#wget -O isofiles/linux https://d-i.debian.org/daily-images/amd64/daily/netboot/debian-installer/amd64/linux
#wget -O isofiles/initrd.gz https://d-i.debian.org/daily-images/amd64/daily/netboot/debian-installer/amd64/initrd.gz
cp "$PRESEED_PATH" preseed.cfg
if [ "$IMAGE" == "netinst-mini" ]; then
ISOLINUX_PATH=""
echo "Patching initrd.gz..."
gunzip isofiles/initrd.gz
echo preseed.cfg | cpio -H newc -o -A -F isofiles/initrd
gzip isofiles/initrd
# Boot Parameters
echo "Patching boot parameters..."
if [ "$MODE" != "none" ]; then
sed -i "/default/c\default $5" isofiles/isolinux.cfg
sed -i '/prompt/c\prompt 1' isofiles/isolinux.cfg
sed -i '/timeout/c\timeout 50' isofiles/isolinux.cfg
fi
sed -i "/append priority=low/c\ append priority=low vga=788 initrd=initrd.gz $5 --- " isofiles/adtxt.cfg
sed -i "/append auto=true/c\ append auto=true priority=critical vga=788 initrd=initrd.gz $5 --- quiet " isofiles/adtxt.cfg
else
if [ "$IMAGE" == "netinst" ]; then
ISOLINUX_PATH="isolinux"
if [ "$ARCH" == "amd64" ]; then
BUILD_ARCH="amd"
else
if [ "$ARCH" == "i386" ]; then
BUILD_ARCH="386"
else
echo "Unsupported arch in the building process. Aborting..."
exit 1
fi
fi
echo "Patching initrd.gz..."
gunzip isofiles/install.${BUILD_ARCH}/initrd.gz
echo preseed.cfg | cpio -H newc -o -A -F isofiles/install.${BUILD_ARCH}/initrd
gzip isofiles/install.${BUILD_ARCH}/initrd
# Boot Parameters
echo "Patching boot parameters..."
if [ "$MODE" != "none" ]; then
sed -i "/default/c\default $5" isofiles/isolinux/isolinux.cfg
sed -i '/prompt/c\prompt 1' isofiles/isolinux/isolinux.cfg
sed -i '/timeout/c\timeout 50' isofiles/isolinux/isolinux.cfg
fi
sed -i "/append priority=low/c\ append priority=low vga=788 initrd=/install.${BUILD_ARCH}/initrd.gz $5 --- " isofiles/isolinux/adtxt.cfg
sed -i "/append auto=true/c\ append auto=true priority=critical vga=788 initrd=/install.${BUILD_ARCH}/initrd.gz $5 --- quiet " isofiles/isolinux/adtxt.cfg
else
echo "An error occurred in selecting the proper image for build process. Aborting..."
exit 1
fi
fi
# Import custom files into the ISO
cp -ar custom isofiles/custom || echo "[WARNING] Cannot import 'custom' folder."
# Creating a New Bootable ISO Image:
# The following instructions suffice for i386 and amd64 in legacy BIOS mode.
# The example names get mapped to their roles here
orig_iso="$FILE_ISO"
new_files=isofiles
new_iso="preseed-${FILE_ISO}"
mbr_template=isohdpfx.bin
# Extract MBR template file to disk
dd if="$orig_iso" bs=1 count=432 of="$mbr_template"
# Create the new ISO image
# https://wiki.debian.org/RepackBootableISO#What_is_a_bootable_ISO_9660_image_.3F
echo "Generating patched iso image..."
xorriso -as mkisofs \
-r -V "Debian $ARCH n" \
-o "$new_iso" \
-J -J -joliet-long -cache-inodes \
-isohybrid-mbr "$mbr_template" \
-b "${ISOLINUX_PATH}/isolinux.bin" \
-c boot.cat \
-boot-load-size 4 -boot-info-table -no-emul-boot \
-eltorito-alt-boot \
-e boot/grub/efi.img \
-no-emul-boot -isohybrid-gpt-basdat -isohybrid-apm-hfsplus \
"$new_files"
#echo "Generating patched iso image..."
#genisoimage -r -J -b isolinux.bin -c boot.cat \
# -no-emul-boot -boot-load-size 4 -boot-info-table \
# -o "preseed-${FILE_ISO}" isofiles
echo "Cleaning temporary files..."
rm -rf isofiles/
rm preseed.cfg
rm isohdpfx.bin
echo "Done."
exit 0