qvm/internal/vm/qemu.go
2026-03-09 23:24:19 -05:00

63 lines
2.3 KiB
Go

package vm
import (
"fmt"
"qvm/internal/config"
"qvm/internal/virtiofsd"
"strconv"
)
// HotplugSlots is the number of PCIe root ports reserved for hot-plugging
// workspace mounts into a running VM. Each hot-mounted virtiofs device needs
// its own root port since pcie.0 does not support hotplug.
const HotplugSlots = 16
// HotplugBusPrefix is the bus ID prefix for hotplug-capable PCIe root ports.
// Slots are named hotplug0, hotplug1, ..., hotplugN.
const HotplugBusPrefix = "hotplug"
// buildQEMUCommand builds the QEMU command line for virtiofsd-based mounts.
// Uses vhost-user-fs-pci devices for boot-time mounts and provisions empty
// PCIe root ports for hot-plugging additional mounts at runtime.
func buildQEMUCommand(cfg *config.Config, sshPort int, mounts []virtiofsd.Mount) []string {
memSize := cfg.VM.Memory
// vhost-user-fs requires shared memory backend with share=on
// We must specify memory size only via the memory backend and attach it to NUMA
// The -m flag must match the memory backend size for QEMU to be happy
args := []string{
"-machine", "q35,memory-backend=mem",
"-accel", "kvm",
"-cpu", "host",
"-object", fmt.Sprintf("memory-backend-memfd,id=mem,size=%s,share=on", memSize),
"-smp", strconv.Itoa(cfg.VM.CPUs),
"-display", "none",
"-daemonize",
"-pidfile", config.PIDFile,
"-drive", fmt.Sprintf("file=%s,if=virtio,format=qcow2", config.Overlay),
"-netdev", fmt.Sprintf("user,id=n0,hostfwd=tcp::%d-:22", sshPort),
"-device", "virtio-net-pci,netdev=n0",
"-serial", fmt.Sprintf("file:%s", config.SerialLog),
"-qmp", fmt.Sprintf("unix:%s,server,nowait", config.QMPSocket),
}
// Add vhost-user-fs devices for each boot-time mount
for _, mount := range mounts {
args = append(args,
"-chardev", fmt.Sprintf("socket,id=%s,path=%s", mount.Tag, mount.SocketPath),
"-device", fmt.Sprintf("vhost-user-fs-pci,queue-size=1024,chardev=%s,tag=%s", mount.Tag, mount.Tag),
)
}
// Provision empty PCIe root ports for hot-plugging workspace mounts.
// The q35 root bus (pcie.0) does not support hotplug, so we need
// dedicated root ports that accept device_add at runtime.
for i := 0; i < HotplugSlots; i++ {
busID := fmt.Sprintf("%s%d", HotplugBusPrefix, i)
args = append(args,
"-device", fmt.Sprintf("pcie-root-port,id=%s,slot=%d", busID, i+1),
)
}
return args
}