@@ -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
2522var linux_amd64 []byte
2623
@@ -45,6 +42,9 @@ var kernel_linux_arm []byte
4542//go:embed kernel_linux_riscv64.gz
4643var 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.
4848type 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.
6366func 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.
103108func 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.
113122func (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.
148160func (* 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.
156174func (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