Skip to content

Instantly share code, notes, and snippets.

@hharnisc
Created January 2, 2019 16:55
Show Gist options
  • Save hharnisc/facfe973a6d42291fa54658e4a981c66 to your computer and use it in GitHub Desktop.
Save hharnisc/facfe973a6d42291fa54658e4a981c66 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"os"
"github.com/firecracker-microvm/firecracker-go-sdk"
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
log "github.com/sirupsen/logrus"
)
func main() {
logger := log.New()
ctx := context.Background()
vmmCtx, vmmCancel := context.WithCancel(ctx)
defer vmmCancel()
devices := []models.Drive{}
rootDrivePath := "../hello-rootfs.ext4"
rootDrive := models.Drive{
DriveID: firecracker.String("1"),
PathOnHost: &rootDrivePath,
IsRootDevice: firecracker.Bool(true),
IsReadOnly: firecracker.Bool(false),
}
devices = append(devices, rootDrive)
kernelImagePath := "../hello-vmlinux.bin"
fcCfg := firecracker.Config{
SocketPath: "./firecracker.sock",
KernelImagePath: kernelImagePath,
KernelArgs: "console=ttyS0 reboot=k panic=1 pci=off",
Drives: devices,
MachineCfg: models.MachineConfiguration{
VcpuCount: 1,
CPUTemplate: models.CPUTemplate("C3"),
HtEnabled: true,
MemSizeMib: 512,
},
}
machineOpts := []firecracker.Opt{
firecracker.WithLogger(log.NewEntry(logger)),
}
cmd := firecracker.VMCommandBuilder{}.
WithBin("../firecracker").
WithSocketPath(fcCfg.SocketPath).
WithStdin(os.Stdin).
WithStdout(os.Stdout).
WithStderr(os.Stderr).
Build(ctx)
machineOpts = append(machineOpts, firecracker.WithProcessRunner(cmd))
m, err := firecracker.NewMachine(vmmCtx, fcCfg, machineOpts...)
if err != nil {
log.Fatalf("Failed creating machine: %s", err)
}
if err := m.Start(vmmCtx); err != nil {
log.Fatalf("Failed to start machine: %v", err)
}
defer m.StopVMM()
// wait for the VMM to exit
if err := m.Wait(vmmCtx); err != nil {
log.Fatalf("Wait returned an error %s", err)
}
log.Printf("Start machine was happy")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment