hypervisor

package
v0.0.0-...-2e3d018 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: Apache-2.0 Imports: 6 Imported by: 7

README

hypervisor

Package hypervisor provides management facilities for one or more QEMU virtual machines on a hypervisor.

Documentation

Overview

Package hypervisor provides management facilities for one or more QEMU virtual machines on a hypervisor.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

type Driver interface {
	NewMonitor(domain string) (qmp.Monitor, error)
	DomainNames() ([]string, error)
}

A Driver is a QEMU QMP monitor driver that a Hypervisor can use to perform actions on groups of virtual machines.

type Hypervisor

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

A Hypervisor enables access to all virtual machines on a QEMU hypervisor.

func New

func New(driver Driver) *Hypervisor

New creates a new Hypervisor using the input Driver.

Example
package main

import (
	"fmt"
	"log"

	"github.com/digitalocean/go-qemu/hypervisor"
	"github.com/digitalocean/go-qemu/qmp"
	"github.com/digitalocean/go-qemu/qmp/qmptest"
)

func main() {
	// Create a hypervisor.Hypervisor using an in-memory hypervisor.Driver,
	// that returns a fixed list of three domains.
	hv := hypervisor.New(newDriver([]string{
		"foo",
		"bar",
		"baz",
	}))

	// Connect to the monitor socket of each domain using the in-memory driver.
	domains, err := hv.Domains()
	if err != nil {
		log.Fatalf("failed to connect to domains: %v", err)
	}

	fmt.Println("hypervisor:")
	for _, d := range domains {
		// Return the domain's QEMU version.
		version, err := d.Version()
		if err != nil {
			log.Fatalf("failed to check domain QEMU version: %v", err)
		}

		// Print some information about the domain.
		fmt.Printf("  - %s - QEMU %s\n", d.Name, version)

		// Close the domain to clean up its resources and underlying monitor.
		if err := d.Close(); err != nil {
			log.Fatalf("failed to close domain: %v", err)
		}
	}

}

var _ hypervisor.Driver = &driver{}

func newDriver(domains []string) *driver {
	return &driver{
		domains: domains,
	}
}

type driver struct {
	domains []string
}

func (d *driver) NewMonitor(domain string) (qmp.Monitor, error) {
	m := qmptest.NewMonitor(func(cmd qmp.Command) (interface{}, error) {
		return run(domain, cmd)
	})

	return m, nil
}

func (d *driver) DomainNames() ([]string, error) {
	return d.domains, nil
}

func run(domain string, cmd qmp.Command) (interface{}, error) {
	switch cmd.Execute {
	case "query-version":
		return runQueryVersion(domain), nil
	}

	return nil, fmt.Errorf("unknown command: %q", cmd.Execute)
}

func runQueryVersion(domain string) interface{} {
	var response struct {
		ID     string      `json:"id"`
		Return qmp.Version `json:"return"`
	}
	response.Return.QEMU.Major = 2

	switch domain {
	case "bar":
		response.Return.QEMU.Micro = 1
	case "baz":
		response.Return.QEMU.Micro = 2
	}

	return response
}
Output:

hypervisor:
  - foo - QEMU 2.0.0
  - bar - QEMU 2.0.1
  - baz - QEMU 2.0.2

func (*Hypervisor) Domain

func (hv *Hypervisor) Domain(name string) (*qemu.Domain, error)

Domain retrieves a single virtual machine from a hypervisor, connecting to its monitor socket. If no virtual machine exists with the given name, an error is returned.

The Domain's Close method must be called to clean up its resources when they are no longer needed.

func (*Hypervisor) DomainNames

func (hv *Hypervisor) DomainNames() ([]string, error)

DomainNames retrieves the names of all virtual machines which reside on a given hypervisor, so that individual connections can be opened using the Domain method, as needed.

func (*Hypervisor) Domains

func (hv *Hypervisor) Domains() ([]*qemu.Domain, error)

Domains retrieves all virtual machines which reside on a given hypervisor, connecting to the monitor socket of each machine.

Each Domain's Close method must be called to clean up its resources when they are no longer needed.

type RPCDriver

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

RPCDriver is a QEMU QMP monitor driver which communicates via libvirt's RPC interface.

func NewRPCDriver

func NewRPCDriver(newConn func() (net.Conn, error)) *RPCDriver

NewRPCDriver configures a hypervisor driver using Libvirt RPC. The provided newConn function should return an established network connection with the target libvirt daemon.

func (*RPCDriver) DomainNames

func (d *RPCDriver) DomainNames() ([]string, error)

DomainNames retrieves all hypervisor domain names using libvirt RPC.

func (*RPCDriver) NewMonitor

func (d *RPCDriver) NewMonitor(domain string) (qmp.Monitor, error)

NewMonitor creates a new qmp.Monitor using libvirt RPC.

func (*RPCDriver) Version

func (d *RPCDriver) Version() (string, error)

Version returns the version string for the libvirt daemon.

type SocketAddress

type SocketAddress struct {
	// Network should be one of "unix", "tcp", etc.
	Network string

	// Address should be a host:port address or UNIX socket filepath,
	// such "8.8.8.8:4444" or "/var/lib/qemu/example.socket".
	Address string

	// Timeout specifies how long the monitor socket will attempt to be
	// reached before timing out.
	Timeout time.Duration
}

A SocketAddress is a QEMU QMP monitor socket address used to configure a SocketDriver.

type SocketDriver

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

A SocketDriver is a QEMU QMP monitor driver which communicates directly with a QEMU monitor socket.

func NewSocketDriver

func NewSocketDriver(addrs []SocketAddress) *SocketDriver

NewSocketDriver configures a SocketDriver using one or more SocketAddress structures for configuration.

func (*SocketDriver) DomainNames

func (d *SocketDriver) DomainNames() ([]string, error)

DomainNames retrieves all hypervisor domain names known to the SocketDriver.

func (*SocketDriver) NewMonitor

func (d *SocketDriver) NewMonitor(addr string) (qmp.Monitor, error)

NewMonitor creates a new qmp.Monitor using a QEMU monitor socket at the specified address.

type Versioner

type Versioner interface {
	Version() (string, error)
}

A Versioner is a Driver that is able to report its version on the Hypervisor.

Jump to

Keyboard shortcuts

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