149 lines
5.3 KiB
Go
149 lines
5.3 KiB
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/macos/reims"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// CloneVMCoW derives a new VM instantaneously from a stopped source VM using a
|
|
// qcow2 copy-on-write overlay (`qemu-img create -f qcow2 -b <src> -F qcow2`)
|
|
// so the source golden disk is never mutated while the clone gets its own fresh
|
|
// OpenCore identity (SMBIOS/serial/MAC) via `reims-ctl create --disk --disk-mode in-place`.
|
|
func (a *App) CloneVMCoW(sourceName, cloneName string) error {
|
|
loc := a.locale()
|
|
cloneName = strings.TrimSpace(cloneName)
|
|
if cloneName == "" {
|
|
return i18n.NewCodedError(loc, "E_VM_CLONE_EMPTY_NAME", "err.clone.missing_name", nil, nil)
|
|
}
|
|
if strings.EqualFold(sourceName, cloneName) {
|
|
return i18n.NewCodedError(loc, "E_VM_CLONE_SAME_NAME", "err.clone.same_name", nil, nil)
|
|
}
|
|
|
|
a.mu.Lock()
|
|
cfgCopy := *a.cfg
|
|
a.mu.Unlock()
|
|
cfgCopy.Locale = loc
|
|
active, err := rt.ResolveActive(&cfgCopy)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c := reims.NewClient(&cfgCopy, *active)
|
|
ctx := a.baseCtx()
|
|
|
|
src, err := a.GetVMDetail(sourceName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if a.isVMRunning(ctx, c, sourceName, src) {
|
|
return i18n.NewCodedError(loc, "E_VM_CLONE_RUNNING", "err.clone.source_running", i18n.Vars{"name": sourceName}, nil)
|
|
}
|
|
if src.HasInstaller {
|
|
return i18n.NewCodedError(loc, "E_VM_CLONE_RECOVERY", "err.clone.in_recovery", i18n.Vars{"name": sourceName}, nil)
|
|
}
|
|
srcDisk := src.Disk
|
|
if srcDisk != "" && !filepath.IsAbs(srcDisk) && src.Directory != "" {
|
|
srcDisk = filepath.Join(src.Directory, srcDisk)
|
|
}
|
|
if srcDisk == "" {
|
|
return i18n.NewCodedError(loc, "E_VM_CLONE_NO_DISK", "err.clone.no_disk", i18n.Vars{"name": sourceName}, nil)
|
|
}
|
|
if _, err := os.Stat(srcDisk); err != nil {
|
|
return i18n.NewCodedError(loc, "E_VM_CLONE_DISK_MISSING", "err.clone.disk_missing", i18n.Vars{"path": srcDisk}, err)
|
|
}
|
|
|
|
// Validate clone name and check for name collisions.
|
|
rep := host.Inspect(cfgCopy.DataRoot)
|
|
nameCheck := reims.ValidateCreateLocalized(reims.CreateOpts{Name: cloneName, CPUs: max(src.CPUs, 1), Memory: src.Memory}, rep, loc)
|
|
for _, iss := range nameCheck {
|
|
if iss.Field == "name" {
|
|
return i18n.NewCodedError(loc, "E_VM_CLONE_INVALID_NAME", "validate."+iss.Code, i18n.Vars{"name": cloneName}, nil)
|
|
}
|
|
}
|
|
if existing, err := c.List(ctx); err == nil {
|
|
for _, vm := range existing {
|
|
if strings.EqualFold(vm.Name, cloneName) {
|
|
return i18n.NewCodedError(loc, "E_VM_NAME_TAKEN", "validate.name_taken", i18n.Vars{"name": cloneName}, nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
overlayPath := filepath.Join(cfgCopy.ImagesDir(), fmt.Sprintf("cow-%s.qcow2", cloneName))
|
|
if _, err := os.Stat(overlayPath); err == nil {
|
|
return i18n.NewCodedError(loc, "E_VM_CLONE_OVERLAY_EXISTS", "err.clone.overlay_exists", i18n.Vars{"path": overlayPath}, nil)
|
|
}
|
|
|
|
w := vmstart.LineWriter(func(line string) { a.emitLog(cloneName, line) })
|
|
env := rt.BuildChildEnv(&cfgCopy, *active)
|
|
a.emitLog(cloneName, fmt.Sprintf("Creating CoW overlay for %s backed by %s...", cloneName, filepath.Base(srcDisk)))
|
|
if err := reims.CreateCoWOverlay(ctx, active.QemuImgPath(), env, srcDisk, overlayPath, w, w); err != nil {
|
|
return err
|
|
}
|
|
|
|
audio := src.Audio
|
|
network := src.Network
|
|
logs := src.Logs
|
|
capture := src.Capture
|
|
createOpts := reims.CreateOpts{
|
|
Name: cloneName,
|
|
SourceMode: "disk",
|
|
Disk: overlayPath,
|
|
DiskMode: "in-place",
|
|
DiskController: src.DiskController,
|
|
CPUs: src.CPUs,
|
|
Memory: src.Memory,
|
|
Graphics: src.Graphics,
|
|
GPU: src.GPU,
|
|
Resolution: src.Resolution,
|
|
Displays: src.Displays,
|
|
Capture: &capture,
|
|
Audio: &audio,
|
|
Network: &network,
|
|
Logs: &logs,
|
|
Start: false,
|
|
}
|
|
if err := c.Create(ctx, createOpts, w, w); err != nil {
|
|
_ = os.Remove(overlayPath)
|
|
return err
|
|
}
|
|
if postSet, ok := reims.PostCreateSetOpts(createOpts); ok {
|
|
if serr := c.Set(ctx, postSet, w, w); serr != nil {
|
|
a.emitLog(cloneName, fmt.Sprintf("Warning: could not apply clone settings: %v", serr))
|
|
}
|
|
}
|
|
if src.GuestLang != "" || vmstart.HasSavedVMAffinity(src.Directory) {
|
|
if cloned, serr := c.Show(ctx, cloneName); serr == nil && cloned.Directory != "" {
|
|
if src.GuestLang != "" {
|
|
prep, lerr := a.prep(cloneName)
|
|
if lerr == nil {
|
|
lerr = prep.ApplyGuestLang(ctx, cloned.Directory, src.GuestLang, false)
|
|
}
|
|
if lerr != nil {
|
|
a.emitLog(cloneName, fmt.Sprintf("Warning: could not apply clone language (%s): %v", src.GuestLang, lerr))
|
|
}
|
|
}
|
|
if werr := writeVMMacOS(cloned.Directory, readVMMacOS(src.Directory)); werr != nil {
|
|
a.emitLog(cloneName, fmt.Sprintf("Warning: could not copy macOS release: %v", werr))
|
|
}
|
|
if vmstart.HasSavedVMAffinity(src.Directory) {
|
|
if _, aerr := vmstart.SaveVMAffinity(cloned.Directory, vmstart.ReadVMAffinity(src.Directory), rep.LogicalCPUs); aerr != nil {
|
|
a.emitLog(cloneName, fmt.Sprintf("Warning: could not copy CPU affinity setting: %v", aerr))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
a.emitLog(cloneName, fmt.Sprintf("CoW clone %s ready (backing image %s untouched)", cloneName, srcDisk))
|
|
if a.ctx != nil {
|
|
wailsRuntime.EventsEmit(a.ctx, "reims:state-changed", true)
|
|
}
|
|
return nil
|
|
}
|