2023-09-13 01:29:56 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -x
|
|
|
|
KICKSTART_BASE="Rocky"
|
2023-10-10 05:36:09 +00:00
|
|
|
TODAY="$(TZ='UTC' date +%Y.%m.%d)"
|
2023-10-16 02:38:05 +00:00
|
|
|
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"
|
2023-11-20 11:31:37 +00:00
|
|
|
COMMANDS_TO_CHECK=('appliance-creator' 'hdparm' 'shasum' 'zstd')
|
2023-10-16 02:38:05 +00:00
|
|
|
|
|
|
|
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"
|
2023-09-13 01:29:56 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2023-10-16 03:51:42 +00:00
|
|
|
function create_new_image {
|
|
|
|
UBOOT_MAX_SIZE='32'
|
|
|
|
IMAGE_SIZE=$(( $(stat -c '%s' "${IMAGE_NAME}") / 1024 / 1024 ))
|
|
|
|
NEW_IMAGE_NAME="${IMAGE_NAME}.new"
|
2023-10-17 03:46:01 +00:00
|
|
|
truncate -s "$(( IMAGE_SIZE + (UBOOT_MAX_SIZE * 2) ))"MiB "${NEW_IMAGE_NAME}"
|
2023-10-16 03:51:42 +00:00
|
|
|
|
|
|
|
LOOPBACK_01=$(losetup -f -P --show "${IMAGE_NAME}")
|
|
|
|
LOOPBACK_02=$(losetup -f -P --show "${NEW_IMAGE_NAME}")
|
2023-11-20 12:10:28 +00:00
|
|
|
# 1 MiB = 2048 * (1 block/sector (512 bytes))
|
|
|
|
UEFI_SECTORS="$(( $(fdisk -l "${LOOPBACK_01}" | grep "${LOOPBACK_01}p1" | awk '{print $4}') + 2048 ))"
|
|
|
|
BOOT_SECTORS="$(( $(fdisk -l "${LOOPBACK_01}" | grep "${LOOPBACK_01}p2" | awk '{print $4}') + 2048 ))"
|
2023-10-16 03:51:42 +00:00
|
|
|
|
2023-10-17 04:01:43 +00:00
|
|
|
cat << EOF | fdisk "${LOOPBACK_02}"
|
|
|
|
g
|
|
|
|
n
|
|
|
|
1
|
|
|
|
65536
|
|
|
|
+${UEFI_SECTORS}
|
|
|
|
n
|
|
|
|
2
|
|
|
|
|
|
|
|
+${BOOT_SECTORS}
|
|
|
|
n
|
|
|
|
3
|
|
|
|
|
|
|
|
|
|
|
|
w
|
|
|
|
EOF
|
2023-10-16 03:51:42 +00:00
|
|
|
sync; sync; sync; sync;
|
|
|
|
sleep 10
|
|
|
|
hdparm -z "${LOOPBACK_02}"
|
|
|
|
|
2023-10-17 03:46:01 +00:00
|
|
|
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"
|
2023-10-16 03:51:42 +00:00
|
|
|
sync; sync; sync; sync;
|
|
|
|
sleep 10
|
|
|
|
|
|
|
|
losetup -d "${LOOPBACK_02}"
|
|
|
|
losetup -d "${LOOPBACK_01}"
|
2023-10-17 04:01:43 +00:00
|
|
|
fdisk -l "${IMAGE_NAME}"
|
2023-10-17 07:48:09 +00:00
|
|
|
fdisk -l "${NEW_IMAGE_NAME}"
|
2023-10-16 03:51:42 +00:00
|
|
|
mv "${NEW_IMAGE_NAME}" "${IMAGE_NAME}"
|
|
|
|
sync; sync; sync; sync;
|
|
|
|
sleep 10
|
|
|
|
}
|
|
|
|
|
2023-11-19 12:56:42 +00:00
|
|
|
script -q -c "appliance-creator \
|
|
|
|
--config ${KICKSTART_FILE} \
|
|
|
|
--name ${APPLIANCE_NAME} \
|
2023-10-16 02:38:05 +00:00
|
|
|
--format raw \
|
2023-11-19 12:56:42 +00:00
|
|
|
--outdir ${PWD} \
|
2023-10-16 02:38:05 +00:00
|
|
|
--no-compress \
|
|
|
|
--debug \
|
|
|
|
--cache /root/cache \
|
2023-11-19 12:56:42 +00:00
|
|
|
--verbose" | tee "${APPLIANCE_NAME}.log" || exit 1
|
2023-11-20 10:45:14 +00:00
|
|
|
# the script command creates a file called typescript
|
|
|
|
rm -vf typescript
|
2023-10-16 02:38:05 +00:00
|
|
|
|
|
|
|
if [[ -d "${APPLIANCE_NAME}" ]]; then
|
|
|
|
pushd "${APPLIANCE_NAME}" || exit 1
|
2023-10-16 05:14:31 +00:00
|
|
|
mv "../${APPLIANCE_NAME}.log" .
|
2023-10-16 02:38:05 +00:00
|
|
|
mv "${APPLIANCE_NAME}-sda.raw" "${IMAGE_NAME}"
|
2023-10-16 03:51:42 +00:00
|
|
|
create_new_image
|
|
|
|
zstd --compress -9 "${IMAGE_NAME}"
|
|
|
|
sha512sum -- *.raw* > SHA512SUMS
|
|
|
|
sha256sum -- *.raw* > SHA256SUMS
|
2023-10-16 02:38:05 +00:00
|
|
|
popd || exit 0
|
|
|
|
else
|
|
|
|
>&2 echo "$0: ERROR: unable to find the appliance output directory"
|
|
|
|
>&2 echo "$0: ${APPLIANCE_NAME}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2023-10-09 03:36:15 +00:00
|
|
|
|
|
|
|
if [[ -n "$1" ]]; then
|
2023-10-16 02:38:05 +00:00
|
|
|
chown "$1":"$1" -vR "${APPLIANCE_NAME}"
|
2023-10-09 03:36:15 +00:00
|
|
|
fi
|