Adapt Intel VM suspend/resume to driver.SuspendDriver and lifecycle.Service, move native VM close confirmation into internal/host, migrate vm-suspend legacy capability detection, drop moved runtime build tooling, and regenerate i18n and Wails bindings.
299 lines
9.5 KiB
Go
299 lines
9.5 KiB
Go
package runtime
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
var reRuntimeKind = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]*$`)
|
|
|
|
const (
|
|
// DefaultKind is the built-in macOS vGPU runtime kind.
|
|
DefaultKind = "reims-vgpu"
|
|
// DefaultRuntimeAPI is the baseline CLI/output contract version for reims-vgpu.
|
|
DefaultRuntimeAPI = "1.0.0"
|
|
)
|
|
|
|
// KindSpec describes a registered runtime kind (e.g. "reims-vgpu", "qemu-ovmf").
|
|
type KindSpec struct {
|
|
Kind string `json:"kind"`
|
|
Name plugin.LocalizedText `json:"name,omitempty"`
|
|
NameZh string `json:"name_zh"`
|
|
NameEn string `json:"name_en"`
|
|
RequiredFiles []string `json:"required_files,omitempty"`
|
|
DefaultAPIVersion string `json:"default_api_version,omitempty"`
|
|
ControlExecutable string `json:"control_executable,omitempty"`
|
|
UtilityExecutable string `json:"utility_executable,omitempty"`
|
|
ControlQemuEnv string `json:"control_qemu_env,omitempty"`
|
|
Builtin bool `json:"builtin,omitempty"`
|
|
PluginID string `json:"plugin_id,omitempty"`
|
|
}
|
|
|
|
// KindRegistry holds all registered runtime kinds (§5.2).
|
|
type KindRegistry struct {
|
|
mu sync.RWMutex
|
|
kinds map[string]KindSpec
|
|
}
|
|
|
|
// NewKindRegistry creates a registry pre-populated with the built-in reims-vgpu kind.
|
|
func NewKindRegistry() *KindRegistry {
|
|
kr := &KindRegistry{
|
|
kinds: make(map[string]KindSpec),
|
|
}
|
|
kr.kinds[DefaultKind] = KindSpec{
|
|
Kind: DefaultKind,
|
|
Name: plugin.LocalizedText{ZhCN: "Reims vGPU 运行时", EnUS: "Reims vGPU Runtime"},
|
|
NameZh: "Reims vGPU 运行时",
|
|
NameEn: "Reims vGPU Runtime",
|
|
RequiredFiles: []string{"bin/reims-ctl.exe", "qemu/bin/qemu-system-x86_64.exe"},
|
|
DefaultAPIVersion: DefaultRuntimeAPI,
|
|
ControlExecutable: "reims-ctl",
|
|
UtilityExecutable: "reims-util",
|
|
ControlQemuEnv: "REIMS_CTL_QEMU",
|
|
Builtin: true,
|
|
PluginID: "builtin.runtime.reims-vgpu",
|
|
}
|
|
return kr
|
|
}
|
|
|
|
// Register adds or updates a runtime kind.
|
|
func (kr *KindRegistry) Register(spec KindSpec) error {
|
|
spec.Kind = strings.TrimSpace(spec.Kind)
|
|
if spec.Kind == "" {
|
|
return errors.New("runtime kind cannot be empty")
|
|
}
|
|
if !reRuntimeKind.MatchString(spec.Kind) || strings.Contains(spec.Kind, "..") {
|
|
return fmt.Errorf("invalid runtime kind %q", spec.Kind)
|
|
}
|
|
kr.mu.Lock()
|
|
defer kr.mu.Unlock()
|
|
if cur, exists := kr.kinds[spec.Kind]; exists {
|
|
if cur.Builtin {
|
|
return fmt.Errorf("cannot overwrite built-in runtime kind %q", spec.Kind)
|
|
}
|
|
if cur.PluginID != "" && spec.PluginID != "" && cur.PluginID != spec.PluginID {
|
|
return fmt.Errorf("runtime kind %q is already owned by plugin %q", spec.Kind, cur.PluginID)
|
|
}
|
|
}
|
|
kr.kinds[spec.Kind] = spec
|
|
return nil
|
|
}
|
|
|
|
// Unregister removes a non-builtin runtime kind registered by pluginID.
|
|
func (kr *KindRegistry) Unregister(kind, pluginID string) {
|
|
kr.mu.Lock()
|
|
if cur, ok := kr.kinds[kind]; ok && !cur.Builtin && (pluginID == "" || cur.PluginID == pluginID) {
|
|
delete(kr.kinds, kind)
|
|
}
|
|
kr.mu.Unlock()
|
|
}
|
|
|
|
// Get returns a registered runtime kind.
|
|
func (kr *KindRegistry) Get(kind string) (KindSpec, bool) {
|
|
kr.mu.RLock()
|
|
spec, ok := kr.kinds[kind]
|
|
kr.mu.RUnlock()
|
|
return spec, ok
|
|
}
|
|
|
|
// List returns all registered runtime kinds sorted deterministically.
|
|
func (kr *KindRegistry) List() []KindSpec {
|
|
kr.mu.RLock()
|
|
defer kr.mu.RUnlock()
|
|
out := make([]KindSpec, 0, len(kr.kinds))
|
|
for _, s := range kr.kinds {
|
|
out = append(out, s)
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
if out[i].Builtin != out[j].Builtin {
|
|
return out[i].Builtin
|
|
}
|
|
return out[i].Kind < out[j].Kind
|
|
})
|
|
return out
|
|
}
|
|
|
|
// EffectiveKind returns Manifest.Kind, defaulting to "reims-vgpu" for legacy packages.
|
|
func (m Manifest) EffectiveKind() string {
|
|
if strings.TrimSpace(m.Kind) != "" {
|
|
return strings.TrimSpace(m.Kind)
|
|
}
|
|
return DefaultKind
|
|
}
|
|
|
|
// EffectiveRuntimeAPI returns Manifest.RuntimeAPI, defaulting to "1.0.0".
|
|
func (m Manifest) EffectiveRuntimeAPI() string {
|
|
if strings.TrimSpace(m.RuntimeAPI) != "" {
|
|
return strings.TrimSpace(m.RuntimeAPI)
|
|
}
|
|
return DefaultRuntimeAPI
|
|
}
|
|
|
|
// EffectiveCapabilities returns only capabilities declared by manifest.json.
|
|
// Runtime packages declare patch-derived capabilities at build time.
|
|
func (m Manifest) EffectiveCapabilities() []string {
|
|
seen := make(map[string]bool, len(m.Capabilities))
|
|
var out []string
|
|
add := func(c string) {
|
|
c = strings.TrimSpace(c)
|
|
if c != "" && !seen[c] {
|
|
seen[c] = true
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
for _, c := range m.Capabilities {
|
|
add(c)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// buggyWin32UnicodeInputPatchSHA256 is the SHA-256 of the initial
|
|
// reims-vgpu-win32-unicode-input.patch shipped in runtime-3731afd-p2/p3, which
|
|
// duplicated physical keystrokes (issue #14). Packages built with it must not
|
|
// get the "win32-unicode-input" capability.
|
|
const buggyWin32UnicodeInputPatchSHA256 = "0b34f7c0ebdf3ca537c7f4523af97967a06924aedba7058dbc978870f561b366"
|
|
|
|
// legacyCapabilities declares capabilities for built-in packages whose
|
|
// manifest.json predates the "capabilities" field. It runs once when the
|
|
// manifest is read, so callers only ever see declared capabilities. Other kinds
|
|
// must declare their own.
|
|
func legacyCapabilities(m Manifest) []string {
|
|
if m.EffectiveKind() != DefaultKind {
|
|
return nil
|
|
}
|
|
var out []string
|
|
var hasWin32MappedRAM, hasIntelWHPXSuspend, hasVGPUSnapshotReplay bool
|
|
for _, p := range m.Patches {
|
|
low := strings.ToLower(p.Name)
|
|
var c string
|
|
switch {
|
|
case strings.Contains(low, "nested-virt-nonfatal"):
|
|
c = "nested-virt-nonfatal"
|
|
case strings.Contains(low, "startup-ipis"):
|
|
c = "win10-startup-ipis"
|
|
case strings.Contains(low, "win10-compat"):
|
|
c = "win10-compat"
|
|
case strings.Contains(low, "coroutine-fiber"):
|
|
c = "win32-coroutine-fiber"
|
|
case strings.Contains(low, "win32-unicode-input"):
|
|
if sha := strings.TrimSpace(p.SHA256); sha != "" && !strings.EqualFold(sha, buggyWin32UnicodeInputPatchSHA256) {
|
|
c = "win32-unicode-input"
|
|
}
|
|
case strings.Contains(low, "display-fixed-mode"):
|
|
c = "display-fixed-mode"
|
|
case strings.Contains(low, "win32-mapped-ram-io"):
|
|
hasWin32MappedRAM = true
|
|
case strings.Contains(low, "intel-whpx-suspend"):
|
|
hasIntelWHPXSuspend = true
|
|
case strings.Contains(low, "reims-vgpu-snapshot-replay"):
|
|
hasVGPUSnapshotReplay = true
|
|
}
|
|
if c != "" && !slices.Contains(out, c) {
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
if hasWin32MappedRAM && hasIntelWHPXSuspend && hasVGPUSnapshotReplay && !slices.Contains(out, "vm-suspend") {
|
|
out = append(out, "vm-suspend")
|
|
}
|
|
return out
|
|
}
|
|
|
|
// HasCapability reports whether manifest.json declares capability capName.
|
|
func (m Manifest) HasCapability(capName string) bool {
|
|
for _, c := range m.EffectiveCapabilities() {
|
|
if strings.EqualFold(c, capName) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// CheckCompatibility verifies that an installed runtime matches a driver's
|
|
// required runtime kind and runtime_api SemVer range (§5.2).
|
|
func CheckCompatibility(requiredKind, requiredAPIRange string, inst Installed) error {
|
|
if requiredKind == "" {
|
|
requiredKind = DefaultKind
|
|
}
|
|
gotKind := inst.Manifest.EffectiveKind()
|
|
if inst.Kind != "" {
|
|
gotKind = inst.Kind
|
|
}
|
|
if !strings.EqualFold(gotKind, requiredKind) {
|
|
return fmt.Errorf("runtime kind mismatch: driver requires %q, installed runtime %s is %q", requiredKind, inst.Commit, gotKind)
|
|
}
|
|
if strings.TrimSpace(requiredAPIRange) != "" {
|
|
gotAPI := inst.Manifest.EffectiveRuntimeAPI()
|
|
if !plugin.MatchesRange(gotAPI, requiredAPIRange) {
|
|
return fmt.Errorf("incompatible runtime_api: driver requires %s, runtime %s (%s) provides %s", requiredAPIRange, inst.Commit, gotKind, gotAPI)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateThirdPartyProvenance enforces GPL/compliance provenance rules for
|
|
// third-party runtime packages (§5.2): must declare license and source archive,
|
|
// and cannot enter the official "stable" channel.
|
|
func ValidateThirdPartyProvenance(m Manifest) error {
|
|
if strings.EqualFold(m.Channel, "stable") {
|
|
return errors.New("third-party runtime packages cannot claim the official stable channel")
|
|
}
|
|
if strings.TrimSpace(m.License) == "" {
|
|
return errors.New("third-party runtime manifest missing license declaration")
|
|
}
|
|
if strings.TrimSpace(m.SourcePackage) == "" {
|
|
return errors.New("third-party runtime manifest missing source_package (required for GPL compliance)")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListByKind returns installed runtimes matching kind.
|
|
func ListByKind(cfg *config.Config, kind string) ([]Installed, error) {
|
|
all, err := List(cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if kind == "" {
|
|
return all, nil
|
|
}
|
|
var out []Installed
|
|
for _, item := range all {
|
|
k := item.Kind
|
|
if k == "" {
|
|
k = item.Manifest.EffectiveKind()
|
|
}
|
|
if strings.EqualFold(k, kind) {
|
|
out = append(out, item)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// ResolveForKind returns the active runtime for kind, falling back to the
|
|
// newest installed runtime of that kind when only one exists (or for non-default kinds).
|
|
func ResolveForKind(cfg *config.Config, kind string) (*Installed, error) {
|
|
if kind == "" {
|
|
kind = DefaultKind
|
|
}
|
|
list, err := ListByKind(cfg, kind)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for i := range list {
|
|
if list[i].Commit == cfg.ActiveRuntime || (kind+"/"+list[i].Commit) == cfg.ActiveRuntime {
|
|
return &list[i], nil
|
|
}
|
|
}
|
|
if len(list) == 1 || (kind != DefaultKind && len(list) > 0) {
|
|
return &list[0], nil
|
|
}
|
|
return nil, fmt.Errorf("no active %s runtime installed", kind)
|
|
}
|