Read port forwards from per-VM forwards.conf files

A QEMU config file section cannot hold more than one hostfwd entry, so
the forwards move out of the ini files into qemu/<vm>/forwards.conf, one
"LABEL HOST_PORT GUEST_PORT" rule per line. The new vmforwards.py parses
them, skips rules whose host port is taken and builds the -netdev
argument, which every launcher now passes instead of relying on a
netdev section in the ini or on -nic.

The ini files move to qemu/<vm>/config.ini and qemu/<vm>/<os>-<arch>.ini.
The *_SSH_PORT / *_RDP_PORT variables and --rdp-port / --ssh-port still
replace the host port of the SSH and RDP rules.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
GuofangandClaude Sonnet 5.5 committed 2026-10-29 14:55:30 +08:00
1 parent 07a2c6db3e
commit 78f6fd424b
29 files changed
+272 -131

No files matched your search

+17 -5
View File
@@ -8,6 +8,8 @@ import subprocess
import sys
from pathlib import Path
import vmforwards
script_dir = Path(__file__).resolve().parent
env = os.environ.get
@@ -55,7 +57,9 @@ state_dir = Path(env("ALMA_VM_STATE_DIR") or script_dir / "vm" / "almalinux10")
disk_size = env("ALMA_VM_DISK_SIZE") or "20G"
memory = env("ALMA_VM_MEMORY") or "8G"
cpus = env("ALMA_VM_CPUS") or "8"
ssh_port = env("ALMA_VM_SSH_PORT") or "2223"
ssh_port = env("ALMA_VM_SSH_PORT")
forwards_file = Path(
env("ALMA_VM_FORWARDS") or script_dir / "qemu" / "almalinux10" / "forwards.conf")
def normalize_arch(arch):
@@ -127,6 +131,8 @@ Usage: {Path(sys.argv[0]).name} [--daemon] [--download-only] [--state-dir DIR] [
Start an AlmaLinux 10 Generic Cloud virtual machine. The launcher obtains a
missing base image through ../vm-images/download-images.sh and creates a
writable overlay in the state directory, leaving the base image unchanged.
Host port forwards, one "LABEL HOST_PORT GUEST_PORT" rule per line, are read
from qemu/almalinux10/forwards.conf.
Options:
--daemon Run in the background while keeping the graphical window
@@ -138,7 +144,9 @@ Options:
Environment variables:
ALMA_VM_MEMORY Guest memory (default: 8G)
ALMA_VM_CPUS Virtual CPU count (default: 8)
ALMA_VM_SSH_PORT Host port forwarded to guest SSH (default: 2223)
ALMA_VM_SSH_PORT Host port of the SSH rule in the port forward file
(default: from the file, 2223)
ALMA_VM_FORWARDS Port forward file (default: qemu/almalinux10/forwards.conf)
ALMA_VM_BASE_IMAGE Base qcow2 image path (default: resolved by shared image helper)
ALMA_VM_SEED_IMAGE cloud-init seed ISO path (default: alongside the VM disk)
ALMA_VM_STATE_DIR Runtime state directory (default: vm/almalinux10)
@@ -240,6 +248,9 @@ if not disk_image.is_file():
is_macos = platform.system() == "Darwin"
display_backend = env("ALMA_VM_DISPLAY") or ("cocoa" if is_macos else "gtk")
forwards = vmforwards.usable_forwards(vmforwards.load_forwards(
forwards_file, {"SSH": ssh_port} if ssh_port else None))
qemu_args = [
"-name", "almalinux10",
"-machine", machine_type,
@@ -252,7 +263,8 @@ qemu_args = [
"-device", "virtio-blk-pci,drive=disk0",
"-drive", f"file={seed_image},if=none,id=seed0,format=raw,readonly=on",
"-device", "virtio-blk-pci,drive=seed0",
"-nic", f"user,model=virtio-net-pci,hostfwd=tcp::{ssh_port}-:22",
"-netdev", vmforwards.netdev_argument(forwards),
"-device", "virtio-net-pci,netdev=net0",
]
if machine_type == "virt":
@@ -274,8 +286,8 @@ else:
qemu_args += ["-accel", "tcg", "-cpu", "max"]
accelerator = "TCG"
print(f"Starting AlmaLinux 10 ({host_arch}) with {accelerator} acceleration; "
f"SSH is forwarded to localhost:{ssh_port}.", flush=True)
print(f"Starting AlmaLinux 10 ({host_arch}) with {accelerator} acceleration.", flush=True)
vmforwards.print_forwards(forwards)
if daemonize:
log_file.write_text("")