libvirt

package
v0.0.0-...-a66760f Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	QemuConnect = "qemu:///system"
)

Variables

View Source
var (
	ErrDomainNotFound = errors.New("domain not found")
)

Functions

func GenerateMacAddress

func GenerateMacAddress() string

func NewDefaultRuncmd

func NewDefaultRuncmd() []string

Types

type Client

type Client interface {
	// CLOUDINIT
	CreateCloudinit(ctx context.Context, params CreateCloudinitParams) error
	CreateCloudinitByReader(ctx context.Context, params CreateCloudinitByReaderParams) error
	WriteCloudinit(ctx context.Context, userdata io.Reader, metadata io.Reader, networkConfig io.Reader, cloudinitFile io.Writer) error

	// DOMAIN
	GetDomain(ctx context.Context, domainID string) (Domain, error)
	GetDomainMonitor(ctx context.Context, domainID string) (DomainMonitor, error)
	ListDomains(ctx context.Context, params ListDomainsParams) ([]Domain, error)
	CreateDomain(ctx context.Context, domain Domain) error
	UpdateDomain(ctx context.Context, domainID string, params UpdateDomainParams) error
	DeleteDomain(ctx context.Context, domainID string) error
	StartDomain(ctx context.Context, domainID string) error
	StopDomain(ctx context.Context, domainID string) error
	GetPrivateIP(ctx context.Context, domainID string) (string, error)

	// QEMU
	CreateImage(ctx context.Context, params CreateImageParams) error
}

func NewClient

func NewClient() Client

type ClientImpl

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

func (*ClientImpl) CreateCloudinit

func (s *ClientImpl) CreateCloudinit(ctx context.Context, params CreateCloudinitParams) error

func (*ClientImpl) CreateCloudinitByReader

func (s *ClientImpl) CreateCloudinitByReader(ctx context.Context, params CreateCloudinitByReaderParams) error

func (*ClientImpl) CreateDomain

func (s *ClientImpl) CreateDomain(ctx context.Context, domain Domain) error

CreateDomain creates a new domain in libvirt

Supports rollback operation, safe to use in anywhere, anytime

func (*ClientImpl) CreateImage

func (s *ClientImpl) CreateImage(ctx context.Context, params CreateImageParams) error

func (*ClientImpl) DeleteDomain

func (s *ClientImpl) DeleteDomain(ctx context.Context, domainID string) error

DeleteDomain removes the domain from libvirt

Does not support rollback operation so it should done last

func (*ClientImpl) GetDomain

func (s *ClientImpl) GetDomain(ctx context.Context, domainID string) (Domain, error)

func (*ClientImpl) GetDomainMonitor

func (s *ClientImpl) GetDomainMonitor(ctx context.Context, domainID string) (DomainMonitor, error)

func (*ClientImpl) GetPrivateIP

func (s *ClientImpl) GetPrivateIP(ctx context.Context, domainID string) (string, error)

func (*ClientImpl) ListDomains

func (s *ClientImpl) ListDomains(ctx context.Context, params ListDomainsParams) ([]Domain, error)

func (*ClientImpl) RemoveImage

func (s *ClientImpl) RemoveImage(ctx context.Context, imgPath string) error

func (*ClientImpl) StartDomain

func (s *ClientImpl) StartDomain(ctx context.Context, domainID string) error

func (*ClientImpl) StopDomain

func (s *ClientImpl) StopDomain(ctx context.Context, domainID string) error

func (*ClientImpl) UpdateDomain

func (s *ClientImpl) UpdateDomain(ctx context.Context, domainID string, params UpdateDomainParams) error

func (*ClientImpl) WriteCloudinit

func (s *ClientImpl) WriteCloudinit(ctx context.Context, userdata io.Reader, metadata io.Reader, networkConfig io.Reader, cloudinitFile io.Writer) error

type Cpu

type Cpu struct {
	Value uint `json:"value"`
}

type CreateCloudinitByReaderParams

type CreateCloudinitByReaderParams struct {
	Filepath      string
	Userdata      io.Reader
	Metadata      io.Reader
	NetworkConfig io.Reader
}

type CreateCloudinitParams

type CreateCloudinitParams struct {
	Filepath      string
	Userdata      Userdata
	Metadata      Metadata
	NetworkConfig NetworkConfig
}

type CreateImageParams

type CreateImageParams struct {
	BaseImagePath  string
	CloneImagePath string
	Size           uint
}

type Domain

type Domain struct {
	ID      string
	Name    string
	Memory  Memory
	Cpu     Cpu
	OS      OS
	Storage uint
	Network DomainNetwork
}

func FromLibvirtToDomain

func FromLibvirtToDomain(domain libvirt.Domain) (Domain, error)

func (Domain) BaseFileName

func (d Domain) BaseFileName() string

func (Domain) BaseImagePath

func (d Domain) BaseImagePath() string

func (Domain) CloudinitFileName

func (d Domain) CloudinitFileName() string

func (Domain) CloudinitPath

func (d Domain) CloudinitPath() string

func (Domain) VMFileName

func (d Domain) VMFileName() string

func (Domain) VMImagePath

func (d Domain) VMImagePath() string

type DomainMonitor

type DomainMonitor struct {
	ID           string
	Status       Status
	CPUUsage     float64 // in percentage
	RAMUsage     float64 // in MB
	StorageUsage float64 // in MB
	NetworkIn    float64 // in MB
	NetworkOut   float64 // in MB
}

type DomainNetwork

type DomainNetwork struct {
	MacAddress string
}

type Ethernet

type Ethernet struct {
	Match          Match  `json:"match" yaml:"match"`
	Dhcp4          bool   `json:"dhcp4,omitempty" yaml:"dhcp4,omitempty"`
	DhcpIdentifier string `json:"dhcp-identifier,omitempty" yaml:"dhcp-identifier,omitempty"`
	SetName        string `json:"set-name,omitempty" yaml:"set-name,omitempty"`
}

type ListDomainsParams

type ListDomainsParams struct {
	Flags libvirt.ConnectListAllDomainsFlags
}

type Match

type Match struct {
	Driver string `json:"driver,omitempty" yaml:"driver,omitempty"`
}

type Memory

type Memory struct {
	Value uint `json:"value"`
	Unit  Unit `json:"unit"`
}

type Metadata

type Metadata struct {
	InstanceID    string `json:"instance-id" yaml:"instance-id"`
	LocalHostname string `json:"local-hostname" yaml:"local-hostname"`
}

func NewDefaultMetadata

func NewDefaultMetadata() Metadata

type Network

type Network struct {
	Version   int                 `json:"version,omitempty" yaml:"version,omitempty"`
	Ethernets map[string]Ethernet `json:"ethernets,omitempty" yaml:"ethernets,omitempty"`
}

type NetworkConfig

type NetworkConfig struct {
	Network Network `json:"network" yaml:"network"`
}

func NewDefaultNetworkConfig

func NewDefaultNetworkConfig() NetworkConfig

type OS

type OS struct {
	Name string `json:"name"`
	Type string `json:"type"`
	Arch string `json:"arch"`
}

type Status

type Status string
const (
	StatusUnknown Status = "STATUS_UNKNOWN"
	StatusPending Status = "STATUS_PENDING"
	StatusRunning Status = "STATUS_RUNNING"
	StatusStopped Status = "STATUS_STOPPED"
	StatusError   Status = "STATUS_ERROR"
)

func ToStatus

func ToStatus(state libvirt.DomainState) Status

type Unit

type Unit string
const (
	UnitKB Unit = "KiB"
	UnitMB Unit = "MiB"
	UnitGB Unit = "GiB"
	UnitTB Unit = "TiB"
)

type UpdateDomainParams

type UpdateDomainParams struct {
	Name    *string
	Cpu     *uint
	Ram     *uint
	Storage *uint
}

type User

type User struct {
	Name              string   `json:"name,omitempty" yaml:"name,omitempty"`
	SSHAuthorizedKeys []string `json:"ssh-authorized-keys,omitempty" yaml:"ssh-authorized-keys,omitempty"`
	Passwd            string   `json:"passwd,omitempty" yaml:"passwd,omitempty"`
	LockPasswd        bool     `json:"lock_passwd" yaml:"lock_passwd"`
	Groups            string   `json:"groups,omitempty" yaml:"groups,omitempty"`
	Sudo              string   `json:"sudo,omitempty" yaml:"sudo,omitempty"`
	Shell             string   `json:"shell,omitempty" yaml:"shell,omitempty"`
}

func NewDefaultUser

func NewDefaultUser() User

type Userdata

type Userdata struct {
	Users  []User   `json:"users,omitempty" yaml:"users,omitempty"`
	Runcmd []string `json:"runcmd,omitempty" yaml:"runcmd,omitempty"`
}

func NewDefaultUserdata

func NewDefaultUserdata() Userdata

Jump to

Keyboard shortcuts

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