sbc-images/create-image.sh
2023-10-17 13:18:09 +05:30

92 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -x
KICKSTART_BASE="Rocky"
TODAY="$(TZ='UTC' date +%Y.%m.%d)"
KICKSTART_FILE="${KICKSTART_BASE}.ks"
APPLIANCE_NAME="${KICKSTART_BASE}-$(dnf config-manager --dump-variables | grep releasever | awk '{print $3}')-$(uname -m)-minimal-${TODAY}"
IMAGE_NAME="${APPLIANCE_NAME}.raw"
COMMANDS_TO_CHECK=('appliance-creator' 'shasum' 'zstd')
for COMMAND in "${COMMANDS_TO_CHECK[@]}"; do
if ! command -v "${COMMAND}" > /dev/null; then
>&2 echo "$0: ERROR: unable to find command '${COMMAND}' in PATH"
exit 1
fi
done
function create_new_image {
UBOOT_MAX_SIZE='32'
IMAGE_SIZE=$(( $(stat -c '%s' "${IMAGE_NAME}") / 1024 / 1024 ))
NEW_IMAGE_NAME="${IMAGE_NAME}.new"
truncate -s "$(( IMAGE_SIZE + (UBOOT_MAX_SIZE * 2) ))"MiB "${NEW_IMAGE_NAME}"
LOOPBACK_01=$(losetup -f -P --show "${IMAGE_NAME}")
LOOPBACK_02=$(losetup -f -P --show "${NEW_IMAGE_NAME}")
UEFI_SECTORS="$(fdisk -l "${LOOPBACK_01}" | grep "${LOOPBACK_01}p1" | awk '{print $4}')"
BOOT_SECTORS="$(fdisk -l "${LOOPBACK_01}" | grep "${LOOPBACK_01}p2" | awk '{print $4}')"
cat << EOF | fdisk "${LOOPBACK_02}"
g
n
1
65536
+${UEFI_SECTORS}
n
2
+${BOOT_SECTORS}
n
3
w
EOF
sync; sync; sync; sync;
sleep 10
hdparm -z "${LOOPBACK_02}"
dd bs=4M conv=sync status=progress if="${LOOPBACK_01}p3" of="${LOOPBACK_02}p3"
dd bs=4M conv=sync status=progress if="${LOOPBACK_01}p2" of="${LOOPBACK_02}p2"
dd bs=4M conv=sync status=progress if="${LOOPBACK_01}p1" of="${LOOPBACK_02}p1"
sync; sync; sync; sync;
sleep 10
losetup -d "${LOOPBACK_02}"
losetup -d "${LOOPBACK_01}"
fdisk -l "${IMAGE_NAME}"
fdisk -l "${NEW_IMAGE_NAME}"
mv "${NEW_IMAGE_NAME}" "${IMAGE_NAME}"
sync; sync; sync; sync;
sleep 10
}
appliance-creator \
--config "${KICKSTART_FILE}" \
--name "${APPLIANCE_NAME}" \
--format raw \
--outdir "${PWD}" \
--no-compress \
--debug \
--cache /root/cache \
--verbose 2>&1 | tee "${APPLIANCE_NAME}.log" || exit 1
if [[ -d "${APPLIANCE_NAME}" ]]; then
pushd "${APPLIANCE_NAME}" || exit 1
mv "../${APPLIANCE_NAME}.log" .
mv "${APPLIANCE_NAME}-sda.raw" "${IMAGE_NAME}"
create_new_image
zstd --compress -9 "${IMAGE_NAME}"
sha512sum -- *.raw* > SHA512SUMS
sha256sum -- *.raw* > SHA256SUMS
popd || exit 0
else
>&2 echo "$0: ERROR: unable to find the appliance output directory"
>&2 echo "$0: ${APPLIANCE_NAME}"
exit 1
fi
if [[ -n "$1" ]]; then
chown "$1":"$1" -vR "${APPLIANCE_NAME}"
fi