From 0ba61d31bf970a81e98bf5b5f5764cd44c53cd29 Mon Sep 17 00:00:00 2001 From: codex Date: Sun, 25 Oct 2026 15:07:08 +0800 Subject: [PATCH] fix(agent): accept empty libvirt backing sentinel --- agent/hypervisor/libvirt_backup.go | 14 +++++++++++--- agent/hypervisor/libvirt_backup_test.go | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/agent/hypervisor/libvirt_backup.go b/agent/hypervisor/libvirt_backup.go index 7a8d7cc8..28e5cc65 100644 --- a/agent/hypervisor/libvirt_backup.go +++ b/agent/hypervisor/libvirt_backup.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "path/filepath" + "reflect" "strings" "time" @@ -234,9 +235,12 @@ func validateLiveBackupDefinition(definition *libvirtxml.Domain, managedRootPath func domainBackingChainPresent(store *libvirtxml.DomainDiskBackingStore) bool { for store != nil { // libvirt commonly serializes an empty sentinel for a - // standalone qcow2 disk. Only a described source/format is an actual - // backing layer which makes the portable snapshot unsafe. - if store.Source != nil || store.Format != nil { + // standalone qcow2 disk. libvirtxml materializes that sentinel with a + // non-nil, zero-valued Source, so pointer presence alone is not evidence + // of a backing layer. Any actual source content, format or index remains + // fail-closed because it makes the portable snapshot unsafe. + if store.Index != 0 || store.Format != nil || + domainDiskSourcePresent(store.Source) { return true } store = store.BackingStore @@ -244,6 +248,10 @@ func domainBackingChainPresent(store *libvirtxml.DomainDiskBackingStore) bool { return false } +func domainDiskSourcePresent(source *libvirtxml.DomainDiskSource) bool { + return source != nil && !reflect.ValueOf(*source).IsZero() +} + func validateStandaloneQCOW2Volume(volume qcow2VolumeInspector, expectedCapacity uint64) (*libvirt.StorageVolInfo, error) { info, err := validateQCOW2Volume(volume, expectedCapacity) if err != nil { diff --git a/agent/hypervisor/libvirt_backup_test.go b/agent/hypervisor/libvirt_backup_test.go index 1a29b5ca..1afb69c8 100644 --- a/agent/hypervisor/libvirt_backup_test.go +++ b/agent/hypervisor/libvirt_backup_test.go @@ -87,6 +87,24 @@ func TestValidateLiveBackupDefinitionRejectsLiveOnlyDiskTopology(t *testing.T) { } } +func TestValidateLiveBackupDefinitionAcceptsSerializedEmptyBackingStoreSentinel(t *testing.T) { + const managedPath = "/var/lib/virt-cloud/managed/root.qcow2" + var domain libvirtxml.Domain + if err := domain.Unmarshal(` + + + + + + + `); err != nil { + t.Fatalf("unmarshal live libvirt XML: %v", err) + } + if err := validateLiveBackupDefinition(&domain, managedPath); err != nil { + t.Fatalf("serialized empty libvirt backing-store sentinel rejected: %v", err) + } +} + func TestValidateStandaloneQCOW2RejectsBackingChain(t *testing.T) { const capacity = 40 << 30 standalone := backupVolumeInspector{