libvirtclient

package
v1.28.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatUptime

func FormatUptime(uptimeSec uint64) string

FormatUptime converts seconds to a human-readable uptime string (e.g., "2h 30m 45s")

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client holds the libvirt connection.

func NewClient

func NewClient(uri string, isoPoolName, templatePoolName string) (*Client, error)

NewClient opens a libvirt connection (e.g. "qemu:///system")

func (*Client) AttachDiskToVM

func (c *Client) AttachDiskToVM(uuidStr string, volumePath string, targetDev string) error

AttachDiskToVM attaches a disk volume to a running VM

func (*Client) AttachNetworkInterfaceToVM

func (c *Client) AttachNetworkInterfaceToVM(uuidStr string, networkName string, model string) error

AttachNetworkInterfaceToVM attaches a network interface to a VM (supports hot-plug and cold-plug)

func (*Client) CheckGuestAgentStatus

func (c *Client) CheckGuestAgentStatus(uuidStr string) (bool, error)

CheckGuestAgentStatus checks if guest agent is installed and running

func (*Client) Close

func (c *Client) Close() error

func (*Client) CreateNWFilter

func (c *Client) CreateNWFilter(req core.CreateNWFilterRequest) error

CreateNWFilter creates a new network filter

func (*Client) CreateNetwork

func (c *Client) CreateNetwork(name string, bridgeName string) error

CreateNetwork creates a new virtual network with the specified name and bridge name.

func (*Client) CreateStoragePool

func (c *Client) CreateStoragePool(cfg core.PoolConfig) error

CreateStoragePool creates a new storage pool

func (*Client) CreateVM

func (c *Client) CreateVM(cfg core.VMCreationConfig) (core.VM_Detailed, error)

CreateVM orchestrates creating a new volume and defining the VM.

func (*Client) CreateVMSnapshot

func (c *Client) CreateVMSnapshot(uuidStr string, cfg core.CreateSnapshotRequest) (core.Snapshot, error)

CreateVMSnapshot creates a new snapshot from a name and description.

func (*Client) CreateVolume

func (c *Client) CreateVolume(poolName string, volConfig core.VolumeConfig) error

func (*Client) DeleteImage

func (c *Client) DeleteImage(imageId string) error

DeleteImage deletes an image from the managed library

func (*Client) DeleteNWFilter

func (c *Client) DeleteNWFilter(name string) error

DeleteNWFilter deletes a network filter by name

func (*Client) DeleteNetwork

func (c *Client) DeleteNetwork(name string) error

func (*Client) DeleteVM

func (c *Client) DeleteVM(uuidStr string, deleteDisks bool) error

DeleteVM(uuidStr, deleteDisks)

func (*Client) DeleteVMSnapshot

func (c *Client) DeleteVMSnapshot(uuidStr string, snapshotName string) error

DeleteVMSnapshot deletes a snapshot by its name.

func (*Client) DeleteVolume

func (c *Client) DeleteVolume(poolName string, volumeName string) error

DeleteVolume deletes a storage volume

func (*Client) ForceStopVM

func (c *Client) ForceStopVM(name string) error

ForceStopVM force stops a VM by name

func (*Client) GetActivity

func (c *Client) GetActivity() []core.ActivityEvent

GetActivity returns the current activity events

func (*Client) GetDomainByName

func (c *Client) GetDomainByName(name string) (*libvirt.Domain, error)

GetDomainByName returns a libvirt domain by name

func (*Client) GetGuestAgentStatus

func (c *Client) GetGuestAgentStatus(vmName string) (string, error)

GetGuestAgentStatus gets guest agent status by VM name

func (*Client) GetHostResources

func (c *Client) GetHostResources() (core.HostResources, error)

GetHostResources returns memory/cpu/storage aggregates.

func (*Client) GetHostStatus

func (c *Client) GetHostStatus() (core.HostStatus, error)

GetHostStatus implements HostStatus gathering per your blueprint.

func (*Client) GetISOs

func (c *Client) GetISOs() ([]core.Image, error)

GetISOs returns available ISO images from the designated pool

func (*Client) GetImages

func (c *Client) GetImages() ([]core.Image, error)

GetImages returns all images from the managed library

func (*Client) GetNWFilter

func (c *Client) GetNWFilter(name string) (core.NWFilter, error)

GetNWFilter gets a specific network filter by name

func (*Client) GetNetworks

func (c *Client) GetNetworks() ([]core.Network, error)

GetNetworks fetches all virtual networks.

func (*Client) GetStoragePools

func (c *Client) GetStoragePools() ([]core.StoragePool, error)

GetStoragePools lists pools and their status/capacities.

func (*Client) GetSystemInterfaces

func (c *Client) GetSystemInterfaces() ([]core.SystemInterface, error)

GetSystemInterfaces returns all system network interfaces

func (*Client) GetTemplates

func (c *Client) GetTemplates() ([]core.Image, error)

GetTemplates returns available VM templates from the designated pool

func (*Client) GetVMDetails

func (c *Client) GetVMDetails(uuidStr string) (core.VM_Detailed, error)

GetVMDetails(uuidStr)

func (*Client) GetVMPerformance

func (c *Client) GetVMPerformance(uuidStr string) (core.PerformanceSample, error)

GetVMPerformance gets a single, real-time sample of performance counters for a VM.

func (*Client) GetVMSerialConsolePath

func (c *Client) GetVMSerialConsolePath(uuidStr string) (string, error)

GetVMSerialConsolePath extracts the PTY path for a VM's serial console

func (*Client) GetVMSnapshots

func (c *Client) GetVMSnapshots(uuidStr string) ([]core.Snapshot, error)

GetVMSnapshots lists all snapshots for a given VM.

func (*Client) GetVMSummaries

func (c *Client) GetVMSummaries() ([]core.VM_Summary, error)

GetVMSummaries returns a brief list, computing per-VM CPU percent via a short sample window. It samples all domains concurrently to avoid serial 1s sleeps per VM.

func (*Client) GetVMVNCInfo

func (c *Client) GetVMVNCInfo(uuidStr string) (core.VNCInfo, error)

GetVMVNCInfo retrieves VNC connection information for a VM

func (*Client) GetVolumes

func (c *Client) GetVolumes(poolName string) ([]core.Volume, error)

GetVolumes lists volumes for a pool

func (*Client) ImportImageFromPath

func (c *Client) ImportImageFromPath(sourcePath string) (core.Image, error)

ImportImageFromPath imports an image from a host path

func (*Client) InstallGuestAgent

func (c *Client) InstallGuestAgent(uuidStr string) error

InstallGuestAgent attempts to install guest agent via common package managers

func (*Client) ListNWFilters

func (c *Client) ListNWFilters() ([]core.NWFilter, error)

ListNWFilters fetches all network filters

func (*Client) NewStream

func (c *Client) NewStream(flags libvirt.StreamFlags) (*libvirt.Stream, error)

NewStream creates a new libvirt stream

func (*Client) PerformVMAction

func (c *Client) PerformVMAction(uuidStr string, action string) error

PerformVMAction(uuidStr, action)

func (*Client) RestartVM

func (c *Client) RestartVM(name string, force bool) error

RestartVM restarts a VM by name

func (*Client) RevertToVMSnapshot

func (c *Client) RevertToVMSnapshot(uuidStr string, snapshotName string) error

RevertToVMSnapshot reverts a VM's state.

func (*Client) StartVM

func (c *Client) StartVM(name string) error

StartVM starts a VM by name

func (*Client) StopVM

func (c *Client) StopVM(name string) error

StopVM stops a VM gracefully by name

func (*Client) UpdateNWFilter

func (c *Client) UpdateNWFilter(name string, req core.CreateNWFilterRequest) error

UpdateNWFilter updates an existing network filter

func (*Client) UpdateNetwork

func (c *Client) UpdateNetwork(name string, action string) error

DeleteNetwork deletes a virtual network by name

func (*Client) UpdateVolume

func (c *Client) UpdateVolume(poolName string, volumeName string, config core.VolumeConfig) error

UpdateVolume updates a storage volume (resize operation)

type ClientInterface

type ClientInterface interface {
	GetVMSummaries() ([]core.VM_Summary, error)
	GetVMDetails(uuidStr string) (core.VM_Detailed, error)
	GetVMSnapshots(uuidStr string) ([]core.Snapshot, error)
	CreateVMSnapshot(uuidStr string, cfg core.CreateSnapshotRequest) (core.Snapshot, error)
	DeleteVMSnapshot(uuidStr string, snapshotName string) error
	RevertToVMSnapshot(uuidStr string, snapshotName string) error
	GetVMPerformance(uuidStr string) (core.PerformanceSample, error)
	PerformVMAction(uuidStr string, action string) error
	DeleteVM(uuidStr string, deleteDisks bool) error
	CreateVM(cfg core.VMCreationConfig) (core.VM_Detailed, error)
	GetHostStatus() (core.HostStatus, error)
	GetHostResources() (core.HostResources, error)
	GetStoragePools() ([]core.StoragePool, error)
	GetVolumes(poolName string) ([]core.Volume, error)
	CreateVolume(poolName string, volConfig core.VolumeConfig) error
	GetNetworks() ([]core.Network, error)
	GetSystemInterfaces() ([]core.SystemInterface, error)
	CreateNetwork(name string, bridgeName string) error
	DeleteNetwork(name string) error
	GetISOs() ([]core.Image, error)
	GetTemplates() ([]core.Image, error)
	GetImages() ([]core.Image, error)
	ImportImageFromPath(path string) (core.Image, error)
	DeleteImage(imageId string) error
	GetVMSerialConsolePath(uuidStr string) (string, error)
	GetDomainByName(name string) (*libvirt.Domain, error)
	NewStream(flags libvirt.StreamFlags) (*libvirt.Stream, error)
	AttachDiskToVM(uuidStr string, volumePath string, targetDev string) error
	AttachNetworkInterfaceToVM(uuidStr string, networkName string, model string) error
	GetActivity() []core.ActivityEvent
	Close() error

	// Guest agent operations
	GetGuestAgentStatus(vmName string) (string, error)
	CheckGuestAgentStatus(uuidStr string) (bool, error)
	InstallGuestAgent(uuidStr string) error

	// Storage operations
	CreateStoragePool(cfg core.PoolConfig) error
	UpdateVolume(poolName string, volumeName string, config core.VolumeConfig) error
	DeleteVolume(poolName string, volumeName string) error

	// Network operations
	UpdateNetwork(name string, bridgeName string) error

	// VNC operations
	GetVMVNCInfo(uuidStr string) (core.VNCInfo, error)

	// Firewall/NWFilter operations
	ListNWFilters() ([]core.NWFilter, error)
	GetNWFilter(name string) (core.NWFilter, error)
	CreateNWFilter(req core.CreateNWFilterRequest) error
	UpdateNWFilter(name string, req core.CreateNWFilterRequest) error
	DeleteNWFilter(name string) error
}

ClientInterface defines the interface for all libvirt operations.

type DomainXML

type DomainXML struct {
	XMLName xml.Name `xml:"domain"`
	Type    string   `xml:"type,attr"`
	Name    string   `xml:"name"`
	Memory  struct {
		Unit  string `xml:"unit,attr"`
		Value uint64 `xml:",chardata"`
	} `xml:"memory"`
	VCPU struct {
		Placement string `xml:"placement,attr"`
		Value     int    `xml:",chardata"`
	} `xml:"vcpu"`
	OS struct {
		Type struct {
			Arch    string `xml:"arch,attr"`
			Machine string `xml:"machine,attr"`
			Value   string `xml:",chardata"`
		} `xml:"type"`
		Boot struct {
			Dev string `xml:"dev,attr"`
		} `xml:"boot"`
		Kernel  string `xml:"kernel,omitempty"`  // For PXE: kernel URL
		Initrd  string `xml:"initrd,omitempty"`  // For PXE: initrd URL
		Cmdline string `xml:"cmdline,omitempty"` // For PXE: kernel arguments
	} `xml:"os"`
	Devices struct {
		Emulator string `xml:"emulator"`
		Disks    []struct {
			Type   string `xml:"type,attr"`
			Device string `xml:"device,attr"`
			Driver struct {
				Name string `xml:"name,attr"`
				Type string `xml:"type,attr"`
			} `xml:"driver"`
			Source struct {
				Pool   string `xml:"pool,attr,omitempty"`
				Volume string `xml:"volume,attr,omitempty"`
				File   string `xml:"file,attr,omitempty"`
			} `xml:"source"`
			Target struct {
				Dev string `xml:"dev,attr"`
				Bus string `xml:"bus,attr"`
			} `xml:"target"`
		} `xml:"disk"`
		Interfaces []struct {
			Type   string `xml:"type,attr"`
			Source struct {
				Network string `xml:"network,attr,omitempty"`
				Bridge  string `xml:"bridge,attr,omitempty"`
				Dev     string `xml:"dev,attr,omitempty"`
				Mode    string `xml:"mode,attr,omitempty"`
			} `xml:"source"`
			Model struct {
				Type string `xml:"type,attr"`
			} `xml:"model"`
		} `xml:"interface"`
		Graphics struct {
			Type     string `xml:"type,attr"`
			Port     int    `xml:"port,attr"`
			Autoport string `xml:"autoport,attr"`
			Listen   struct {
				Type string `xml:"type,attr"`
			} `xml:"listen"`
		} `xml:"graphics"`
		Serial struct {
			Type   string `xml:"type,attr"`
			Target struct {
				Type  string `xml:"type,attr"`
				Port  int    `xml:"port,attr"`
				Model struct {
					Name string `xml:"name,attr"`
				} `xml:"model"`
			} `xml:"target"`
		} `xml:"serial"`
		Console struct {
			Type   string `xml:"type,attr"`
			Target struct {
				Type string `xml:"type,attr"`
				Port int    `xml:"port,attr"`
			} `xml:"target"`
		} `xml:"console"`
	} `xml:"devices"`
}

DomainXML defines the structure for marshalling a libvirt domain XML. This is a simplified version for v0.

type DomainXMLForPTY

type DomainXMLForPTY struct {
	Devices struct {
		Consoles []struct {
			Type   string `xml:"type,attr"`
			Source struct {
				Path string `xml:"path,attr"`
			} `xml:"source"`
		} `xml:"console"`
		Serials []struct {
			Type   string `xml:"type,attr"`
			Source struct {
				Path string `xml:"path,attr"`
			} `xml:"source"`
		} `xml:"serial"`
	} `xml:"devices"`
}

DomainXMLForPTY represents the structure for parsing libvirt domain XML to extract PTY paths

type DomainXMLForVNC

type DomainXMLForVNC struct {
	Devices struct {
		Graphics struct {
			Type   string `xml:"type,attr"`
			Port   string `xml:"port,attr"`
			Listen string `xml:"listen,attr"`
		} `xml:"graphics"`
	} `xml:"devices"`
}

DomainXMLForVNC represents the structure for parsing libvirt domain XML to extract VNC details

type GuestAgentInfo

type GuestAgentInfo struct {
	OSName      string    `json:"os_name"`
	OSVersion   string    `json:"os_version"`
	IPAddresses []string  `json:"ip_addresses"`
	Hostname    string    `json:"hostname"`
	Available   bool      `json:"available"`
	LastSeen    time.Time `json:"last_seen"`
}

GuestAgentInfo holds information retrieved from qemu guest agent

type InterfaceStats

type InterfaceStats struct {
	RxBytes   uint64
	TxBytes   uint64
	RxPackets uint64
	TxPackets uint64
}

InterfaceStats represents network interface statistics

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL