Files
Reims-Go/internal/app/disk_flow.go
T
NwflowerandClaude Opus 5.5 c639682a88 refactor: remove unused functions and keep the codebase free of them
Delete functions that no product code calls, and fold "simple plus
WithOptions" pairs into the one signature callers actually use. Rename
the live macOS lifecycle functions that were labelled Legacy.

The architecture tests now share one source loader and reject functions
without a product caller and catalog messages nobody reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:55:03 -07:00

95 lines
3.5 KiB
Go

package app
import (
"context"
"errors"
"fmt"
"strings"
"github.com/Nwflower/reims-go/internal/driver"
)
// ResizeVMDisk expands a stopped VM's virtual disk through its owning driver.
func (a *App) ResizeVMDisk(name, newSize string) (driver.DiskResizeResult, error) {
return a.ResizeVMDiskWithDriver("", name, newSize)
}
func (a *App) ResizeVMDiskWithDriver(driverID, name, newSize string) (driver.DiskResizeResult, error) {
a.mu.Lock()
svc := a.lifecycleSvc
a.mu.Unlock()
if svc == nil {
return driver.DiskResizeResult{}, errors.New("driver lifecycle is not initialized")
}
ref := driver.VMRef{DriverID: driverID, Name: name}
taskCtx, cancel := context.WithCancel(a.baseCtx())
defer cancel()
_, w, finishTask := a.ctlTaskWriter("vm-disk-resize", ref.Key(), cancel)
a.emitVMLogForDriver(driverID, name, fmt.Sprintf("Resizing disk for %s (%s)...", name, strings.TrimSpace(newSize)))
res, err := svc.ResizeDisk(taskCtx, ref, newSize, w, w)
if err != nil {
finishTask(err)
a.emitVMLogForDriver(driverID, name, "Disk resize error: "+err.Error())
return driver.DiskResizeResult{}, err
}
finishTask(nil)
a.emitVMLogForDriver(driverID, name, fmt.Sprintf("Expanded disk for %s: %.0f GB -> %.0f GB", name, float64(res.OldVirtualBytes)/float64(1<<30), float64(res.NewVirtualBytes)/float64(1<<30)))
a.wakePoll()
a.emitLifecycleStateChanged()
return res, nil
}
// CompactVMDisk reclaims unused host space through the owning driver.
func (a *App) CompactVMDisk(name string) (driver.DiskCompactResult, error) {
return a.CompactVMDiskWithDriver("", name)
}
func (a *App) CompactVMDiskWithDriver(driverID, name string) (driver.DiskCompactResult, error) {
return a.runCompactVMDisk(driverID, name, false)
}
// OptimizeVMDisk rewrites a stopped VM's qcow2 disk with extended L2 subclusters.
func (a *App) OptimizeVMDisk(name string) (driver.DiskCompactResult, error) {
return a.OptimizeVMDiskWithDriver("", name)
}
func (a *App) OptimizeVMDiskWithDriver(driverID, name string) (driver.DiskCompactResult, error) {
return a.runCompactVMDisk(driverID, name, true)
}
func (a *App) runCompactVMDisk(driverID, name string, optimize bool) (driver.DiskCompactResult, error) {
a.mu.Lock()
svc := a.lifecycleSvc
a.mu.Unlock()
if svc == nil {
return driver.DiskCompactResult{}, errors.New("driver lifecycle is not initialized")
}
ref := driver.VMRef{DriverID: driverID, Name: name}
taskCtx, cancel := context.WithCancel(a.baseCtx())
defer cancel()
taskKind := "vm-disk-compact"
message := fmt.Sprintf("Compacting disk for %s to reclaim unused space...", name)
if optimize {
taskKind = "vm-disk-optimize"
message = fmt.Sprintf("Optimizing qcow2 disk for %s with extended L2 subclusters...", name)
}
_, w, finishTask := a.ctlTaskWriter(taskKind, ref.Key(), cancel)
a.emitVMLogForDriver(driverID, name, message)
var (
res driver.DiskCompactResult
err error
)
if optimize {
res, err = svc.OptimizeDisk(taskCtx, ref, w, w)
} else {
res, err = svc.CompactDisk(taskCtx, ref, w, w)
}
if err != nil {
finishTask(err)
a.emitVMLogForDriver(driverID, name, "Disk rewrite error: "+err.Error())
return driver.DiskCompactResult{}, err
}
finishTask(nil)
a.emitVMLogForDriver(driverID, name, fmt.Sprintf("Rewrote disk for %s: %.2f GB -> %.2f GB (reclaimed %.2f GB, extended L2: %t)", name, float64(res.BeforeBytes)/float64(1<<30), float64(res.AfterBytes)/float64(1<<30), float64(res.ReclaimedBytes)/float64(1<<30), res.ExtendedL2Enabled))
a.wakePoll()
a.emitLifecycleStateChanged()
return res, nil
}