-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfirecracker.go
More file actions
450 lines (400 loc) · 13.3 KB
/
Copy pathfirecracker.go
File metadata and controls
450 lines (400 loc) · 13.3 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package firecracker
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/kernel/hypeman/lib/forkvm"
"github.com/kernel/hypeman/lib/hypervisor"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
type apiError struct {
FaultMessage string `json:"fault_message"`
}
// Firecracker implements hypervisor.Hypervisor for the Firecracker VMM.
type Firecracker struct {
socketPath string
client *http.Client
}
func New(socketPath string) (*Firecracker, error) {
transport := &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "unix", socketPath)
},
DisableKeepAlives: true,
}
return &Firecracker{
socketPath: socketPath,
client: &http.Client{
Transport: transport,
Timeout: 90 * time.Second,
},
}, nil
}
var _ hypervisor.Hypervisor = (*Firecracker)(nil)
func (f *Firecracker) Capabilities() hypervisor.Capabilities {
return capabilities()
}
func capabilities() hypervisor.Capabilities {
return hypervisor.Capabilities{
SupportsSnapshot: true,
SupportsHotplugMemory: false,
SupportsBalloonControl: true,
SupportsPause: true,
SupportsVsock: true,
SupportsGPUPassthrough: false,
SupportsDiskIOLimit: true,
SupportsGracefulVMMShutdown: false,
SupportsSnapshotBaseReuse: true,
SupportsConcurrentForkPrepare: true,
UsesDetachableSnapshotMemoryPager: true,
}
}
func (f *Firecracker) DeleteVM(ctx context.Context) error {
return f.postAction(ctx, "SendCtrlAltDel")
}
func (f *Firecracker) Shutdown(ctx context.Context) error {
return hypervisor.ErrNotSupported
}
func (f *Firecracker) GetVMInfo(ctx context.Context) (*hypervisor.VMInfo, error) {
body, err := f.do(ctx, http.MethodGet, "/", nil, http.StatusOK)
if err != nil {
return nil, fmt.Errorf("get vm info: %w", err)
}
var info instanceInfo
if err := json.Unmarshal(body, &info); err != nil {
return nil, fmt.Errorf("decode vm info: %w", err)
}
state, err := mapVMState(info.State)
if err != nil {
return nil, err
}
return &hypervisor.VMInfo{State: state}, nil
}
func (f *Firecracker) Pause(ctx context.Context) error {
_, err := f.do(ctx, http.MethodPatch, "/vm", vmState{State: "Paused"}, http.StatusNoContent)
if err != nil {
return fmt.Errorf("pause vm: %w", err)
}
return nil
}
func (f *Firecracker) Resume(ctx context.Context) error {
_, err := f.do(ctx, http.MethodPatch, "/vm", vmState{State: "Resumed"}, http.StatusNoContent)
if err != nil {
return fmt.Errorf("resume vm: %w", err)
}
return nil
}
func (f *Firecracker) Snapshot(ctx context.Context, destPath string, opts hypervisor.SnapshotOptions) error {
if err := os.MkdirAll(destPath, 0755); err != nil {
return fmt.Errorf("create snapshot directory: %w", err)
}
if err := materializeDeferredSnapshotMemory(destPath, opts.DeferredMemoryBackingPath); err != nil {
return err
}
params := toSnapshotCreateParams(destPath)
if _, err := f.do(ctx, http.MethodPut, "/snapshot/create", params, http.StatusNoContent); err != nil {
return fmt.Errorf("create snapshot: %w", err)
}
return nil
}
func materializeDeferredSnapshotMemory(destPath, sourcePath string) error {
sourcePath = strings.TrimSpace(sourcePath)
if sourcePath == "" {
return nil
}
targetPath := filepath.Join(destPath, "memory")
if _, err := os.Stat(targetPath); err == nil {
return nil
} else if !os.IsNotExist(err) {
return fmt.Errorf("stat deferred snapshot memory target: %w", err)
}
resolvedSourcePath, err := resolveDeferredSnapshotMemorySourcePath(sourcePath)
if err != nil {
return err
}
if err := forkvm.CopyRegularFile(resolvedSourcePath, targetPath); err != nil {
return fmt.Errorf("materialize deferred snapshot memory: %w", err)
}
return nil
}
func resolveDeferredSnapshotMemorySourcePath(sourcePath string) (string, error) {
if _, err := os.Stat(sourcePath); err == nil {
return sourcePath, nil
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("stat deferred snapshot memory source: %w", err)
}
alternatePath := alternateRetainedSnapshotMemoryPath(sourcePath)
if alternatePath == "" {
return sourcePath, nil
}
if _, err := os.Stat(alternatePath); err == nil {
return alternatePath, nil
} else if !os.IsNotExist(err) {
return "", fmt.Errorf("stat alternate deferred snapshot memory source: %w", err)
}
return sourcePath, nil
}
func alternateRetainedSnapshotMemoryPath(sourcePath string) string {
if filepath.Base(sourcePath) != "memory" {
return ""
}
snapshotDir := filepath.Dir(sourcePath)
snapshotsDir := filepath.Dir(snapshotDir)
switch filepath.Base(snapshotDir) {
case "snapshot-base":
return filepath.Join(snapshotsDir, "snapshot-latest", "memory")
case "snapshot-latest":
return filepath.Join(snapshotsDir, "snapshot-base", "memory")
default:
return ""
}
}
func (f *Firecracker) ResizeMemory(ctx context.Context, bytes int64) error {
return hypervisor.ErrNotSupported
}
func (f *Firecracker) ResizeMemoryAndWait(ctx context.Context, bytes int64, timeout time.Duration) error {
return hypervisor.ErrNotSupported
}
func (f *Firecracker) SetTargetGuestMemoryBytes(ctx context.Context, bytes int64) error {
cfg, err := f.getVMConfig(ctx)
if err != nil {
return err
}
desiredBalloonMiB := cfg.MachineConfig.MemSizeMiB - guestTargetBytesToMiB(bytes)
if desiredBalloonMiB < 0 {
return fmt.Errorf("target guest memory %d exceeds configured memory %d MiB", bytes, cfg.MachineConfig.MemSizeMiB)
}
body := map[string]int64{"amount_mib": desiredBalloonMiB}
if _, err := f.do(ctx, http.MethodPatch, "/balloon", body, http.StatusNoContent); err != nil {
if strings.Contains(err.Error(), "Invalid request method and/or path") {
if _, putErr := f.do(ctx, http.MethodPut, "/balloon", body, http.StatusNoContent); putErr != nil {
if strings.Contains(putErr.Error(), "Invalid request method and/or path") {
return hypervisor.ErrNotSupported
}
return fmt.Errorf("set balloon target: %w", putErr)
}
return nil
}
return fmt.Errorf("set balloon target: %w", err)
}
return nil
}
func (f *Firecracker) GetTargetGuestMemoryBytes(ctx context.Context) (int64, error) {
cfg, err := f.getVMConfig(ctx)
if err != nil {
return 0, err
}
return (cfg.MachineConfig.MemSizeMiB - cfg.Balloon.AmountMiB) * 1024 * 1024, nil
}
func (f *Firecracker) configureForBoot(ctx context.Context, cfg hypervisor.VMConfig) error {
if cfg.SerialLogPath != "" {
if err := os.MkdirAll(filepath.Dir(cfg.SerialLogPath), 0755); err != nil {
return fmt.Errorf("create serial log directory: %w", err)
}
if _, err := f.do(ctx, http.MethodPut, "/serial", serialDevice{
SerialOutPath: cfg.SerialLogPath,
}, http.StatusNoContent); err != nil {
// The /serial endpoint was added in Firecracker v1.14.0.
// Keep this fallback for custom/older binaries that may not expose it.
if !strings.Contains(err.Error(), "Invalid request method and/or path") {
return fmt.Errorf("configure serial: %w", err)
}
}
}
if _, err := f.do(ctx, http.MethodPut, "/boot-source", toBootSource(cfg), http.StatusNoContent); err != nil {
return fmt.Errorf("configure boot source: %w", err)
}
if _, err := f.do(ctx, http.MethodPut, "/machine-config", toMachineConfiguration(cfg), http.StatusNoContent); err != nil {
return fmt.Errorf("configure machine: %w", err)
}
if balloonCfg := toBalloonConfig(cfg); balloonCfg != nil {
if _, err := f.do(ctx, http.MethodPut, "/balloon", balloonCfg, http.StatusNoContent); err != nil {
// Keep compatibility with older/custom binaries that may not expose balloon API.
if !strings.Contains(err.Error(), "Invalid request method and/or path") {
return fmt.Errorf("configure balloon: %w", err)
}
}
}
for _, driveCfg := range toDriveConfigs(cfg) {
path := "/drives/" + url.PathEscape(driveCfg.DriveID)
if _, err := f.do(ctx, http.MethodPut, path, driveCfg, http.StatusNoContent); err != nil {
return fmt.Errorf("configure drive %s: %w", driveCfg.DriveID, err)
}
}
for _, netCfg := range toNetworkInterfaces(cfg) {
path := "/network-interfaces/" + url.PathEscape(netCfg.IfaceID)
if _, err := f.do(ctx, http.MethodPut, path, netCfg, http.StatusNoContent); err != nil {
return fmt.Errorf("configure network interface %s: %w", netCfg.IfaceID, err)
}
}
vsockCfg := toVsockConfig(cfg)
if vsockCfg != nil {
if _, err := f.do(ctx, http.MethodPut, "/vsock", vsockCfg, http.StatusNoContent); err != nil {
return fmt.Errorf("configure vsock: %w", err)
}
}
return nil
}
func (f *Firecracker) instanceStart(ctx context.Context) error {
return f.postAction(ctx, "InstanceStart")
}
func (f *Firecracker) loadSnapshot(ctx context.Context, snapshotDir string, networkOverrides []networkOverride, backend snapshotMemBackend) error {
params := toSnapshotLoadParams(snapshotDir, networkOverrides, backend)
if _, err := f.do(ctx, http.MethodPut, "/snapshot/load", params, http.StatusNoContent); err != nil {
return err
}
return nil
}
func (f *Firecracker) postAction(ctx context.Context, action string) error {
_, err := f.do(ctx, http.MethodPut, "/actions", instanceActionInfo{ActionType: action}, http.StatusNoContent)
if err != nil {
return fmt.Errorf("firecracker action %s failed: %w", action, err)
}
return nil
}
type firecrackerVMConfig struct {
MachineConfig struct {
MemSizeMiB int64 `json:"mem_size_mib"`
} `json:"machine-config"`
Balloon struct {
AmountMiB int64 `json:"amount_mib"`
} `json:"balloon"`
}
func (f *Firecracker) getVMConfig(ctx context.Context) (*firecrackerVMConfig, error) {
body, err := f.do(ctx, http.MethodGet, "/vm/config", nil, http.StatusOK)
if err != nil {
if strings.Contains(err.Error(), "Invalid request method and/or path") {
return nil, hypervisor.ErrNotSupported
}
return nil, fmt.Errorf("get vm config: %w", err)
}
var cfg firecrackerVMConfig
if err := json.Unmarshal(body, &cfg); err != nil {
return nil, fmt.Errorf("decode vm config: %w", err)
}
return &cfg, nil
}
func guestTargetBytesToMiB(bytes int64) int64 {
if bytes <= 0 {
return 0
}
const mib = 1024 * 1024
out := bytes / mib
if bytes%mib != 0 {
out++
}
return out
}
func (f *Firecracker) do(ctx context.Context, method, path string, reqBody any, expectedStatus ...int) ([]byte, error) {
attrs := hypervisor.TraceAttributesFromContext(ctx)
attrs = append(attrs,
attribute.String("operation", method+" "+path),
attribute.String("http.method", method),
attribute.String("http.route", path),
)
tracer := otel.Tracer("hypeman/hypervisor/firecracker")
spanName := "hypervisor.http " + method + " " + path
shouldTrace := hypervisor.ShouldTraceHypervisorHTTPSpan(method, path)
var span trace.Span
if shouldTrace {
var spanCtx context.Context
spanCtx, span = tracer.Start(ctx, spanName, trace.WithAttributes(attrs...))
ctx = spanCtx
defer span.End()
}
recordError := func(err error) {
if shouldTrace {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
}
var bodyReader io.Reader
if reqBody != nil {
data, err := json.Marshal(reqBody)
if err != nil {
recordError(err)
return nil, fmt.Errorf("marshal request body: %w", err)
}
bodyReader = bytes.NewReader(data)
}
req, err := http.NewRequestWithContext(ctx, method, "http://localhost"+path, bodyReader)
if err != nil {
recordError(err)
return nil, fmt.Errorf("create request: %w", err)
}
req.Header.Set("Accept", "application/json")
if reqBody != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := f.client.Do(req)
if err != nil {
recordError(err)
return nil, fmt.Errorf("request %s %s: %w", method, path, err)
}
defer resp.Body.Close()
if shouldTrace {
span.SetAttributes(attribute.Int("http.status_code", resp.StatusCode))
}
data, err := io.ReadAll(resp.Body)
if err != nil {
recordError(err)
return nil, fmt.Errorf("read response body: %w", err)
}
for _, status := range expectedStatus {
if resp.StatusCode == status {
if shouldTrace {
span.SetStatus(codes.Ok, "")
}
return data, nil
}
}
if len(data) > 0 {
var apiErr apiError
if err := json.Unmarshal(data, &apiErr); err == nil && apiErr.FaultMessage != "" {
if shouldTrace {
span.SetAttributes(attribute.Int("http.status_code", resp.StatusCode))
span.SetStatus(codes.Error, apiErr.FaultMessage)
}
return nil, fmt.Errorf("status %d: %s", resp.StatusCode, apiErr.FaultMessage)
}
}
if shouldTrace {
span.SetAttributes(attribute.Int("http.status_code", resp.StatusCode))
span.SetStatus(codes.Error, resp.Status)
}
return nil, fmt.Errorf("status %d: %s", resp.StatusCode, string(data))
}
const (
// State strings returned by Firecracker GET "/".
// Source of truth:
// - src/vmm/src/vmm_config/instance_info.rs (Display impl for VmState)
// - src/firecracker/swagger/firecracker.yaml (InstanceInfo.state enum)
firecrackerStateNotStarted = "Not started"
firecrackerStateRunning = "Running"
firecrackerStatePaused = "Paused"
)
func mapVMState(state string) (hypervisor.VMState, error) {
switch state {
case firecrackerStateNotStarted:
return hypervisor.StateCreated, nil
case firecrackerStateRunning:
return hypervisor.StateRunning, nil
case firecrackerStatePaused:
return hypervisor.StatePaused, nil
default:
return "", fmt.Errorf("unknown firecracker state: %q", state)
}
}