feat(disk,host,ui): add qcow2 extended_l2 optimization, snapshot rewrite guard, and Defender exclusion guidance
74 lines
2.5 KiB
Go
74 lines
2.5 KiB
Go
package driver
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
// DiskSnapshot is a driver-neutral restore point description.
|
|
type DiskSnapshot struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
CreatedAt int64 `json:"created_at"`
|
|
VMStateBytes int64 `json:"vm_state_bytes"`
|
|
}
|
|
|
|
// DiskResizeResult describes a virtual-disk expansion and the guest follow-up command.
|
|
type DiskResizeResult struct {
|
|
Path string `json:"path"`
|
|
OldVirtualBytes uint64 `json:"old_virtual_bytes"`
|
|
NewVirtualBytes uint64 `json:"new_virtual_bytes"`
|
|
GuestExpandCmd string `json:"guest_expand_cmd"`
|
|
}
|
|
|
|
// DiskCompactResult describes host space reclaimed by rewriting a disk.
|
|
type DiskCompactResult struct {
|
|
ExtendedL2Enabled bool `json:"extended_l2_enabled,omitempty"`
|
|
Path string `json:"path"`
|
|
BeforeBytes uint64 `json:"before_bytes"`
|
|
AfterBytes uint64 `json:"after_bytes"`
|
|
ReclaimedBytes uint64 `json:"reclaimed_bytes"`
|
|
}
|
|
|
|
// ValidateSnapshotName checks a restore-point tag without assuming a disk engine.
|
|
func ValidateSnapshotName(snapName string) (string, error) {
|
|
snapName = strings.TrimSpace(snapName)
|
|
if snapName == "" {
|
|
return "", fmt.Errorf("snapshot name is required")
|
|
}
|
|
if len([]rune(snapName)) > 64 {
|
|
return "", fmt.Errorf("snapshot name must be 64 characters or fewer")
|
|
}
|
|
for _, r := range snapName {
|
|
if r < 0x20 || r == 0x7f || r == 34 || r == 39 || r == 96 || r == 92 {
|
|
return "", fmt.Errorf("snapshot name contains invalid characters")
|
|
}
|
|
}
|
|
return snapName, nil
|
|
}
|
|
|
|
// SnapshotDriver exposes driver-owned restore points.
|
|
type SnapshotDriver interface {
|
|
ListSnapshots(context.Context, string) ([]DiskSnapshot, error)
|
|
RunSnapshot(context.Context, string, string, string, io.Writer, io.Writer) error
|
|
}
|
|
|
|
// CloneDriver creates a copy-on-write VM from a stopped source VM.
|
|
type CloneDriver interface {
|
|
CloneCoW(context.Context, string, string, io.Writer, io.Writer) error
|
|
}
|
|
|
|
// DiskDriver exposes online-safe orchestration around driver-owned disk images.
|
|
type DiskDriver interface {
|
|
ResizeDisk(context.Context, string, string, io.Writer, io.Writer) (DiskResizeResult, error)
|
|
CompactDisk(context.Context, string, io.Writer, io.Writer) (DiskCompactResult, error)
|
|
}
|
|
|
|
// DiskOptimizer is an optional extension for drivers that can rewrite a disk
|
|
// with performance-oriented image options (such as qcow2 extended_l2 subclusters).
|
|
type DiskOptimizer interface {
|
|
OptimizeDisk(context.Context, string, io.Writer, io.Writer) (DiskCompactResult, error)
|
|
}
|