try.sh (4816B)
1 #!/usr/bin/env bash 2 # Boots the live ISO (the newest out/iso/sector-f-linux-*.iso) in QEMU with UEFI and two blank 16 GB drives, to try 3 # the installer (ADR-0014): the chosen one should get the system, the other stay untouched. Its own 4 # VNC display (localhost:5902) and monitor, so the test VM (vm/) can run beside it. 5 # 6 # linux/live/try.sh start boot it (in the background) 7 # linux/live/try.sh screenshot save the screen to out/iso/screen.png 8 # linux/live/try.sh type 'text' type into it (\t Tab, \n Enter) 9 # linux/live/try.sh click X Y click at a pixel of the 1280x800 screen 10 # linux/live/try.sh ssh [cmd] root over SSH (a LIVE_DEBUG=1 ISO only), localhost:2223 11 # linux/live/try.sh disk [a|b] boot an installed drive on its own, without the ISO 12 # linux/live/try.sh stop power it off 13 # linux/live/try.sh reset also delete the blank drives 14 set -euo pipefail 15 ROOT=$(cd "$(dirname "$0")/.." && pwd) 16 OUT="$ROOT/out/iso" 17 ISO=$(ls -t "$OUT"/sector-f-linux-*.iso 2>/dev/null | head -1 || true) # the newest build 18 MON="$OUT/monitor.sock" 19 PID="$OUT/qemu.pid" 20 VARS="$OUT/OVMF_VARS.fd" 21 running() { [[ -f "$PID" ]] && kill -0 "$(cat "$PID")" 2>/dev/null; } 22 23 case "${1:-}" in 24 start) 25 running && { echo "already running"; exit 0; } 26 [[ -f "$ISO" ]] || { echo "no $ISO: linux/live/build.sh first" >&2; exit 1; } 27 for d in a b; do [[ -f "$OUT/drive-$d.qcow2" ]] || qemu-img create -q -f qcow2 "$OUT/drive-$d.qcow2" 16G; done 28 cp -n /usr/share/OVMF/OVMF_VARS_4M.fd "$VARS" 29 rm -f "$MON" 30 qemu-system-x86_64 -enable-kvm -cpu host -m 4096 -smp 4 -machine q35 \ 31 -drive if=pflash,format=raw,readonly=on,file=/usr/share/OVMF/OVMF_CODE_4M.fd \ 32 -drive if=pflash,format=raw,file="$VARS" \ 33 -drive file="$ISO",media=cdrom,readonly=on \ 34 -drive file="$OUT/drive-a.qcow2",if=none,id=a -device virtio-blk-pci,drive=a,serial=DRIVE-A \ 35 -drive file="$OUT/drive-b.qcow2",if=none,id=b -device virtio-blk-pci,drive=b,serial=DRIVE-B \ 36 -device qemu-xhci -device usb-tablet -device virtio-vga -vnc 127.0.0.1:2 -monitor "unix:$MON,server,nowait" \ 37 -netdev user,id=n0,hostfwd=tcp:127.0.0.1:2223-:22 -device virtio-net-pci,netdev=n0 \ 38 -daemonize -pidfile "$PID" 39 echo "booting: VNC localhost:5902; $0 screenshot" 40 ;; 41 screenshot) 42 running || { echo "not running" >&2; exit 1; } 43 shot=${2:-$OUT/screen.png}; shot=$(cd "$(dirname "$shot")" && pwd)/$(basename "$shot") 44 rm -f "$shot"; printf 'screendump %s -f png\n' "$shot" | nc -U -q 1 "$MON" >/dev/null 45 for _ in $(seq 1 20); do [[ -s "$shot" ]] && break; sleep 0.2; done 46 echo "$shot" 47 ;; 48 type) 49 MON="$MON" python3 -I - "$2" <<'PY' 50 import os, socket, sys, time 51 s = socket.socket(socket.AF_UNIX); s.connect(os.environ["MON"]); s.settimeout(1) 52 try: s.recv(4096) 53 except Exception: pass 54 SPECIAL = {" ": "spc", "-": "minus", "_": "shift-minus", ".": "dot", "\t": "tab", "\n": "ret", "\b": "backspace"} 55 for ch in sys.argv[1].encode().decode("unicode_escape"): 56 key = SPECIAL.get(ch) or ("shift-" + ch.lower() if ch.isupper() else ch) 57 s.sendall(("sendkey " + key + "\n").encode()); time.sleep(0.07) 58 try: s.recv(4096) 59 except Exception: pass 60 PY 61 ;; 62 click) 63 MON="$MON" python3 -I - "$2" "$3" <<'PY' 64 import os, socket, sys, time 65 s = socket.socket(socket.AF_UNIX); s.connect(os.environ["MON"]); s.settimeout(1) 66 try: s.recv(4096) 67 except Exception: pass 68 x, y = int(int(sys.argv[1]) * 32767 / 1280), int(int(sys.argv[2]) * 32767 / 800) 69 for cmd in (f"mouse_move {x} {y}", "mouse_button 1", "mouse_button 0"): 70 s.sendall((cmd + "\n").encode()); time.sleep(0.15) 71 try: s.recv(4096) 72 except Exception: pass 73 PY 74 ;; 75 ssh) 76 shift; exec ssh -i "$ROOT/out/id_ed25519" -p 2223 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR root@127.0.0.1 "$@" ;; 77 disk) 78 running && { echo "already running"; exit 0; } 79 d=${2:-a} 80 cp -n /usr/share/OVMF/OVMF_VARS_4M.fd "$OUT/OVMF_VARS_disk.fd" 2>/dev/null || true 81 rm -f "$MON" 82 qemu-system-x86_64 -enable-kvm -cpu host -m 4096 -smp 4 -machine q35 \ 83 -drive if=pflash,format=raw,readonly=on,file=/usr/share/OVMF/OVMF_CODE_4M.fd \ 84 -drive if=pflash,format=raw,file="$OUT/OVMF_VARS_disk.fd" \ 85 -drive file="$OUT/drive-$d.qcow2",if=none,id=a -device virtio-blk-pci,drive=a,serial=DRIVE-A \ 86 -device virtio-vga -vnc 127.0.0.1:2 -monitor "unix:$MON,server,nowait" \ 87 -netdev user,id=n0,hostfwd=tcp:127.0.0.1:2223-:22 -device virtio-net-pci,netdev=n0 \ 88 -daemonize -pidfile "$PID" 89 echo "booting drive $d alone: VNC localhost:5902" 90 ;; 91 stop) 92 running && kill "$(cat "$PID")"; rm -f "$PID" ;; 93 reset) 94 "$0" stop; rm -f "$OUT"/drive-*.qcow2 "$OUT"/OVMF_VARS*.fd ;; 95 *) sed -n 2,13p "$0" | sed 's/^# \{0,1\}//'; exit 2 ;; 96 esac