feature: run firecracker from go with tap interface

This commit is contained in:
RouxAntoine 2024-01-03 22:54:43 +01:00
parent 1f637ccc77
commit e128f29785
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
3 changed files with 1177 additions and 11 deletions

View File

@ -7,14 +7,20 @@ package main
import (
"antoine-roux.tk/projects/go/firecracker-netns/internal/netlink"
"antoine-roux.tk/projects/go/firecracker-netns/internal/netns"
"context"
"fmt"
"github.com/firecracker-microvm/firecracker-go-sdk"
"github.com/firecracker-microvm/firecracker-go-sdk/client/models"
"github.com/sirupsen/logrus"
"net"
"os"
"os/exec"
"runtime"
)
func setupEnv() int {
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
newNs, err := netns.New()
if err != nil {
fmt.Println("new ns error", err)
@ -74,19 +80,73 @@ func setupEnv() int {
// Do something with the network namespace
interfaces, _ := net.Interfaces()
fmt.Printf("Interfaces: %v\n", interfaces)
log.Debugf("Interfaces: %v\n", interfaces)
cmd := exec.Command("/bin/sh")
ctx := context.Background()
cancel, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cpuCount := int64(4)
memorySize := int64(1024)
isSmt := true
cmd.Env = []string{"PS1=-[ns-process]- # "}
cfg := firecracker.Config{
SocketPath: "/tmp/firecracker.socket",
KernelImagePath: "./vmlinux-5.10.204",
LogPath: "./firecracker.log",
LogLevel: "Debug",
KernelArgs: "console=ttyS0 reboot=k panic=1 pci=off",
Drives: []models.Drive{
{
DriveID: firecracker.String("rootfs"),
PathOnHost: firecracker.String("./ubuntu-22.04.ext4"),
IsReadOnly: firecracker.Bool(false),
IsRootDevice: firecracker.Bool(true),
},
},
NetworkInterfaces: firecracker.NetworkInterfaces{
firecracker.NetworkInterface{
StaticConfiguration: &firecracker.StaticNetworkConfiguration{
MacAddress: "06:00:AC:10:00:02",
HostDevName: tap.Link.Attrs().Name,
},
},
},
MachineCfg: models.MachineConfiguration{
VcpuCount: &cpuCount,
MemSizeMib: &memorySize,
Smt: &isSmt,
TrackDirtyPages: true,
},
}
if err := cmd.Run(); err != nil {
fmt.Printf("Error running the /bin/sh command - %s\n", err)
os.Exit(1)
firecrackerOpts := []firecracker.Opt{
firecracker.WithProcessRunner(
firecracker.VMCommandBuilder{}.
WithBin("firecracker").
WithSocketPath("/tmp/firecracker.socket").
Build(ctx),
),
firecracker.WithLogger(logrus.NewEntry(log)),
}
vm, err := firecracker.NewMachine(cancel, cfg, firecrackerOpts...)
if err != nil {
log.Errorln("create vm error", err)
return 1
}
defer os.Remove(cfg.SocketPath)
defer vm.StopVMM()
if err := vm.Start(ctx); err != nil {
log.Errorln("start vm error", err)
return 1
}
if err := vm.Wait(ctx); err != nil {
log.Errorln("wait vm error", err)
return 1
}
return 0

36
go.mod
View File

@ -10,4 +10,38 @@ require (
golang.org/x/sys v0.15.0
)
require github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/containerd/fifo v1.0.0 // indirect
github.com/containernetworking/cni v1.0.1 // indirect
github.com/containernetworking/plugins v1.0.1 // indirect
github.com/firecracker-microvm/firecracker-go-sdk v1.0.0 // indirect
github.com/go-openapi/analysis v0.21.2 // indirect
github.com/go-openapi/errors v0.20.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/loads v0.21.1 // indirect
github.com/go-openapi/runtime v0.24.0 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/strfmt v0.21.2 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/go-openapi/validate v0.22.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
go.mongodb.org/mongo-driver v1.8.3 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

1072
go.sum

File diff suppressed because it is too large Load Diff