-
Notifications
You must be signed in to change notification settings - Fork 1
/
kvm.go
760 lines (700 loc) · 18 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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
package kvm
import (
"archive/tar"
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"time"
libvirt "github.com/libvirt/libvirt-go"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
)
const (
connectionString = "qemu:///system"
privateNetworkName = "docker-machines"
isoFilename = "boot2docker.iso"
dnsmasqLeases = "/var/lib/libvirt/dnsmasq/%s.leases"
dnsmasqStatus = "/var/lib/libvirt/dnsmasq/%s.status"
defaultSSHUser = "docker"
domainXMLTemplate = `<domain type='kvm'>
<name>{{.MachineName}}</name> <memory unit='M'>{{.Memory}}</memory>
<vcpu>{{.CPU}}</vcpu>
<features><acpi/><apic/><pae/></features>
<cpu mode='host-passthrough'></cpu>
<os>
<type>hvm</type>
<boot dev='cdrom'/>
<boot dev='hd'/>
<bootmenu enable='no'/>
</os>
<devices>
<disk type='file' device='cdrom'>
<source file='{{.ISO}}'/>
<target dev='hdc' bus='ide'/>
<readonly/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='{{.CacheMode}}' io='{{.IOMode}}' />
<source file='{{.DiskPath}}'/>
<target dev='hda' bus='ide'/>
</disk>
<graphics type='vnc' autoport='yes' listen='127.0.0.1'>
<listen type='address' address='127.0.0.1'/>
</graphics>
<interface type='network'>
<source network='{{.Network}}'/>
</interface>
<interface type='network'>
<source network='{{.PrivateNetwork}}'/>
</interface>
</devices>
</domain>`
networkXML = `<network>
<name>%s</name>
<ip address='%s' netmask='%s'>
<dhcp>
<range start='%s' end='%s'/>
</dhcp>
</ip>
</network>`
)
type Driver struct {
*drivers.BaseDriver
Memory int
DiskSize int
CPU int
Network string
PrivateNetwork string
ISO string
Boot2DockerURL string
CaCertPath string
PrivateKeyPath string
DiskPath string
CacheMode string
IOMode string
connectionString string
conn *libvirt.Connect
VM *libvirt.Domain
vmLoaded bool
}
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.IntFlag{
Name: "kvm-memory",
Usage: "Size of memory for host in MB",
Value: 1024,
},
mcnflag.IntFlag{
Name: "kvm-disk-size",
Usage: "Size of disk for host in MB",
Value: 20000,
},
mcnflag.IntFlag{
Name: "kvm-cpu-count",
Usage: "Number of CPUs",
Value: 1,
},
// TODO - support for multiple networks
mcnflag.StringFlag{
Name: "kvm-network",
Usage: "Name of network to connect to",
Value: "default",
},
mcnflag.StringFlag{
EnvVar: "KVM_BOOT2DOCKER_URL",
Name: "kvm-boot2docker-url",
Usage: "The URL of the boot2docker image. Defaults to the latest available version",
Value: "",
},
mcnflag.StringFlag{
Name: "kvm-cache-mode",
Usage: "Disk cache mode: default, none, writethrough, writeback, directsync, or unsafe",
Value: "default",
},
mcnflag.StringFlag{
Name: "kvm-io-mode",
Usage: "Disk IO mode: threads, native",
Value: "threads",
},
mcnflag.StringFlag{
EnvVar: "KVM_SSH_USER",
Name: "kvm-ssh-user",
Usage: "SSH username",
Value: defaultSSHUser,
},
/* Not yet implemented
mcnflag.Flag{
Name: "kvm-no-share",
Usage: "Disable the mount of your home directory",
},
*/
}
}
func (d *Driver) GetMachineName() string {
return d.MachineName
}
func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}
func (d *Driver) GetSSHKeyPath() string {
return d.ResolveStorePath("id_rsa")
}
func (d *Driver) GetSSHPort() (int, error) {
if d.SSHPort == 0 {
d.SSHPort = 22
}
return d.SSHPort, nil
}
func (d *Driver) GetSSHUsername() string {
if d.SSHUser == "" {
d.SSHUser = "docker"
}
return d.SSHUser
}
func (d *Driver) DriverName() string {
return "kvm"
}
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
log.Debugf("SetConfigFromFlags called")
d.Memory = flags.Int("kvm-memory")
d.DiskSize = flags.Int("kvm-disk-size")
d.CPU = flags.Int("kvm-cpu-count")
d.Network = flags.String("kvm-network")
d.Boot2DockerURL = flags.String("kvm-boot2docker-url")
d.CacheMode = flags.String("kvm-cache-mode")
d.IOMode = flags.String("kvm-io-mode")
d.SwarmMaster = flags.Bool("swarm-master")
d.SwarmHost = flags.String("swarm-host")
d.SwarmDiscovery = flags.String("swarm-discovery")
d.ISO = d.ResolveStorePath(isoFilename)
d.SSHUser = flags.String("kvm-ssh-user")
d.SSHPort = 22
d.DiskPath = d.ResolveStorePath(fmt.Sprintf("%s.img", d.MachineName))
return nil
}
func (d *Driver) GetURL() (string, error) {
log.Debugf("GetURL called")
ip, err := d.GetIP()
if err != nil {
log.Warnf("Failed to get IP: %s", err)
return "", err
}
if ip == "" {
return "", nil
}
return fmt.Sprintf("tcp://%s:2376", ip), nil // TODO - don't hardcode the port!
}
func (d *Driver) getConn() (*libvirt.Connect, error) {
if d.conn == nil {
conn, err := libvirt.NewConnect(connectionString)
if err != nil {
log.Errorf("Failed to connect to libvirt: %s", err)
return &libvirt.Connect{}, errors.New("Unable to connect to kvm driver, did you add yourself to the libvirtd group?")
}
d.conn = conn
}
return d.conn, nil
}
// Create, or verify the private network is properly configured
func (d *Driver) validatePrivateNetwork() error {
log.Debug("Validating private network")
conn, err := d.getConn()
if err != nil {
return err
}
network, err := conn.LookupNetworkByName(d.PrivateNetwork)
if err == nil {
xmldoc, err := network.GetXMLDesc(0)
if err != nil {
return err
}
/* XML structure:
<network>
...
<ip address='a.b.c.d' netmask='255.255.255.0'>
<dhcp>
<range start='a.b.c.d' end='w.x.y.z'/>
</dhcp>
*/
type Ip struct {
Address string `xml:"address,attr"`
Netmask string `xml:"netmask,attr"`
}
type Network struct {
Ip Ip `xml:"ip"`
}
var nw Network
err = xml.Unmarshal([]byte(xmldoc), &nw)
if err != nil {
return err
}
if nw.Ip.Address == "" {
return fmt.Errorf("%s network doesn't have DHCP configured properly", d.PrivateNetwork)
}
// Corner case, but might happen...
if active, err := network.IsActive(); !active {
log.Debugf("Reactivating private network: %s", err)
err = network.Create()
if err != nil {
log.Warnf("Failed to Start network: %s", err)
return err
}
}
return nil
}
// TODO - try a couple pre-defined networks and look for conflicts before
// settling on one
xml := fmt.Sprintf(networkXML, d.PrivateNetwork,
"192.168.42.1",
"255.255.255.0",
"192.168.42.2",
"192.168.42.254")
network, err = conn.NetworkDefineXML(xml)
if err != nil {
log.Errorf("Failed to create private network: %s", err)
return nil
}
err = network.SetAutostart(true)
if err != nil {
log.Warnf("Failed to set private network to autostart: %s", err)
}
err = network.Create()
if err != nil {
log.Warnf("Failed to Start network: %s", err)
return err
}
return nil
}
func (d *Driver) validateNetwork(name string) error {
log.Debugf("Validating network %s", name)
conn, err := d.getConn()
if err != nil {
return err
}
_, err = conn.LookupNetworkByName(name)
if err != nil {
log.Errorf("Unable to locate network %s", name)
return err
}
return nil
}
func (d *Driver) PreCreateCheck() error {
conn, err := d.getConn()
if err != nil {
return err
}
// TODO We could look at conn.GetCapabilities()
// parse the XML, and look for kvm
log.Debug("About to check libvirt version")
// TODO might want to check minimum version
_, err = conn.GetLibVersion()
if err != nil {
log.Warnf("Unable to get libvirt version")
return err
}
err = d.validatePrivateNetwork()
if err != nil {
return err
}
err = d.validateNetwork(d.Network)
if err != nil {
return err
}
// Others...?
return nil
}
func (d *Driver) Create() error {
b2dutils := mcnutils.NewB2dUtils(d.StorePath)
if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, d.MachineName); err != nil {
return err
}
log.Infof("Creating SSH key...")
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}
if err := os.MkdirAll(d.ResolveStorePath("."), 0755); err != nil {
return err
}
// Libvirt typically runs as a deprivileged service account and
// needs the execute bit set for directories that contain disks
for dir := d.ResolveStorePath("."); dir != "/"; dir = filepath.Dir(dir) {
log.Debugf("Verifying executable bit set on %s", dir)
info, err := os.Stat(dir)
if err != nil {
return err
}
mode := info.Mode()
if mode&0001 != 1 {
log.Debugf("Setting executable bit set on %s", dir)
mode |= 0001
os.Chmod(dir, mode)
}
}
log.Debugf("Creating VM data disk...")
if err := d.generateDiskImage(d.DiskSize); err != nil {
return err
}
log.Debugf("Defining VM...")
tmpl, err := template.New("domain").Parse(domainXMLTemplate)
if err != nil {
return err
}
var xml bytes.Buffer
err = tmpl.Execute(&xml, d)
if err != nil {
return err
}
conn, err := d.getConn()
if err != nil {
return err
}
vm, err := conn.DomainDefineXML(xml.String())
if err != nil {
log.Warnf("Failed to create the VM: %s", err)
return err
}
d.VM = vm
d.vmLoaded = true
return d.Start()
}
func (d *Driver) Start() error {
log.Debugf("Starting VM %s", d.MachineName)
if err := d.validateVMRef(); err != nil {
return err
}
if err := d.VM.Create(); err != nil {
log.Warnf("Failed to start: %s", err)
return err
}
// They wont start immediately
time.Sleep(5 * time.Second)
for i := 0; i < 90; i++ {
time.Sleep(time.Second)
ip, _ := d.GetIP()
if ip != "" {
// Add a second to let things settle
time.Sleep(time.Second)
return nil
}
log.Debugf("Waiting for the VM to come up... %d", i)
}
log.Warnf("Unable to determine VM's IP address, did it fail to boot?")
return nil
}
func (d *Driver) Stop() error {
log.Debugf("Stopping VM %s", d.MachineName)
if err := d.validateVMRef(); err != nil {
return err
}
s, err := d.GetState()
if err != nil {
return err
}
if s != state.Stopped {
err := d.VM.Shutdown()
if err != nil {
log.Warnf("Failed to gracefully shutdown VM")
return err
}
for i := 0; i < 90; i++ {
time.Sleep(time.Second)
s, _ := d.GetState()
log.Debugf("VM state: %s", s)
if s == state.Stopped {
return nil
}
}
return errors.New("VM Failed to gracefully shutdown, try the kill command")
}
return nil
}
func (d *Driver) Remove() error {
log.Debugf("Removing VM %s", d.MachineName)
if err := d.validateVMRef(); err != nil {
return err
}
// Note: If we switch to qcow disks instead of raw the user
// could take a snapshot. If you do, then Undefine
// will fail unless we nuke the snapshots first
d.VM.Destroy() // Ignore errors
return d.VM.Undefine()
}
func (d *Driver) Restart() error {
log.Debugf("Restarting VM %s", d.MachineName)
if err := d.Stop(); err != nil {
return err
}
return d.Start()
}
func (d *Driver) Kill() error {
log.Debugf("Killing VM %s", d.MachineName)
if err := d.validateVMRef(); err != nil {
return err
}
return d.VM.Destroy()
}
func (d *Driver) GetState() (state.State, error) {
log.Debugf("Getting current state...")
if err := d.validateVMRef(); err != nil {
return state.None, err
}
virState, _, err := d.VM.GetState()
if err != nil {
return state.None, err
}
switch virState {
case libvirt.DOMAIN_NOSTATE:
return state.None, nil
case libvirt.DOMAIN_RUNNING:
return state.Running, nil
case libvirt.DOMAIN_BLOCKED:
// TODO - Not really correct, but does it matter?
return state.Error, nil
case libvirt.DOMAIN_PAUSED:
return state.Paused, nil
case libvirt.DOMAIN_SHUTDOWN:
return state.Stopped, nil
case libvirt.DOMAIN_CRASHED:
return state.Error, nil
case libvirt.DOMAIN_PMSUSPENDED:
return state.Saved, nil
case libvirt.DOMAIN_SHUTOFF:
return state.Stopped, nil
}
return state.None, nil
}
func (d *Driver) validateVMRef() error {
if !d.vmLoaded {
log.Debugf("Fetching VM...")
conn, err := d.getConn()
if err != nil {
return err
}
vm, err := conn.LookupDomainByName(d.MachineName)
if err != nil {
log.Warnf("Failed to fetch machine")
} else {
d.VM = vm
d.vmLoaded = true
}
}
return nil
}
// This implementation is specific to default networking in libvirt
// with dnsmasq
func (d *Driver) getMAC() (string, error) {
if err := d.validateVMRef(); err != nil {
return "", err
}
xmldoc, err := d.VM.GetXMLDesc(0)
if err != nil {
return "", err
}
/* XML structure:
<domain>
...
<devices>
...
<interface type='network'>
...
<mac address='52:54:00:d2:3f:ba'/>
...
</interface>
...
*/
type Mac struct {
Address string `xml:"address,attr"`
}
type Source struct {
Network string `xml:"network,attr"`
}
type Interface struct {
Type string `xml:"type,attr"`
Mac Mac `xml:"mac"`
Source Source `xml:"source"`
}
type Devices struct {
Interfaces []Interface `xml:"interface"`
}
type Domain struct {
Devices Devices `xml:"devices"`
}
var dom Domain
err = xml.Unmarshal([]byte(xmldoc), &dom)
if err != nil {
return "", err
}
// Always assume the second interface is the one we want
if len(dom.Devices.Interfaces) < 2 {
return "", fmt.Errorf("VM doesn't have enough network interfaces. Expected at least 2, found %d",
len(dom.Devices.Interfaces))
}
return dom.Devices.Interfaces[1].Mac.Address, nil
}
func (d *Driver) getIPByMACFromLeaseFile(mac string) (string, error) {
leaseFile := fmt.Sprintf(dnsmasqLeases, d.PrivateNetwork)
data, err := ioutil.ReadFile(leaseFile)
if err != nil {
log.Debugf("Failed to retrieve dnsmasq leases from %s", leaseFile)
return "", err
}
for lineNum, line := range strings.Split(string(data), "\n") {
if len(line) == 0 {
continue
}
entries := strings.Split(line, " ")
if len(entries) < 3 {
log.Warnf("Malformed dnsmasq line %d", lineNum+1)
return "", errors.New("Malformed dnsmasq file")
}
if strings.ToLower(entries[1]) == strings.ToLower(mac) {
log.Debugf("IP address: %s", entries[2])
return entries[2], nil
}
}
return "", nil
}
func (d *Driver) getIPByMacFromSettings(mac string) (string, error) {
conn, err := d.getConn()
if err != nil {
return "", err
}
network, err := conn.LookupNetworkByName(d.PrivateNetwork)
if err != nil {
log.Warnf("Failed to find network: %s", err)
return "", err
}
bridge_name, err := network.GetBridgeName()
if err != nil {
log.Warnf("Failed to get network bridge: %s", err)
return "", err
}
statusFile := fmt.Sprintf(dnsmasqStatus, bridge_name)
data, err := ioutil.ReadFile(statusFile)
type Lease struct {
Ip_address string `json:"ip-address"`
Mac_address string `json:"mac-address"`
// Other unused fields omitted
}
var s []Lease
err = json.Unmarshal(data, &s)
if err != nil {
log.Warnf("Failed to decode dnsmasq lease status: %s", err)
return "", err
}
ipAddr := ""
for _, value := range s {
if strings.ToLower(value.Mac_address) == strings.ToLower(mac) {
// If there are multiple entries,
// the last one is the most current
ipAddr = value.Ip_address
}
}
if ipAddr != "" {
log.Debugf("IP address: %s", ipAddr)
}
return ipAddr, nil
}
func (d *Driver) GetIP() (string, error) {
log.Debugf("GetIP called for %s", d.MachineName)
mac, err := d.getMAC()
if err != nil {
return "", err
}
/*
* TODO - Figure out what version of libvirt changed behavior and
* be smarter about selecting which algorithm to use
*/
ip, err := d.getIPByMACFromLeaseFile(mac)
if ip == "" {
ip, err = d.getIPByMacFromSettings(mac)
}
log.Debugf("Unable to locate IP address for MAC %s", mac)
return ip, err
}
func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
}
// Make a boot2docker VM disk image.
func (d *Driver) generateDiskImage(size int) error {
log.Debugf("Creating %d MB hard disk image...", size)
magicString := "boot2docker, please format-me"
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
// magicString first so the automount script knows to format the disk
file := &tar.Header{Name: magicString, Size: int64(len(magicString))}
if err := tw.WriteHeader(file); err != nil {
return err
}
if _, err := tw.Write([]byte(magicString)); err != nil {
return err
}
// .ssh/key.pub => authorized_keys
file = &tar.Header{Name: ".ssh", Typeflag: tar.TypeDir, Mode: 0700}
if err := tw.WriteHeader(file); err != nil {
return err
}
pubKey, err := ioutil.ReadFile(d.publicSSHKeyPath())
if err != nil {
return err
}
file = &tar.Header{Name: ".ssh/authorized_keys", Size: int64(len(pubKey)), Mode: 0644}
if err := tw.WriteHeader(file); err != nil {
return err
}
if _, err := tw.Write([]byte(pubKey)); err != nil {
return err
}
file = &tar.Header{Name: ".ssh/authorized_keys2", Size: int64(len(pubKey)), Mode: 0644}
if err := tw.WriteHeader(file); err != nil {
return err
}
if _, err := tw.Write([]byte(pubKey)); err != nil {
return err
}
if err := tw.Close(); err != nil {
return err
}
raw := bytes.NewReader(buf.Bytes())
return createDiskImage(d.DiskPath, size, raw)
}
// createDiskImage makes a disk image at dest with the given size in MB. If r is
// not nil, it will be read as a raw disk image to convert from.
func createDiskImage(dest string, size int, r io.Reader) error {
// Convert a raw image from stdin to the dest VMDK image.
sizeBytes := int64(size) << 20 // usually won't fit in 32-bit int (max 2GB)
f, err := os.Create(dest)
if err != nil {
return err
}
_, err = io.Copy(f, r)
if err != nil {
return err
}
// Rely on seeking to create a sparse raw file for qemu
f.Seek(sizeBytes-1, 0)
f.Write([]byte{0})
return f.Close()
}
func NewDriver(hostName, storePath string) drivers.Driver {
return &Driver{
PrivateNetwork: privateNetworkName,
BaseDriver: &drivers.BaseDriver{
SSHUser: defaultSSHUser,
MachineName: hostName,
StorePath: storePath,
},
}
}