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.
359 lines
11 KiB
Go
359 lines
11 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"libvirt.org/go/libvirt"
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
const (
|
|
maxCapabilityNetworkSelectors = 64
|
|
capabilityMissing = "not_configured"
|
|
capabilityUnreadable = "not_readable"
|
|
capabilityInvalid = "invalid_resource"
|
|
capabilityUnavailable = "unavailable"
|
|
capabilityInactive = "inactive"
|
|
capabilityExhausted = "no_available_capacity"
|
|
capabilityUnsupported = "unsupported"
|
|
capabilitySelectorMissing = "no_network_selectors"
|
|
capabilitySelectorUnusable = "network_selector_unavailable"
|
|
)
|
|
|
|
type capabilityPrerequisites struct {
|
|
firmwareLoader bool
|
|
nvramTemplate bool
|
|
nvramDirectory bool
|
|
managedPool bool
|
|
snapshotPool bool
|
|
templatePool bool
|
|
seedPool bool
|
|
networks bool
|
|
nwfilter bool
|
|
q35UEFI bool
|
|
liveBackup bool
|
|
}
|
|
|
|
func (p capabilityPrerequisites) features() FeatureSet {
|
|
uefi := p.firmwareLoader && p.nvramTemplate && p.nvramDirectory && p.q35UEFI
|
|
create := uefi && p.managedPool && p.templatePool && p.seedPool && p.networks && p.nwfilter
|
|
return FeatureSet{
|
|
CreateVM: create,
|
|
DeleteVM: p.managedPool && p.seedPool,
|
|
Power: true,
|
|
Settings: p.seedPool,
|
|
Network: p.seedPool && p.networks && p.nwfilter,
|
|
Reinstall: create,
|
|
Snapshots: p.managedPool && p.snapshotPool && p.liveBackup,
|
|
Firewall: p.nwfilter,
|
|
Rescue: p.templatePool && p.seedPool,
|
|
Metrics: true,
|
|
Console: true,
|
|
Templates: p.templatePool && p.seedPool,
|
|
UserISO: p.seedPool,
|
|
UEFI: uefi,
|
|
SecureBoot: false,
|
|
Reconciliation: create,
|
|
}
|
|
}
|
|
|
|
func (l *Libvirt) probeRuntimeCapabilities() (FeatureSet, map[string]CapabilityCheck, []string) {
|
|
q35UEFI, liveBackup := l.probeDomainCapabilities()
|
|
networkCheck, networkSelectors := probeNetworkSelectorSet(
|
|
l.local.NetworkSelectors, l.probeNetworkSource,
|
|
)
|
|
checks := map[string]CapabilityCheck{
|
|
"firmware_loader": probeReadableRegularFile(l.local.FirmwareLoader),
|
|
"firmware_nvram_template": probeReadableRegularFile(l.local.FirmwareNVRAMTemplate),
|
|
"nvram_directory": probeExistingDirectory(l.local.NVRAMDirectory),
|
|
"managed_storage_pool": l.probeStoragePool(l.local.ManagedStoragePool),
|
|
"snapshot_storage_pool": l.probeSnapshotStoragePool(l.local.ManagedStoragePool),
|
|
"template_storage_pool": l.probeStoragePool(l.local.TemplateStoragePool),
|
|
"seed_storage_pool": l.probeStoragePool(l.local.SeedStoragePool),
|
|
"network_selectors": networkCheck,
|
|
"nwfilter_api": l.probeNWFilterAPI(),
|
|
"q35_uefi": q35UEFI,
|
|
"live_backup": liveBackup,
|
|
}
|
|
prerequisites := capabilityPrerequisites{
|
|
firmwareLoader: checks["firmware_loader"].Available,
|
|
nvramTemplate: checks["firmware_nvram_template"].Available,
|
|
nvramDirectory: checks["nvram_directory"].Available,
|
|
managedPool: checks["managed_storage_pool"].Available,
|
|
snapshotPool: checks["snapshot_storage_pool"].Available,
|
|
templatePool: checks["template_storage_pool"].Available,
|
|
seedPool: checks["seed_storage_pool"].Available,
|
|
networks: checks["network_selectors"].Available,
|
|
nwfilter: checks["nwfilter_api"].Available,
|
|
q35UEFI: checks["q35_uefi"].Available,
|
|
liveBackup: checks["live_backup"].Available,
|
|
}
|
|
return prerequisites.features(), checks, networkSelectors
|
|
}
|
|
|
|
func availableCheck() CapabilityCheck { return CapabilityCheck{Available: true} }
|
|
|
|
func unavailableCheck(reason string) CapabilityCheck {
|
|
return CapabilityCheck{Available: false, Reason: reason}
|
|
}
|
|
|
|
func probeReadableRegularFile(path string) CapabilityCheck {
|
|
if strings.TrimSpace(path) == "" {
|
|
return unavailableCheck(capabilityMissing)
|
|
}
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnreadable)
|
|
}
|
|
defer file.Close()
|
|
info, err := file.Stat()
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnreadable)
|
|
}
|
|
if !info.Mode().IsRegular() || info.Size() <= 0 {
|
|
return unavailableCheck(capabilityInvalid)
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
func probeExistingDirectory(path string) CapabilityCheck {
|
|
if strings.TrimSpace(path) == "" {
|
|
return unavailableCheck(capabilityMissing)
|
|
}
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
if !info.IsDir() {
|
|
return unavailableCheck(capabilityInvalid)
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
func (l *Libvirt) probeStoragePool(name string) CapabilityCheck {
|
|
if strings.TrimSpace(name) == "" {
|
|
return unavailableCheck(capabilityMissing)
|
|
}
|
|
pool, err := l.conn.LookupStoragePoolByName(name)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
defer pool.Free()
|
|
active, err := pool.IsActive()
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
if !active {
|
|
return unavailableCheck(capabilityInactive)
|
|
}
|
|
info, err := pool.GetInfo()
|
|
if err != nil || info.State != libvirt.STORAGE_POOL_RUNNING {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
if info.Available == 0 {
|
|
return unavailableCheck(capabilityExhausted)
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
// Snapshot and restore use QEMU file-backed backup targets and direct volume
|
|
// paths. An active logical, RBD, iSCSI, or other non-file pool can satisfy
|
|
// ordinary volume operations but cannot safely satisfy that contract.
|
|
func (l *Libvirt) probeSnapshotStoragePool(name string) CapabilityCheck {
|
|
if strings.TrimSpace(name) == "" {
|
|
return unavailableCheck(capabilityMissing)
|
|
}
|
|
pool, err := l.conn.LookupStoragePoolByName(name)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
defer pool.Free()
|
|
document, err := pool.GetXMLDesc(0)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
if !snapshotStoragePoolXMLSupported(document) {
|
|
return unavailableCheck(capabilityUnsupported)
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
func snapshotStoragePoolXMLSupported(document string) bool {
|
|
var definition libvirtxml.StoragePool
|
|
if definition.Unmarshal(document) != nil || definition.Target == nil {
|
|
return false
|
|
}
|
|
if definition.Type != "dir" && definition.Type != "fs" && definition.Type != "netfs" {
|
|
return false
|
|
}
|
|
path := filepath.Clean(strings.TrimSpace(definition.Target.Path))
|
|
return filepath.IsAbs(path) && path != string(filepath.Separator)
|
|
}
|
|
|
|
func probeNetworkSelectorSet(selectors map[string]LocalNetworkSource,
|
|
probe func(LocalNetworkSource) CapabilityCheck) (CapabilityCheck, []string) {
|
|
if len(selectors) == 0 {
|
|
return unavailableCheck(capabilitySelectorMissing), []string{}
|
|
}
|
|
if len(selectors) > maxCapabilityNetworkSelectors {
|
|
return unavailableCheck(capabilityInvalid), []string{}
|
|
}
|
|
usable := make([]string, 0, len(selectors))
|
|
for selector, source := range selectors {
|
|
if probe(source).Available {
|
|
usable = append(usable, selector)
|
|
}
|
|
}
|
|
sort.Strings(usable)
|
|
if len(usable) != 0 {
|
|
return availableCheck(), usable
|
|
}
|
|
return unavailableCheck(capabilitySelectorUnusable), usable
|
|
}
|
|
|
|
func (l *Libvirt) probeNetworkSource(source LocalNetworkSource) CapabilityCheck {
|
|
switch source.Type {
|
|
case "bridge":
|
|
device, err := net.InterfaceByName(source.Source)
|
|
if err != nil || device.Flags&net.FlagUp == 0 {
|
|
return unavailableCheck(capabilitySelectorUnusable)
|
|
}
|
|
case "network":
|
|
network, err := l.conn.LookupNetworkByName(source.Source)
|
|
if err != nil {
|
|
return unavailableCheck(capabilitySelectorUnusable)
|
|
}
|
|
active, activeErr := network.IsActive()
|
|
network.Free()
|
|
if activeErr != nil || !active {
|
|
return unavailableCheck(capabilitySelectorUnusable)
|
|
}
|
|
default:
|
|
return unavailableCheck(capabilitySelectorUnusable)
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
func resolveConfiguredNetworkSelector(
|
|
selectors map[string]LocalNetworkSource,
|
|
selector string,
|
|
probe func(LocalNetworkSource) CapabilityCheck,
|
|
) (LocalNetworkSource, error) {
|
|
source, ok := selectors[selector]
|
|
if !ok {
|
|
return LocalNetworkSource{}, NewError(ErrInvalid, "network_not_found",
|
|
"host network selector is not configured", nil)
|
|
}
|
|
if !probe(source).Available {
|
|
return LocalNetworkSource{}, NewError(ErrUnavailable,
|
|
"network_unavailable", "host network selector is not active", nil)
|
|
}
|
|
return source, nil
|
|
}
|
|
|
|
func (l *Libvirt) probeNWFilterAPI() CapabilityCheck {
|
|
filters, err := l.conn.ListAllNWFilters(0)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
for index := range filters {
|
|
_ = filters[index].Free()
|
|
}
|
|
baseline, err := l.conn.LookupNWFilterByName("clean-traffic")
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
document, readErr := baseline.GetXMLDesc(0)
|
|
_ = baseline.Free()
|
|
if readErr != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
if !cleanTrafficFilterXMLValid(document) {
|
|
return unavailableCheck(capabilityInvalid)
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
func cleanTrafficFilterXMLValid(document string) bool {
|
|
var filter libvirtxml.NWFilter
|
|
return filter.Unmarshal(document) == nil && filter.Name == "clean-traffic"
|
|
}
|
|
|
|
func (l *Libvirt) probeDomainCapabilities() (CapabilityCheck, CapabilityCheck) {
|
|
document, err := l.conn.GetDomainCapabilities("", "x86_64", "q35", "kvm", 0)
|
|
if err != nil {
|
|
unavailable := unavailableCheck(capabilityUnsupported)
|
|
return unavailable, unavailable
|
|
}
|
|
q35UEFI := availableCheck()
|
|
if !domainCapsSupportQ35UEFI(document, l.local.FirmwareLoader) {
|
|
q35UEFI = unavailableCheck(capabilityUnsupported)
|
|
}
|
|
liveBackup := availableCheck()
|
|
if !domainCapsSupportLiveBackup(document) {
|
|
liveBackup = unavailableCheck(capabilityUnsupported)
|
|
}
|
|
return q35UEFI, liveBackup
|
|
}
|
|
|
|
func domainCapsSupportLiveBackup(document string) bool {
|
|
var capabilities libvirtxml.DomainCaps
|
|
return xml.Unmarshal([]byte(document), &capabilities) == nil &&
|
|
capabilities.Features != nil && capabilities.Features.Backup != nil &&
|
|
capabilities.Features.Backup.Supported == "yes"
|
|
}
|
|
|
|
func domainCapsSupportQ35UEFI(document, configuredLoader string) bool {
|
|
var capabilities libvirtxml.DomainCaps
|
|
if err := xml.Unmarshal([]byte(document), &capabilities); err != nil ||
|
|
capabilities.Domain != "kvm" || capabilities.Arch != "x86_64" ||
|
|
!strings.Contains(strings.ToLower(capabilities.Machine), "q35") ||
|
|
capabilities.OS == nil || capabilities.OS.Supported != "yes" ||
|
|
capabilities.OS.Loader == nil || capabilities.OS.Loader.Supported != "yes" {
|
|
return false
|
|
}
|
|
loaderFound := false
|
|
for _, value := range capabilities.OS.Loader.Values {
|
|
if value == configuredLoader {
|
|
loaderFound = true
|
|
break
|
|
}
|
|
}
|
|
if !loaderFound || !domainCapsEnumSupports(capabilities.OS.Loader.Enums, "type", "pflash") ||
|
|
!domainCapsEnumSupports(capabilities.OS.Loader.Enums, "readonly", "yes") ||
|
|
!domainCapsEnumSupports(capabilities.OS.Loader.Enums, "secure", "no") {
|
|
return false
|
|
}
|
|
hostPassthrough := false
|
|
if capabilities.CPU != nil {
|
|
for _, mode := range capabilities.CPU.Modes {
|
|
if mode.Name == "host-passthrough" && mode.Supported == "yes" {
|
|
hostPassthrough = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return hostPassthrough
|
|
}
|
|
|
|
func domainCapsEnumSupports(enums []libvirtxml.DomainCapsEnum, name, value string) bool {
|
|
for _, enum := range enums {
|
|
if enum.Name != name {
|
|
continue
|
|
}
|
|
for _, current := range enum.Values {
|
|
if current == value {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|