170 lines
5.2 KiB
Go
170 lines
5.2 KiB
Go
package macosdriver
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/macos/reims"
|
|
"github.com/Nwflower/reims-go/internal/macos/startprep"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/internal/qmp"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
var _ driver.SuspendDriver = (*ReimsMacOSDriver)(nil)
|
|
|
|
func (d *ReimsMacOSDriver) vmCapabilities(active rt.Installed, kind string) []string {
|
|
caps := d.Capabilities()
|
|
low := strings.ToLower(kind)
|
|
if active.Manifest.HasCapability("vm-suspend") && !strings.Contains(low, "arm64") && !strings.Contains(low, "aarch64") {
|
|
caps = append(caps, driver.CapSuspend)
|
|
}
|
|
return caps
|
|
}
|
|
|
|
func enrichSuspendDetail(det *reims.VMDetail) {
|
|
if det != nil && det.State == "stopped" && reims.HasSuspendState(det.Directory) {
|
|
det.State = "suspended"
|
|
if info, err := reims.ReadSuspendInfo(det.Directory); err == nil {
|
|
det.SuspendInfo = info
|
|
}
|
|
}
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Suspend(ctx context.Context, req driver.SuspendRequest) (driver.SuspendResult, error) {
|
|
c, active, err := d.startClient()
|
|
if err != nil {
|
|
return driver.SuspendResult{}, err
|
|
}
|
|
cur, release, err := startprep.LockVM(ctx, c, req.Ref.Name)
|
|
if err != nil {
|
|
return driver.SuspendResult{}, err
|
|
}
|
|
defer release()
|
|
stdout := req.Stdout
|
|
if stdout == nil {
|
|
stdout = io.Discard
|
|
}
|
|
prep, err := startprep.New(d.getConfig(), func(line string) { startLog(stdout, line) })
|
|
if err != nil {
|
|
return driver.SuspendResult{}, err
|
|
}
|
|
prep.Client, prep.Runtime = c, active
|
|
var prog func(qmp.MigrateProgress)
|
|
if req.Progress != nil {
|
|
prog = func(p qmp.MigrateProgress) {
|
|
req.Progress(driver.SuspendProgress{
|
|
Status: p.Status,
|
|
Transferred: p.Transferred,
|
|
Remaining: p.Remaining,
|
|
Total: p.Total,
|
|
})
|
|
}
|
|
}
|
|
info, err := prep.SuspendSnapshot(ctx, cur, prog)
|
|
if err != nil {
|
|
return driver.SuspendResult{}, err
|
|
}
|
|
return driver.SuspendResult{
|
|
VMStateSize: info.VMStateSize,
|
|
SavedAt: info.CreatedAt,
|
|
}, nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) Resume(ctx context.Context, req driver.ResumeRequest) (driver.StartResult, error) {
|
|
return d.resumeWithHost(ctx, req, host.Inspect)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) resumeWithHost(ctx context.Context, req driver.ResumeRequest, inspect func(string) host.Report) (driver.StartResult, error) {
|
|
c, active, err := d.startClient()
|
|
if err != nil {
|
|
return driver.StartResult{}, err
|
|
}
|
|
cur, release, err := startprep.LockVM(ctx, c, req.Ref.Name)
|
|
if err != nil {
|
|
return driver.StartResult{}, err
|
|
}
|
|
defer release()
|
|
return d.resumeLocked(ctx, c, active, cur, req.ExtraEnv, req.Stdout, req.Stderr, inspect)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) resumeLocked(ctx context.Context, c *reims.Client, active rt.Installed, cur reims.VMDetail, extraEnv []string, stdout, stderr io.Writer, inspect func(string) host.Report) (driver.StartResult, error) {
|
|
if d.opts.PrepareNetwork != nil {
|
|
releaseNet, err := d.opts.PrepareNetwork(ctx, cur.Name, true)
|
|
if err != nil {
|
|
return driver.StartResult{}, err
|
|
}
|
|
if releaseNet != nil {
|
|
defer releaseNet()
|
|
}
|
|
}
|
|
if stdout == nil {
|
|
stdout = io.Discard
|
|
}
|
|
if stderr == nil {
|
|
stderr = io.Discard
|
|
}
|
|
cfg := d.getConfig()
|
|
rep := inspect(cfg.DataRoot)
|
|
var qmpAddr string
|
|
if d.opts.ManagedByDesktop && cur.State == "stopped" && reims.HasSuspendState(cur.Directory) && rep.NeedsColdRebootWorkaround {
|
|
sockPath := qmp.RebootSocketPath(cur.Directory, cfg.TempDir(), cur.Name)
|
|
if qmp.HasWin10RebootUnixArgs(cur.ExtraArgs, sockPath) {
|
|
if err := os.Remove(sockPath); err != nil && !os.IsNotExist(err) {
|
|
return driver.StartResult{}, fmt.Errorf("remove stale reboot socket: %w", err)
|
|
}
|
|
qmpAddr = "unix:" + sockPath
|
|
}
|
|
}
|
|
if len(extraEnv) > 0 {
|
|
c.Env = append(c.Env, extraEnv...)
|
|
}
|
|
if active.Manifest.HasCapability("display-fixed-mode") &&
|
|
vmstart.UsesFixedGuestMode(cur.Directory, rep.Displays, cur.Borderless, cur.Resolution) {
|
|
c.Env = append(c.Env, "REIMS_VGPU_WINDOW_FIXED_MODE=1")
|
|
}
|
|
if d.opts.ManagedByDesktop && active.Manifest.HasCapability("client-window-close") {
|
|
c.Env = append(c.Env, vmstart.CloseRequestEnv(cur.Name)...)
|
|
}
|
|
prep, err := startprep.New(cfg, func(line string) { startLog(stdout, line) })
|
|
if err != nil {
|
|
return driver.StartResult{}, err
|
|
}
|
|
prep.Client, prep.Runtime = c, active
|
|
launchArgs := cur.ExtraArgs
|
|
if !d.opts.ManagedByDesktop {
|
|
launchArgs = qmp.StripLegacyWin10RebootArgs(launchArgs)
|
|
}
|
|
if err := prep.ResumeSnapshot(ctx, cur, launchArgs, rep, stdout, stderr); err != nil {
|
|
return driver.StartResult{}, err
|
|
}
|
|
var res driver.StartResult
|
|
if qmpAddr != "" {
|
|
res.PostStart = append(res.PostStart, plugin.PostStartAction{
|
|
PluginID: d.opts.OwnerID,
|
|
Kind: "qmp.watch_cold_reboot",
|
|
Params: map[string]any{"qmp_addr": qmpAddr},
|
|
})
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) DiscardSuspend(ctx context.Context, name string) error {
|
|
c, err := d.client()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cur, release, err := startprep.LockVM(ctx, c, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer release()
|
|
return startprep.DiscardSnapshot(c.Locale, cur)
|
|
}
|