-
Notifications
You must be signed in to change notification settings - Fork 452
/
launch.go
476 lines (392 loc) · 12.1 KB
/
launch.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package qemu
import (
"context"
"errors"
"fmt"
"log"
"net/netip"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/containernetworking/cni/libcni"
types100 "github.com/containernetworking/cni/pkg/types/100"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/testutils"
"github.com/containernetworking/plugins/pkg/utils"
"github.com/coreos/go-iptables/iptables"
"github.com/google/uuid"
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-blockdevice/blockdevice/partition/gpt"
sideronet "github.com/siderolabs/net"
"github.com/siderolabs/talos/pkg/provision"
"github.com/siderolabs/talos/pkg/provision/internal/cniutils"
"github.com/siderolabs/talos/pkg/provision/providers/vm"
)
// LaunchConfig is passed in to the Launch function over stdin.
type LaunchConfig struct {
StatePath string
// VM options
DiskPaths []string
VCPUCount int64
MemSize int64
QemuExecutable string
KernelImagePath string
InitrdPath string
ISOPath string
PFlashImages []string
KernelArgs string
MachineType string
MonitorPath string
DefaultBootOrder string
EnableKVM bool
BootloaderEnabled bool
TPM2Config tpm2Config
NodeUUID uuid.UUID
BadRTC bool
// Talos config
Config string
// Network
BridgeName string
NetworkConfig *libcni.NetworkConfigList
CNI provision.CNIConfig
IPs []netip.Addr
CIDRs []netip.Prefix
Hostname string
GatewayAddrs []netip.Addr
MTU int
Nameservers []netip.Addr
// PXE
TFTPServer string
BootFilename string
IPXEBootFileName string
// API
APIPort int
// filled by CNI invocation
tapName string
vmMAC string
ns ns.NetNS
// signals
c chan os.Signal
// controller
controller *Controller
}
type tpm2Config struct {
NodeName string
StateDir string
}
// withCNI creates network namespace, launches CNI and passes control to the next function
// filling config with netNS and interface details.
//
//nolint:gocyclo
func withCNI(ctx context.Context, config *LaunchConfig, f func(config *LaunchConfig) error) error {
// random ID for the CNI, maps to single VM
containerID := uuid.New().String()
cniConfig := libcni.NewCNIConfigWithCacheDir(config.CNI.BinPath, config.CNI.CacheDir, nil)
// create a network namespace
ns, err := testutils.NewNS()
if err != nil {
return err
}
defer func() {
ns.Close() //nolint:errcheck
testutils.UnmountNS(ns) //nolint:errcheck
}()
ips := make([]string, len(config.IPs))
for j := range ips {
ips[j] = sideronet.FormatCIDR(config.IPs[j], config.CIDRs[j])
}
gatewayAddrs := xslices.Map(config.GatewayAddrs, netip.Addr.String)
runtimeConf := libcni.RuntimeConf{
ContainerID: containerID,
NetNS: ns.Path(),
IfName: "veth0",
Args: [][2]string{
{"IP", strings.Join(ips, ",")},
{"GATEWAY", strings.Join(gatewayAddrs, ",")},
{"IgnoreUnknown", "1"},
},
}
// attempt to clean up network in case it was deployed previously
err = cniConfig.DelNetworkList(ctx, config.NetworkConfig, &runtimeConf)
if err != nil {
return fmt.Errorf("error deleting CNI network: %w", err)
}
res, err := cniConfig.AddNetworkList(ctx, config.NetworkConfig, &runtimeConf)
if err != nil {
return fmt.Errorf("error provisioning CNI network: %w", err)
}
defer func() {
if e := cniConfig.DelNetworkList(ctx, config.NetworkConfig, &runtimeConf); e != nil {
log.Printf("error cleaning up CNI: %s", e)
}
}()
currentResult, err := types100.NewResultFromResult(res)
if err != nil {
return fmt.Errorf("failed to parse cni result: %w", err)
}
vmIface, tapIface, err := cniutils.VMTapPair(currentResult, containerID)
if err != nil {
return fmt.Errorf(
"failed to parse VM network configuration from CNI output, ensure CNI is configured with a plugin " +
"that supports automatic VM network configuration such as tc-redirect-tap",
)
}
cniChain := utils.FormatChainName(config.NetworkConfig.Name, containerID)
ipt, err := iptables.New()
if err != nil {
return fmt.Errorf("failed to initialize iptables: %w", err)
}
// don't masquerade traffic with "broadcast" destination from the VM
//
// no need to clean up the rule, as CNI drops the whole chain
if err = ipt.InsertUnique("nat", cniChain, 1, "--destination", "255.255.255.255/32", "-j", "ACCEPT"); err != nil {
return fmt.Errorf("failed to insert iptables rule to allow broadcast traffic: %w", err)
}
config.tapName = tapIface.Name
config.vmMAC = vmIface.Mac
config.ns = ns
for j := range config.CIDRs {
nameservers := make([]netip.Addr, 0, len(config.Nameservers))
// filter nameservers by IPv4/IPv6 matching IPs
for i := range config.Nameservers {
if config.IPs[j].Is6() {
if config.Nameservers[i].Is6() {
nameservers = append(nameservers, config.Nameservers[i])
}
} else {
if config.Nameservers[i].Is4() {
nameservers = append(nameservers, config.Nameservers[i])
}
}
}
// dump node IP/mac/hostname for dhcp
if err = vm.DumpIPAMRecord(config.StatePath, vm.IPAMRecord{
IP: config.IPs[j],
Netmask: byte(config.CIDRs[j].Bits()),
MAC: vmIface.Mac,
Hostname: config.Hostname,
Gateway: config.GatewayAddrs[j],
MTU: config.MTU,
Nameservers: nameservers,
TFTPServer: config.TFTPServer,
IPXEBootFilename: config.IPXEBootFileName,
}); err != nil {
return err
}
}
return f(config)
}
func checkPartitions(config *LaunchConfig) (bool, error) {
disk, err := os.Open(config.DiskPaths[0])
if err != nil {
return false, fmt.Errorf("failed to open disk file %w", err)
}
defer disk.Close() //nolint:errcheck
diskTable, err := gpt.Open(disk)
if err != nil {
if errors.Is(err, gpt.ErrPartitionTableDoesNotExist) {
return false, nil
}
return false, fmt.Errorf("error creating GPT object: %w", err)
}
if err = diskTable.Read(); err != nil {
return false, err
}
return len(diskTable.Partitions().Items()) > 0, nil
}
// launchVM runs qemu with args built based on config.
//
//nolint:gocyclo,cyclop
func launchVM(config *LaunchConfig) error {
bootOrder := config.DefaultBootOrder
if config.controller.ForcePXEBoot() {
bootOrder = "nc"
}
cpuArg := "max"
if config.BadRTC {
cpuArg += ",-kvmclock"
}
args := []string{
"-m", strconv.FormatInt(config.MemSize, 10),
"-smp", fmt.Sprintf("cpus=%d", config.VCPUCount),
"-cpu", cpuArg,
"-nographic",
"-netdev", fmt.Sprintf("tap,id=net0,ifname=%s,script=no,downscript=no", config.tapName),
"-device", fmt.Sprintf("virtio-net-pci,netdev=net0,mac=%s", config.vmMAC),
// TODO: uncomment the following line to get another eth interface not connected to anything
// "-nic", "tap,model=virtio-net-pci",
"-device", "virtio-rng-pci",
"-device", "virtio-balloon,deflate-on-oom=on",
"-monitor", fmt.Sprintf("unix:%s,server,nowait", config.MonitorPath),
"-no-reboot",
"-boot", fmt.Sprintf("order=%s,reboot-timeout=5000", bootOrder),
"-smbios", fmt.Sprintf("type=1,uuid=%s", config.NodeUUID),
"-chardev",
fmt.Sprintf("socket,path=%s/%s.sock,server=on,wait=off,id=qga0", config.StatePath, config.Hostname),
"-device",
"virtio-serial",
"-device",
"virtserialport,chardev=qga0,name=org.qemu.guest_agent.0",
}
for i, disk := range config.DiskPaths {
driver := "virtio"
if i > 0 {
driver = "ide"
}
args = append(args, "-drive", fmt.Sprintf("format=raw,if=%s,file=%s,cache=unsafe", driver, disk))
}
machineArg := config.MachineType
if config.EnableKVM {
machineArg += ",accel=kvm,smm=on"
}
args = append(args, "-machine", machineArg)
pflashArgs := make([]string, 2*len(config.PFlashImages))
for i := range config.PFlashImages {
pflashArgs[2*i] = "-drive"
pflashArgs[2*i+1] = fmt.Sprintf("file=%s,format=raw,if=pflash", config.PFlashImages[i])
}
args = append(args, pflashArgs...)
// check if disk is empty/wiped
diskBootable, err := checkPartitions(config)
if err != nil {
return err
}
if config.TPM2Config.NodeName != "" {
tpm2SocketPath := filepath.Join(config.TPM2Config.StateDir, "swtpm.sock")
cmd := exec.Command("swtpm", []string{
"socket",
"--tpmstate",
fmt.Sprintf("dir=%s,mode=0644", config.TPM2Config.StateDir),
"--ctrl",
fmt.Sprintf("type=unixio,path=%s", tpm2SocketPath),
"--tpm2",
"--pid",
fmt.Sprintf("file=%s", filepath.Join(config.TPM2Config.StateDir, "swtpm.pid")),
"--log",
fmt.Sprintf("file=%s,level=20", filepath.Join(config.TPM2Config.StateDir, "swtpm.log")),
}...)
log.Printf("starting swtpm: %s", cmd.String())
if err := cmd.Start(); err != nil {
return err
}
args = append(args,
"-chardev",
fmt.Sprintf("socket,id=chrtpm,path=%s", tpm2SocketPath),
"-tpmdev",
"emulator,id=tpm0,chardev=chrtpm",
"-device",
"tpm-tis,tpmdev=tpm0",
)
}
if !diskBootable || !config.BootloaderEnabled {
if config.ISOPath != "" {
args = append(args,
"-cdrom", config.ISOPath,
)
} else if config.KernelImagePath != "" {
args = append(args,
"-kernel", config.KernelImagePath,
"-initrd", config.InitrdPath,
"-append", config.KernelArgs,
)
}
}
if config.BadRTC {
args = append(args,
"-rtc",
"base=2011-11-11T11:11:00,clock=rt",
)
}
fmt.Fprintf(os.Stderr, "starting %s with args:\n%s\n", config.QemuExecutable, strings.Join(args, " "))
cmd := exec.Command(
config.QemuExecutable,
args...,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := ns.WithNetNSPath(config.ns.Path(), func(_ ns.NetNS) error {
return cmd.Start()
}); err != nil {
return err
}
done := make(chan error)
go func() {
done <- cmd.Wait()
}()
for {
select {
case sig := <-config.c:
fmt.Fprintf(os.Stderr, "exiting VM as signal %s was received\n", sig)
if err := cmd.Process.Kill(); err != nil {
return fmt.Errorf("failed to kill process %w", err)
}
<-done
return fmt.Errorf("process stopped")
case err := <-done:
if err != nil {
return fmt.Errorf("process exited with error %s", err)
}
// graceful exit
return nil
case command := <-config.controller.CommandsCh():
if command == VMCommandStop {
fmt.Fprintf(os.Stderr, "exiting VM as stop command via API was received\n")
if err := cmd.Process.Kill(); err != nil {
return fmt.Errorf("failed to kill process %w", err)
}
<-done
return nil
}
}
}
}
// Launch a control process around qemu VM manager.
//
// This function is invoked from 'talosctl qemu-launch' hidden command
// and wraps starting, controlling 'qemu' VM process.
//
// Launch restarts VM forever until control process is stopped itself with a signal.
//
// Process is expected to receive configuration on stdin. Current working directory
// should be cluster state directory, process output should be redirected to the
// logfile in state directory.
//
// When signals SIGINT, SIGTERM are received, control process stops qemu and exits.
func Launch() error {
var config LaunchConfig
ctx := context.Background()
if err := vm.ReadConfig(&config); err != nil {
return err
}
config.c = vm.ConfigureSignals()
config.controller = NewController()
httpServer, err := vm.NewHTTPServer(config.GatewayAddrs[0], config.APIPort, []byte(config.Config), config.controller)
if err != nil {
return err
}
httpServer.Serve()
defer httpServer.Shutdown(ctx) //nolint:errcheck
// patch kernel args
config.KernelArgs = strings.ReplaceAll(config.KernelArgs, "{TALOS_CONFIG_URL}", fmt.Sprintf("http://%s/config.yaml", httpServer.GetAddr()))
return withCNI(ctx, &config, func(config *LaunchConfig) error {
for {
for config.controller.PowerState() != PoweredOn {
select {
case <-config.controller.CommandsCh():
// machine might have been powered on
case sig := <-config.c:
fmt.Fprintf(os.Stderr, "exiting stopped launcher as signal %s was received\n", sig)
return fmt.Errorf("process stopped")
}
}
if err := launchVM(config); err != nil {
return err
}
}
})
}