Insert a kickstart file into a iso image

From Leo's Notes
Last edited on 18 February 2022, at 04:37.

You may wish to include a customized kickstart file tailored to your needs with the installer ISO. Here's a script that will take your custom kickstart file and stuff it into an existing ISO image. It will then adjust the boot options to include the inst.ks= option and rebuild the ISO.

This script should work for all Red Hat Enterprise Linux / CentOS / Rocky Linux / Alma Linux. It does not support UEFI.

# Creates a ISO image with a custom kickstart file
# Requires: genisoimage, syslinux, createrepo

if [ $# -lt 2 ]; then
	echo "Usage1: $0 source_iso kickstart"
	exit 1

if [ ! -f $1 ]; then
	echo "ISO image $1 does not exist!"
	exit 1

if [ ! -f $2 ]; then
	echo "Kickstart file $2 does not exist!"
	exit 1

OutputImage=$(basename "$SourceImage" | sed 's/.iso$/-ks.iso/')

echo "Source file: $SourceImage"
echo "Kickstart file: $KickstartFile"

if [ -d $WorkDir ] ; then
	echo "An existing work directory exists."
	echo "Delete it and start from scratch."
	exit 1

echo "Copying iso contents"
mkdir -p $WorkDir/source $WorkDir/output
mount -o loop $SourceImage $WorkDir/source
rsync -a $WorkDir/source/ $WorkDir/output/
umount $WorkDir/source

# Copy kickstart
cp -v $KickstartFile $WorkDir/output/ks.cfg

# Tweak the isolinux.cfg so default boot option uses the kickstart
sed 's/\(hd:LABEL[^ ]*\)/\1 inst.ks=\1:\/ks.cfg/' -i $WorkDir/output/isolinux/isolinux.cfg

Label=$(file $SourceImage | grep -ohE "'.*'" | tr -d \')

pushd .
cd $WorkDir/output
genisoimage \
	-V "$Label" \
	-A "$Label" \
	-o $WorkDir/$OutputImage \
	-joliet-long \
	-b isolinux/isolinux.bin \
	-c isolinux/ \
	-no-emul-boot \
	-boot-load-size 4 \
	-boot-info-table \
	-R -J -v -T \


echo "Output file: $WorkDir/$OutputImage"