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.
170 lines
7.7 KiB
Go
170 lines
7.7 KiB
Go
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/agent/objecttransfer"
|
|
"git.luoli.live/root/virt/agent/operation"
|
|
)
|
|
|
|
const maxBackupObjectBytes = int64(5 << 40)
|
|
|
|
const backupLocalCleanupTimeout = 2 * time.Minute
|
|
|
|
func backupTemplateID(backupKey string) string { return "backup-" + backupKey }
|
|
|
|
func ValidateBackupRequest(request BackupRequest) error {
|
|
if !operationIdentifierPattern.MatchString(request.OperationID) ||
|
|
!catalogIDPattern.MatchString(request.BackupKey) ||
|
|
!displayNamePattern.MatchString(request.Name) ||
|
|
(request.ConsistencyMode != "filesystem" && request.ConsistencyMode != "crash") {
|
|
return NewError(ErrInvalid, "invalid_backup", "backup operation id, key, name, or consistency is invalid", nil)
|
|
}
|
|
if request.ObjectUpload.MaximumSizeBytes > maxBackupObjectBytes {
|
|
return NewError(ErrInvalid, "backup_object_too_large", "backup object size limit is too large", nil)
|
|
}
|
|
if err := objecttransfer.ValidateMultipartPlan(request.ObjectUpload); err != nil {
|
|
return NewError(ErrInvalid, "invalid_multipart_plan", "multipart upload plan is invalid", err)
|
|
}
|
|
if request.MigrationBundle == nil {
|
|
if request.PrepareMaintenanceMigration {
|
|
return NewError(ErrInvalid, "invalid_migration_bundle", "maintenance migration requires a migration bundle", nil)
|
|
}
|
|
return nil
|
|
}
|
|
if !request.PrepareMaintenanceMigration || request.MigrationBundle.Version != 1 ||
|
|
len(request.MigrationBundle.Snapshots) > 2 {
|
|
return NewError(ErrInvalid, "invalid_migration_bundle", "migration bundle version or snapshot count is invalid", nil)
|
|
}
|
|
seen := make(map[string]struct{}, len(request.MigrationBundle.Snapshots))
|
|
for _, snapshot := range request.MigrationBundle.Snapshots {
|
|
if !catalogIDPattern.MatchString(snapshot.SnapshotKey) || snapshot.SnapshotKey == request.BackupKey {
|
|
return NewError(ErrInvalid, "invalid_migration_bundle", "migration bundle snapshot identity is invalid", nil)
|
|
}
|
|
if _, exists := seen[snapshot.SnapshotKey]; exists {
|
|
return NewError(ErrInvalid, "invalid_migration_bundle", "migration bundle snapshot identities must be unique", nil)
|
|
}
|
|
seen[snapshot.SnapshotKey] = struct{}{}
|
|
if snapshot.ObjectUpload.MaximumSizeBytes > maxBackupObjectBytes {
|
|
return NewError(ErrInvalid, "backup_object_too_large", "migration snapshot object size limit is too large", nil)
|
|
}
|
|
if err := objecttransfer.ValidateMultipartPlan(snapshot.ObjectUpload); err != nil {
|
|
return NewError(ErrInvalid, "invalid_multipart_plan", "migration snapshot upload plan is invalid", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateRestoreBackupRequest(request RestoreBackupRequest) error {
|
|
if !operationIdentifierPattern.MatchString(request.OperationID) ||
|
|
!catalogIDPattern.MatchString(request.BackupKey) ||
|
|
request.Debian12NetworkFix == nil {
|
|
return NewError(ErrInvalid, "invalid_backup_restore", "backup restore operation id or key is invalid", nil)
|
|
}
|
|
if request.Source != "" || request.TemplateID != "" || request.TemplateRevision != 0 ||
|
|
request.TemplateGenerationToken != "" || request.GuestSSHPublicKey != "" ||
|
|
request.RootPassword != "" || request.StartupScript != "" {
|
|
return NewError(ErrInvalid, "invalid_backup_restore", "backup restore cannot replace the source image or guest credentials", nil)
|
|
}
|
|
if request.SourceSizeBytes > maxBackupObjectBytes {
|
|
return NewError(ErrInvalid, "backup_object_too_large", "backup object is too large", nil)
|
|
}
|
|
if err := objecttransfer.ValidateDownload(request.SourceURL, request.SourceSizeBytes, request.SourceSHA256); err != nil {
|
|
return NewError(ErrInvalid, "invalid_backup_object", "backup object identity is invalid", err)
|
|
}
|
|
if request.MigrationBundle != nil {
|
|
bundle := request.MigrationBundle
|
|
if bundle.Version != 1 || !bundle.StagePoweredOff || len(bundle.Snapshots) > 2 {
|
|
return NewError(ErrInvalid, "invalid_migration_bundle", "migration restore must stage a supported powered-off bundle", nil)
|
|
}
|
|
seen := make(map[string]struct{}, len(bundle.Snapshots))
|
|
for _, snapshot := range bundle.Snapshots {
|
|
if !catalogIDPattern.MatchString(snapshot.SnapshotKey) || snapshot.SnapshotKey == request.BackupKey ||
|
|
snapshot.Name == "" || len(snapshot.Name) > 64 || strings.ContainsRune(snapshot.Name, 0) ||
|
|
(snapshot.Consistency != "filesystem" && snapshot.Consistency != "crash") ||
|
|
snapshot.VirtualSizeBytes == 0 ||
|
|
snapshot.VirtualSizeBytes > uint64(maxBackupObjectBytes) ||
|
|
snapshot.CreatedAt < 1 {
|
|
return NewError(ErrInvalid, "invalid_migration_bundle", "migration snapshot metadata is invalid", nil)
|
|
}
|
|
if _, exists := seen[snapshot.SnapshotKey]; exists {
|
|
return NewError(ErrInvalid, "invalid_migration_bundle", "migration snapshot identities must be unique", nil)
|
|
}
|
|
seen[snapshot.SnapshotKey] = struct{}{}
|
|
if snapshot.SourceSizeBytes > maxBackupObjectBytes {
|
|
return NewError(ErrInvalid, "backup_object_too_large", "migration snapshot object is too large", nil)
|
|
}
|
|
if err := objecttransfer.ValidateDownload(snapshot.SourceURL, snapshot.SourceSizeBytes, snapshot.SourceSHA256); err != nil {
|
|
return NewError(ErrInvalid, "invalid_backup_object", "migration snapshot object identity is invalid", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reuse the complete create request validation for resource, network,
|
|
// hostname, policy, UUID, and derived-domain constraints. The placeholders
|
|
// satisfy only template/credential fields that are intentionally replaced
|
|
// by an existing backup disk and credential-preserving NoCloud seed.
|
|
candidate := request.CreateVMRequest
|
|
candidate.Source = "cloud"
|
|
candidate.TemplateID = backupTemplateID(request.BackupKey)
|
|
candidate.TemplateRevision = 1
|
|
candidate.TemplateGenerationToken = "backup_restore_validation"
|
|
candidate.RootPassword = "Restore1!Validation"
|
|
if err := ValidateCreateVM(candidate); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateRecoverBackupRequest(request RecoverBackupRequest) error {
|
|
if !operationIdentifierPattern.MatchString(request.OperationID) ||
|
|
!catalogIDPattern.MatchString(request.BackupKey) {
|
|
return NewError(ErrInvalid, "invalid_backup_recovery", "backup recovery operation id or key is invalid", nil)
|
|
}
|
|
if request.SourceSizeBytes > maxBackupObjectBytes {
|
|
return NewError(ErrInvalid, "backup_object_too_large", "backup object is too large", nil)
|
|
}
|
|
if err := objecttransfer.ValidateDownload(request.SourceURL, request.SourceSizeBytes, request.SourceSHA256); err != nil {
|
|
return NewError(ErrInvalid, "invalid_backup_object", "backup object identity is invalid", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func translateObjectTransferError(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
var transferErr *objecttransfer.Error
|
|
if !errors.As(err, &transferErr) {
|
|
return NewError(ErrUnavailable, "object_transfer_failed", "backup object transfer failed", err)
|
|
}
|
|
code := strings.TrimSpace(transferErr.Code)
|
|
if code == "" {
|
|
code = "object_transfer_failed"
|
|
}
|
|
translated := NewError(ErrUnavailable, code, transferErr.Message, transferErr)
|
|
if objecttransfer.IsAmbiguous(err) {
|
|
return operation.MarkUncertain(translated)
|
|
}
|
|
return translated
|
|
}
|
|
|
|
func classifyBackupUploadFailure(transferErr, cleanupErr error) error {
|
|
if cleanupErr != nil {
|
|
return operation.MarkUncertain(errors.Join(transferErr, cleanupErr))
|
|
}
|
|
// The object transfer was rejected deterministically and the complete local
|
|
// snapshot rollback was confirmed. No durable backup effect remains, so a
|
|
// new operation must not be fenced behind an inconclusive result.
|
|
return operation.MarkNotApplied(transferErr)
|
|
}
|
|
|
|
func backupLocalCleanupContext(parent context.Context) (context.Context, context.CancelFunc) {
|
|
// Remote cancellation stops data transfer, but must not prevent the local
|
|
// bounded rollback which releases a system-disk-sized temporary snapshot.
|
|
return context.WithTimeout(context.WithoutCancel(parent), backupLocalCleanupTimeout)
|
|
}
|