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/sendkeys.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100755 scripts/sendkeys.py (limited to 'scripts/sendkeys.py') 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()) -- cgit v1.2.3