diff options
| author | salmonik <salmonik@tuta.io> | 2026-08-01 01:56:12 +0200 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-08-01 01:56:12 +0200 |
| commit | 4ea992c71dd0cdcffe0f74216e748256bd700a31 (patch) | |
| tree | 2992b0063f08fbf79586d35ad481539fd7ee9af4 /scripts/sendkeys.py | |
| parent | ahci: skip ATAPI ports and fast-fail on device abort (diff) | |
| download | rs-dos-4ea992c71dd0cdcffe0f74216e748256bd700a31.tar.gz rs-dos-4ea992c71dd0cdcffe0f74216e748256bd700a31.zip | |
scripts: automate the data-disk workflow (mkdisk / sendkeys / test-disk)
- 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
Diffstat (limited to 'scripts/sendkeys.py')
| -rwxr-xr-x | scripts/sendkeys.py | 103 |
1 files changed, 103 insertions, 0 deletions
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 @@ | |||
| 1 | #!/usr/bin/env python3 | ||
| 2 | """Type lines into a QEMU guest via the monitor's `sendkey` command. | ||
| 3 | |||
| 4 | Each positional argument is one line: it is typed character by character | ||
| 5 | (US layout) followed by Enter. Use it to drive the rs-dos shell from a | ||
| 6 | test script without a display. | ||
| 7 | |||
| 8 | Usage: | ||
| 9 | sendkeys.py "ls /disk" "echo hi > /disk/a.txt" "poweroff" | ||
| 10 | sendkeys.py --sock /tmp/qmon.sock "date" | ||
| 11 | |||
| 12 | Environment: QMON_SOCK (default: /tmp/qmon.sock) | ||
| 13 | """ | ||
| 14 | import os | ||
| 15 | import socket | ||
| 16 | import sys | ||
| 17 | import time | ||
| 18 | |||
| 19 | SOCK = os.environ.get("QMON_SOCK", "/tmp/qmon.sock") | ||
| 20 | |||
| 21 | # Unshifted punctuation needs QEMU's *named* keys — bare '-'/'.'/'/' are | ||
| 22 | # not valid sendkey names (and '-' is the modifier separator). | ||
| 23 | BASE = { | ||
| 24 | "-": "minus", "=": "equal", "[": "bracketleft", "]": "bracketright", | ||
| 25 | "\\": "backslash", ";": "semicolon", "'": "apostrophe", | ||
| 26 | ",": "comma", ".": "dot", "/": "slash", "`": "grave", | ||
| 27 | } | ||
| 28 | |||
| 29 | # Characters that need a shift key on US layout, and the key QEMU's | ||
| 30 | # `sendkey` understands for them. | ||
| 31 | SHIFT = { | ||
| 32 | "~": "grave", "!": "1", "@": "2", "#": "3", "$": "4", "%": "5", | ||
| 33 | "^": "6", "&": "7", "*": "8", "(": "9", ")": "0", | ||
| 34 | "_": "minus", "+": "equal", "{": "bracketleft", "}": "bracketright", | ||
| 35 | "|": "backslash", ":": "semicolon", '"': "apostrophe", | ||
| 36 | "<": "comma", ">": "dot", "?": "slash", | ||
| 37 | } | ||
| 38 | |||
| 39 | ALNUM = set("abcdefghijklmnopqrstuvwxyz0123456789") | ||
| 40 | |||
| 41 | |||
| 42 | def connect(path, timeout=60): | ||
| 43 | """Connect to the monitor socket, retrying until QEMU is up.""" | ||
| 44 | deadline = time.time() + timeout | ||
| 45 | while True: | ||
| 46 | try: | ||
| 47 | s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | ||
| 48 | s.connect(path) | ||
| 49 | return s | ||
| 50 | except OSError: | ||
| 51 | if time.time() > deadline: | ||
| 52 | raise | ||
| 53 | time.sleep(0.2) | ||
| 54 | |||
| 55 | |||
| 56 | def sendkey(sock, keys): | ||
| 57 | sock.sendall(("sendkey %s\n" % keys).encode()) | ||
| 58 | |||
| 59 | |||
| 60 | def type_line(sock, line): | ||
| 61 | for ch in line: | ||
| 62 | if ch == " ": | ||
| 63 | sendkey(sock, "spc") | ||
| 64 | elif ch in BASE: | ||
| 65 | sendkey(sock, BASE[ch]) | ||
| 66 | elif ch in SHIFT: | ||
| 67 | sendkey(sock, "shift-" + SHIFT[ch]) | ||
| 68 | elif ch.isupper(): | ||
| 69 | sendkey(sock, "shift-" + ch.lower()) | ||
| 70 | elif ch in ALNUM: | ||
| 71 | sendkey(sock, ch) | ||
| 72 | else: | ||
| 73 | sys.stderr.write("warning: no sendkey mapping for %r\n" % ch) | ||
| 74 | continue | ||
| 75 | time.sleep(0.05) | ||
| 76 | sendkey(sock, "ret") | ||
| 77 | time.sleep(0.7) | ||
| 78 | |||
| 79 | |||
| 80 | def main(): | ||
| 81 | args = [a for a in sys.argv[1:] if a != "--sock"] | ||
| 82 | sock_path = SOCK | ||
| 83 | if "--sock" in sys.argv: | ||
| 84 | i = sys.argv.index("--sock") | ||
| 85 | sock_path = sys.argv[i + 1] | ||
| 86 | args = sys.argv[i + 2:] | ||
| 87 | if not args: | ||
| 88 | sys.stderr.write(__doc__ or "") | ||
| 89 | return 1 | ||
| 90 | sock = connect(sock_path) | ||
| 91 | # Let the monitor drain any startup banner before typing. | ||
| 92 | time.sleep(0.5) | ||
| 93 | for line in args: | ||
| 94 | try: | ||
| 95 | type_line(sock, line) | ||
| 96 | except OSError: | ||
| 97 | # Guest powered off (last line was usually "poweroff"). | ||
| 98 | break | ||
| 99 | return 0 | ||
| 100 | |||
| 101 | |||
| 102 | if __name__ == "__main__": | ||
| 103 | sys.exit(main()) | ||
