Add --state-dir option to the Python VM launchers

Let each run-*.py launcher take the VM directory (disk, seed ISO, UEFI
variables, PID file, console log) from the command line, taking
precedence over the *_STATE_DIR environment variable. Paths derived
from the state directory are now computed after argument parsing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01K1dybYsc36kf3gUPB7NuNB
This commit is contained in:
GuofangandClaude Opus 5.5 committed 2026-09-29 13:35:32 +08:00
1 parent a03fb23977
commit 8a7f974c11
5 files changed
+84 -37

No files matched your search

+19 -9
View File
@@ -37,8 +37,6 @@ def is_executable(path):
state_dir = Path(env("WIN11_VM_STATE_DIR") or script_dir / ".vm" / "windows11")
disk_image = Path(env("WIN11_VM_DISK_IMAGE") or state_dir / "disk.qcow2")
seed_iso = Path(env("WIN11_VM_SEED_ISO") or disk_image.parent / "seed.iso")
disk_size = env("WIN11_VM_DISK_SIZE") or "64G"
memory = env("WIN11_VM_MEMORY") or "8G"
cpus = env("WIN11_VM_CPUS") or "8"
@@ -69,11 +67,6 @@ uefi_vars_template = pick_firmware(
"/opt/homebrew/share/qemu/edk2-i386-vars.fd",
"/usr/local/share/qemu/edk2-i386-vars.fd",
)
uefi_vars = state_dir / "OVMF_VARS_4M.fd"
pid_file = state_dir / "qemu.pid"
log_file = state_dir / "console.log"
monitor_socket = state_dir / "qemu-monitor.sock"
guest_agent_socket = state_dir / "qemu-guest-agent.sock"
daemonize = False
install_from_iso = False
extra_args = []
@@ -81,15 +74,18 @@ extra_args = []
def usage(stream=sys.stdout):
stream.write(f"""\
Usage: {Path(sys.argv[0]).name} [--daemon] [--install-from-iso] [-- QEMU_ARGUMENT ...]
Usage: {Path(sys.argv[0]).name} [--daemon] [--install-from-iso] [--state-dir DIR] [-- QEMU_ARGUMENT ...]
If {disk_image} is missing, create it from the Windows 11 QCOW2 base image.
If the system disk (DIR/disk.qcow2, see --state-dir) is missing, create it from
the Windows 11 QCOW2 base image.
Use --install-from-iso to perform an automatic ISO installation instead.
If it exists, start Windows normally without attaching installation media.
Options:
--daemon Run QEMU in the background
--install-from-iso Install from ISO when the system disk is missing
--state-dir DIR Directory for the VM disk, seed ISO and runtime files
(default: .vm/windows11; overrides WIN11_VM_STATE_DIR)
-h, --help Show this help
Environment variables:
@@ -117,6 +113,12 @@ while args:
daemonize = True
elif arg == "--install-from-iso":
install_from_iso = True
elif arg == "--state-dir":
if not args:
die(f"{arg} requires a directory argument.", 2)
state_dir = Path(args.pop(0))
elif arg.startswith("--state-dir="):
state_dir = Path(arg.split("=", 1)[1])
elif arg in ("-h", "--help"):
usage()
sys.exit(0)
@@ -128,6 +130,14 @@ while args:
usage(sys.stderr)
sys.exit(2)
disk_image = Path(env("WIN11_VM_DISK_IMAGE") or state_dir / "disk.qcow2")
seed_iso = Path(env("WIN11_VM_SEED_ISO") or disk_image.parent / "seed.iso")
uefi_vars = state_dir / "OVMF_VARS_4M.fd"
pid_file = state_dir / "qemu.pid"
log_file = state_dir / "console.log"
monitor_socket = state_dir / "qemu-monitor.sock"
guest_agent_socket = state_dir / "qemu-guest-agent.sock"
installing = False
installing_from_iso = False
if not disk_image.is_file():