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.
510 lines
17 KiB
Go
510 lines
17 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"encoding/xml"
|
|
"errors"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.luoli.live/root/virt/agent/peermigration"
|
|
"git.luoli.live/root/virt/agent/publicnetwork"
|
|
"libvirt.org/go/libvirt"
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
func publicNetworkCapability(checks map[string]CapabilityCheck) PublicNetworkCapability {
|
|
return PublicNetworkCapability{
|
|
Contract: publicnetwork.ContractVersion,
|
|
DualStackRequired: true,
|
|
ProjectOwnedFilter: checks["nwfilter_api"].Available,
|
|
NDPGuard: checks["public_ndp_guard"].Available,
|
|
}
|
|
}
|
|
|
|
func applyVPCCleanTrafficGate(capability *VPCL3Capability, check CapabilityCheck) bool {
|
|
if capability == nil {
|
|
return false
|
|
}
|
|
if !check.Available {
|
|
capability.ApplySupported = false
|
|
capability.PortSupported = false
|
|
}
|
|
return capability.ApplySupported && capability.PortSupported && check.Available
|
|
}
|
|
|
|
type capabilityPrerequisites struct {
|
|
firmwareLoader bool
|
|
nvramTemplate bool
|
|
nvramDirectory bool
|
|
managedPool bool
|
|
snapshotPool bool
|
|
templatePool bool
|
|
seedPool bool
|
|
userISOPool bool
|
|
rescuePool bool
|
|
storageConfig bool
|
|
networks bool
|
|
nwfilter bool
|
|
publicGuard bool
|
|
q35UEFI bool
|
|
liveBackup bool
|
|
}
|
|
|
|
func (l *Libvirt) peerMigrationCapability(storageReady bool) (*PeerMigrationCapability, CapabilityCheck) {
|
|
if !storageReady || l.local.PeerMigrationStatePath == "" ||
|
|
len(l.local.PeerMigrationEndpoints) == 0 || l.local.PeerMigrationIdentitySHA256 == "" ||
|
|
l.local.PeerMigrationStagingPath == "" {
|
|
return nil, unavailableCheck(capabilityMissing)
|
|
}
|
|
if err := peermigration.ValidateEndpoints(l.local.PeerMigrationEndpoints); err != nil {
|
|
return nil, unavailableCheck(capabilityInvalid)
|
|
}
|
|
if _, err := peermigration.PinnedClientTLSConfig(l.local.PeerMigrationIdentitySHA256); err != nil {
|
|
return nil, unavailableCheck(capabilityUnreadable)
|
|
}
|
|
if _, err := peermigration.NewStore(
|
|
l.local.PeerMigrationStatePath, l.local.StoragePathRoots,
|
|
l.local.PeerMigrationStagingPath,
|
|
); err != nil {
|
|
return nil, unavailableCheck(capabilityUnavailable)
|
|
}
|
|
return &PeerMigrationCapability{
|
|
Version: peermigration.ProtocolVersion,
|
|
Endpoints: append([]string(nil), l.local.PeerMigrationEndpoints...),
|
|
IdentitySHA256: l.local.PeerMigrationIdentitySHA256,
|
|
PinnedTLS: true, ChunkSize: peermigration.DefaultChunkSize,
|
|
}, availableCheck()
|
|
}
|
|
|
|
func (p capabilityPrerequisites) features() FeatureSet {
|
|
uefi := p.firmwareLoader && p.nvramTemplate && p.nvramDirectory && p.q35UEFI
|
|
create := p.storageConfig && uefi && p.managedPool && p.templatePool && p.seedPool && p.networks && p.nwfilter && p.publicGuard
|
|
return FeatureSet{
|
|
CreateVM: create,
|
|
DeleteVM: p.storageConfig && p.managedPool && p.seedPool,
|
|
Power: true,
|
|
Resources: p.storageConfig && p.managedPool,
|
|
Settings: p.storageConfig && p.seedPool,
|
|
Network: p.storageConfig && p.seedPool && p.networks && p.nwfilter && p.publicGuard,
|
|
Reinstall: create,
|
|
Snapshots: p.storageConfig && p.managedPool && p.snapshotPool && p.liveBackup,
|
|
Backups: create && p.snapshotPool && p.liveBackup,
|
|
Firewall: p.nwfilter,
|
|
Rescue: p.storageConfig && p.rescuePool && p.seedPool,
|
|
Metrics: true,
|
|
Console: true,
|
|
Templates: p.storageConfig && p.templatePool,
|
|
UserISO: p.storageConfig && p.userISOPool,
|
|
UEFI: uefi,
|
|
SecureBoot: false,
|
|
}
|
|
}
|
|
|
|
func (l *Libvirt) probeRuntimeCapabilities(ctx context.Context) (FeatureSet, map[string]CapabilityCheck, []string) {
|
|
q35UEFI, liveBackup := l.probeDomainCapabilities()
|
|
networkCheck, networkSelectors := l.probeHostNetworks()
|
|
checks := map[string]CapabilityCheck{
|
|
"firmware_loader": probeReadableRegularFile(l.local.FirmwareLoader),
|
|
"firmware_nvram_template": probeReadableRegularFile(l.local.FirmwareNVRAMTemplate),
|
|
"nvram_directory": probeExistingDirectory(l.local.NVRAMDirectory),
|
|
"snapshot_storage_pool": l.probeSnapshotStoragePool(l.storagePoolName(StorageRoleManaged)),
|
|
"network_selectors": networkCheck,
|
|
"nwfilter_api": l.probeNWFilterAPI(),
|
|
"public_ndp_guard": l.probePublicGuard(ctx),
|
|
"q35_uefi": q35UEFI,
|
|
"live_backup": liveBackup,
|
|
}
|
|
storageRequest := StorageConfigurationRequest{}
|
|
storageReady := true
|
|
for _, role := range storageRoles() {
|
|
binding, check := l.probeConfiguredStoragePool(role)
|
|
checks[role+"_storage_pool"] = check
|
|
storageReady = storageReady && check.Available
|
|
setStorageRequestDirectory(&storageRequest, binding)
|
|
}
|
|
checks["storage_configuration"] = unavailableCheck(capabilityMissing)
|
|
if storageReady && validateStorageDirectories(storageRequest, l.local.StoragePathRoots) == nil {
|
|
checks["storage_configuration"] = availableCheck()
|
|
}
|
|
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,
|
|
userISOPool: checks["user_iso_storage_pool"].Available,
|
|
rescuePool: checks["rescue_storage_pool"].Available,
|
|
storageConfig: checks["storage_configuration"].Available,
|
|
networks: checks["network_selectors"].Available,
|
|
nwfilter: checks["nwfilter_api"].Available,
|
|
publicGuard: checks["public_ndp_guard"].Available,
|
|
q35UEFI: checks["q35_uefi"].Available,
|
|
liveBackup: checks["live_backup"].Available,
|
|
}
|
|
return prerequisites.features(), checks, networkSelectors
|
|
}
|
|
|
|
func (l *Libvirt) probePublicGuard(ctx context.Context) CapabilityCheck {
|
|
if err := l.reconcilePublicGuard(ctx, nil); err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
func (l *Libvirt) probeConfiguredStoragePool(role string) (storagePoolBinding, CapabilityCheck) {
|
|
binding, pool, err := l.fixedStoragePoolBinding(role)
|
|
if err != nil {
|
|
return binding, unavailableCheck(capabilityInvalid)
|
|
}
|
|
if pool == nil {
|
|
return binding, unavailableCheck(capabilityMissing)
|
|
}
|
|
defer pool.Free()
|
|
return binding, probeStoragePoolHandle(pool, binding)
|
|
}
|
|
|
|
func probeStoragePoolHandle(pool storagePoolReader, binding storagePoolBinding) CapabilityCheck {
|
|
ready, err := storagePoolReady(pool, binding)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
if !ready {
|
|
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 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()
|
|
}
|
|
|
|
// 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 (l *Libvirt) probeHostNetworks() (CapabilityCheck, []string) {
|
|
var selectors []string
|
|
// Each source is optional: a failed enumeration must not hide usable
|
|
// networks independently confirmed by the other source.
|
|
if devices, err := net.Interfaces(); err == nil {
|
|
for _, device := range devices {
|
|
selectors = append(selectors, "bridge:"+device.Name)
|
|
}
|
|
}
|
|
networks, err := l.conn.ListAllNetworks(libvirt.CONNECT_LIST_NETWORKS_ACTIVE)
|
|
for index := range networks {
|
|
name, nameErr := networks[index].GetName()
|
|
_ = networks[index].Free()
|
|
if err == nil && nameErr == nil {
|
|
selectors = append(selectors, "network:"+name)
|
|
}
|
|
}
|
|
return probeNetworkSelectorSet(selectors, l.probeNetworkSource)
|
|
}
|
|
|
|
func probeNetworkSelectorSet(selectors []string,
|
|
probe func(LocalNetworkSource) CapabilityCheck) (CapabilityCheck, []string) {
|
|
if len(selectors) == 0 {
|
|
return unavailableCheck(capabilitySelectorMissing), []string{}
|
|
}
|
|
usable := make([]string, 0)
|
|
seen := make(map[string]bool)
|
|
for _, selector := range selectors {
|
|
source, err := ParseNetworkSelector(selector)
|
|
if err == nil && !seen[selector] && probe(source).Available {
|
|
seen[selector] = true
|
|
usable = append(usable, selector)
|
|
}
|
|
}
|
|
if len(usable) > maxCapabilityNetworkSelectors {
|
|
return unavailableCheck(capabilityInvalid), []string{}
|
|
}
|
|
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 {
|
|
return unavailableCheck(capabilitySelectorUnusable)
|
|
}
|
|
return probeBridgeInterface(*device, "/sys/class/net")
|
|
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 probeBridgeInterface(device net.Interface, sysfsRoot string) CapabilityCheck {
|
|
info, err := os.Stat(filepath.Join(sysfsRoot, device.Name, "bridge"))
|
|
if err != nil || !info.IsDir() || device.Flags&net.FlagUp == 0 {
|
|
return unavailableCheck(capabilitySelectorUnusable)
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
func resolveNetworkSelector(
|
|
selector string,
|
|
probe func(LocalNetworkSource) CapabilityCheck,
|
|
) (LocalNetworkSource, error) {
|
|
source, err := ParseNetworkSelector(selector)
|
|
if err != nil {
|
|
return LocalNetworkSource{}, err
|
|
}
|
|
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()
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
func (l *Libvirt) probeVPCCleanTrafficFilter() CapabilityCheck {
|
|
filter, err := l.conn.LookupNWFilterByName(managedVPCInterfaceFilterName)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityMissing)
|
|
}
|
|
defer filter.Free()
|
|
document, err := filter.GetXMLDesc(0)
|
|
if err != nil {
|
|
return unavailableCheck(capabilityUnavailable)
|
|
}
|
|
if !cleanTrafficFilterXMLValid(document) {
|
|
return unavailableCheck(capabilityInvalid)
|
|
}
|
|
return availableCheck()
|
|
}
|
|
|
|
func cleanTrafficFilterXMLValid(document string) bool {
|
|
expectedDocument, err := BuildManagedVPCAntiSpoofFilterXML()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
var actual, expected libvirtxml.NWFilter
|
|
if actual.Unmarshal(document) != nil || expected.Unmarshal(expectedDocument) != nil {
|
|
return false
|
|
}
|
|
// libvirt assigns a stable UUID to a defined filter; it is host state, not
|
|
// part of the project-owned security semantics.
|
|
actual.UUID = ""
|
|
expected.UUID = ""
|
|
return reflect.DeepEqual(actual, expected)
|
|
}
|
|
|
|
func (l *Libvirt) ensureManagedVPCAntiSpoofFilter() error {
|
|
existing, err := l.lookupOptionalNetworkFilter(managedVPCInterfaceFilterName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if existing != nil {
|
|
defer existing.Free()
|
|
document, inspectErr := existing.GetXMLDesc(0)
|
|
if inspectErr != nil {
|
|
return translateLibvirtError("inspect project-owned VPC anti-spoof filter", inspectErr)
|
|
}
|
|
if !cleanTrafficFilterXMLValid(document) {
|
|
return NewError(ErrConflict, "vpc_antispoof_filter_conflict", "reserved VPC anti-spoof filter has foreign or incomplete semantics", nil)
|
|
}
|
|
return nil
|
|
}
|
|
document, err := BuildManagedVPCAntiSpoofFilterXML()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defined, defineErr := l.conn.NWFilterDefineXML(document)
|
|
if defineErr != nil {
|
|
observed, inspectErr := l.lookupOptionalNetworkFilter(managedVPCInterfaceFilterName)
|
|
if observed != nil {
|
|
defer observed.Free()
|
|
observedDocument, xmlErr := observed.GetXMLDesc(0)
|
|
if xmlErr == nil && cleanTrafficFilterXMLValid(observedDocument) {
|
|
return nil
|
|
}
|
|
inspectErr = errors.Join(inspectErr, xmlErr)
|
|
}
|
|
return errors.Join(
|
|
mutationLibvirtError("define project-owned VPC anti-spoof filter", defineErr),
|
|
translateOptionalLibvirtError("inspect project-owned VPC anti-spoof filter after define failure", inspectErr),
|
|
)
|
|
}
|
|
defer defined.Free()
|
|
applied, err := defined.GetXMLDesc(0)
|
|
if err != nil || !cleanTrafficFilterXMLValid(applied) {
|
|
return errors.Join(
|
|
NewError(ErrUnavailable, "vpc_antispoof_filter_unconfirmed", "project-owned VPC anti-spoof filter definition was not confirmed", err),
|
|
)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|