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 ¶
- func Uroot(d, GOOS, GOARCH string, opts ...string) (string, error)
- type Image
- func (i *Image) CPUCommand(arg string, args ...string) (*client.Cmd, error)
- func (image *Image) CommandContext(ctx context.Context, d string, extra ...string) (*exec.Cmd, error)
- func (i *Image) Socket() string
- func (*Image) StartVM(c *exec.Cmd) error
- func (i *Image) String() string
- func (i *Image) Uroot(d string) (string, error)
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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
Socket is the host unix socket forwarded to cpud, or "" when the default port is forwarded instead.
type Option ¶ added in v0.2.0
An Option configures an Image as New creates it.
func WithSocket ¶ added in v0.2.0
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.