From 4ea992c71dd0cdcffe0f74216e748256bd700a31 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 1 Aug 2026 01:56:12 +0200 Subject: scripts: automate the data-disk workflow (mkdisk / sendkeys / test-disk) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - scripts/mkdisk.sh: create the FAT32 disk with mkfs.fat -C (the flag that actually creates the file — plain mkfs.fat fails on a missing path), accept M/G/K sizes, seed seed/ files via mtools; --fresh re-formats, --no-seed skips seeding - scripts/sendkeys.py: type lines into the guest via the QEMU monitor (US layout). Punctuation must use QEMU's named keys (minus, slash, dot, ...) — bare '-', '.', '/' are not valid sendkey names, and '-' is the modifier separator, so they were silently dropped before - scripts/test-disk.sh: full automated round trip — build, deploy, ensure image, boot headless QEMU, drive the shell (ls /disk, mkdir, echo >, cat, poweroff), then verify with fsck.fat (must be clean) and mtype (file content must have survived poweroff) - Makefile: make disk / disk-fresh / inspect / test-disk; run, run-gui and debug now auto-create a missing DISK_IMG; fix the stale comment (mkfs.fat without -C) - scripts/boot.sh: auto-create a missing DISK_IMG via mkdisk.sh - seed/: motd.txt + lines.txt seeded onto fresh images - Verified end to end: ./scripts/test-disk.sh --fresh passes --- scripts/boot.sh | 5 +++ scripts/mkdisk.sh | 85 +++++++++++++++++++++++++++++++++++++++ scripts/sendkeys.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++ scripts/test-disk.sh | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 304 insertions(+) create mode 100755 scripts/mkdisk.sh create mode 100755 scripts/sendkeys.py create mode 100755 scripts/test-disk.sh (limited to 'scripts') diff --git a/scripts/boot.sh b/scripts/boot.sh index 26064f9..99fd103 100755 --- a/scripts/boot.sh +++ b/scripts/boot.sh @@ -95,6 +95,11 @@ echo " gui: $([ -n "$GUI" ] && echo yes || echo no)" echo " gdb: $([ -n "$DEBUG" ] && echo ':1234' || echo off)" echo "" +if [ ! -f "$DISK_IMG" ]; then + echo " (creating missing disk image: $DISK_IMG)" + "$SCRIPT_DIR/mkdisk.sh" "$DISK_IMG" +fi + if [ -f "$DISK_IMG" ]; then DISK_ARGS=( -drive "file=$DISK_IMG,format=raw,if=none,id=disk" diff --git a/scripts/mkdisk.sh b/scripts/mkdisk.sh new file mode 100755 index 0000000..f9a8c09 --- /dev/null +++ b/scripts/mkdisk.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +# +# scripts/mkdisk.sh — create (or re-create) the FAT32 data disk that the +# kernel mounts at /disk. +# +# Usage: +# ./scripts/mkdisk.sh # create /tmp/disk.img (64M) if missing +# ./scripts/mkdisk.sh --fresh # re-format (wipes the image!) +# ./scripts/mkdisk.sh --size 128M # different size (default 64M) +# ./scripts/mkdisk.sh /path/img # custom image path +# ./scripts/mkdisk.sh --no-seed # skip the seed/ directory +# +# The image is created with `mkfs.fat -C`, which is the option that +# actually creates the file — plain `mkfs.fat` only writes into an +# existing one. Files from seed/ (if present) are copied in with mtools. +# +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PROJECT_DIR="$(dirname "$SCRIPT_DIR")" + +IMG="/tmp/disk.img" +SIZE="64M" +FRESH=0 +SEED_DIR="$PROJECT_DIR/seed" + +while [ $# -gt 0 ]; do + case "$1" in + --fresh) FRESH=1 ;; + --size) + shift + SIZE="$1" + ;; + --no-seed) SEED_DIR="" ;; + --help | -h) + head -20 "$0" | grep "^#" | sed 's/^# \?//' + exit 0 + ;; + *) + IMG="$1" + ;; + esac + shift +done + +if [ -f "$IMG" ] && [ "$FRESH" -eq 0 ]; then + echo "mkdisk: $IMG already exists (use --fresh to re-format)" + exit 0 +fi + +if ! command -v mkfs.fat >/dev/null 2>&1; then + echo "mkdisk: mkfs.fat not found — install dosfstools (sudo apt install dosfstools)" >&2 + exit 1 +fi + +# mkfs.fat sizes are 1024-byte blocks; accept M/G/K suffixes too. +case "$SIZE" in +*G) SIZE_BLOCKS=$(( ${SIZE%G} * 1024 * 1024 )) ;; +*M) SIZE_BLOCKS=$(( ${SIZE%M} * 1024 )) ;; +*K) SIZE_BLOCKS=$(( ${SIZE%K} )) ;; +*) SIZE_BLOCKS="$SIZE" ;; +esac +if [ -z "$SIZE_BLOCKS" ] || [ "$SIZE_BLOCKS" -le 0 ] 2>/dev/null; then + echo "mkdisk: bad size '$SIZE' (use e.g. 64M or 65536)" >&2 + exit 1 +fi + +echo "mkdisk: creating $IMG ($SIZE FAT32, label RSDOS)" +rm -f "$IMG" +mkfs.fat -C -F 32 -n RSDOS "$IMG" "$SIZE_BLOCKS" + +if [ -n "$SEED_DIR" ] && [ -d "$SEED_DIR" ]; then + if ! command -v mcopy >/dev/null 2>&1; then + echo "mkdisk: warning: mcopy not found — skipping seed files (install mtools)" >&2 + else + echo "mkdisk: seeding from $SEED_DIR/" + for f in "$SEED_DIR"/*; do + [ -f "$f" ] || continue + mcopy -i "$IMG" "$f" "::/$(basename "$f")" + echo " -> $(basename "$f")" + done + fi +fi + +echo "mkdisk: done — boot with: make run (or: ./scripts/boot.sh)" diff --git a/scripts/sendkeys.py b/scripts/sendkeys.py new file mode 100755 index 0000000..5d1692c --- /dev/null +++ b/scripts/sendkeys.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +"""Type lines into a QEMU guest via the monitor's `sendkey` command. + +Each positional argument is one line: it is typed character by character +(US layout) followed by Enter. Use it to drive the rs-dos shell from a +test script without a display. + +Usage: + sendkeys.py "ls /disk" "echo hi > /disk/a.txt" "poweroff" + sendkeys.py --sock /tmp/qmon.sock "date" + +Environment: QMON_SOCK (default: /tmp/qmon.sock) +""" +import os +import socket +import sys +import time + +SOCK = os.environ.get("QMON_SOCK", "/tmp/qmon.sock") + +# Unshifted punctuation needs QEMU's *named* keys — bare '-'/'.'/'/' are +# not valid sendkey names (and '-' is the modifier separator). +BASE = { + "-": "minus", "=": "equal", "[": "bracketleft", "]": "bracketright", + "\\": "backslash", ";": "semicolon", "'": "apostrophe", + ",": "comma", ".": "dot", "/": "slash", "`": "grave", +} + +# Characters that need a shift key on US layout, and the key QEMU's +# `sendkey` understands for them. +SHIFT = { + "~": "grave", "!": "1", "@": "2", "#": "3", "$": "4", "%": "5", + "^": "6", "&": "7", "*": "8", "(": "9", ")": "0", + "_": "minus", "+": "equal", "{": "bracketleft", "}": "bracketright", + "|": "backslash", ":": "semicolon", '"': "apostrophe", + "<": "comma", ">": "dot", "?": "slash", +} + +ALNUM = set("abcdefghijklmnopqrstuvwxyz0123456789") + + +def connect(path, timeout=60): + """Connect to the monitor socket, retrying until QEMU is up.""" + deadline = time.time() + timeout + while True: + try: + s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + s.connect(path) + return s + except OSError: + if time.time() > deadline: + raise + time.sleep(0.2) + + +def sendkey(sock, keys): + sock.sendall(("sendkey %s\n" % keys).encode()) + + +def type_line(sock, line): + for ch in line: + if ch == " ": + sendkey(sock, "spc") + elif ch in BASE: + sendkey(sock, BASE[ch]) + elif ch in SHIFT: + sendkey(sock, "shift-" + SHIFT[ch]) + elif ch.isupper(): + sendkey(sock, "shift-" + ch.lower()) + elif ch in ALNUM: + sendkey(sock, ch) + else: + sys.stderr.write("warning: no sendkey mapping for %r\n" % ch) + continue + time.sleep(0.05) + sendkey(sock, "ret") + time.sleep(0.7) + + +def main(): + args = [a for a in sys.argv[1:] if a != "--sock"] + sock_path = SOCK + if "--sock" in sys.argv: + i = sys.argv.index("--sock") + sock_path = sys.argv[i + 1] + args = sys.argv[i + 2:] + if not args: + sys.stderr.write(__doc__ or "") + return 1 + sock = connect(sock_path) + # Let the monitor drain any startup banner before typing. + time.sleep(0.5) + for line in args: + try: + type_line(sock, line) + except OSError: + # Guest powered off (last line was usually "poweroff"). + break + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/test-disk.sh b/scripts/test-disk.sh new file mode 100755 index 0000000..f7be7c6 --- /dev/null +++ b/scripts/test-disk.sh @@ -0,0 +1,111 @@ +#!/usr/bin/env bash +# +# scripts/test-disk.sh — full automated data-disk round trip. +# +# Builds the kernel, ensures the FAT32 image exists (with --fresh it is +# re-formatted and re-seeded), boots QEMU headless, drives the shell via +# the monitor, then verifies the results with mtools and fsck. +# +# Usage: +# ./scripts/test-disk.sh # reuse existing image +# ./scripts/test-disk.sh --fresh # re-format the image first +# +# Environment: +# DISK_IMG data disk path (default: /tmp/disk.img) +# OVMF firmware (default: /usr/share/edk2/x64/OVMF.4m.fd) +# QEMU qemu binary (default: qemu-system-x86_64) +# +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PROJECT_DIR="$(dirname "$SCRIPT_DIR")" +cd "$PROJECT_DIR" + +FRESH=0 +[ "${1:-}" = "--fresh" ] && FRESH=1 + +OVMF="${OVMF:-/usr/share/edk2/x64/OVMF.4m.fd}" +QEMU="${QEMU:-qemu-system-x86_64}" +IMG="${DISK_IMG:-/tmp/disk.img}" +ESP_DIR="$PROJECT_DIR/esp" +QPID=/tmp/rsdos-test-qpid +SOCK=/tmp/rsdos-test-qmon.sock +LOG=/tmp/rsdos-test-serial.log +STAMP="$(date +%s)" + +fail() { + echo "FAIL: $1" + [ -f "$LOG" ] && tail -20 "$LOG" + exit 1 +} + +# wait_log FILE PATTERN TIMEOUT-SECONDS — poll a file for a regex. +wait_log() { + local i=0 + while [ "$i" -lt $(( ${3:-60} * 10 )) ]; do + grep -q "$2" "$1" 2>/dev/null && return 0 + sleep 0.1 + i=$((i + 1)) + done + return 1 +} + +# wait_qemu_exit TIMEOUT-SECONDS — poll until the pid is gone. +wait_qemu_exit() { + local i=0 + while [ "$i" -lt $(( ${1:-30} * 10 )) ]; do + kill -0 "$(cat "$QPID" 2>/dev/null)" 2>/dev/null || return 0 + sleep 0.1 + i=$((i + 1)) + done + return 1 +} + +echo "==> 1/6 build kernel (debug)" +cargo build --package rsdos --target x86_64-unknown-uefi + +echo "==> 2/6 deploy to ESP + ensure disk image" +mkdir -p "$ESP_DIR/EFI/BOOT" +cp target/x86_64-unknown-uefi/debug/rsdos.efi "$ESP_DIR/EFI/BOOT/BOOTX64.EFI" +"$SCRIPT_DIR/mkdisk.sh" $([ "$FRESH" -eq 1 ] && echo --fresh) "$IMG" + +echo "==> 3/6 boot QEMU (headless)" +kill "$(cat "$QPID" 2>/dev/null)" 2>/dev/null || true +sleep 1 +rm -f "$SOCK" "$LOG" +"$QEMU" -bios "$OVMF" \ + -drive file="$IMG",format=raw,if=none,id=disk \ + -device ide-hd,drive=disk,bus=ide.0 \ + -drive file=fat:rw:"$ESP_DIR",format=raw,if=none,id=esp \ + -device ide-hd,drive=esp,bus=ide.1 \ + -serial file:"$LOG" \ + -monitor unix:"$SOCK",server,nowait \ + -m 256M -machine q35 -cpu qemu64 -no-reboot -display none & +echo $! > "$QPID" + +wait_log "$LOG" "boot: entering shell" 90 || fail "kernel never reached the shell" +sleep 1 + +echo "==> 4/6 drive the shell (ls / mkdir / echo > / cat / poweroff)" +python3 "$SCRIPT_DIR/sendkeys.py" --sock "$SOCK" \ + "ls /disk" \ + "mkdir /disk/sub" \ + "echo disk-test-$STAMP > /disk/sub/hello.txt" \ + "cat /disk/sub/hello.txt" \ + "poweroff" + +echo "==> 5/6 wait for shutdown" +wait_qemu_exit 30 || fail "QEMU did not exit after poweroff" + +echo "==> 6/6 verify" +grep -a "disk-test-$STAMP" "$LOG" >/dev/null || fail "cat output missing from serial log" +if ! fsck.fat -n "$IMG" >/tmp/rsdos-fsck.out 2>&1; then + fail "fsck.fat reports problems: $(cat /tmp/rsdos-fsck.out | tail -3)" +fi +MT="$(mtype -i "$IMG" "::/sub/hello.txt" 2>/dev/null || true)" +[ "$MT" = "disk-test-$STAMP" ] || fail "mtype mismatch: got '$MT'" + +echo "" +echo "PASS — disk round trip OK (fsck clean, file survived poweroff)" +echo " image: $IMG" +echo " written: /disk/sub/hello.txt = \"disk-test-$STAMP\"" -- cgit v1.2.3