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.
266 lines
8.0 KiB
Go
266 lines
8.0 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
|
|
"libvirt.org/go/libvirt"
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
const (
|
|
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
|
|
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.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) {
|
|
q35UEFI, liveBackup := l.probeDomainCapabilities()
|
|
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),
|
|
"template_storage_pool": l.probeStoragePool(l.local.TemplateStoragePool),
|
|
"seed_storage_pool": l.probeStoragePool(l.local.SeedStoragePool),
|
|
"network_selectors": l.probeNetworkSelectors(),
|
|
"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,
|
|
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
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
func (l *Libvirt) probeNetworkSelectors() CapabilityCheck {
|
|
if len(l.local.NetworkSelectors) == 0 {
|
|
return unavailableCheck(capabilitySelectorMissing)
|
|
}
|
|
for _, source := range l.local.NetworkSelectors {
|
|
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 (l *Libvirt) probeNWFilterAPI() CapabilityCheck {
|
|
filters, err := l.conn.ListAllNWFilters(0)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
for index := range filters {
|
|
_ = filters[index].Free()
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
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
|
|
}
|