You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
309 lines
9.4 KiB
Go
309 lines
9.4 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"git.luoli.live/root/virt/agent/operation"
|
|
"libvirt.org/go/libvirt"
|
|
)
|
|
|
|
// backupWorkspaceResidue resolves exactly one operation-owned portable-backup
|
|
// copy. New backups use vbackup-* without domain snapshot metadata. The
|
|
// legacy vsnap-* form is accepted only so an evidence-bound cleanup can retire
|
|
// residue produced before the workspace split.
|
|
func (l *Libvirt) backupWorkspaceResidue(uuid, key string) (
|
|
name string, volume *libvirt.StorageVol, legacy bool, err error,
|
|
) {
|
|
workspaceName := backupWorkspaceVolumeName(uuid, key)
|
|
workspace, err := l.lookupOptionalManagedVolume(
|
|
l.storagePoolName(StorageRoleManaged), workspaceName,
|
|
)
|
|
if err != nil {
|
|
return "", nil, false, err
|
|
}
|
|
legacyName := snapshotVolumeName(uuid, key)
|
|
legacyVolume, legacyErr := l.lookupOptionalManagedVolume(
|
|
l.storagePoolName(StorageRoleManaged), legacyName,
|
|
)
|
|
if legacyErr != nil {
|
|
if workspace != nil {
|
|
workspace.Free()
|
|
}
|
|
return "", nil, false, legacyErr
|
|
}
|
|
if workspace != nil && legacyVolume != nil {
|
|
workspace.Free()
|
|
legacyVolume.Free()
|
|
return "", nil, false, NewError(
|
|
ErrConflict, "backup_workspace_ambiguous",
|
|
"both current and legacy backup workspaces are present", nil,
|
|
)
|
|
}
|
|
if workspace != nil {
|
|
return workspaceName, workspace, false, nil
|
|
}
|
|
if legacyVolume != nil {
|
|
return legacyName, legacyVolume, true, nil
|
|
}
|
|
return workspaceName, nil, false, nil
|
|
}
|
|
|
|
// cleanupBackupWorkspace is the bounded fast-path used immediately after an
|
|
// upload. It never mutates a user snapshot: current workspaces have no
|
|
// snapshot metadata, while a legacy workspace must match its exact metadata
|
|
// before the existing snapshot deletion contract is reused.
|
|
func (l *Libvirt) cleanupBackupWorkspace(ctx context.Context, uuid, key string) error {
|
|
domain, err := l.domainByUUID(ctx, uuid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer domain.Free()
|
|
_, metadata, err := requireManagedDomain(domain)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
metadataFound := false
|
|
for _, stored := range metadata.Snapshots {
|
|
if stored.Key == key {
|
|
metadataFound = true
|
|
break
|
|
}
|
|
}
|
|
name, volume, legacy, err := l.backupWorkspaceResidue(uuid, key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if volume == nil {
|
|
if metadataFound {
|
|
return NewError(
|
|
ErrConflict, "backup_workspace_inconsistent",
|
|
"backup workspace metadata remains without its volume", nil,
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
volume.Free()
|
|
if legacy {
|
|
if !metadataFound {
|
|
return NewError(
|
|
ErrConflict, "backup_workspace_inconsistent",
|
|
"legacy backup workspace is missing its metadata", nil,
|
|
)
|
|
}
|
|
_, err := l.DeleteSnapshot(ctx, uuid, key, SnapshotMutationRequest{})
|
|
return err
|
|
}
|
|
if metadataFound {
|
|
return NewError(
|
|
ErrConflict, "backup_workspace_inconsistent",
|
|
"current backup workspace collides with user snapshot metadata", nil,
|
|
)
|
|
}
|
|
if name != backupWorkspaceVolumeName(uuid, key) {
|
|
return NewError(
|
|
ErrConflict, "backup_workspace_identity_invalid",
|
|
"backup workspace identity is invalid", nil,
|
|
)
|
|
}
|
|
return l.deleteUnreferencedBackupVolume(domain, uuid, key, name)
|
|
}
|
|
|
|
func (l *Libvirt) deleteUnreferencedBackupVolume(domain *libvirt.Domain,
|
|
uuid, key, volumeName string) error {
|
|
if !catalogIDPattern.MatchString(key) {
|
|
return operation.MarkNotApplied(NewError(
|
|
ErrConflict, "backup_workspace_identity_invalid",
|
|
"backup workspace identity is invalid", nil,
|
|
))
|
|
}
|
|
if volumeName != backupWorkspaceVolumeName(uuid, key) &&
|
|
volumeName != snapshotVolumeName(uuid, key) {
|
|
return operation.MarkNotApplied(NewError(
|
|
ErrConflict, "backup_workspace_identity_invalid",
|
|
"backup workspace identity is invalid", nil,
|
|
))
|
|
}
|
|
if domain != nil {
|
|
if err := ensureNoActiveDomainJobIfRunning(domain); err != nil {
|
|
return operation.MarkNotApplied(err)
|
|
}
|
|
}
|
|
volume, err := l.lookupOptionalManagedVolume(
|
|
l.storagePoolName(StorageRoleManaged), volumeName,
|
|
)
|
|
if err != nil {
|
|
return operation.MarkNotApplied(err)
|
|
}
|
|
if volume == nil {
|
|
return nil
|
|
}
|
|
defer volume.Free()
|
|
if _, err := validateStandaloneQCOW2Volume(volume, 0); err != nil {
|
|
return operation.MarkNotApplied(err)
|
|
}
|
|
path, err := volume.GetPath()
|
|
if err != nil {
|
|
return operation.MarkNotApplied(
|
|
translateLibvirtError("resolve backup workspace path", err),
|
|
)
|
|
}
|
|
referenced, err := l.anyDomainUsesVolume(
|
|
path, l.storagePoolName(StorageRoleManaged), volumeName,
|
|
)
|
|
if err != nil {
|
|
return operation.MarkNotApplied(err)
|
|
}
|
|
if referenced {
|
|
return operation.MarkNotApplied(NewError(
|
|
ErrConflict, "backup_workspace_in_use",
|
|
"backup workspace is referenced by a domain", nil,
|
|
))
|
|
}
|
|
if err := volume.Delete(libvirt.STORAGE_VOL_DELETE_NORMAL); err != nil {
|
|
observed, inspectErr := l.lookupOptionalManagedVolume(
|
|
l.storagePoolName(StorageRoleManaged), volumeName,
|
|
)
|
|
if observed != nil {
|
|
observed.Free()
|
|
}
|
|
if inspectErr != nil || observed != nil {
|
|
return operation.MarkUncertain(errors.Join(
|
|
mutationLibvirtError("delete backup workspace", err), inspectErr,
|
|
))
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CleanupBackupWorkspace executes one fresh evidence-bound plan. It is a
|
|
// successor operation: the original successful backup receipt remains
|
|
// immutable and continues to prove the remote object identity.
|
|
func (l *Libvirt) CleanupBackupWorkspace(ctx context.Context, uuid string,
|
|
request CleanupBackupWorkspaceRequest) (CleanupBackupWorkspaceResult, error) {
|
|
if err := ValidateCleanupBackupWorkspaceRequest(request); err != nil {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkNotApplied(err)
|
|
}
|
|
prior := request.PriorOperation
|
|
parts := strings.Split(prior.Action, ":")
|
|
if prior.OperationID != request.PriorOperationID ||
|
|
!strings.EqualFold(prior.ResourceID, uuid) || len(parts) != 3 ||
|
|
parts[0] != "create-backup" || !catalogIDPattern.MatchString(parts[1]) ||
|
|
!successfulBackupCleanupPending(prior) {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkNotApplied(NewError(
|
|
ErrConflict, "backup_workspace_cleanup_not_authorized",
|
|
"backup workspace cleanup requires the exact successful backup operation", nil,
|
|
))
|
|
}
|
|
key := parts[1]
|
|
domain, err := l.domainByUUID(ctx, uuid)
|
|
domainAbsent := errors.Is(err, ErrNotFound)
|
|
if err != nil && !domainAbsent {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkNotApplied(err)
|
|
}
|
|
if domain != nil {
|
|
defer domain.Free()
|
|
}
|
|
|
|
inspection := l.inspectSuccessfulBackupCleanupPending(ctx, prior)
|
|
if inspection.Cleanup.Available {
|
|
if request.CleanupPlanID == "" || !hashEqualsFold(
|
|
request.CleanupPlanID, inspection.Cleanup.PlanID,
|
|
) {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkNotApplied(NewError(
|
|
ErrConflict, "backup_workspace_cleanup_plan_changed",
|
|
"backup workspace cleanup plan changed; inspect again", nil,
|
|
))
|
|
}
|
|
} else if request.CleanupPlanID != "" {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkNotApplied(NewError(
|
|
ErrConflict, "backup_workspace_cleanup_plan_changed",
|
|
"backup workspace cleanup plan is no longer present", nil,
|
|
))
|
|
}
|
|
|
|
metadataFound := false
|
|
if !domainAbsent {
|
|
_, metadata, metadataErr := requireManagedDomain(domain)
|
|
if metadataErr != nil {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkNotApplied(metadataErr)
|
|
}
|
|
for _, stored := range metadata.Snapshots {
|
|
if stored.Key == key {
|
|
metadataFound = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
volumeName, volume, legacy, err := l.backupWorkspaceResidue(uuid, key)
|
|
if err != nil {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkNotApplied(err)
|
|
}
|
|
volumePresent := volume != nil
|
|
if volume != nil {
|
|
volume.Free()
|
|
}
|
|
if !volumePresent && !metadataFound {
|
|
return CleanupBackupWorkspaceResult{
|
|
BackupKey: key, Cleaned: true, WorkspaceAbsent: true, MetadataAbsent: true,
|
|
}, nil
|
|
}
|
|
if !inspection.Cleanup.Available {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkNotApplied(NewError(
|
|
ErrConflict, "backup_workspace_cleanup_unavailable",
|
|
"backup workspace is not currently safe to clean", nil,
|
|
))
|
|
}
|
|
if metadataFound {
|
|
if volumePresent && !legacy {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkNotApplied(NewError(
|
|
ErrConflict, "backup_workspace_inconsistent",
|
|
"current backup workspace collides with user snapshot metadata", nil,
|
|
))
|
|
}
|
|
if _, err := l.DeleteSnapshot(
|
|
ctx, uuid, key, SnapshotMutationRequest{OperationID: request.OperationID},
|
|
); err != nil {
|
|
return CleanupBackupWorkspaceResult{}, err
|
|
}
|
|
} else if volumePresent {
|
|
if err := l.deleteUnreferencedBackupVolume(
|
|
domain, uuid, key, volumeName,
|
|
); err != nil {
|
|
return CleanupBackupWorkspaceResult{}, err
|
|
}
|
|
}
|
|
|
|
_, remaining, _, err := l.backupWorkspaceResidue(uuid, key)
|
|
if err != nil {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkUncertain(err)
|
|
}
|
|
if remaining != nil {
|
|
remaining.Free()
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkUncertain(NewError(
|
|
ErrConflict, "backup_workspace_cleanup_pending",
|
|
"backup workspace remains after cleanup", nil,
|
|
))
|
|
}
|
|
if !domainAbsent {
|
|
_, current, metadataErr := requireManagedDomain(domain)
|
|
if metadataErr != nil {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkUncertain(metadataErr)
|
|
}
|
|
for _, stored := range current.Snapshots {
|
|
if stored.Key == key {
|
|
return CleanupBackupWorkspaceResult{}, operation.MarkUncertain(NewError(
|
|
ErrConflict, "backup_workspace_cleanup_pending",
|
|
"legacy backup metadata remains after cleanup", nil,
|
|
))
|
|
}
|
|
}
|
|
}
|
|
return CleanupBackupWorkspaceResult{
|
|
BackupKey: key, Cleaned: true, WorkspaceAbsent: true, MetadataAbsent: true,
|
|
}, nil
|
|
}
|