Files
Reims-Go/internal/vmstart/nestedvirt.go
T
zhongruan0522 38551b79be feat(vm): add per-VM nested virtualization switch
reims-ctl adds `vmx=on` to `-cpu` only where KVM offers nested VMX, so a
macOS guest on Windows never sees VMX and cannot run VMs of its own such as
Docker Desktop. Add a switch under Settings > CPU & Memory > Performance,
off by default and applied on the next start.

The switch is Reims-Go's own per-VM setting, kept in the VM directory. At
start the macOS driver owns one argument that asks QEMU for VMX on every
vCPU without restating the CPU model; it is not a plugin and never shows up
among the VM's extra QEMU arguments. A switch file that cannot be read stops
the start instead of dropping VMX silently.

assisted-by:claude-code:anthropic/claude-fable-5-2
2026-12-01 15:40:15 +00:00

70 lines
1.8 KiB
Go

package vmstart
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
const vmNestedVirtConfigFile = ".reims-go-nested-virt.json"
// VMNestedVirtConfig is persisted per VM in <vmDir>/.reims-go-nested-virt.json. The switch is
// the VM's own; QEMU is told at each start (macosdriver.prepareNestedVirt).
type VMNestedVirtConfig struct {
Enabled bool `json:"enabled"`
}
// ReadVMNestedVirt loads <vmDir>/.reims-go-nested-virt.json. A VM without the file has the
// switch off. A file that cannot be read is an error: the VM must not silently start without
// a feature that was switched on.
func ReadVMNestedVirt(vmDir string) (VMNestedVirtConfig, error) {
if strings.TrimSpace(vmDir) == "" {
return VMNestedVirtConfig{}, nil
}
path := filepath.Join(vmDir, vmNestedVirtConfigFile)
data, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return VMNestedVirtConfig{}, nil
}
if err != nil {
return VMNestedVirtConfig{}, err
}
var cfg VMNestedVirtConfig
if err := json.Unmarshal(data, &cfg); err != nil {
return VMNestedVirtConfig{}, fmt.Errorf("%s: %w", path, err)
}
return cfg, nil
}
// SaveVMNestedVirt writes the switch; off removes the file.
func SaveVMNestedVirt(vmDir string, cfg VMNestedVirtConfig) error {
if strings.TrimSpace(vmDir) == "" {
return errors.New("VM directory is empty")
}
dst := filepath.Join(vmDir, vmNestedVirtConfigFile)
if !cfg.Enabled {
if err := os.Remove(dst); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return nil
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
tmp, err := os.CreateTemp(vmDir, vmNestedVirtConfigFile+".*")
if err != nil {
return err
}
defer os.Remove(tmp.Name())
_, writeErr := tmp.Write(data)
if err := errors.Join(writeErr, tmp.Close()); err != nil {
return err
}
return os.Rename(tmp.Name(), dst)
}