-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
kvm.go
561 lines (476 loc) · 15 KB
/
kvm.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//go:build linux
/*
Copyright 2016 The Kubernetes Authors 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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 kvm
import (
"fmt"
"os"
"path/filepath"
"syscall"
"time"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"
pkgdrivers "k8s.io/minikube/pkg/drivers"
"k8s.io/minikube/pkg/util/retry"
"libvirt.org/go/libvirt"
)
// Driver is the machine driver for KVM
type Driver struct {
*drivers.BaseDriver
*pkgdrivers.CommonDriver
// How much memory, in MB, to allocate to the VM
Memory int
// How many cpus to allocate to the VM
CPU int
// The name of the default network
Network string
// The name of the private network
PrivateNetwork string
// The size of the disk to be created for the VM, in MB
DiskSize int
// The path of the disk .img
DiskPath string
// A file or network URI to fetch the minikube ISO
Boot2DockerURL string
// The location of the iso to boot from
ISO string
// The randomly generated MAC Address
// If empty, a random MAC will be generated.
MAC string
// The randomly generated MAC Address for the NIC attached to the private network
// If empty, a random MAC will be generated.
PrivateMAC string
// Whether to passthrough GPU devices from the host to the VM.
GPU bool
// Whether to hide the KVM hypervisor signature from the guest
Hidden bool
// XML that needs to be added to passthrough GPU devices.
DevicesXML string
// QEMU Connection URI
ConnectionURI string
// NUMA node count default value is 1
NUMANodeCount int
// NUMA XML
NUMANodeXML string
// Extra Disks
ExtraDisks int
// Extra Disks XML
ExtraDisksXML []string
}
const (
qemusystem = "qemu:///system"
defaultPrivateNetworkName = "minikube-net"
defaultNetworkName = "default"
)
// NewDriver creates a new driver for a host
func NewDriver(hostName, storePath string) *Driver {
return &Driver{
BaseDriver: &drivers.BaseDriver{
MachineName: hostName,
StorePath: storePath,
SSHUser: "docker",
},
CommonDriver: &pkgdrivers.CommonDriver{},
PrivateNetwork: defaultPrivateNetworkName,
Network: defaultNetworkName,
ConnectionURI: qemusystem,
}
}
// PreCommandCheck checks the connection before issuing a command
func (d *Driver) PreCommandCheck() error {
conn, err := getConnection(d.ConnectionURI)
if err != nil {
return errors.Wrap(err, "error connecting to libvirt socket. Have you added yourself to the libvirtd group?")
}
defer conn.Close()
libVersion, err := conn.GetLibVersion()
if err != nil {
return errors.Wrap(err, "getting libvirt version")
}
log.Debugf("Using libvirt version %d", libVersion)
return nil
}
// GetURL returns a Docker URL inside this host
// e.g. tcp://1.2.3.4:2376
// more info https://github.com/docker/machine/blob/b170508bf44c3405e079e26d5fdffe35a64c6972/libmachine/provision/utils.go#L159_L175
func (d *Driver) GetURL() (string, error) {
if err := d.PreCommandCheck(); err != nil {
return "", errors.Wrap(err, "getting URL, precheck failed")
}
ip, err := d.GetIP()
if err != nil {
return "", errors.Wrap(err, "getting URL, could not get IP")
}
if ip == "" {
return "", nil
}
return fmt.Sprintf("tcp://%s:2376", ip), nil
}
// GetState returns the state that the host is in (running, stopped, etc)
func (d *Driver) GetState() (st state.State, err error) {
dom, conn, err := d.getDomain()
if err != nil {
return state.None, errors.Wrap(err, "getting connection")
}
defer func() {
if ferr := closeDomain(dom, conn); ferr != nil {
err = ferr
}
}()
lvs, _, err := dom.GetState() // state, reason, error
if err != nil {
return state.None, errors.Wrap(err, "getting domain state")
}
st = machineState(lvs)
return // st, err
}
// machineState converts libvirt state to libmachine state
func machineState(lvs libvirt.DomainState) state.State {
// Possible States (ref: https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainState):
// - VIR_DOMAIN_NOSTATE no state
// - VIR_DOMAIN_RUNNING the domain is running
// - VIR_DOMAIN_BLOCKED the domain is blocked on resource
// - VIR_DOMAIN_PAUSED the domain is paused by user
// - VIR_DOMAIN_SHUTDOWN the domain is being shut down
// - VIR_DOMAIN_SHUTOFF the domain is shut off
// - VIR_DOMAIN_CRASHED the domain is crashed
// - VIR_DOMAIN_PMSUSPENDED the domain is suspended by guest power management
// - VIR_DOMAIN_LAST this enum value will increase over time as new events are added to the libvirt API. It reflects the last state supported by this version of the libvirt API.
switch lvs {
case libvirt.DOMAIN_RUNNING:
return state.Running
case libvirt.DOMAIN_BLOCKED, libvirt.DOMAIN_CRASHED:
return state.Error
case libvirt.DOMAIN_PAUSED:
return state.Paused
case libvirt.DOMAIN_SHUTDOWN:
return state.Stopping
case libvirt.DOMAIN_SHUTOFF:
return state.Stopped
case libvirt.DOMAIN_PMSUSPENDED:
return state.Saved
case libvirt.DOMAIN_NOSTATE:
return state.None
default:
return state.None
}
}
// GetIP returns an IP or hostname that this host is available at
func (d *Driver) GetIP() (string, error) {
s, err := d.GetState()
if err != nil {
return "", errors.Wrap(err, "machine in unknown state")
}
if s != state.Running {
return "", errors.New("host is not running")
}
conn, err := getConnection(d.ConnectionURI)
if err != nil {
return "", errors.Wrap(err, "getting libvirt connection")
}
defer conn.Close()
return ipFromXML(conn, d.MachineName, d.PrivateNetwork)
}
// GetSSHHostname returns hostname for use with ssh
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
// DriverName returns the name of the driver
func (d *Driver) DriverName() string {
return "kvm2"
}
// Kill stops a host forcefully, including any containers that we are managing.
func (d *Driver) Kill() (err error) {
dom, conn, err := d.getDomain()
if err != nil {
return errors.Wrap(err, "getting connection")
}
defer func() {
if ferr := closeDomain(dom, conn); ferr != nil {
err = ferr
}
}()
return dom.Destroy()
}
// Restart a host
func (d *Driver) Restart() error {
return pkgdrivers.Restart(d)
}
// Start a host
func (d *Driver) Start() (err error) {
// this call ensures that all networks are active
log.Info("Ensuring networks are active...")
err = d.ensureNetwork()
if err != nil {
return errors.Wrap(err, "ensuring active networks")
}
log.Info("Getting domain xml...")
dom, conn, err := d.getDomain()
if err != nil {
return errors.Wrap(err, "getting connection")
}
defer func() {
if ferr := closeDomain(dom, conn); ferr != nil {
err = ferr
}
}()
log.Info("Creating domain...")
if err := dom.Create(); err != nil {
return errors.Wrap(err, "error creating VM")
}
log.Info("Waiting to get IP...")
if err := d.waitForStaticIP(conn); err != nil {
return errors.Wrap(err, "IP not available after waiting")
}
log.Info("Waiting for SSH to be available...")
if err := drivers.WaitForSSH(d); err != nil {
return errors.Wrap(err, "SSH not available after waiting")
}
return nil
}
// waitForStaticIP waits for IP address of domain that has been created & starting and then makes that IP static.
func (d *Driver) waitForStaticIP(conn *libvirt.Connect) error {
query := func() error {
sip, err := ipFromAPI(conn, d.MachineName, d.PrivateNetwork)
if err != nil {
return fmt.Errorf("failed getting IP during machine start, will retry: %w", err)
}
if sip == "" {
return fmt.Errorf("waiting for machine to come up")
}
log.Infof("Found IP for machine: %s", sip)
d.IPAddress = sip
return nil
}
if err := retry.Local(query, 1*time.Minute); err != nil {
return fmt.Errorf("machine %s didn't return IP after 1 minute", d.MachineName)
}
log.Info("Reserving static IP address...")
if err := addStaticIP(conn, d.PrivateNetwork, d.MachineName, d.PrivateMAC, d.IPAddress); err != nil {
log.Warnf("Failed reserving static IP %s for host %s, will continue anyway: %v", d.IPAddress, d.MachineName, err)
} else {
log.Infof("Reserved static IP address: %s", d.IPAddress)
}
return nil
}
// Create a host using the driver's config
func (d *Driver) Create() (err error) {
log.Info("Creating KVM machine...")
err = d.createNetwork()
if err != nil {
return errors.Wrap(err, "creating network")
}
if d.GPU {
log.Info("Creating devices...")
d.DevicesXML, err = getDevicesXML()
if err != nil {
return errors.Wrap(err, "creating devices")
}
}
if d.NUMANodeCount > 1 {
numaXML, err := numaXML(d.CPU, d.Memory, d.NUMANodeCount)
if err != nil {
return errors.Wrap(err, "creating NUMA XML")
}
d.NUMANodeXML = numaXML
}
store := d.ResolveStorePath(".")
log.Infof("Setting up store path in %s ...", store)
// 0755 because it must be accessible by libvirt/qemu across a variety of configs
if err := os.MkdirAll(store, 0755); err != nil {
return errors.Wrap(err, "creating store")
}
log.Infof("Building disk image from %s", d.Boot2DockerURL)
if err = pkgdrivers.MakeDiskImage(d.BaseDriver, d.Boot2DockerURL, d.DiskSize); err != nil {
return errors.Wrap(err, "error creating disk")
}
if d.ExtraDisks > 20 {
// Limiting the number of disks to 20 arbitrarily. If more disks are
// needed, the logical name generation has to changed to create them if
// the form hdaa, hdab, etc
return errors.Wrap(err, "cannot create more than 20 extra disks")
}
for i := 0; i < d.ExtraDisks; i++ {
diskpath := pkgdrivers.ExtraDiskPath(d.BaseDriver, i)
if err := pkgdrivers.CreateRawDisk(diskpath, d.DiskSize); err != nil {
return errors.Wrap(err, "creating extra disks")
}
// Starting the logical names for the extra disks from hdd as the cdrom device is set to hdc.
// TODO: Enhance the domain template to use variable for the logical name of the main disk and the cdrom disk.
extraDisksXML, err := getExtraDiskXML(diskpath, fmt.Sprintf("hd%v", string(rune('d'+i))))
if err != nil {
return errors.Wrap(err, "creating extraDisk XML")
}
d.ExtraDisksXML = append(d.ExtraDisksXML, extraDisksXML)
}
if err := ensureDirPermissions(store); err != nil {
log.Errorf("unable to ensure permissions on %s: %v", store, err)
}
log.Info("Creating domain...")
dom, err := d.createDomain()
if err != nil {
return errors.Wrap(err, "creating domain")
}
defer func() {
if ferr := dom.Free(); ferr != nil {
log.Warnf("unable to free domain: %v", err)
err = ferr
}
}()
if err = d.Start(); err != nil {
log.Errorf("unable to start VM: %v", err)
return err
}
log.Infof("KVM machine creation complete!")
return nil
}
// ensureDirPermissions ensures that libvirt has access to access the image store directory
func ensureDirPermissions(store string) error {
// traverse upwards from /home/user/.minikube/machines to ensure
// that libvirt/qemu has execute access
for dir := store; dir != "/"; dir = filepath.Dir(dir) {
log.Debugf("Checking permissions on dir: %s", dir)
s, err := os.Stat(dir)
if err != nil {
return err
}
owner := int(s.Sys().(*syscall.Stat_t).Uid)
if owner != os.Geteuid() {
log.Debugf("Skipping %s - not owner", dir)
continue
}
mode := s.Mode()
if mode&0011 != 1 {
log.Infof("Setting executable bit set on %s (perms=%s)", dir, mode)
mode |= 0011
if err := os.Chmod(dir, mode); err != nil {
return err
}
}
}
return nil
}
// Stop a host gracefully
func (d *Driver) Stop() (err error) {
s, err := d.GetState()
if err != nil {
return errors.Wrap(err, "getting state of VM")
}
if s != state.Stopped {
dom, conn, err := d.getDomain()
defer func() {
if ferr := closeDomain(dom, conn); ferr != nil {
err = ferr
}
}()
if err != nil {
return errors.Wrap(err, "getting connection")
}
err = dom.Shutdown()
if err != nil {
return errors.Wrap(err, "stopping vm")
}
maxsec := 120
for i := 0; i < maxsec; i++ {
s, err := d.GetState()
if err != nil {
return errors.Wrap(err, "error getting state of VM")
}
if s == state.Stopped {
return nil
}
log.Infof("Waiting for machine to stop %d/%d", i, maxsec)
time.Sleep(time.Second)
}
}
return fmt.Errorf("unable to stop vm, current state %q", s.String())
}
// Remove a host
func (d *Driver) Remove() error {
log.Debug("Removing machine...")
conn, err := getConnection(d.ConnectionURI)
if err != nil {
return errors.Wrap(err, "getting connection")
}
defer conn.Close()
// Tear down network if it exists and is not in use by another minikube instance
log.Debug("Trying to delete the networks (if possible)")
if err := d.deleteNetwork(); err != nil {
log.Warnf("Deleting of networks failed: %v", err)
} else {
log.Info("Successfully deleted networks")
}
// Tear down the domain now
log.Debug("Checking if the domain needs to be deleted")
dom, err := conn.LookupDomainByName(d.MachineName)
if err != nil {
log.Warnf("Domain %s does not exist, nothing to clean up...", d.MachineName)
return nil
}
log.Infof("Domain %s exists, removing...", d.MachineName)
if err := d.destroyRunningDomain(dom); err != nil {
return errors.Wrap(err, "destroying running domain")
}
if err := d.undefineDomain(conn, dom); err != nil {
return errors.Wrap(err, "undefine domain")
}
log.Info("Removing static IP address...")
if err := delStaticIP(conn, d.PrivateNetwork, "", "", d.IPAddress); err != nil {
log.Warnf("failed removing static IP %s for host %s, will continue anyway: %v", d.IPAddress, d.MachineName, err)
} else {
log.Info("Removed static IP address")
}
return nil
}
func (d *Driver) destroyRunningDomain(dom *libvirt.Domain) error {
state, _, err := dom.GetState()
if err != nil {
return errors.Wrap(err, "getting domain state")
}
// if the domain is not running, we don't destroy it
if state != libvirt.DOMAIN_RUNNING {
log.Warnf("Domain %s already destroyed, skipping...", d.MachineName)
return nil
}
return dom.Destroy()
}
func (d *Driver) undefineDomain(conn *libvirt.Connect, dom *libvirt.Domain) error {
definedDomains, err := conn.ListDefinedDomains()
if err != nil {
return errors.Wrap(err, "list defined domains")
}
var found bool
for _, domain := range definedDomains {
if domain == d.MachineName {
found = true
break
}
}
if !found {
log.Warnf("Domain %s not defined, skipping undefine...", d.MachineName)
return nil
}
return dom.UndefineFlags(libvirt.DOMAIN_UNDEFINE_KEEP_NVRAM)
}
// lvErr will return libvirt Error struct containing specific libvirt error code, domain, message and level
func lvErr(err error) libvirt.Error {
if err != nil {
if lverr, ok := err.(libvirt.Error); ok {
return lverr
}
return libvirt.Error{Code: libvirt.ERR_INTERNAL_ERROR, Message: "internal error"}
}
return libvirt.Error{Code: libvirt.ERR_OK, Message: ""}
}