forked from firecracker-microvm/firecracker-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
297 lines (255 loc) · 8.06 KB
/
example_test.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
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package firecracker_test
import (
"context"
"fmt"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/firecracker-microvm/firecracker-go-sdk"
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
)
func ExampleWithProcessRunner_logging() {
const socketPath = "/tmp/firecracker.sock"
cfg := firecracker.Config{
SocketPath: socketPath,
KernelImagePath: "/path/to/kernel",
Drives: firecracker.NewDrivesBuilder("/path/to/rootfs").Build(),
MachineCfg: models.MachineConfiguration{
VcpuCount: firecracker.Int64(1),
},
}
// stdout will be directed to this file
stdoutPath := "/tmp/stdout.log"
stdout, err := os.OpenFile(stdoutPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
panic(fmt.Errorf("failed to create stdout file: %v", err))
}
// stderr will be directed to this file
stderrPath := "/tmp/stderr.log"
stderr, err := os.OpenFile(stderrPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
panic(fmt.Errorf("failed to create stderr file: %v", err))
}
ctx := context.Background()
// build our custom command that contains our two files to
// write to during process execution
cmd := firecracker.VMCommandBuilder{}.
WithBin("firecracker").
WithSocketPath(socketPath).
WithStdout(stdout).
WithStderr(stderr).
Build(ctx)
m, err := firecracker.NewMachine(ctx, cfg, firecracker.WithProcessRunner(cmd))
if err != nil {
panic(fmt.Errorf("failed to create new machine: %v", err))
}
defer os.Remove(cfg.SocketPath)
if err := m.Start(ctx); err != nil {
panic(fmt.Errorf("failed to initialize machine: %v", err))
}
// wait for VMM to execute
if err := m.Wait(ctx); err != nil {
panic(err)
}
}
func ExampleDrivesBuilder() {
drivesParams := []struct {
Path string
ReadOnly bool
}{
{
Path: "/first/path/drive.img",
ReadOnly: true,
},
{
Path: "/second/path/drive.img",
ReadOnly: false,
},
}
// construct a new builder with the given rootfs path
b := firecracker.NewDrivesBuilder("/path/to/rootfs")
for _, param := range drivesParams {
// add our additional drives
b = b.AddDrive(param.Path, param.ReadOnly)
}
const socketPath = "/tmp/firecracker.sock"
cfg := firecracker.Config{
SocketPath: socketPath,
KernelImagePath: "/path/to/kernel",
// build our drives into the machine's configuration
Drives: b.Build(),
MachineCfg: models.MachineConfiguration{
VcpuCount: firecracker.Int64(1),
},
}
ctx := context.Background()
m, err := firecracker.NewMachine(ctx, cfg)
if err != nil {
panic(fmt.Errorf("failed to create new machine: %v", err))
}
if err := m.Start(ctx); err != nil {
panic(fmt.Errorf("failed to initialize machine: %v", err))
}
// wait for VMM to execute
if err := m.Wait(ctx); err != nil {
panic(err)
}
}
func ExampleDrivesBuilder_driveOpt() {
drives := firecracker.NewDrivesBuilder("/path/to/rootfs").
AddDrive("/path/to/drive1.img", true).
AddDrive("/path/to/drive2.img", false, func(drive *models.Drive) {
// set our custom bandwidth rate limiter
drive.RateLimiter = &models.RateLimiter{
Bandwidth: &models.TokenBucket{
OneTimeBurst: firecracker.Int64(1024 * 1024),
RefillTime: firecracker.Int64(500),
Size: firecracker.Int64(1024 * 1024),
},
}
}).
Build()
const socketPath = "/tmp/firecracker.sock"
cfg := firecracker.Config{
SocketPath: socketPath,
KernelImagePath: "/path/to/kernel",
// build our drives into the machine's configuration
Drives: drives,
MachineCfg: models.MachineConfiguration{
VcpuCount: firecracker.Int64(1),
},
}
ctx := context.Background()
m, err := firecracker.NewMachine(ctx, cfg)
if err != nil {
panic(fmt.Errorf("failed to create new machine: %v", err))
}
if err := m.Start(ctx); err != nil {
panic(fmt.Errorf("failed to initialize machine: %v", err))
}
// wait for VMM to execute
if err := m.Wait(ctx); err != nil {
panic(err)
}
}
func ExampleNetworkInterface_rateLimiting() {
// construct the limitations of the bandwidth for firecracker
bandwidthBuilder := firecracker.TokenBucketBuilder{}.
WithInitialSize(1024 * 1024). // Initial token amount
WithBucketSize(1024 * 1024). // Max number of tokens
WithRefillDuration(30 * time.Second) // Refill rate
// construct the limitations of the number of operations per duration for firecracker
opsBuilder := firecracker.TokenBucketBuilder{}.
WithInitialSize(5).
WithBucketSize(5).
WithRefillDuration(5 * time.Second)
// create the inbound rate limiter
inbound := firecracker.NewRateLimiter(bandwidthBuilder.Build(), opsBuilder.Build())
bandwidthBuilder = bandwidthBuilder.WithBucketSize(1024 * 1024 * 10)
opsBuilder = opsBuilder.
WithBucketSize(100).
WithInitialSize(100)
// create the outbound rate limiter
outbound := firecracker.NewRateLimiter(bandwidthBuilder.Build(), opsBuilder.Build())
networkIfaces := []firecracker.NetworkInterface{{
StaticConfiguration: &firecracker.StaticNetworkConfiguration{
MacAddress: "01-23-45-67-89-AB-CD-EF",
HostDevName: "tap-name",
},
InRateLimiter: inbound,
OutRateLimiter: outbound,
}}
cfg := firecracker.Config{
SocketPath: "/path/to/socket",
KernelImagePath: "/path/to/kernel",
Drives: firecracker.NewDrivesBuilder("/path/to/rootfs").Build(),
MachineCfg: models.MachineConfiguration{
VcpuCount: firecracker.Int64(1),
},
NetworkInterfaces: networkIfaces,
}
ctx := context.Background()
m, err := firecracker.NewMachine(ctx, cfg)
if err != nil {
panic(fmt.Errorf("failed to create new machine: %v", err))
}
defer os.Remove(cfg.SocketPath)
if err := m.Start(ctx); err != nil {
panic(fmt.Errorf("failed to initialize machine: %v", err))
}
// wait for VMM to execute
if err := m.Wait(ctx); err != nil {
panic(err)
}
}
func ExampleJailerConfig_enablingJailer() {
ctx := context.Background()
vmmCtx, vmmCancel := context.WithCancel(ctx)
defer vmmCancel()
const id = "my-jailer-test"
const path = "/path/to/jailer-workspace"
const kernelImagePath = "/path/to/kernel-image"
uid := 123
gid := 100
fcCfg := firecracker.Config{
SocketPath: "api.socket",
KernelImagePath: kernelImagePath,
KernelArgs: "console=ttyS0 reboot=k panic=1 pci=off",
Drives: firecracker.NewDrivesBuilder("/path/to/rootfs").Build(),
LogLevel: "Debug",
MachineCfg: models.MachineConfiguration{
VcpuCount: firecracker.Int64(1),
Smt: firecracker.Bool(false),
MemSizeMib: firecracker.Int64(256),
},
JailerCfg: &firecracker.JailerConfig{
UID: &uid,
GID: &gid,
ID: id,
NumaNode: firecracker.Int(0),
ChrootBaseDir: path,
ChrootStrategy: firecracker.NewNaiveChrootStrategy(kernelImagePath),
ExecFile: "/path/to/firecracker-binary",
},
}
// Check if kernel image is readable
f, err := os.Open(fcCfg.KernelImagePath)
if err != nil {
panic(fmt.Errorf("Failed to open kernel image: %v", err))
}
f.Close()
// Check each drive is readable and writable
for _, drive := range fcCfg.Drives {
drivePath := firecracker.StringValue(drive.PathOnHost)
f, err := os.OpenFile(drivePath, os.O_RDWR, 0666)
if err != nil {
panic(fmt.Errorf("Failed to open drive with read/write permissions: %v", err))
}
f.Close()
}
logger := log.New()
m, err := firecracker.NewMachine(vmmCtx, fcCfg, firecracker.WithLogger(log.NewEntry(logger)))
if err != nil {
panic(err)
}
if err := m.Start(vmmCtx); err != nil {
panic(err)
}
defer m.StopVMM()
// wait for the VMM to exit
if err := m.Wait(vmmCtx); err != nil {
panic(err)
}
}