summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-08-01 01:56:12 +0200
committersalmonik <salmonik@tuta.io>2026-08-01 01:56:12 +0200
commit4ea992c71dd0cdcffe0f74216e748256bd700a31 (patch)
tree2992b0063f08fbf79586d35ad481539fd7ee9af4 /scripts
parentahci: skip ATAPI ports and fast-fail on device abort (diff)
downloadrs-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')
-rwxr-xr-xscripts/boot.sh5
-rwxr-xr-xscripts/mkdisk.sh85
-rwxr-xr-xscripts/sendkeys.py103
-rwxr-xr-xscripts/test-disk.sh111
4 files changed, 304 insertions, 0 deletions
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)"
95echo " gdb: $([ -n "$DEBUG" ] && echo ':1234' || echo off)" 95echo " gdb: $([ -n "$DEBUG" ] && echo ':1234' || echo off)"
96echo "" 96echo ""
97 97
98if [ ! -f "$DISK_IMG" ]; then
99 echo " (creating missing disk image: $DISK_IMG)"
100 "$SCRIPT_DIR/mkdisk.sh" "$DISK_IMG"
101fi
102
98if [ -f "$DISK_IMG" ]; then 103if [ -f "$DISK_IMG" ]; then
99 DISK_ARGS=( 104 DISK_ARGS=(
100 -drive "file=$DISK_IMG,format=raw,if=none,id=disk" 105 -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 @@
1#!/usr/bin/env bash
2#
3# scripts/mkdisk.sh — create (or re-create) the FAT32 data disk that the
4# kernel mounts at /disk.
5#
6# Usage:
7# ./scripts/mkdisk.sh # create /tmp/disk.img (64M) if missing
8# ./scripts/mkdisk.sh --fresh # re-format (wipes the image!)
9# ./scripts/mkdisk.sh --size 128M # different size (default 64M)
10# ./scripts/mkdisk.sh /path/img # custom image path
11# ./scripts/mkdisk.sh --no-seed # skip the seed/ directory
12#
13# The image is created with `mkfs.fat -C`, which is the option that
14# actually creates the file — plain `mkfs.fat` only writes into an
15# existing one. Files from seed/ (if present) are copied in with mtools.
16#
17set -euo pipefail
18
19SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
20PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
21
22IMG="/tmp/disk.img"
23SIZE="64M"
24FRESH=0
25SEED_DIR="$PROJECT_DIR/seed"
26
27while [ $# -gt 0 ]; do
28 case "$1" in
29 --fresh) FRESH=1 ;;
30 --size)
31 shift
32 SIZE="$1"
33 ;;
34 --no-seed) SEED_DIR="" ;;
35 --help | -h)
36 head -20 "$0" | grep "^#" | sed 's/^# \?//'
37 exit 0
38 ;;
39 *)
40 IMG="$1"
41 ;;
42 esac
43 shift
44done
45
46if [ -f "$IMG" ] && [ "$FRESH" -eq 0 ]; then
47 echo "mkdisk: $IMG already exists (use --fresh to re-format)"
48 exit 0
49fi
50
51if ! command -v mkfs.fat >/dev/null 2>&1; then
52 echo "mkdisk: mkfs.fat not found — install dosfstools (sudo apt install dosfstools)" >&2
53 exit 1
54fi
55
56# mkfs.fat sizes are 1024-byte blocks; accept M/G/K suffixes too.
57case "$SIZE" in
58*G) SIZE_BLOCKS=$(( ${SIZE%G} * 1024 * 1024 )) ;;
59*M) SIZE_BLOCKS=$(( ${SIZE%M} * 1024 )) ;;
60*K) SIZE_BLOCKS=$(( ${SIZE%K} )) ;;
61*) SIZE_BLOCKS="$SIZE" ;;
62esac
63if [ -z "$SIZE_BLOCKS" ] || [ "$SIZE_BLOCKS" -le 0 ] 2>/dev/null; then
64 echo "mkdisk: bad size '$SIZE' (use e.g. 64M or 65536)" >&2
65 exit 1
66fi
67
68echo "mkdisk: creating $IMG ($SIZE FAT32, label RSDOS)"
69rm -f "$IMG"
70mkfs.fat -C -F 32 -n RSDOS "$IMG" "$SIZE_BLOCKS"
71
72if [ -n "$SEED_DIR" ] && [ -d "$SEED_DIR" ]; then
73 if ! command -v mcopy >/dev/null 2>&1; then
74 echo "mkdisk: warning: mcopy not found — skipping seed files (install mtools)" >&2
75 else
76 echo "mkdisk: seeding from $SEED_DIR/"
77 for f in "$SEED_DIR"/*; do
78 [ -f "$f" ] || continue
79 mcopy -i "$IMG" "$f" "::/$(basename "$f")"
80 echo " -> $(basename "$f")"
81 done
82 fi
83fi
84
85echo "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 @@
1#!/usr/bin/env python3
2"""Type lines into a QEMU guest via the monitor's `sendkey` command.
3
4Each 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
6test script without a display.
7
8Usage:
9 sendkeys.py "ls /disk" "echo hi > /disk/a.txt" "poweroff"
10 sendkeys.py --sock /tmp/qmon.sock "date"
11
12Environment: QMON_SOCK (default: /tmp/qmon.sock)
13"""
14import os
15import socket
16import sys
17import time
18
19SOCK = 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).
23BASE = {
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.
31SHIFT = {
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
39ALNUM = set("abcdefghijklmnopqrstuvwxyz0123456789")
40
41
42def 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
56def sendkey(sock, keys):
57 sock.sendall(("sendkey %s\n" % keys).encode())
58
59
60def 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
80def 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
102if __name__ == "__main__":
103 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 @@
1#!/usr/bin/env bash
2#
3# scripts/test-disk.sh — full automated data-disk round trip.
4#
5# Builds the kernel, ensures the FAT32 image exists (with --fresh it is
6# re-formatted and re-seeded), boots QEMU headless, drives the shell via
7# the monitor, then verifies the results with mtools and fsck.
8#
9# Usage:
10# ./scripts/test-disk.sh # reuse existing image
11# ./scripts/test-disk.sh --fresh # re-format the image first
12#
13# Environment:
14# DISK_IMG data disk path (default: /tmp/disk.img)
15# OVMF firmware (default: /usr/share/edk2/x64/OVMF.4m.fd)
16# QEMU qemu binary (default: qemu-system-x86_64)
17#
18set -euo pipefail
19
20SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
21PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
22cd "$PROJECT_DIR"
23
24FRESH=0
25[ "${1:-}" = "--fresh" ] && FRESH=1
26
27OVMF="${OVMF:-/usr/share/edk2/x64/OVMF.4m.fd}"
28QEMU="${QEMU:-qemu-system-x86_64}"
29IMG="${DISK_IMG:-/tmp/disk.img}"
30ESP_DIR="$PROJECT_DIR/esp"
31QPID=/tmp/rsdos-test-qpid
32SOCK=/tmp/rsdos-test-qmon.sock
33LOG=/tmp/rsdos-test-serial.log
34STAMP="$(date +%s)"
35
36fail() {
37 echo "FAIL: $1"
38 [ -f "$LOG" ] && tail -20 "$LOG"
39 exit 1
40}
41
42# wait_log FILE PATTERN TIMEOUT-SECONDS — poll a file for a regex.
43wait_log() {
44 local i=0
45 while [ "$i" -lt $(( ${3:-60} * 10 )) ]; do
46 grep -q "$2" "$1" 2>/dev/null && return 0
47 sleep 0.1
48 i=$((i + 1))
49 done
50 return 1
51}
52
53# wait_qemu_exit TIMEOUT-SECONDS — poll until the pid is gone.
54wait_qemu_exit() {
55 local i=0
56 while [ "$i" -lt $(( ${1:-30} * 10 )) ]; do
57 kill -0 "$(cat "$QPID" 2>/dev/null)" 2>/dev/null || return 0
58 sleep 0.1
59 i=$((i + 1))
60 done
61 return 1
62}
63
64echo "==> 1/6 build kernel (debug)"
65cargo build --package rsdos --target x86_64-unknown-uefi
66
67echo "==> 2/6 deploy to ESP + ensure disk image"
68mkdir -p "$ESP_DIR/EFI/BOOT"
69cp target/x86_64-unknown-uefi/debug/rsdos.efi "$ESP_DIR/EFI/BOOT/BOOTX64.EFI"
70"$SCRIPT_DIR/mkdisk.sh" $([ "$FRESH" -eq 1 ] && echo --fresh) "$IMG"
71
72echo "==> 3/6 boot QEMU (headless)"
73kill "$(cat "$QPID" 2>/dev/null)" 2>/dev/null || true
74sleep 1
75rm -f "$SOCK" "$LOG"
76"$QEMU" -bios "$OVMF" \
77 -drive file="$IMG",format=raw,if=none,id=disk \
78 -device ide-hd,drive=disk,bus=ide.0 \
79 -drive file=fat:rw:"$ESP_DIR",format=raw,if=none,id=esp \
80 -device ide-hd,drive=esp,bus=ide.1 \
81 -serial file:"$LOG" \
82 -monitor unix:"$SOCK",server,nowait \
83 -m 256M -machine q35 -cpu qemu64 -no-reboot -display none &
84echo $! > "$QPID"
85
86wait_log "$LOG" "boot: entering shell" 90 || fail "kernel never reached the shell"
87sleep 1
88
89echo "==> 4/6 drive the shell (ls / mkdir / echo > / cat / poweroff)"
90python3 "$SCRIPT_DIR/sendkeys.py" --sock "$SOCK" \
91 "ls /disk" \
92 "mkdir /disk/sub" \
93 "echo disk-test-$STAMP > /disk/sub/hello.txt" \
94 "cat /disk/sub/hello.txt" \
95 "poweroff"
96
97echo "==> 5/6 wait for shutdown"
98wait_qemu_exit 30 || fail "QEMU did not exit after poweroff"
99
100echo "==> 6/6 verify"
101grep -a "disk-test-$STAMP" "$LOG" >/dev/null || fail "cat output missing from serial log"
102if ! fsck.fat -n "$IMG" >/tmp/rsdos-fsck.out 2>&1; then
103 fail "fsck.fat reports problems: $(cat /tmp/rsdos-fsck.out | tail -3)"
104fi
105MT="$(mtype -i "$IMG" "::/sub/hello.txt" 2>/dev/null || true)"
106[ "$MT" = "disk-test-$STAMP" ] || fail "mtype mismatch: got '$MT'"
107
108echo ""
109echo "PASS — disk round trip OK (fsck clean, file survived poweroff)"
110echo " image: $IMG"
111echo " written: /disk/sub/hello.txt = \"disk-test-$STAMP\""