RockyRpi/Rocky8_Rpi4_mkimage.sh

59 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
# Simple script that runs a Rocky Raspberry pi creation (via appliance-creator), then inserts a UUID to the kernel boot line of the image
# after-the-fact
#
# Usage: ./Rocky8_Rpi4_mkimage.sh /path/to/outputfolder/
#
#
# Needs to be run in the same directory as the rocky rpi kickstart, as it relies on it!
#
# Exit with error if we don't have an output directory:
OUTDIR=$1
if [[ -z "$OUTDIR" ]]; then
echo "Need to run this script with a path to output directory. Like: ${0} /path/to/output/"
exit 1
fi
mkdir -p "${OUTDIR}"
# Actually create the image. Our kickstart data should be in the same git repo as this script:
# (This takes a while, especially building on an rpi. Patience!)
appliance-creator -v -c ./Rocky8_Rpi4.ks -n RockyRpi --version=20210626 --release=1 --vmem=2048 --vcpu=2 --no-compress -o "${OUTDIR}"
# Post appliance-creator sequence to add UUID to the cmdline.txt file under /boot :
# (We don't want to rely on a /dev/ device name, what if a user wants to use a non-sdcard boot mechanism?)
mkdir -p /mnt/tmp
# find the image we just made, and make it available on /dev/maper/loop* devices:
image=$(find "${OUTDIR}" -iname '*.raw' | head -1)
echo "Getting UUID and inserting to boot from ${image} ...."
kpartx -av "${image}"
# Get the loop partition; it might be loop0p3, loop1p3, ...
looppart=$(kpartx -l "${image}" | awk '/p3/{print $1}')
# Get the UUID of our root partition (the ext4 one) (UUID=e3984938429 , strip out quotes("), and force upper case)
partuuid=$(blkid | grep "mapper/${looppart}" | head -1 | awk '{gsub(/\"/,"", $NF); print toupper($0)}')
# Mount the /boot partition (swap the 3rd partition for the first):
umount /mnt/tmp
mount /dev/mapper/${looppart/3/1} /mnt/tmp
# Swap out the "root=" part of cmdline.txt for our "root=UUID=blah"
sed -i "s/root= /root=${partuuid} /" /mnt/tmp/cmdline.txt
# Debug check to make sure it looks right:
echo "cmdline.txt looks like this, please review:"
cat /mnt/tmp/cmdline.txt
# Finished, unmount and clean loopbacks:
umount /mnt/tmp
kpartx -d "${image}"