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-13 04:17:42 +00:00
|
|
|
"sync"
|
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-11 02:32:11 +00:00
|
|
|
"libvirt.org/go/libvirt"
|
2024-03-19 15:13:44 +00:00
|
|
|
"libvirt.org/go/libvirtxml"
|
2024-03-11 02:32:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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-13 04:17:42 +00:00
|
|
|
// HostName used to make the connection
|
|
|
|
HostName string
|
|
|
|
// SystemHostName is the hostname as reported by the system itself
|
|
|
|
SystemHostName string
|
|
|
|
// LibVersion is the version of Libvirt on the host
|
|
|
|
LibVersion uint32
|
|
|
|
// SysInfo is the XML representation of the host system information
|
|
|
|
SysInfo string
|
|
|
|
// Alive indicates if the connection is alive
|
|
|
|
Alive bool
|
|
|
|
// Encrypted indicates if the connection is encrypted
|
|
|
|
Encrypted bool
|
|
|
|
// Secure indicates if the connection is secure
|
|
|
|
Secure bool
|
2024-03-19 21:25:57 +00:00
|
|
|
// HostInfo provides basic HW information about a host
|
2024-03-20 17:36:53 +00:00
|
|
|
HostInfo *libvirtxml.CapsHost
|
2024-03-19 21:25:57 +00:00
|
|
|
// NodeMemory provides basic memory information about the Host
|
2024-03-20 17:36:53 +00:00
|
|
|
NodeMemory *NodeMemoryInfo
|
2024-03-13 04:17:42 +00:00
|
|
|
// VMList is the list of virtual machines available to the host
|
2024-03-20 17:36:53 +00:00
|
|
|
VMList []*libvirtxml.Domain
|
2024-03-13 04:17:42 +00:00
|
|
|
// NetIfList is the list of network interfaces on the host
|
2024-03-20 17:36:53 +00:00
|
|
|
NetIfFList []*libvirtxml.Interface
|
2024-03-13 04:17:42 +00:00
|
|
|
// NetworkList is the list of defined networks on the host
|
2024-03-20 17:36:53 +00:00
|
|
|
NetworkList []*libvirtxml.Network
|
2024-03-13 04:17:42 +00:00
|
|
|
// DeviceList is the list of devices on the host
|
2024-03-20 17:36:53 +00:00
|
|
|
DeviceList []*libvirtxml.NodeDevice
|
2024-03-13 04:17:42 +00:00
|
|
|
// SecretList provides a list of secrets available to the host
|
2024-03-20 17:36:53 +00:00
|
|
|
SecretList []*libvirtxml.Secret
|
2024-03-13 04:17:42 +00:00
|
|
|
// StoragePoolList provides the list of stoarge ppols available to the host
|
2024-03-20 17:36:53 +00:00
|
|
|
StoragePoolList []*libvirtxml.StoragePool
|
2024-03-19 21:25:57 +00:00
|
|
|
// VolumeList is the list of volumes available on the host
|
2024-03-20 17:36:53 +00:00
|
|
|
VolumeList []*libvirtxml.StorageVolume
|
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 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-13 04:17:42 +00:00
|
|
|
h.getInfo()
|
|
|
|
|
|
|
|
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
|
|
|
|
h.conn, err = libvirt.NewConnect(h.uri.ConnectionString(h.HostName))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGuestByName returns a GuestVM instance that exists on the given host
|
|
|
|
func (h *Host) GetGuestByName(name string) (*guest.VM, error) {
|
|
|
|
g, err := guest.GetGuest(name, h.conn)
|
|
|
|
if err == nil {
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close triggers closing the host connection
|
|
|
|
func (h *Host) Close() error {
|
|
|
|
log.Println("Closing Host", h.HostName)
|
|
|
|
close(h.close)
|
|
|
|
return <-h.closeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// private methods that load the different informational parts
|
|
|
|
|
|
|
|
func (h *Host) getInfo() {
|
2024-03-19 21:25:57 +00:00
|
|
|
wg := new(sync.WaitGroup)
|
2024-03-13 04:17:42 +00:00
|
|
|
|
2024-03-19 15:13:44 +00:00
|
|
|
infoFuncs := []func(){
|
2024-03-20 17:36:53 +00:00
|
|
|
h.hostInfo,
|
|
|
|
h.memoryInfo,
|
|
|
|
h.getStoragePools,
|
2024-03-13 04:17:42 +00:00
|
|
|
h.getNetsInfo,
|
2024-03-20 17:36:53 +00:00
|
|
|
h.getIfaceInfo,
|
|
|
|
h.getDevicesInfo,
|
|
|
|
h.getDomainInfo,
|
|
|
|
h.getSecretsInfo,
|
2024-03-13 04:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range infoFuncs {
|
|
|
|
wg.Add(1)
|
2024-03-19 15:13:44 +00:00
|
|
|
go func(wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
f()
|
|
|
|
}(wg)
|
2024-03-13 04:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2024-03-20 17:36:53 +00:00
|
|
|
func (h *Host) hostInfo() {
|
2024-03-12 05:35:32 +00:00
|
|
|
var err error
|
|
|
|
|
2024-03-20 17:36:53 +00:00
|
|
|
rawxml, err := h.conn.GetCapabilities()
|
2024-03-12 05:35:32 +00:00
|
|
|
if err != nil {
|
2024-03-20 17:36:53 +00:00
|
|
|
log.Printf("error getting host capabilities XML: %s", err)
|
|
|
|
}
|
|
|
|
xmldoc := &libvirtxml.Caps{}
|
|
|
|
if err = xmldoc.Unmarshal(rawxml); err != nil {
|
|
|
|
log.Printf("error parsing host capabilities XML: %s", err)
|
2024-03-12 05:35:32 +00:00
|
|
|
}
|
2024-03-20 17:36:53 +00:00
|
|
|
h.HostInfo = &xmldoc.Host
|
2024-03-12 07:38:29 +00:00
|
|
|
h.SystemHostName, err = h.conn.GetHostname()
|
|
|
|
if err != nil {
|
2024-03-20 17:36:53 +00:00
|
|
|
log.Println("error getting system host name: %s", err)
|
|
|
|
} else {
|
2024-03-12 07:38:29 +00:00
|
|
|
h.SystemHostName = h.HostName
|
|
|
|
}
|
|
|
|
h.LibVersion, err = h.conn.GetLibVersion()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2024-03-20 17:36:53 +00:00
|
|
|
}
|
2024-03-12 07:38:29 +00:00
|
|
|
|
2024-03-20 17:36:53 +00:00
|
|
|
func (h *Host) memoryInfo() {
|
2024-03-12 07:38:29 +00:00
|
|
|
mi, err := h.conn.GetMemoryStats(libvirt.NODE_MEMORY_STATS_ALL_CELLS, 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2024-03-20 17:36:53 +00:00
|
|
|
h.NodeMemory = &NodeMemoryInfo{
|
2024-03-20 21:45:25 +00:00
|
|
|
Total: mi.Total,
|
|
|
|
Free: mi.Free,
|
2024-03-20 17:36:53 +00:00
|
|
|
Buffers: mi.Buffers,
|
2024-03-20 21:45:25 +00:00
|
|
|
Cached: mi.Cached,
|
2024-03-12 07:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-13 04:17:42 +00:00
|
|
|
}
|
2024-03-12 07:38:29 +00:00
|
|
|
|
2024-03-20 17:36:53 +00:00
|
|
|
func (h *Host) getStoragePools() {
|
|
|
|
// Get list of all storage pools on the host
|
|
|
|
spools, err := h.conn.ListAllStoragePools(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if len(spools) > 0 {
|
|
|
|
h.StoragePoolList = make([]*libvirtxml.StoragePool, len(spools))
|
|
|
|
for i, s := range spools {
|
|
|
|
// run in a function to allow defer s.Free()
|
|
|
|
func() {
|
|
|
|
defer s.Free()
|
|
|
|
// Get the XML represenation of each storage pool, parse it with libvirtxml
|
|
|
|
rawxml, err := s.GetXMLDesc(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error getting storage pool xml: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
xmldoc := &libvirtxml.StoragePool{}
|
|
|
|
err = xmldoc.Unmarshal(rawxml)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error parsing storage pool XML: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.StoragePoolList[i] = xmldoc
|
|
|
|
|
|
|
|
// Get list of all storage volumes in the current storage pool
|
|
|
|
svols, err := s.ListAllStorageVolumes(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if len(svols) > 0 {
|
|
|
|
// define temporary variable to hold slice of StorageVolume, that can
|
|
|
|
// either be appended to the existing slice, or used in place of the newly
|
|
|
|
// defined slice
|
|
|
|
tvl := make([]*libvirtxml.StorageVolume, len(svols))
|
|
|
|
for j, sv := range svols {
|
|
|
|
// run in a function so I can defer the sv.Free() call
|
|
|
|
func() {
|
|
|
|
defer sv.Free()
|
|
|
|
rawxml, err = sv.GetXMLDesc(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting XML from storage volume: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
xmldoc := &libvirtxml.StorageVolume{}
|
|
|
|
err = xmldoc.Unmarshal(rawxml)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error parsing storage volume XML: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tvl[j] = xmldoc
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
// Append the contents of tvl to h.VolumeList
|
|
|
|
if h.VolumeList == nil {
|
|
|
|
h.VolumeList = []*libvirtxml.StorageVolume{}
|
|
|
|
}
|
|
|
|
// Only append if the temporary storage volume isn't nil
|
|
|
|
for _, tsv := range tvl {
|
|
|
|
if tsv != nil {
|
|
|
|
h.VolumeList = append(h.VolumeList, tsv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Host) getSecretsInfo() {
|
|
|
|
nsecrets, err := h.conn.ListAllSecrets(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error loading secrets from host: %s", err)
|
|
|
|
}
|
|
|
|
if len(nsecrets) > 0 {
|
|
|
|
h.SecretList = make([]*libvirtxml.Secret, len(nsecrets))
|
|
|
|
for i, s := range nsecrets {
|
|
|
|
func() {
|
|
|
|
defer s.Free()
|
|
|
|
rawxml, err := s.GetXMLDesc(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting secret XML", err)
|
|
|
|
}
|
|
|
|
xmldoc := &libvirtxml.Secret{}
|
|
|
|
if err = xmldoc.Unmarshal(rawxml); err != nil {
|
|
|
|
log.Printf("error parsing secret XML: %s", err)
|
|
|
|
}
|
|
|
|
h.SecretList[i] = xmldoc
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:13:44 +00:00
|
|
|
func (h *Host) getDomainInfo() {
|
2024-03-13 04:17:42 +00:00
|
|
|
// getDomainInfo
|
|
|
|
doms, err := h.conn.ListAllDomains(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if len(doms) > 0 {
|
2024-03-20 21:45:25 +00:00
|
|
|
h.VMList = make([]*libvirtxml.Domain, len(doms))
|
2024-03-13 04:17:42 +00:00
|
|
|
for i, d := range doms {
|
2024-03-20 21:45:25 +00:00
|
|
|
func() {
|
|
|
|
defer d.Free()
|
|
|
|
rawxml, err := d.GetXMLDesc(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting domain XML: %s", err)
|
|
|
|
}
|
|
|
|
h.VMList[i] = &libvirtxml.Domain{}
|
|
|
|
if err = h.VMList[i].Unmarshal(rawxml); err != nil {
|
|
|
|
log.Printf("error parsing domain XML: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
2024-03-13 04:17:42 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 02:32:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:13:44 +00:00
|
|
|
func (h *Host) getIfaceInfo() {
|
2024-03-13 04:17:42 +00:00
|
|
|
// getIfaceInfo
|
2024-03-20 21:45:25 +00:00
|
|
|
ifaces, err := h.conn.ListAllInterfaces(0)
|
2024-03-13 04:17:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2024-03-11 02:32:11 +00:00
|
|
|
}
|
2024-03-13 04:17:42 +00:00
|
|
|
if len(ifaces) > 0 {
|
2024-03-20 21:45:25 +00:00
|
|
|
h.NetIfFList = make([]*libvirtxml.Interface, len(ifaces))
|
2024-03-13 04:17:42 +00:00
|
|
|
for i, ni := range ifaces {
|
2024-03-20 21:45:25 +00:00
|
|
|
func() {
|
|
|
|
defer ni.Free()
|
|
|
|
rawxml, err := ni.GetXMLDesc(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting interface XML: %s")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.NetIfFList[i] = &libvirtxml.Interface{}
|
|
|
|
if err = h.NetIfFList[i].Unmarshal(rawxml); err != nil {
|
|
|
|
log.Printf("error parsing interface XML: %s")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2024-03-13 04:17:42 +00:00
|
|
|
}
|
2024-03-11 18:48:14 +00:00
|
|
|
}
|
2024-03-11 02:32:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-19 15:13:44 +00:00
|
|
|
func (h *Host) getNetsInfo() {
|
2024-03-13 04:17:42 +00:00
|
|
|
// getNetsInfo
|
|
|
|
nets, err := h.conn.ListNetworks()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if len(nets) > 0 {
|
|
|
|
h.NetworkList = make([]NetworkInfo, len(nets))
|
|
|
|
for i, netName := range nets {
|
|
|
|
net, err := h.conn.LookupNetworkByName(netName)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if h.NetworkList[i].Name, err = net.GetName(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if h.NetworkList[i].UUID, err = net.GetUUID(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if h.NetworkList[i].XML, err = net.GetXMLDesc(0); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2024-03-19 21:25:57 +00:00
|
|
|
if h.NetworkList[i].Active, err = net.IsActive(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2024-03-13 04:17:42 +00:00
|
|
|
|
|
|
|
net.Free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:13:44 +00:00
|
|
|
func (h *Host) getDevicesInfo() {
|
2024-03-13 04:17:42 +00:00
|
|
|
ndevs, err := h.conn.ListAllNodeDevices(0)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if len(ndevs) > 0 {
|
|
|
|
h.DeviceList = make([]DeviceInfo, len(ndevs))
|
|
|
|
for i, dev := range ndevs {
|
|
|
|
if h.DeviceList[i].Name, err = dev.GetName(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if h.DeviceList[i].Capabilities, err = dev.ListCaps(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if h.DeviceList[i].XML, err = dev.GetXMLDesc(0); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2024-03-19 21:25:57 +00:00
|
|
|
dx := &libvirtxml.NodeDevice{}
|
|
|
|
if err != dx.Unmarshal(h.DeviceList[i].XML); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
h.DeviceList[i].Driver = dx.Driver.Name
|
|
|
|
dx.Capability.PCI.Class
|
2024-03-13 04:17:42 +00:00
|
|
|
|
|
|
|
dev.Free()
|
|
|
|
}
|
|
|
|
}
|
2024-03-11 02:32:11 +00:00
|
|
|
}
|