baseVolumeName was base-<key>.<format>, byte-identical to what the kvm provider builds in the same pool -- for URL images too, since kvm takes hex(sum[:8]) and kvm2 takes hex(sum[:])[:16], the same eight bytes -- with a completely separate lock table. The shipped config has a live kvm-home entry and a commented-out kvm2-home entry on the same host, the same key debian-13 and the same default pool, and the comment invites the operator to enable it. Both integration tests use key test-image. On beelink both live overlays back onto kvm's base-debian-13.qcow2. Since cachedImage accepts any 64-hex marker when no sha256 is configured and never re-hashes the base, that meant two entries pointing one key at different URLs silently shared a file; a concurrent cold fetch let one entry delete the other's half-uploaded base and boot a guest from the remains; and a sha256 edit deleted the base in place under live overlays, which libvirt permits without complaint. The name now carries the provider entry: base-<entry>-<key>.<format>. imageSource gained an Entry field and resolveImage takes the provider name, so every production call site follows from imageSource.volumeName(). That closes the cross-entry sharing outright, kvm<->kvm2 and kvm2<->kvm2 alike, and turns the per-Provider imageLocks from a latent bug into a correct one: with names unique per entry, two entries cannot contend for a file, so no process-wide lock table is needed. The entry name now reaches a file name, so New checks it against the existing kvmIdentifier rule, exported as provider.IsKVMIdentifier rather than duplicated. Provider names are otherwise validated for all types at once and only for being non-empty and unique, so a name with a slash would have produced a volume nothing could address. One companion fix, because the naming alone does not stop it: cachedImage's digest-mismatch branch deleted the marker and the base and refetched. It now returns ErrPermanent naming both digests and telling the operator what to do. That branch was the remaining path that unlinks a good file under running domains, and the contract already said "add a new key instead" -- the code was contradicting it. The no-marker and non-hex-marker branches keep deleting, since there the base is debris. Not fixed, and said so in the docs rather than left to look closed: editing images.<key>.url is still a silent no-op, since nothing records the URL; and cross-process fencing still needs the deferred atomic publish (upload to a temp name, verify, publish), which lands where H2 put the discard. Migration is a non-event. The base-debian-13.qcow2 on beelink is kvm's, kvm keeps using it untouched, and kvm2 downloads its own copy once per host. Nothing is replaced under a running guest; the pool needs room for both, which the prerequisites now say. The eviction recipe in the guide named base-debian-13.qcow2, which after this change is kvm's file -- following it would have deleted the wrong provider's base. It now greps for the prefix and deletes the marker first. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
103 lines
3.4 KiB
Go
103 lines
3.4 KiB
Go
package kvm2
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/xml"
|
|
|
|
"gitea.home.hrajfrisbee.cz/kacerr/egress-proxies-operator/internal/provider"
|
|
)
|
|
|
|
// Volume names in the host's pool (spec entries 4 and 10). The per-VM
|
|
// volumes derive from the domain name; the base image from its catalogue
|
|
// key or, for a URL image, from a hash of the URL.
|
|
func overlayVolumeName(name string) string { return name + ".qcow2" }
|
|
func seedVolumeName(name string) string { return name + "-seed.iso" }
|
|
|
|
// baseVolumeName carries the provider entry because the base is shared by
|
|
// every Proxy on a host but must not be shared by two entries: the kvm
|
|
// provider builds base-<key>.<format> in the same pool from the same
|
|
// catalogue shape, and two kvm2 entries pointing one key at different URLs
|
|
// would otherwise agree on the name and disagree on the contents.
|
|
func baseVolumeName(entry, key, format string) string {
|
|
return "base-" + entry + "-" + key + "." + format
|
|
}
|
|
func markerVolumeName(baseName string) string { return baseName + ".sha256" }
|
|
|
|
// urlImageKey stands in for the catalogue key of an image given as a URL:
|
|
// the first 16 hex characters of sha256(url).
|
|
func urlImageKey(url string) string {
|
|
sum := sha256.Sum256([]byte(url))
|
|
return hex.EncodeToString(sum[:])[:16]
|
|
}
|
|
|
|
// volumeSpec is what StorageVolCreateXML needs. BackingPath and
|
|
// BackingFormat are set for an overlay only.
|
|
type volumeSpec struct {
|
|
Name string
|
|
CapacityBytes int64
|
|
Format string
|
|
BackingPath string
|
|
BackingFormat string
|
|
}
|
|
|
|
type volumeXML struct {
|
|
XMLName xml.Name `xml:"volume"`
|
|
Type string `xml:"type,attr"`
|
|
Name string `xml:"name"`
|
|
Capacity volumeCapacityXML `xml:"capacity"`
|
|
Target volumeTargetXML `xml:"target"`
|
|
BackingStore *backingStoreXML `xml:"backingStore"`
|
|
}
|
|
|
|
type volumeCapacityXML struct {
|
|
Unit string `xml:"unit,attr"`
|
|
Value int64 `xml:",chardata"`
|
|
}
|
|
|
|
type volumeTargetXML struct {
|
|
Format volumeFormatXML `xml:"format"`
|
|
}
|
|
|
|
type volumeFormatXML struct {
|
|
Type string `xml:"type,attr"`
|
|
}
|
|
|
|
type backingStoreXML struct {
|
|
Path string `xml:"path"`
|
|
Format volumeFormatXML `xml:"format"`
|
|
}
|
|
|
|
func validVolumeFormat(f string) bool {
|
|
return f == provider.KVMFormatQcow2 || f == provider.KVMFormatRaw
|
|
}
|
|
|
|
func buildVolumeXML(v volumeSpec) (string, error) {
|
|
if v.Name == "" {
|
|
return "", permanentf("volume XML: name is empty")
|
|
}
|
|
if v.CapacityBytes <= 0 {
|
|
return "", permanentf("volume XML for %q: capacity must be positive, got %d", v.Name, v.CapacityBytes)
|
|
}
|
|
if !validVolumeFormat(v.Format) {
|
|
return "", permanentf("volume XML for %q: unknown format %q (want %s or %s)", v.Name, v.Format, provider.KVMFormatQcow2, provider.KVMFormatRaw)
|
|
}
|
|
x := volumeXML{
|
|
Type: "file",
|
|
Name: v.Name,
|
|
Capacity: volumeCapacityXML{Unit: "bytes", Value: v.CapacityBytes},
|
|
Target: volumeTargetXML{Format: volumeFormatXML{Type: v.Format}},
|
|
}
|
|
if v.BackingPath != "" {
|
|
if !validVolumeFormat(v.BackingFormat) {
|
|
return "", permanentf("volume XML for %q: unknown backing format %q (want %s or %s)", v.Name, v.BackingFormat, provider.KVMFormatQcow2, provider.KVMFormatRaw)
|
|
}
|
|
x.BackingStore = &backingStoreXML{Path: v.BackingPath, Format: volumeFormatXML{Type: v.BackingFormat}}
|
|
}
|
|
out, err := xml.MarshalIndent(x, "", " ")
|
|
if err != nil {
|
|
return "", permanentf("volume XML for %q: %v", v.Name, err)
|
|
}
|
|
return string(out), nil
|
|
}
|