Events to the frontend go through App.emit, and a test keeps their names in step with wails.ts. QEMU argument splitting, hidden console processes, the macOS release record, the running check and a few smaller helpers now exist once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
5.7 KiB
Go
166 lines
5.7 KiB
Go
package macosdriver
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/macos/reims"
|
|
"github.com/Nwflower/reims-go/internal/macos/startprep"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
)
|
|
|
|
var _ driver.SnapshotDriver = (*ReimsMacOSDriver)(nil)
|
|
var _ driver.DiskDriver = (*ReimsMacOSDriver)(nil)
|
|
|
|
func (d *ReimsMacOSDriver) ListSnapshots(ctx context.Context, name string) ([]driver.DiskSnapshot, error) {
|
|
c, active, err := d.startClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
det, err := c.Show(ctx, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
diskPath := det.DiskPath()
|
|
if diskPath == "" || !strings.EqualFold(filepath.Ext(diskPath), ".qcow2") {
|
|
return nil, nil
|
|
}
|
|
cfg := d.getConfig()
|
|
snaps, err := reims.ListDiskSnapshots(ctx, active.QemuImgPath(), rt.BuildChildEnv(&cfg, active), diskPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]driver.DiskSnapshot, 0, len(snaps))
|
|
for _, snap := range snaps {
|
|
out = append(out, driver.DiskSnapshot{ID: snap.ID, Name: snap.Name, CreatedAt: snap.CreatedAt, VMStateBytes: snap.VMStateBytes})
|
|
}
|
|
sort.SliceStable(out, func(i, j int) bool { return out[i].CreatedAt > out[j].CreatedAt })
|
|
return out, nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) RunSnapshot(ctx context.Context, name, operation, snapName string, stdout, stderr io.Writer) error {
|
|
clean, err := driver.ValidateSnapshotName(snapName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
flag, ok := map[string]string{"create": "-c", "restore": "-a", "delete": "-d"}[operation]
|
|
if !ok {
|
|
return fmt.Errorf("unknown snapshot operation %q", operation)
|
|
}
|
|
c, active, err := d.startClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
det, release, err := startprep.LockVM(ctx, c, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer release()
|
|
if c.IsRunning(ctx, det) {
|
|
return fmt.Errorf("stop %s before changing restore points", name)
|
|
}
|
|
if reims.HasSuspendState(det.Directory) {
|
|
return i18n.NewCodedError(c.Locale, "E_SNAPSHOT_VM_SUSPENDED", "err.suspend.action_blocked", i18n.Vars{"name": name}, nil)
|
|
}
|
|
diskPath := det.DiskPath()
|
|
if diskPath == "" {
|
|
return fmt.Errorf("VM %s has no disk path", name)
|
|
}
|
|
if !strings.EqualFold(filepath.Ext(diskPath), ".qcow2") {
|
|
return fmt.Errorf("restore points require a qcow2 disk")
|
|
}
|
|
releaseDisk, err := c.LockDiskForWrite(ctx, name, diskPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer releaseDisk()
|
|
cfg := d.getConfig()
|
|
return reims.RunDiskSnapshot(ctx, active.QemuImgPath(), rt.BuildChildEnv(&cfg, active), flag, clean, diskPath)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) ResizeDisk(ctx context.Context, name, newSize string, stdout, stderr io.Writer) (driver.DiskResizeResult, error) {
|
|
c, active, err := d.startClient()
|
|
if err != nil {
|
|
return driver.DiskResizeResult{}, err
|
|
}
|
|
det, release, err := startprep.LockVM(ctx, c, name)
|
|
if err != nil {
|
|
return driver.DiskResizeResult{}, err
|
|
}
|
|
defer release()
|
|
if c.IsRunning(ctx, det) {
|
|
return driver.DiskResizeResult{}, fmt.Errorf("stop %s before resizing its disk", name)
|
|
}
|
|
if reims.HasSuspendState(det.Directory) {
|
|
return driver.DiskResizeResult{}, i18n.NewCodedError(c.Locale, "E_RESIZE_VM_SUSPENDED", "err.suspend.action_blocked", i18n.Vars{"name": name}, nil)
|
|
}
|
|
diskPath := det.DiskPath()
|
|
if diskPath == "" {
|
|
return driver.DiskResizeResult{}, fmt.Errorf("VM %s has no disk path", name)
|
|
}
|
|
releaseDisk, err := c.LockDiskForWrite(ctx, name, diskPath)
|
|
if err != nil {
|
|
return driver.DiskResizeResult{}, err
|
|
}
|
|
defer releaseDisk()
|
|
cfg := d.getConfig()
|
|
res, err := reims.ResizeDisk(ctx, active.QemuImgPath(), rt.BuildChildEnv(&cfg, active), diskPath, newSize, stdout, stderr)
|
|
if err != nil {
|
|
return driver.DiskResizeResult{}, err
|
|
}
|
|
return driver.DiskResizeResult{Path: res.Path, OldVirtualBytes: res.OldVirtualBytes, NewVirtualBytes: res.NewVirtualBytes, GuestExpandCmd: res.GuestExpandCmd}, nil
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) CompactDisk(ctx context.Context, name string, stdout, stderr io.Writer) (driver.DiskCompactResult, error) {
|
|
return d.compactDiskWithOptions(ctx, name, reims.CompactDiskOptions{}, stdout, stderr)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) OptimizeDisk(ctx context.Context, name string, stdout, stderr io.Writer) (driver.DiskCompactResult, error) {
|
|
return d.compactDiskWithOptions(ctx, name, reims.CompactDiskOptions{EnableExtendedL2: true}, stdout, stderr)
|
|
}
|
|
|
|
func (d *ReimsMacOSDriver) compactDiskWithOptions(ctx context.Context, name string, opts reims.CompactDiskOptions, stdout, stderr io.Writer) (driver.DiskCompactResult, error) {
|
|
c, active, err := d.startClient()
|
|
if err != nil {
|
|
return driver.DiskCompactResult{}, err
|
|
}
|
|
det, release, err := startprep.LockVM(ctx, c, name)
|
|
if err != nil {
|
|
return driver.DiskCompactResult{}, err
|
|
}
|
|
defer release()
|
|
if c.IsRunning(ctx, det) {
|
|
return driver.DiskCompactResult{}, fmt.Errorf("stop %s before rewriting its disk", name)
|
|
}
|
|
if reims.HasSuspendState(det.Directory) {
|
|
return driver.DiskCompactResult{}, i18n.NewCodedError(c.Locale, "E_COMPACT_VM_SUSPENDED", "err.suspend.action_blocked", i18n.Vars{"name": name}, nil)
|
|
}
|
|
diskPath := det.DiskPath()
|
|
if diskPath == "" {
|
|
return driver.DiskCompactResult{}, fmt.Errorf("VM %s has no disk path", name)
|
|
}
|
|
releaseDisk, err := c.LockDiskForWrite(ctx, name, diskPath)
|
|
if err != nil {
|
|
return driver.DiskCompactResult{}, err
|
|
}
|
|
defer releaseDisk()
|
|
cfg := d.getConfig()
|
|
res, err := reims.CompactDisk(ctx, active.QemuImgPath(), rt.BuildChildEnv(&cfg, active), diskPath, opts, stdout, stderr)
|
|
if err != nil {
|
|
return driver.DiskCompactResult{}, err
|
|
}
|
|
return driver.DiskCompactResult{
|
|
ExtendedL2Enabled: res.ExtendedL2Enabled,
|
|
Path: res.Path,
|
|
BeforeBytes: res.BeforeBytes,
|
|
AfterBytes: res.AfterBytes,
|
|
ReclaimedBytes: res.ReclaimedBytes,
|
|
}, nil
|
|
}
|