forked from cneira/firecracker-task-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirevm.go
213 lines (176 loc) · 5.61 KB
/
firevm.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
/* Firecracker-task-driver is a task driver for Hashicorp's nomad that allows
* to create microvms using AWS Firecracker vmm
* Copyright (C) 2019 Carlos Neira [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package firevm
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"github.com/containerd/console"
firecracker "github.com/radixbio/firecracker-go-sdk"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/plugins/drivers"
log "github.com/sirupsen/logrus"
)
const (
// executableMask is the mask needed to check whether or not a file's
// permissions are executable.
executableMask = 0111
// containerMonitorIntv is the interval at which the driver checks if the
// firecracker micro-vm is still running
containerMonitorIntv = 2 * time.Second
defaultbootoptions = " console=ttyS0 reboot=k panic=1 pci=off nomodules"
)
func taskConfig2FirecrackerOpts(taskConfig TaskConfig, cfg *drivers.TaskConfig) (*options, error) {
opts := newOptions()
if len(taskConfig.KernelImage) > 0 {
opts.FcKernelImage = taskConfig.KernelImage
} else {
opts.FcKernelImage = filepath.Join(cfg.AllocDir, cfg.Name) + "/vmlinux"
}
if len(taskConfig.BootDisk) > 0 {
opts.FcRootDrivePath = taskConfig.BootDisk
} else {
opts.FcRootDrivePath = filepath.Join(cfg.AllocDir, cfg.Name) + "/rootfs.ext4"
}
if len(taskConfig.Disks) > 0 {
opts.FcAdditionalDrives = taskConfig.Disks
}
if len(taskConfig.BootOptions) > 0 {
opts.FcKernelCmdLine = taskConfig.BootOptions + defaultbootoptions
} else {
opts.FcKernelCmdLine = defaultbootoptions
}
if len(taskConfig.Nic.Ip) > 0 {
opts.FcNicConfig = taskConfig.Nic
}
if len(taskConfig.Network) > 0 {
opts.FcNetworkName = taskConfig.Network
}
if len(taskConfig.Log) > 0 {
opts.FcFifoLogFile = taskConfig.Log
opts.FcLogLevel = "Debug"
}
if cfg.Resources.NomadResources.Cpu.CpuShares > 100 {
opts.FcCPUCount = cfg.Resources.NomadResources.Cpu.CpuShares / 100
} else {
opts.FcCPUCount = 1
}
opts.FcCPUTemplate = taskConfig.Cputype
opts.FcDisableHt = taskConfig.DisableHt
if cfg.Resources.NomadResources.Memory.MemoryMB > 0 {
opts.FcMemSz = cfg.Resources.NomadResources.Memory.MemoryMB
} else {
opts.FcMemSz = 300
}
opts.FcBinary = taskConfig.Firecracker
return opts, nil
}
type vminfo struct {
Machine *firecracker.Machine
tty string
Info Instance_info
}
type Instance_info struct {
AllocId string
Ip string
Serial string
Pid string
Vnic string
}
func (d *Driver) initializeContainer(ctx context.Context, cfg *drivers.TaskConfig, taskConfig TaskConfig) (*vminfo, error) {
opts, _ := taskConfig2FirecrackerOpts(taskConfig, cfg)
fcCfg, err := opts.getFirecrackerConfig(cfg.AllocID)
if err != nil {
log.Errorf("Error: %s", err)
return nil, err
}
d.logger.Info("Starting firecracker", "driver_initialize_container", hclog.Fmt("%v+", opts))
logger := log.New()
vmmCtx, vmmCancel := context.WithCancel(ctx)
defer vmmCancel()
machineOpts := []firecracker.Opt{
firecracker.WithLogger(log.NewEntry(logger)),
}
fcenv := os.Getenv("FIRECRACKER_BIN")
var firecrackerBinary string
if len(opts.FcBinary) > 0 {
firecrackerBinary = opts.FcBinary
} else if len(fcenv) > 0 {
firecrackerBinary = fcenv
} else {
firecrackerBinary = "/usr/bin/firecracker"
}
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()&executableMask == 0 {
return nil, fmt.Errorf("Binary, %q, is not executable. Check permissions of binary", firecrackerBinary)
}
tty, ftty, err := console.NewPty()
if err != nil {
return nil, fmt.Errorf("Could not create serial console %v+", err)
}
cmd := firecracker.VMCommandBuilder{}.
WithBin(firecrackerBinary).
WithSocketPath(fcCfg.SocketPath).
WithStdin(tty).
WithStdout(tty).
WithStderr(nil).
Build(ctx)
machineOpts = append(machineOpts, firecracker.WithProcessRunner(cmd))
m, err := firecracker.NewMachine(vmmCtx, fcCfg, machineOpts...)
if err != nil {
return nil, fmt.Errorf("Failed creating machine: %v", err)
}
if err := m.Start(vmmCtx); err != nil {
return nil, fmt.Errorf("Failed to start machine: %v", err)
}
if opts.validMetadata != nil {
m.SetMetadata(vmmCtx, opts.validMetadata)
}
pid, errpid := m.PID()
if errpid != nil {
return nil, fmt.Errorf("Failed getting pid for machine: %v", errpid)
}
var ip string
var vnic string
if len(opts.FcNetworkName) > 0 {
ip = fcCfg.NetworkInterfaces[0].StaticConfiguration.IPConfiguration.IPAddr.String()
vnic = fcCfg.NetworkInterfaces[0].CNIConfiguration.IfName + "vm"
} else {
ip = "No network chosen"
vnic = ip
}
info := Instance_info{Serial: ftty, AllocId: cfg.AllocID,
Ip: ip,
Pid: strconv.Itoa(pid), Vnic: vnic}
f, _ := json.MarshalIndent(info, "", " ")
logfile := fmt.Sprintf("/tmp/%s-%s", cfg.Name, cfg.AllocID)
d.logger.Info("Writing to", "driver_initialize_container", hclog.Fmt("%v+", logfile))
log, err := os.OpenFile(logfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return nil, fmt.Errorf("Failed creating info file=%s err=%v", logfile, err)
}
defer log.Close()
fmt.Fprintf(log, "%s", f)
return &vminfo{Machine: m, tty: ftty, Info: info}, nil
}