Files
lotherkandClaude Sonnet 5.5 73c9de51a6 licence: the CNCL line in every file Makronix wrote (tools, docs, www, mobile, top level)
consmirror.c: the BSD notice is replaced (Makronix's own file).  Files with
changes of other sessions pending are left for their commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:50:26 +00:00

245 lines
8.2 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
# vmsh.sh -- drive a QEMU machine through its serial console, from sh.
#
# usage: vmsh.sh start [-i iso | -I] [-U stickimage] [-d disk] [-s size] [-f bios|uefi] [-m mem] [-c cpus] [-p sshport] [-N] [-h dir]
# -I is the install CD this tree built
# vmsh.sh send 'printf-format' keys to the console; \r \033 \344 ok
# vmsh.sh wait REGEX [seconds] output since the last send matches
# vmsh.sh show [lines] the console, escape codes removed
# vmsh.sh since the console since the last send
# vmsh.sh log path of the raw console log
# vmsh.sh cd ISO change the CD of the running machine
# vmsh.sh stop
#
# The console is a pair of named pipes (qemu -serial pipe:), the way an expect
# script would hold a pty, but with nothing but sh, mkfifo, grep and sed.
# `send' remembers where the console log ended; `wait' only looks at what came
# after that, so it waits for the answer to the keys, not for old output.
#
# The VM is only ever started by an explicit `start' and stopped by `stop'; no
# timer, no loop outside the call. Everything lives in build/vmsession/.
#
# `start' without -i boots the disk only (the second boot of an installation);
# with -i the CD is the first boot device, the disk the second. A disk that
# does not exist is created sparse (qcow2, default 20G).
set -u
TOP=${TOP:-$(cd "$(dirname "$0")/.." && pwd)}
# the version of the release, as newvers.sh reads it
VER=$(cat "$TOP/src/sys/conf/makronix-release")
D=${VMDIR:-$TOP/build/vmsession}
LOG=$D/console.log
prog=${0##*/}
ESC=$(printf '\033')
die()
{
echo "$prog: $*" >&2
exit 1
}
# The console without cursor movement and colour codes, CR as line end.
plain()
{
sed -e "s/${ESC}\[[0-9;?]*[A-Za-z]//g" \
-e "s/${ESC}[()][0-9A-B]//g" \
-e "s/${ESC}[=>78M]//g" \
-e 's/\r/\n/g' | tr -d '\000'
}
running()
{
[ -f "$D/qemu.pid" ] && kill -0 "$(cat "$D/qemu.pid")" 2>/dev/null
}
cmd_start()
{
iso= usb= disk=$D/disk.qcow2 size=20G fw=bios mem=2G cpus= sshport= nonet=no share=
while getopts i:IU:d:s:f:m:c:p:Nh: ch; do
case $ch in
i) iso=$OPTARG ;;
I) iso=$TOP/build/obj/releasedir/images/Makronix-$VER-amd64.iso ;;
U) usb=$OPTARG ;;
d) disk=$OPTARG ;;
s) size=$OPTARG ;;
f) fw=$OPTARG ;;
m) mem=$OPTARG ;;
c) cpus=$OPTARG ;;
p) sshport=$OPTARG ;;
N) nonet=yes ;;
h) share=$OPTARG ;;
*) die "usage: $prog start [-i iso | -I] [-d disk] [-s size] [-f bios|uefi] [-m mem] [-c cpus] [-p sshport] [-N] [-h dir]" ;;
esac
done
running && die "a VM is already running (pid $(cat "$D/qemu.pid")); stop it first"
mkdir -p "$D"
rm -f "$D/serial.in" "$D/serial.out" "$D/mark"
mkfifo "$D/serial.in" "$D/serial.out" || die "mkfifo failed"
: > "$LOG"
[ -f "$disk" ] || qemu-img create -q -f qcow2 "$disk" "$size" || die "qemu-img failed"
# The monitor is a socket, for `cd' (changing the CD of the running VM).
rm -f "$D/mon"
echo "$fw" > "$D/fw"
if [ "$fw" = uefi ] || [ "${VMSH_AHCI:-}" = yes ]; then echo ahci; else echo ide; fi > "$D/bus"
set -- -m "$mem" -display none -no-reboot \
-monitor "unix:$D/mon,server,nowait" \
-serial "pipe:$D/serial"
[ -n "$cpus" ] && set -- "$@" -smp "$cpus"
# the host reaches the guest's sshd on localhost:PORT (user-mode network)
[ -n "$sshport" ] && [ $nonet = no ] && set -- "$@" -nic "user,model=e1000,hostfwd=tcp:127.0.0.1:$sshport-:22"
# -h DIR: the host directory DIR is a virtio-9p share, mount_tag `host'; the
# guest mounts it with `modload vio9p; mount_9p -c /dev/vio9p0 /MOUNTPOINT'
[ -n "$share" ] && set -- "$@" -virtfs "local,id=host9p,path=$share,mount_tag=host,security_model=none"
# -N: no network card at all (the offline proof: nothing can leak in or out)
[ $nonet = yes ] && set -- "$@" -nic none
# The disks: IDE for the BIOS; with UEFI they are on AHCI, because the
# OVMF IDE driver reads a CD or a disk so slowly that the boot loader
# needs many minutes for the kernel (measured 2026-11-06).
# VMSH_AHCI=yes: the same for the BIOS (QEMU 11.1.2 lost interrupts of the
# emulated PIIX IDE under load: "bus-master DMA error: missing interrupt",
# the guest froze twice, 2026-11-07)
if [ "$fw" = uefi ] || [ "${VMSH_AHCI:-}" = yes ]; then
set -- "$@" -device ich9-ahci,id=ahci \
-drive "file=$disk,if=none,id=hd0,format=qcow2" \
-device ide-hd,drive=hd0,bus=ahci.0
else
set -- "$@" -drive "file=$disk,if=ide,index=0,format=qcow2"
fi
# Hardware acceleration where the host allows it; otherwise plain emulation.
[ -w /dev/kvm ] && set -- "$@" -enable-kvm -cpu host
case $fw in
bios) ;;
uefi)
code=/usr/share/OVMF/OVMF_CODE_4M.fd
[ -r "$code" ] || code=/usr/share/OVMF/OVMF_CODE.fd
[ -r "$code" ] || die "no OVMF firmware found"
vars=$D/OVMF_VARS.fd
[ -f "$vars" ] || {
src=/usr/share/OVMF/OVMF_VARS_4M.fd
[ -r "$src" ] || src=/usr/share/OVMF/OVMF_VARS.fd
cp "$src" "$vars" || die "no OVMF variable store found"
}
set -- "$@" \
-drive "if=pflash,format=raw,readonly=on,file=$code" \
-drive "if=pflash,format=raw,file=$vars" ;;
*) die "firmware: bios or uefi" ;;
esac
if [ -n "$iso" ]; then
[ -r "$iso" ] || die "cannot read $iso"
if [ "$fw" = uefi ] || [ "${VMSH_AHCI:-}" = yes ]; then
set -- "$@" \
-drive "file=$iso,if=none,id=cd0,media=cdrom,readonly=on" \
-device ide-cd,drive=cd0,bus=ahci.1 -boot order=dc
else
set -- "$@" -drive "file=$iso,media=cdrom,readonly=on,if=ide,index=2" -boot order=dc
fi
fi
# -U: a raw install image as a USB stick (xhci, usb-storage), the first thing
# to boot. snapshot=on: what the installer writes to the stick is thrown away,
# the image stays as built.
if [ -n "$usb" ]; then
[ -r "$usb" ] || die "cannot read $usb"
set -- "$@" -device qemu-xhci,id=xhci \
-drive "file=$usb,if=none,id=usb0,format=raw,snapshot=on" \
-device usb-storage,drive=usb0,bootindex=0
fi
# The reader must exist before qemu writes, or the pipe blocks it.
cat "$D/serial.out" >> "$LOG" &
echo $! > "$D/cat.pid"
qemu-system-x86_64 "$@" > "$D/qemu.out" 2>&1 &
echo $! > "$D/qemu.pid"
sleep 1
running || { cat "$D/qemu.out" >&2; die "qemu did not start"; }
echo "$prog: started ($fw, ${mem}, disk $disk${iso:+, cd $iso}${usb:+, usb $usb}), console $LOG"
}
cmd_send()
{
[ $# -eq 1 ] || die "usage: $prog send 'printf-format'"
running || die "no VM running"
wc -c < "$LOG" | tr -d ' ' > "$D/mark"
# shellcheck disable=SC2059
printf "$1" > "$D/serial.in"
}
cmd_wait()
{
[ $# -ge 1 ] || die "usage: $prog wait REGEX [seconds]"
re=$1
secs=${2:-30}
mark=0
[ -f "$D/mark" ] && mark=$(cat "$D/mark")
i=0
while [ $i -lt "$secs" ]; do
if tail -c "+$((mark + 1))" "$LOG" | plain | grep -a -E -q -e "$re"; then
echo "$prog: matched '$re' after ${i}s"
return 0
fi
running || die "the VM has stopped while waiting for '$re'"
sleep 1
i=$((i + 1))
done
echo "$prog: timeout after ${secs}s waiting for '$re'" >&2
return 1
}
# What the console printed since the last `send', escape codes removed.
cmd_since()
{
mark=0
[ -f "$D/mark" ] && mark=$(cat "$D/mark")
tail -c "+$((mark + 1))" "$LOG" | plain
}
cmd_show()
{
n=${1:-40}
plain < "$LOG" | sed -e '/^[[:space:]]*$/d' | tail -n "$n"
}
cmd_cd()
{
[ $# -eq 1 ] || die "usage: $prog cd ISO"
running || die "no VM running"
[ -r "$1" ] || die "cannot read $1"
dev=ide1-cd0
# the CD sits on AHCI (UEFI, or VMSH_AHCI=yes at start) or on IDE
[ "$(cat "$D/bus" 2>/dev/null)" = ahci ] && dev=cd0
{ echo "eject -f $dev"; sleep 1; echo "change $dev $1"; sleep 1; } \
| socat - "UNIX-CONNECT:$D/mon" > /dev/null
echo "$prog: CD changed to ${1##*/}"
}
cmd_stop()
{
if [ -f "$D/qemu.pid" ] && running; then
kill "$(cat "$D/qemu.pid")" 2>/dev/null
sleep 1
fi
[ -f "$D/cat.pid" ] && kill "$(cat "$D/cat.pid")" 2>/dev/null
rm -f "$D/qemu.pid" "$D/cat.pid" "$D/serial.in" "$D/serial.out"
echo "$prog: stopped"
}
[ $# -ge 1 ] || die "usage: $prog start|send|wait|show|log|cd|stop"
cmd=$1
shift
case $cmd in
start) cmd_start "$@" ;;
send) cmd_send "$@" ;;
cd) cmd_cd "$@" ;;
wait) cmd_wait "$@" ;;
show) cmd_show "$@" ;;
since) cmd_since ;;
log) echo "$LOG" ;;
stop) cmd_stop ;;
*) die "unknown command $cmd" ;;
esac