hyperkit

package
v0.0.0-...-3cb0d54 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2022 License: BSD-2-Clause Imports: 18 Imported by: 9

Documentation

Overview

Package hyperkit provides a Go wrapper around the hyperkit command. It currently shells out to start hyperkit with the provided configuration.

Most of the arguments should be self explanatory, but console handling deserves a mention. If the Console is configured with ConsoleStdio, the hyperkit is started with stdin, stdout, and stderr plumbed through to the VM console. If Console is set to ConsoleFile hyperkit the console output is redirected to a file and console input is disabled. For this mode StateDir has to be set and the interactive console is accessible via a 'tty' file created there.

Currently this module has some limitations: - Only supports zero or one disk image - Only support zero or one network interface connected to VPNKit - Only kexec boot

This package is currently implemented by shelling out a hyperkit process. In the future we may change this to become a wrapper around the hyperkit library.

Index

Constants

View Source
const (
	// ConsoleStdio configures console to use Stdio (deprecated)
	ConsoleStdio = iota
	// ConsoleFile configures console to a tty and output to a file (deprecated)
	ConsoleFile
	// ConsoleLog configures console to a tty and sends its contents to the logs (deprecated)
	ConsoleLog
)
View Source
const (
	// NoInteractiveConsole disables the interactive console.
	NoInteractiveConsole = InteractiveConsole(iota)
	// StdioInteractiveConsole creates a console on stdio.
	StdioInteractiveConsole
	// TTYInteractiveConsole creates a console on a TTY.
	TTYInteractiveConsole
)

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(l Logger)

SetLogger sets the logger to use.

Types

type Disk

type Disk interface {
	// GetPath returns the location of the disk image file.
	GetPath() string
	// SetPath changes the location of the disk image file.
	SetPath(p string)
	// GetSize returns the desired disk size.
	GetSize() int
	// GetCurrentSize returns the current disk size in MiB.
	GetCurrentSize() (int, error)
	// String returns the path.
	String() string

	// Exists iff the disk image file can be stat'd without error.
	Exists() bool
	// Ensure creates the disk image if needed, and resizes it if needed.
	Ensure() error
	// Stop can be called when hyperkit has quit.  It performs sanity checks, compaction, etc.
	Stop() error

	// AsArgument returns the command-line option to pass after `-s <slot>:0,` to hyperkit for this disk.
	AsArgument() string
	// contains filtered or unexported methods
}

Disk in an interface for qcow2 and raw disk images.

func NewDisk

func NewDisk(spec string, size int) (Disk, error)

NewDisk creates a qcow/raw disk configuration based on the spec.

type DiskFormat

type DiskFormat int

DiskFormat describes the physical format of the disk data

const (
	// DiskFormatQcow means the disk is a qcow2.
	DiskFormatQcow DiskFormat = iota

	// DiskFormatRaw means the disk is a raw file.
	DiskFormatRaw
)

func GetDiskFormat

func GetDiskFormat(path string) DiskFormat

GetDiskFormat computes the format based on the path's extensions.

type HyperKit

type HyperKit struct {
	// HyperKit is the path to the hyperkit binary.
	HyperKit string `json:"hyperkit"`
	// Argv0 is the name to declare as argv[0].  If left empty, argv[0] is left untouched.
	Argv0 string `json:"argv0"`

	// StateDir is the directory where runtime state is kept. If left empty, no state will be kept.
	StateDir string `json:"state_dir"`
	// VPNKitSock is the location of the VPNKit socket used for networking.
	VPNKitSock string `json:"vpnkit_sock"`
	// VPNKitUUID is a string containing a UUID, it can be used in conjunction with VPNKit to get consistent IP address.
	VPNKitUUID string `json:"vpnkit_uuid"`
	// VPNKitPreferredIPv4 is a string containing an IPv4 address, it can be used to request a specific IP for a UUID from VPNKit.
	VPNKitPreferredIPv4 string `json:"vpnkit_preferred_ipv4"`
	// UUID is a string containing a UUID, it sets BIOS DMI UUID for the VM (as found in /sys/class/dmi/id/product_uuid on Linux).
	UUID string `json:"uuid"`
	// Disks contains disk images to use/create.
	Disks []Disk `json:"disks"`
	// ISOImage is the (optional) path to a ISO image to attach.
	ISOImages []string `json:"iso"`

	// VSock enables the virtio-socket device and exposes it on the host.
	VSock bool `json:"vsock"`
	// VSockDir specifies where the unix domain sockets will be created. Defaults to StateDir when empty.
	VSockDir string `json:"vsock_dir"`
	// VSockPorts is a list of guest VSock ports that should be exposed as sockets on the host.
	VSockPorts []int `json:"vsock_ports"`
	// VSock guest CID
	VSockGuestCID int `json:"vsock_guest_cid"`

	// VMNet is whether to create vmnet network.
	VMNet bool `json:"vmnet"`

	// Sockets9P holds the 9P sockets.
	Sockets9P []Socket9P `json:"9p_sockets"`

	// Kernel is the path to the kernel image to boot.
	Kernel string `json:"kernel"`
	// Initrd is the path to the initial ramdisk to boot off.
	Initrd string `json:"initrd"`
	// Bootrom is the path to a boot rom eg for UEFI boot.
	Bootrom string `json:"bootrom"`

	// CPUs is the number CPUs to configure.
	CPUs int `json:"cpus"`
	// Memory is the amount of megabytes of memory for the VM.
	Memory int `json:"memory"`

	// Console defines where the console of the VM should be connected to. (deprecated)
	Console int `json:"console"`

	// Serials defines what happens to the I/O on the serial ports. If this is not nil
	// it overrides the Console setting.
	Serials []Serial `json:"serials"`

	// Pid of the hyperkit process
	Pid int `json:"pid"`
	// Arguments used to execute the hyperkit process
	Arguments []string `json:"arguments"`
	// CmdLine is a single string of the command line
	CmdLine string `json:"cmdline"`
	// contains filtered or unexported fields
}

HyperKit contains the configuration of the hyperkit VM

func New

func New(hyperkit, vpnkitsock, statedir string) (*HyperKit, error)

New creates a template config structure.

  • If hyperkit can't be found an error is returned.
  • If vpnkitsock is empty no networking is configured. If it is set to "auto" it tries to re-use the Docker for Mac VPNKit connection.
  • If statedir is "" no state is written to disk.

func (*HyperKit) IsRunning

func (h *HyperKit) IsRunning() bool

IsRunning returns true if the hyperkit process is running.

func (*HyperKit) Remove

func (h *HyperKit) Remove(keepDisk bool) error

Remove deletes all statefiles if present. This also removes the StateDir if empty. If keepDisk is set, the disks will not get removed.

func (*HyperKit) Run

func (h *HyperKit) Run(cmdline string) error

Run the VM with a given command line until it exits.

func (*HyperKit) Start

func (h *HyperKit) Start(cmdline string) (chan error, error)

Start the VM with a given command line in the background. On success, returns a channel on which the result of Wait'ing for hyperkit is sent. Return failures to start the process.

func (*HyperKit) Stop

func (h *HyperKit) Stop() error

Stop the running VM

func (*HyperKit) String

func (h *HyperKit) String() string

Convert to json string

type InteractiveConsole

type InteractiveConsole int

InteractiveConsole is an optional interactive VM console.

type Logger

type Logger interface {
	// Debugf logs a message with "debug" severity (very verbose).
	Debugf(format string, v ...interface{})
	// Infof logs a message with "info" severity (less verbose).
	Infof(format string, v ...interface{})
	// Warnf logs a message with "warn" (non-fatal) severity.
	Warnf(format string, v ...interface{})
	// Errorf logs an (non-fatal) error.
	Errorf(format string, v ...interface{})
	// Fatalf logs a fatal error message, and exits 1.
	Fatalf(format string, v ...interface{})
}

Logger is an interface for logging.

type QcowDisk

type QcowDisk struct {
	// Path specifies where the image file will be.
	Path string `json:"path"`
	// Size specifies the size of the disk.
	Size int `json:"size"`
	// Format is passed as-is to the driver.
	Format string `json:"format"`
	// Trim specifies whether we should trim the image file.
	Trim bool `json:"trim"`
	// QcowToolPath is the path to the binary to use to manage this image.
	// Defaults to "qcow-tool" when empty.
	QcowToolPath   string
	OnFlush        string
	CompactAfter   int
	KeepErased     int
	RuntimeAsserts bool
	Stats          string
}

QcowDisk describes a qcow2 disk image file.

func (*QcowDisk) AsArgument

func (d *QcowDisk) AsArgument() string

AsArgument returns the command-line option to pass after `-s <slot>:0,` to hyperkit for this disk.

func (*QcowDisk) Ensure

func (d *QcowDisk) Ensure() error

Ensure creates the disk image if needed, and resizes it if needed.

func (*QcowDisk) Exists

func (d *QcowDisk) Exists() bool

Exists iff the image file can be stat'd without error.

func (*QcowDisk) GetCurrentSize

func (d *QcowDisk) GetCurrentSize() (int, error)

GetCurrentSize returns the current disk size in MiB.

func (*QcowDisk) GetPath

func (d *QcowDisk) GetPath() string

GetPath returns the location of the disk image file.

func (*QcowDisk) GetSize

func (d *QcowDisk) GetSize() int

GetSize returns the desired disk size.

func (*QcowDisk) QcowTool

func (d *QcowDisk) QcowTool(verb string, args ...string) *exec.Cmd

QcowTool prepares a call to qcow-tool on this image.

func (*QcowDisk) SetPath

func (d *QcowDisk) SetPath(p string)

SetPath changes the location of the disk image file.

func (*QcowDisk) Stop

func (d *QcowDisk) Stop() error

Stop cleans up this disk when we are quitting.

func (*QcowDisk) String

func (d *QcowDisk) String() string

String returns the path.

type RawDisk

type RawDisk struct {
	// Path specifies where the image file will be.
	Path string `json:"path"`
	// Size specifies the size of the disk.
	Size int `json:"size"`
	// Format is passed as-is to the driver.
	Format string `json:"format"`
	// Trim specifies whether we should trim the image file.
	Trim bool `json:"trim"`
}

RawDisk describes a raw disk image file.

func (*RawDisk) AsArgument

func (d *RawDisk) AsArgument() string

AsArgument returns the command-line option to pass after `-s <slot>:0,` to hyperkit for this disk.

func (*RawDisk) Ensure

func (d *RawDisk) Ensure() error

Ensure creates the disk image if needed, and resizes it if needed.

func (*RawDisk) Exists

func (d *RawDisk) Exists() bool

Exists iff the image file can be stat's without error.

func (*RawDisk) GetCurrentSize

func (d *RawDisk) GetCurrentSize() (int, error)

GetCurrentSize returns the current disk size in MiB.

func (*RawDisk) GetPath

func (d *RawDisk) GetPath() string

GetPath returns the location of the disk image file.

func (*RawDisk) GetSize

func (d *RawDisk) GetSize() int

GetSize returns the desired disk size.

func (*RawDisk) SetPath

func (d *RawDisk) SetPath(p string)

SetPath changes the location of the disk image file.

func (*RawDisk) Stop

func (d *RawDisk) Stop() error

Stop cleans up this disk when we are quitting.

func (*RawDisk) String

func (d *RawDisk) String() string

String returns the path.

type Serial

type Serial struct {
	// InteractiveConsole allows a user to connect to a live VM serial console.
	InteractiveConsole InteractiveConsole
	// LogToRingBuffer will write console output to a fixed size ring buffer file.
	LogToRingBuffer bool
	// LogToASL will write console output to the Apple System Log.
	LogToASL bool
}

Serial port.

type Socket9P

type Socket9P struct {
	Path string `json:"path"`
	Tag  string `json:"tag"`
}

Socket9P contains a unix domain socket path and 9p tag

type StandardLogger

type StandardLogger struct{}

StandardLogger makes the go standard logger comply to our Logger interface.

func (*StandardLogger) Debugf

func (*StandardLogger) Debugf(f string, v ...interface{})

Debugf logs a message with "debug" severity.

func (*StandardLogger) Errorf

func (*StandardLogger) Errorf(f string, v ...interface{})

Errorf logs an (non-fatal) error.

func (*StandardLogger) Fatalf

func (*StandardLogger) Fatalf(f string, v ...interface{})

Fatalf logs a fatal error message, and exits 1.

func (*StandardLogger) Infof

func (*StandardLogger) Infof(f string, v ...interface{})

Infof logs a message with "info" severity.

func (*StandardLogger) Warnf

func (*StandardLogger) Warnf(f string, v ...interface{})

Warnf logs a message with "warn" (non-fatal) severity.

Directories

Path Synopsis
This mostly serves as sample and test code for the hyperkit package
This mostly serves as sample and test code for the hyperkit package

Jump to

Keyboard shortcuts

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