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.
110 lines
3.1 KiB
Go
110 lines
3.1 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"libvirt.org/go/libvirt"
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
type storagePoolVolumeAbsenceProbe struct {
|
|
refresh func() error
|
|
lookup func() (bool, error)
|
|
poolXML func() (string, error)
|
|
lstat func(string) (os.FileInfo, error)
|
|
}
|
|
|
|
// proveDirPoolVolumeAbsent requires independent agreement from libvirt's
|
|
// refreshed inventory and the dir pool filesystem. A single cache miss is not
|
|
// sufficient evidence after a volume-create call may have committed.
|
|
func proveDirPoolVolumeAbsent(volumeName string, probe storagePoolVolumeAbsenceProbe) (bool, error) {
|
|
if err := probe.refresh(); err != nil {
|
|
return false, err
|
|
}
|
|
present, err := probe.lookup()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if present {
|
|
return false, nil
|
|
}
|
|
document, err := probe.poolXML()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
path, err := dirPoolVolumePath(document, volumeName)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if _, err := probe.lstat(path); err == nil {
|
|
return false, nil
|
|
} else if errors.Is(err, os.ErrNotExist) {
|
|
return true, nil
|
|
} else {
|
|
return false, err
|
|
}
|
|
}
|
|
|
|
func dirPoolVolumePath(document, volumeName string) (string, error) {
|
|
if volumeName == "" || volumeName == "." || volumeName == ".." || filepath.Base(volumeName) != volumeName {
|
|
return "", errors.New("storage volume name is not a safe filename")
|
|
}
|
|
var definition libvirtxml.StoragePool
|
|
if err := definition.Unmarshal(document); err != nil {
|
|
return "", fmt.Errorf("parse storage pool XML: %w", err)
|
|
}
|
|
if definition.Type != "dir" || definition.Target == nil {
|
|
return "", errors.New("storage pool is not a supported dir pool")
|
|
}
|
|
target := filepath.Clean(strings.TrimSpace(definition.Target.Path))
|
|
if !filepath.IsAbs(target) || target == string(filepath.Separator) {
|
|
return "", errors.New("storage dir pool target is invalid")
|
|
}
|
|
path := filepath.Join(target, volumeName)
|
|
if filepath.Dir(path) != target {
|
|
return "", errors.New("storage volume path escapes the pool target")
|
|
}
|
|
return path, nil
|
|
}
|
|
|
|
func (l *Libvirt) dirPoolVolumeAbsent(pool *libvirt.StoragePool, volumeName string) (bool, error) {
|
|
absent, err := proveDirPoolVolumeAbsent(volumeName, storagePoolVolumeAbsenceProbe{
|
|
refresh: func() error {
|
|
if err := pool.Refresh(0); err != nil {
|
|
return translateLibvirtError("refresh storage pool for absence evidence", err)
|
|
}
|
|
return nil
|
|
},
|
|
lookup: func() (bool, error) {
|
|
volume, err := pool.LookupStorageVolByName(volumeName)
|
|
if err == nil {
|
|
_ = volume.Free()
|
|
return true, nil
|
|
}
|
|
if isNoStorageVolume(err) {
|
|
return false, nil
|
|
}
|
|
return false, translateLibvirtError("lookup storage volume for absence evidence", err)
|
|
},
|
|
poolXML: func() (string, error) {
|
|
document, err := pool.GetXMLDesc(0)
|
|
if err != nil {
|
|
return "", translateLibvirtError("read storage pool definition for absence evidence", err)
|
|
}
|
|
return document, nil
|
|
},
|
|
lstat: os.Lstat,
|
|
})
|
|
if err != nil {
|
|
return false, NewError(ErrUnavailable, "storage_absence_unconfirmed",
|
|
"storage volume absence could not be confirmed", err)
|
|
}
|
|
return absent, nil
|
|
}
|