Documentation
¶
Overview ¶
Package libvirt provides a client wrapper for interacting with libvirt.
This package wraps github.com/digitalocean/go-libvirt to provide:
- Connection management (connect, disconnect, ping)
- Domain XML generation from VirtualMachine specs
- Utility functions for libvirt operations
The Client type provides a high-level interface for libvirt operations, while exposing the underlying *libvirt.Libvirt for packages that need direct access to the libvirt API.
Connection Management:
The package establishes connections to the local libvirt daemon via Unix socket:
client, err := libvirt.Connect()
if err != nil {
return err
}
defer client.Close()
// Check connection
if err := client.Ping(); err != nil {
return err
}
Domain XML Generation:
The package generates libvirt domain XML from VirtualMachine specs:
vm := &v1alpha1.VirtualMachine{
ObjectMeta: metav1.ObjectMeta{Name: "myvm"},
Spec: v1alpha1.VirtualMachineSpec{
VCPUs: 2,
MemoryGiB: 4,
BootDisk: v1alpha1.BootDisk{SizeGB: 20, Image: "fedora-43.qcow2"},
},
}
xml, err := libvirt.GenerateDomainXML(vm, "foundry-vms")
if err != nil {
return err
}
// Define domain in libvirt
dom, err := client.Libvirt().DomainDefineXML(xml)
if err != nil {
return err
}
Consumer-Side Interfaces:
This package does not define interfaces. Instead, consumers (internal/vm, internal/storage, internal/metadata) define their own LibvirtClient interfaces specifying only the operations they need. The *libvirt.Libvirt type satisfies these interfaces implicitly, enabling clean dependency injection.
See internal/vm/interfaces.go, internal/storage/types.go, and internal/metadata/storage.go for examples of consumer-side interfaces.
Index ¶
- Constants
- func GenerateDomainXML(vm *v1alpha1.VirtualMachine) (string, error)
- func GetBootVolumeName(vm *v1alpha1.VirtualMachine) string
- func GetCloudInitVolumeName(vm *v1alpha1.VirtualMachine) string
- func GetDataVolumeName(vm *v1alpha1.VirtualMachine, device string) string
- func GetStoragePool(vm *v1alpha1.VirtualMachine) string
- type Client
Constants ¶
const (
// BaseStoragePath is the default base path for VM storage
BaseStoragePath = "/var/lib/libvirt/images"
)
Variables ¶
This section is empty.
Functions ¶
func GenerateDomainXML ¶
func GenerateDomainXML(vm *v1alpha1.VirtualMachine) (string, error)
GenerateDomainXML generates libvirt domain XML from VM configuration
func GetBootVolumeName ¶ added in v0.3.0
func GetBootVolumeName(vm *v1alpha1.VirtualMachine) string
GetBootVolumeName returns the volume name for the boot disk. Format: <vm-name>_boot.qcow2
func GetCloudInitVolumeName ¶ added in v0.3.0
func GetCloudInitVolumeName(vm *v1alpha1.VirtualMachine) string
GetCloudInitVolumeName returns the volume name for the cloud-init ISO. Format: <vm-name>_cloudinit.iso
func GetDataVolumeName ¶ added in v0.3.0
func GetDataVolumeName(vm *v1alpha1.VirtualMachine, device string) string
GetDataVolumeName returns the volume name for a data disk. Format: <vm-name>_data-<device>.qcow2
func GetStoragePool ¶ added in v0.3.0
func GetStoragePool(vm *v1alpha1.VirtualMachine) string
GetStoragePool returns the storage pool name, using default if not set.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps a go-libvirt connection and provides high-level operations for managing VMs.
func Connect ¶
Connect establishes a connection to the local libvirt daemon. It returns a Client that must be closed via Close() when done.
If socketPath is empty, defaults to "/var/run/libvirt/libvirt-sock" (qemu:///system) If timeout is zero, defaults to 5 seconds.
This matches the Ansible implementation which uses the default local qemu:///system connection (UNIX domain socket).
func ConnectWithContext ¶
func ConnectWithContext(ctx context.Context, socketPath string, timeout time.Duration) (*Client, error)
ConnectWithContext establishes a connection with context support for cancellation.
func (*Client) Close ¶
Close closes the libvirt connection and releases resources. It is safe to call Close multiple times.