Files

72 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
# vm-boot-iso.sh -- boot an install CD once under QEMU and keep the console.
#
# usage: vm-boot-iso.sh [-f bios|uefi] [-t seconds] [-i iso] [-o logfile]
#
# The CD this tree builds carries `console=com0' in its bootxx
# (src/distrib/amd64/... boot.cfg), so the loader and the
# kernel talk on the serial line;
# QEMU writes that line to the log file. Nothing is typed: this answers one
# question -- how far does the medium get on its own -- and the log is the
# evidence. The medium is attached read-only; the script never writes to it.
#
# Exit status: 0 the VM ran until the time limit, 1 usage or QEMU failure.
set -u
TOP=$(cd "$(dirname "$0")/.." && pwd)
# the version of the release, as newvers.sh reads it
VER=$(cat "$TOP/src/sys/conf/makronix-release")
ISO=$TOP/build/obj/releasedir/images/Makronix-$VER-amd64.iso
LOG=$TOP/build/logs/vm-boot-iso.log
FW=bios
SECS=90
MEM=2G
usage()
{
echo "usage: ${0##*/} [-f bios|uefi] [-t seconds] [-i iso] [-o logfile]" >&2
exit 1
}
while getopts f:t:i:o: ch; do
case $ch in
f) FW=$OPTARG ;;
t) SECS=$OPTARG ;;
i) ISO=$OPTARG ;;
o) LOG=$OPTARG ;;
*) usage ;;
esac
done
[ -r "$ISO" ] || { echo "${0##*/}: cannot read $ISO" >&2; exit 1; }
mkdir -p "$(dirname "$LOG")"
: > "$LOG"
case $FW in
bios) FWARGS= ;;
uefi)
CODE=/usr/share/OVMF/OVMF_CODE_4M.fd
[ -r "$CODE" ] || CODE=/usr/share/OVMF/OVMF_CODE.fd
[ -r "$CODE" ] || { echo "${0##*/}: no OVMF firmware found" >&2; exit 1; }
FWARGS="-drive if=pflash,format=raw,readonly=on,file=$CODE"
;;
*) usage ;;
esac
echo "${0##*/}: $FW, ${SECS}s, $ISO -> $LOG"
# $FWARGS is intentionally unquoted: it is empty or a fixed option list.
# shellcheck disable=SC2086
timeout "$SECS" qemu-system-x86_64 -m "$MEM" -display none -no-reboot \
$FWARGS \
-drive "file=$ISO,media=cdrom,readonly=on" -boot d \
-serial "file:$LOG" -monitor none
rc=$?
# timeout(1) reports 124 when the limit stopped the VM; that is the normal end.
[ $rc -eq 124 ] && rc=0
echo "${0##*/}: qemu finished, status $rc, $(wc -c < "$LOG") bytes of console"
exit $rc