MCPcopy Index your code
hub / github.com/codebench-dev/worker / createAndStartVM

Function createAndStartVM

vmm.go:25–100  ·  view source on GitHub ↗

Create a VMM with a given set of options and start the VM

(ctx context.Context)

Source from the content-addressed store, hash-verified

23
24// Create a VMM with a given set of options and start the VM
25func createAndStartVM(ctx context.Context) (*runningFirecracker, error) {
26 vmmID := xid.New().String()
27
28 copy("../agent/rootfs.ext4", "/tmp/rootfs-"+vmmID+".ext4")
29
30 fcCfg, err := getFirecrackerConfig(vmmID)
31 if err != nil {
32 log.Errorf("Error: %s", err)
33 return nil, err
34 }
35 logger := log.New()
36
37 if false { // TODO
38 log.SetLevel(log.DebugLevel)
39 logger.SetLevel(log.DebugLevel)
40 }
41
42 machineOpts := []firecracker.Opt{
43 firecracker.WithLogger(log.NewEntry(logger)),
44 }
45
46 firecrackerBinary, err := exec.LookPath("firecracker")
47 if err != nil {
48 return nil, err
49 }
50
51 finfo, err := os.Stat(firecrackerBinary)
52 if os.IsNotExist(err) {
53 return nil, fmt.Errorf("binary %q does not exist: %v", firecrackerBinary, err)
54 }
55 if err != nil {
56 return nil, fmt.Errorf("failed to stat binary, %q: %v", firecrackerBinary, err)
57 }
58
59 if finfo.IsDir() {
60 return nil, fmt.Errorf("binary, %q, is a directory", firecrackerBinary)
61 } else if finfo.Mode()&0111 == 0 {
62 return nil, fmt.Errorf("binary, %q, is not executable. Check permissions of binary", firecrackerBinary)
63 }
64
65 // if the jailer is used, the final command will be built in NewMachine()
66 if fcCfg.JailerCfg == nil {
67 cmd := firecracker.VMCommandBuilder{}.
68 WithBin(firecrackerBinary).
69 WithSocketPath(fcCfg.SocketPath).
70 // WithStdin(os.Stdin).
71 // WithStdout(os.Stdout).
72 WithStderr(os.Stderr).
73 Build(ctx)
74
75 machineOpts = append(machineOpts, firecracker.WithProcessRunner(cmd))
76 }
77
78 vmmCtx, vmmCancel := context.WithCancel(ctx)
79
80 m, err := firecracker.NewMachine(vmmCtx, fcCfg, machineOpts...)
81 if err != nil {
82 vmmCancel()

Callers 1

fillVMPoolFunction · 0.85

Calls 2

copyFunction · 0.85
getFirecrackerConfigFunction · 0.85

Tested by

no test coverage detected