2024-03-12 05:35:32 +00:00
|
|
|
// Package host provides utilities and data structures in relation to a libvirt host.
|
|
|
|
// This includes getting a list of virtual machines running on a host, launching
|
|
|
|
// a new virtual machine on a host, triggering a virtual machine migration to another
|
|
|
|
// host, getting hardware and resource usage from a host, and eventually more.
|
|
|
|
// Most of this is data at the moment, ensuring data can be gathered efficiently without
|
|
|
|
// slowing down the host from it's main job of running virtual machines.
|
2024-03-11 18:48:14 +00:00
|
|
|
package host
|
2024-03-11 02:32:11 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-12 01:39:17 +00:00
|
|
|
"log"
|
2024-03-11 02:32:11 +00:00
|
|
|
|
2024-03-11 18:48:14 +00:00
|
|
|
"git.staur.ca/stobbsm/clustvirt/lib/guest"
|
2024-03-12 05:35:32 +00:00
|
|
|
"git.staur.ca/stobbsm/clustvirt/util"
|
2024-03-11 02:32:11 +00:00
|
|
|
"libvirt.org/go/libvirt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Host holds information and acts as a connection handle for a Host
|
|
|
|
// If a connection is closed prematurely, will re-open the connection and
|
|
|
|
// try the attempted method again
|
|
|
|
type Host struct {
|
2024-03-12 07:38:29 +00:00
|
|
|
HostName string
|
|
|
|
SystemHostName string
|
|
|
|
FreeMemory uint64
|
|
|
|
LibVersion uint32
|
|
|
|
HostInfo NodeInfo
|
|
|
|
HostSEVInfo SEVInfo
|
|
|
|
AvailableCPUTypes []string
|
|
|
|
NodeMemory NodeMemoryInfo
|
|
|
|
StorageCapabilities string
|
|
|
|
SysInfo string
|
|
|
|
Alive bool
|
|
|
|
Encrypted bool
|
|
|
|
Secure bool
|
|
|
|
//alldomains
|
|
|
|
//allinterfaces
|
|
|
|
//allnetworks
|
|
|
|
//alldevices
|
|
|
|
//allsecrets
|
|
|
|
//allstoragepools
|
|
|
|
//defineddomains
|
|
|
|
//definedinterfaces
|
|
|
|
//definednetworks
|
|
|
|
//definedstoragepools
|
|
|
|
//listdomains []int id
|
|
|
|
//listinterfaces []string interfacename
|
|
|
|
//listnetworks []string networkname
|
|
|
|
//listserets []string secretname
|
|
|
|
//liststoragepools []string storagepoolname
|
|
|
|
|
2024-03-11 02:32:11 +00:00
|
|
|
|
2024-03-12 05:35:32 +00:00
|
|
|
uri *URI
|
2024-03-11 02:32:11 +00:00
|
|
|
conn *libvirt.Connect
|
|
|
|
close chan struct{}
|
|
|
|
closeErr chan error
|
|
|
|
}
|
|
|
|
|
2024-03-12 05:35:32 +00:00
|
|
|
// NodeInfo represents the basic HW info for a host node
|
|
|
|
type NodeInfo struct {
|
|
|
|
// livirt.NodeInfo section
|
|
|
|
Model string
|
|
|
|
Memory uint64
|
|
|
|
Cpus uint
|
|
|
|
MHz uint
|
|
|
|
Nodes uint32
|
|
|
|
Sockets uint32
|
|
|
|
Cores uint32
|
|
|
|
Threads uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// SEVInfo provides information about AMD SEV support
|
2024-03-12 07:38:29 +00:00
|
|
|
type SEVInfo struct {
|
2024-03-12 05:35:32 +00:00
|
|
|
// livirt.NodeSEVParameters section
|
2024-03-12 07:38:29 +00:00
|
|
|
SEVEnabled bool
|
2024-03-12 05:35:32 +00:00
|
|
|
PDH string
|
|
|
|
CertChain string
|
|
|
|
CBitPos uint
|
|
|
|
ReducedPhysBits uint
|
|
|
|
MaxGuests uint
|
|
|
|
MaxEsGuests uint
|
|
|
|
CPU0ID string
|
|
|
|
}
|
|
|
|
|
2024-03-12 07:38:29 +00:00
|
|
|
// NodeMemoryInfo provides statistis about node memory usage from libvirt.NodeMemoryStats
|
|
|
|
type NodeMemoryInfo struct {
|
|
|
|
Total uint64
|
|
|
|
Free uint64
|
|
|
|
Buffers uint64
|
|
|
|
Cached uint64
|
|
|
|
}
|
|
|
|
|
2024-03-11 02:32:11 +00:00
|
|
|
// ConnectHost creates a host connection wrapper that can be used regularly
|
2024-03-12 05:35:32 +00:00
|
|
|
func ConnectHost(uri *URI, host string) (*Host, error) {
|
2024-03-11 02:32:11 +00:00
|
|
|
h := &Host{
|
|
|
|
HostName: host,
|
2024-03-12 05:35:32 +00:00
|
|
|
uri: uri,
|
2024-03-11 02:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.connect(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.close = make(chan struct{})
|
|
|
|
h.closeErr = make(chan error)
|
|
|
|
|
2024-03-12 05:35:32 +00:00
|
|
|
var err error
|
|
|
|
h.AvailableCPUTypes, err = h.conn.GetCPUModelNames("x86_64", 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error getting cpu model names", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract libvirt.NodeInfo and libvirt.NodeSEVParameters
|
|
|
|
ni, err := h.conn.GetNodeInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
h.HostInfo.Model = ni.Model
|
|
|
|
h.HostInfo.Memory = ni.Memory
|
|
|
|
h.HostInfo.Cpus = ni.Cpus
|
|
|
|
h.HostInfo.MHz = ni.MHz
|
|
|
|
h.HostInfo.Nodes = ni.Nodes
|
|
|
|
h.HostInfo.Sockets = ni.Sockets
|
|
|
|
h.HostInfo.Cores = ni.Cores
|
|
|
|
h.HostInfo.Threads = ni.Threads
|
|
|
|
|
|
|
|
// Assume SEV is enabled, until we know otherwise
|
|
|
|
h.HostSEVInfo.SEVEnabled = true
|
|
|
|
ns, err := h.conn.GetSEVInfo(0)
|
|
|
|
if err != nil {
|
|
|
|
lverr, ok := err.(libvirt.Error)
|
|
|
|
if !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch lverr.Code {
|
|
|
|
case 84:
|
|
|
|
log.Println("SEV functions not supported")
|
|
|
|
h.HostSEVInfo.SEVEnabled = false
|
|
|
|
default:
|
|
|
|
log.Println("Error encountered", lverr.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if h.HostSEVInfo.SEVEnabled {
|
|
|
|
h.HostSEVInfo.PDH = util.SetNotSet(ns.PDH, ns.PDHSet)
|
|
|
|
h.HostSEVInfo.CertChain = util.SetNotSet(ns.CertChain, ns.CertChainSet)
|
|
|
|
h.HostSEVInfo.CBitPos = ns.CBitPos
|
|
|
|
h.HostSEVInfo.ReducedPhysBits = ns.ReducedPhysBits
|
|
|
|
h.HostSEVInfo.MaxGuests = ns.MaxGuests
|
|
|
|
h.HostSEVInfo.MaxEsGuests = ns.MaxEsGuests
|
|
|
|
h.HostSEVInfo.CPU0ID = util.SetNotSet(ns.CPU0ID, ns.CPU0IDSet)
|
|
|
|
}
|
|
|
|
|
2024-03-12 07:38:29 +00:00
|
|
|
h.SystemHostName, err = h.conn.GetHostname()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if h.SystemHostName == "" {
|
|
|
|
h.SystemHostName = h.HostName
|
|
|
|
}
|
|
|
|
h.LibVersion, err = h.conn.GetLibVersion()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
h.FreeMemory, err = h.conn.GetFreeMemory()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mi, err := h.conn.GetMemoryStats(libvirt.NODE_MEMORY_STATS_ALL_CELLS, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
h.NodeMemory.Total = mi.Total
|
|
|
|
h.NodeMemory.Free = mi.Free
|
|
|
|
h.NodeMemory.Buffers = mi.Buffers
|
|
|
|
h.NodeMemory.Cached = mi.Cached
|
|
|
|
|
|
|
|
h.StorageCapabilities, err = h.conn.GetStoragePoolCapabilities(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.SysInfo, err = h.conn.GetSysinfo(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Alive, err = h.conn.IsAlive()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
h.Encrypted, err = h.conn.IsEncrypted()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
h.Secure, err = h.conn.IsSecure()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2024-03-11 02:32:11 +00:00
|
|
|
go func() {
|
|
|
|
defer close(h.closeErr)
|
|
|
|
<-h.close
|
|
|
|
_, err := h.conn.Close()
|
|
|
|
h.closeErr <- err
|
|
|
|
}()
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect creates a host connection
|
|
|
|
func (h *Host) connect() error {
|
|
|
|
var err error
|
2024-03-12 05:35:32 +00:00
|
|
|
h.conn, err = libvirt.NewConnect(h.uri.ConnectionString(h.HostName))
|
2024-03-11 02:32:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGuestByName returns a GuestVM instance that exists on the given host
|
2024-03-11 18:48:14 +00:00
|
|
|
func (h *Host) GetGuestByName(name string) (*guest.VM, error) {
|
|
|
|
g, err := guest.GetGuest(name, h.conn)
|
|
|
|
if err == nil {
|
2024-03-11 02:32:11 +00:00
|
|
|
return g, nil
|
|
|
|
}
|
2024-03-11 18:48:14 +00:00
|
|
|
lverr, ok := err.(libvirt.Error)
|
|
|
|
if ok && lverr.Code == libvirt.ERR_INVALID_CONN {
|
|
|
|
// try again after creating a new connection
|
|
|
|
return guest.GetGuest(name, h.conn)
|
|
|
|
}
|
|
|
|
return nil, err
|
2024-03-11 02:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close triggers closing the host connection
|
|
|
|
func (h *Host) Close() error {
|
2024-03-12 01:39:17 +00:00
|
|
|
log.Println("Closing Host", h.HostName)
|
2024-03-11 02:32:11 +00:00
|
|
|
close(h.close)
|
|
|
|
return <-h.closeErr
|
|
|
|
}
|