vm

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: BSD-3-Clause Imports: 14 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package vm provides an exec-like interface to VMs running cpud. It is designed to make running commands in a vm as easy as using exec.

For purposes of convenience, this package provides, via embed, a set of kernels and initramfs, for several architectures. Currently, it provides Linux kernels and initramfs for amd64, arm, arm64, and riscv64.

The kernels are minimal, hence small; but enable enough options to run cpud and u-root programs. The initramfs only contain the cpud and dhclient commands, and are also small.

The package asssumes that qemu for a target architecture is available on the system.

Users can not yet provide their own kernel and initramfs, though that is planned.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Uroot

func Uroot(d, GOOS, GOARCH string, opts ...string) (string, error)

Uroot builds a uroot cpio into the a directory. It returns the full path of the file, or an error.

Types

type Image

type Image struct {
	Kernel    []byte
	InitRAMFS []byte
	Cmd       []string
	Env       []string

	GOOS   string
	GOARCH string
	Opts   []string
	// contains filtered or unexported fields
}

Image defines an image, including []byte for a kernel and initramfs; a []string for the Cmd and its args; and its environment; and a directory in which to run.

func New

func New(goos, arch string, opts ...Option) (*Image, error)

New creates an Image, using the kernel and arch to select the Image. It will return an error if there is a problem uncompressing the kernel and initramfs.

func (*Image) CPUCommand

func (i *Image) CPUCommand(arg string, args ...string) (*client.Cmd, error)

CPUCommand runs a command in a guest running cpud. It is similar to exec.Command, in that it accepts an arg and a set of optional args. It differs in that it can return an error. If there are no errors, it returns a client.Cmd. The returned client.Cmd can be called with CombinedOutput, to make it easier to scan output from a command for error messages.

func (*Image) CommandContext

func (image *Image) CommandContext(ctx context.Context, d string, extra ...string) (*exec.Cmd, error)

CommandContext starts qemu, given a context, directory in which to run the command. The variadic arguments are a set of cpios which will be merged into Image.InitRAMFS. A typical use of the extra arguments will be to extend the initramfs; they are usually not needed.

func (*Image) Socket added in v0.2.0

func (i *Image) Socket() string

Socket is the host unix socket forwarded to cpud, or "" when the default port is forwarded instead.

func (*Image) StartVM

func (*Image) StartVM(c *exec.Cmd) error

StartVM is used to start a VM (or in fact any exec.Cmd). Once cmd.Start is called, StartVM delays for one second. This time has been experimentally determined as the minimum required for the guest network to be ready.

func (*Image) String

func (i *Image) String() string

func (*Image) Uroot

func (i *Image) Uroot(d string) (string, error)

Uroot builds a uroot cpio for an Image

type Option added in v0.2.0

type Option func(*Image) error

An Option configures an Image as New creates it.

func WithSocket added in v0.2.0

func WithSocket(path string) Option

WithSocket forwards a host unix socket to cpud rather than the default host port.

There is deliberately no option for a different port. A port is a global name, so picking a free one is a race: nothing stops another process taking it between the look and the bind, and the failure that follows is a confusing one. A socket path is a name the caller already owns, so concurrent VMs cannot collide and nothing has to be reserved. qemu creates the socket; the directory must exist.

The path must be absolute. qemu refuses a relative one outright -- "unix socket path must be absolute" -- and CommandContext runs it with Dir set to the VM's directory, so a relative path would not mean the same thing to qemu as it does to the dial here. Refusing it names the rule instead.

unix(7) caps sun_path, so the path must fit in maxSockPath bytes. A directory from os.MkdirTemp with a short prefix leaves room.

Jump to

Keyboard shortcuts

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