Skip to content

Commit 3ea81eb

Browse files
committed
Add documentation
Signed-off-by: Ronald G Minnich <[email protected]>
1 parent 3e93145 commit 3ea81eb

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

vm/doc.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Package vm provides an exec-like interface to VMs running cpud. It is
2+
// designed to make running commands in a vm as easy as using exec.
3+
//
4+
// For purposes of convenience, this package provides, via embed, a set of
5+
// kernels and initramfs, for several architectures. Currently, it provides
6+
// Linux kernels and initramfs for am64, arm, arm64, and riscv64.
7+
//
8+
// The kernels are minimal, hence small; but enable enough options to run
9+
// cpud and u-root programs. The initramfs only contain the cpud and
10+
// dhclient commands, and are also small.
11+
//
12+
// The package asssumes that qemu for a target architecture is available on
13+
// the system.
14+
//
15+
// Users can not yet provide their own kernel and initramfs, though
16+
// that is planned.
17+
package vm

vm/vm.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import (
1818
"github.com/u-root/cpu/client"
1919
)
2020

21-
// generate this boiler plate code? Not clear it's worth it,
22-
// it changes maybe once a year.
23-
2421
//go:embed initramfs_linux_amd64.cpio.gz
2522
var linux_amd64 []byte
2623

@@ -45,6 +42,9 @@ var kernel_linux_arm []byte
4542
//go:embed kernel_linux_riscv64.gz
4643
var kernel_linux_riscv64 []byte
4744

45+
// Image defines an image, including []byte for a kernel and initramfs;
46+
// a []string for the Cmd and its args; and its environment;
47+
// and a directory in which to run.
4848
type Image struct {
4949
Kernel []byte
5050
InitRAMFS []byte
@@ -60,6 +60,9 @@ var images = map[string]Image{
6060
"linux_riscv64": {Kernel: kernel_linux_riscv64, InitRAMFS: linux_riscv64, Cmd: []string{"qemu-system-riscv64", "-M", "virt", "-cpu", "rv64", "-m", "1G"}},
6161
}
6262

63+
// New creates an Image, using the kernel and arch to select the Image.
64+
// It will return an error if there is a problem uncompressing
65+
// the kernel and initramfs.
6366
func New(kernel, arch string) (*Image, error) {
6467
common := []string{ /*"-serial", "/dev/tty",*/ "-nographic",
6568
"-netdev", "user,id=net0,ipv4=on,hostfwd=tcp::17010-:17010",
@@ -100,6 +103,8 @@ func New(kernel, arch string) (*Image, error) {
100103
return &Image{Kernel: k, InitRAMFS: i, Cmd: append(im.Cmd, common...), Env: append(env, im.Env...)}, nil
101104
}
102105

106+
// Uroot builds a uroot cpio into the a directory.
107+
// It returns the full path of the file, or an error.
103108
func Uroot(d string) (string, error) {
104109
c := exec.Command("u-root", "-o", filepath.Join(d, "uroot.cpio"))
105110
c.Env = append(os.Environ(), "CGO_ENABLED=0")
@@ -110,6 +115,10 @@ func Uroot(d string) (string, error) {
110115

111116
}
112117

118+
// CommandContext starts qemu, given a context, directory in which to
119+
// run the command. The variadic arguments are a set of cpios which will
120+
// be merged into Image.InitRAMFS. A typical use of the extra arguments
121+
// will be to extend the initramfs; they are usually not needed.
113122
func (image *Image) CommandContext(ctx context.Context, d string, extra ...string) (*exec.Cmd, error) {
114123
image.dir = d
115124
i, k := filepath.Join(d, "initramfs"), filepath.Join(d, "kernel")
@@ -145,6 +154,9 @@ func (image *Image) CommandContext(ctx context.Context, d string, extra ...strin
145154
return c, nil
146155
}
147156

157+
// StartVm is used to starta VM (or in fact any exec.Cmd).
158+
// It includes a one second delay, experimentally needed to wait for the
159+
// guest network to be ready.
148160
func (*Image) StartVM(c *exec.Cmd) error {
149161
if err := c.Start(); err != nil {
150162
return fmt.Errorf("starting VM: %w", err)
@@ -153,6 +165,12 @@ func (*Image) StartVM(c *exec.Cmd) error {
153165
return nil
154166
}
155167

168+
// CPUCommand runs a command in a guest runnign cpud.
169+
// It is similar to exec.Command, in that it accepts an arg and
170+
// a set of optional args. It differs in that it can return an error.
171+
// If there are no errors, it returns a client.Cmd.
172+
// The returned client.Cmd can be called with CombinedOutput, to make
173+
// it easier to scan output from a command for error messages.
156174
func (i *Image) CPUCommand(arg string, args ...string) (*client.Cmd, error) {
157175
cpu := client.Command("127.0.0.1", append([]string{arg}, args...)...)
158176
cpu.Env = os.Environ()

0 commit comments

Comments
 (0)