forked from synadia-io/nex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunning_vm.go
242 lines (209 loc) · 6.75 KB
/
running_vm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package nexnode
import (
"context"
"errors"
"fmt"
"io/fs"
"log/slog"
"net"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/firecracker-microvm/firecracker-go-sdk"
"github.com/firecracker-microvm/firecracker-go-sdk/client/models"
"github.com/rs/xid"
agentapi "github.com/synadia-io/nex/internal/agent-api"
controlapi "github.com/synadia-io/nex/internal/control-api"
)
// Represents an instance of a single firecracker VM containing the nex agent.
type runningFirecracker struct {
vmmCtx context.Context
vmmCancel context.CancelFunc
vmmID string
machine *firecracker.Machine
ip net.IP
workloadStarted time.Time
machineStarted time.Time
workloadSpecification controlapi.RunRequest
namespace string
deployedWorkload agentapi.DeployRequest
}
func (vm *runningFirecracker) shutDown(log *slog.Logger) {
log.Info("Machine stopping",
slog.String("vmid", vm.vmmID),
slog.String("ip", string(vm.ip)),
)
err := vm.machine.StopVMM()
if err != nil {
log.Error("Failed to stop firecracker VM", slog.Any("err", err))
}
err = os.Remove(vm.machine.Cfg.SocketPath)
if err != nil {
if !errors.Is(err, fs.ErrExist) {
log.Warn("Failed to delete firecracker socket", slog.Any("err", err))
}
}
// NOTE: we're not deleting the firecracker machine logs ... they're in a tempfs so they'll eventually
// go away but we might want them kept around for troubleshooting
rootFs := getRootFsPath(vm.vmmID)
err = os.Remove(rootFs)
if err != nil {
if !errors.Is(err, fs.ErrExist) {
log.Warn("Failed to delete firecracker rootfs", slog.Any("err", err))
}
}
}
// Create a VMM with a given set of options and start the VM
func createAndStartVM(ctx context.Context, config *NodeConfiguration, log *slog.Logger) (*runningFirecracker, error) {
vmmID := xid.New().String()
fcCfg, err := generateFirecrackerConfig(vmmID, config)
if err != nil {
log.Error("Failed to generate firecracker configuration", slog.Any("config", config))
return nil, err
}
err = copy(config.RootFsFile, *fcCfg.Drives[0].PathOnHost)
if err != nil {
log.Error("Failed to copy rootfs to temp location", slog.Any("err", err))
return nil, err
}
// TODO: can we please not use logrus here amazon?
machineOpts := []firecracker.Opt{
firecracker.WithLogger(log.With(slog.Bool("firecracker", true), slog.String("vmmid", vmmID))),
}
firecrackerBinary, err := exec.LookPath("firecracker")
if err != nil {
return nil, err
}
finfo, err := os.Stat(firecrackerBinary)
if os.IsNotExist(err) {
return nil, fmt.Errorf("binary %q does not exist: %v", firecrackerBinary, err)
}
if err != nil {
return nil, fmt.Errorf("failed to stat binary, %q: %v", firecrackerBinary, err)
}
if finfo.IsDir() {
return nil, fmt.Errorf("binary, %q, is a directory", firecrackerBinary)
} else if finfo.Mode()&0111 == 0 {
return nil, fmt.Errorf("binary, %q, is not executable. Check permissions of binary", firecrackerBinary)
}
if fcCfg.JailerCfg == nil {
cmd := firecracker.VMCommandBuilder{}.
WithBin(firecrackerBinary).
WithSocketPath(fcCfg.SocketPath).
WithStderr(os.Stderr).
Build(ctx)
machineOpts = append(machineOpts, firecracker.WithProcessRunner(cmd))
}
vmmCtx, vmmCancel := context.WithCancel(ctx)
m, err := firecracker.NewMachine(vmmCtx, fcCfg, machineOpts...)
if err != nil {
vmmCancel()
return nil, fmt.Errorf("failed creating machine: %s", err)
}
md := agentapi.MachineMetadata{
VmId: &vmmID,
NodeNatsHost: config.InternalNodeHost,
NodeNatsPort: config.InternalNodePort,
Message: agentapi.StringOrNil("Host-supplied metadata"),
}
if err := m.Start(vmmCtx); err != nil {
vmmCancel()
return nil, fmt.Errorf("failed to start machine: %v", err)
}
err = m.SetMetadata(vmmCtx, md)
if err != nil {
vmmCancel()
return nil, fmt.Errorf("failed to set machine metadata: %s", err)
}
gw := m.Cfg.NetworkInterfaces[0].StaticConfiguration.IPConfiguration.Gateway
ip := m.Cfg.NetworkInterfaces[0].StaticConfiguration.IPConfiguration.IPAddr.IP
hosttap := m.Cfg.NetworkInterfaces[0].StaticConfiguration.HostDevName
mask := m.Cfg.NetworkInterfaces[0].StaticConfiguration.IPConfiguration.IPAddr.Mask
log.Info("Machine started",
slog.String("vmid", vmmID),
slog.Any("ip", ip),
slog.Any("gateway", gw),
slog.String("netmask", mask.String()),
slog.String("hosttap", hosttap),
slog.String("nats_host", *md.NodeNatsHost),
slog.Int("nats_port", *md.NodeNatsPort),
)
return &runningFirecracker{
vmmCtx: vmmCtx,
vmmCancel: vmmCancel,
vmmID: vmmID,
machine: m,
ip: ip,
machineStarted: time.Now().UTC(),
}, nil
}
func copy(src string, dst string) error {
data, err := os.ReadFile(src)
if err != nil {
return err
}
err = os.WriteFile(dst, data, 0644)
return err
}
func generateFirecrackerConfig(id string, config *NodeConfiguration) (firecracker.Config, error) {
socket := getSocketPath(id)
rootPath := getRootFsPath(id)
return firecracker.Config{
SocketPath: socket,
KernelImagePath: config.KernelFile,
LogPath: fmt.Sprintf("%s.log", socket),
Drives: []models.Drive{{
DriveID: firecracker.String("1"),
PathOnHost: &rootPath,
IsRootDevice: firecracker.Bool(true),
IsReadOnly: firecracker.Bool(false),
// RateLimiter: firecracker.NewRateLimiter(
// // bytes/s
// models.TokenBucket{
// OneTimeBurst: firecracker.Int64(1024 * 1024), // 1 MiB/s
// RefillTime: firecracker.Int64(500), // 0.5s
// Size: firecracker.Int64(1024 * 1024),
// },
// // ops/s
// models.TokenBucket{
// OneTimeBurst: firecracker.Int64(100), // 100 iops
// RefillTime: firecracker.Int64(1000), // 1s
// Size: firecracker.Int64(100),
// }),
}},
NetworkInterfaces: []firecracker.NetworkInterface{{
AllowMMDS: true,
// Use CNI to get dynamic IP
CNIConfiguration: &firecracker.CNIConfiguration{
IfName: *config.CNI.InterfaceName,
NetworkName: *config.CNI.NetworkName,
},
//OutRateLimiter: firecracker.NewRateLimiter(..., ...),
//InRateLimiter: firecracker.NewRateLimiter(..., ...),
}},
MachineCfg: models.MachineConfiguration{
VcpuCount: firecracker.Int64(int64(*config.MachineTemplate.VcpuCount)),
MemSizeMib: firecracker.Int64(int64(*config.MachineTemplate.MemSizeMib)),
},
MmdsVersion: firecracker.MMDSv2,
}, nil
}
func getRootFsPath(vmmID string) string {
filename := fmt.Sprintf("rootfs-%s.ext4", vmmID)
dir := os.TempDir()
return filepath.Join(dir, filename)
}
func getSocketPath(vmmID string) string {
filename := strings.Join([]string{
".firecracker.sock",
strconv.Itoa(os.Getpid()),
vmmID,
},
"-",
)
dir := os.TempDir()
return filepath.Join(dir, filename)
}