forked from machine-drivers/docker-machine-driver-xhyve
/
xhyve.go
575 lines (493 loc) · 13.9 KB
/
xhyve.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
package xhyve
import (
"archive/tar"
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"regexp"
"strings"
"time"
"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"
"github.com/zchee/docker-machine-xhyve/version"
"github.com/zchee/docker-machine-xhyve/vmnet"
"libguestfs.org/guestfs"
)
const (
isoFilename = "boot2docker.iso"
)
type Driver struct {
*drivers.BaseDriver
Memory int
DiskSize int64
CPU int
TmpISO string
UUID string
MacAddr string
BootCmd string
Boot2DockerURL string
CaCertPath string
PrivateKeyPath string
}
var (
ErrMachineExist = errors.New("machine already exists")
ErrMachineNotExist = errors.New("machine does not exist")
)
// RegisterCreateFlags registers the flags this driver adds to
// "docker hosts create"
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return []mcnflag.Flag{
mcnflag.StringFlag{
EnvVar: "XHYVE_BOOT2DOCKER_URL",
Name: "xhyve-boot2docker-url",
Usage: "The URL of the boot2docker image. Defaults to the latest available version",
Value: "",
},
mcnflag.IntFlag{
EnvVar: "XHYVE_CPU_COUNT",
Name: "xhyve-cpu-count",
Usage: "Number of CPUs for the machine (-1 to use the number of CPUs available)",
Value: 1,
},
mcnflag.IntFlag{
EnvVar: "XHYVE_MEMORY_SIZE",
Name: "xhyve-memory",
Usage: "Size of memory for host in MB",
Value: 1024,
},
mcnflag.IntFlag{
EnvVar: "XHYVE_DISK_SIZE",
Name: "xhyve-disk-size",
Usage: "Size of disk for host in MB",
Value: 20000,
},
mcnflag.StringFlag{
EnvVar: "XHYVE_BOOT_CMD",
Name: "xhyve-boot-cmd",
Usage: "Command of booting kexec protocol",
Value: "loglevel=3 user=docker console=ttyS0 console=tty0 noembed nomodeset norestore waitusb=10:LABEL=boot2docker-data base host=boot2docker",
},
}
}
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 "xhyve"
}
func (d *Driver) SetConfigFromFlags(flags drivers.DriverOptions) error {
d.Boot2DockerURL = flags.String("xhyve-boot2docker-url")
d.CPU = flags.Int("xhyve-cpu-count")
d.Memory = flags.Int("xhyve-memory")
d.DiskSize = int64(flags.Int("xhyve-disk-size"))
d.BootCmd = flags.String("xhyve-boot-cmd")
d.SwarmMaster = flags.Bool("swarm-master")
d.SwarmHost = flags.String("swarm-host")
d.SwarmDiscovery = flags.String("swarm-discovery")
d.SSHUser = "docker"
d.SSHPort = 22
return nil
}
func (d *Driver) GetURL() (string, error) {
ip, err := d.GetIP()
if err != nil {
return "", err
}
if ip == "" {
return "", nil
}
return fmt.Sprintf("tcp://%s:2376", ip), nil
}
func (d *Driver) GetIP() (string, error) {
s, err := d.GetState()
if err != nil {
return "", err
}
if s != state.Running {
return "", drivers.ErrHostIsNotRunning
}
ip, err := d.getIPfromDHCPLease()
if err != nil {
return "", err
}
return ip, nil
}
func (d *Driver) GetState() (state.State, error) { // TODO
// VMRUN only tells use if the vm is running or not
// if stdout, _, _ := vmrun("list"); strings.Contains(stdout, d.vmxPath()) {
return state.Running, nil
// }
// return state.Stopped, nil
}
// Check VirtualBox version
func (d *Driver) PreCreateCheck() error {
//TODO:libmachine PLEASE output driver version API!
v := version.Version
c := version.GitCommit
log.Debugf("===== Docker Machine %s Driver Version %s (%s) =====\n", d.DriverName(), v, c)
ver, err := vboxVersionDetect()
if err != nil {
return fmt.Errorf("Error detecting VBox version: %s", err)
}
if !strings.HasPrefix(ver, "5") {
return fmt.Errorf("Virtual Box version 4 or lower will cause a kernel panic" +
"if xhyve tries to run. You are running version: " +
ver +
"\n\t Please upgrade to version 5 at https://www.virtualbox.org/wiki/Downloads")
}
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
}
log.Infof("Creating VM...")
if err := os.MkdirAll(d.ResolveStorePath("."), 0755); err != nil {
return err
}
log.Infof("Extracting vmlinuz64 and initrd.img from %s...", isoFilename)
if err := d.extractKernelImages(); err != nil {
return err
}
log.Infof("Make a boot2docker userdata.tar key bundle...")
if err := d.generateKeyBundle(); err != nil {
return err
}
// Fix file permission root to current user.
// In order to avoid require sudo of vmnet.framework, Execute the root owner(and root uid)
// "docker-machine-xhyve" and "goxhyve" binary in golang.
log.Infof("Fix file permission...")
os.Chown(d.ResolveStorePath("."), 501, 20) //TODO Parse current user uid and gid
files, _ := ioutil.ReadDir(d.ResolveStorePath("."))
for _, f := range files {
log.Debugf(d.ResolveStorePath(f.Name()))
os.Chown(d.ResolveStorePath(f.Name()), 501, 20)
}
log.Infof("Creating blank ext4 filesystem disk image...")
if err := d.generateBlankDiskImage(d.DiskSize); err != nil {
return err
}
os.Chown(d.ResolveStorePath(d.MachineName+".img"), 501, 20)
log.Debugf("Created disk size: %dMB", d.DiskSize)
log.Infof("Generate UUID...")
d.UUID = uuidgen() //TODO Native golang instead execute "uuidgen"
log.Debugf("uuidgen generated UUID: %s", d.UUID)
log.Infof("Convert UUID to MAC address...")
//Trim "0" string
re := regexp.MustCompile(`[0]`)
mac, _ := vmnet.GetMACAddressByUUID(d.UUID)
d.MacAddr = re.ReplaceAllString(mac, "")
log.Debugf("uuid2mac output MAC address: %s", d.MacAddr)
log.Infof("Starting %s...", d.MachineName)
if err := d.Start(); err != nil {
return err
}
var ip string
var err error
log.Infof("Waiting for VM to come online...")
log.Debugf("d.MacAddr", d.MacAddr)
for i := 1; i <= 60; i++ {
ip, err = d.getIPfromDHCPLease()
if err != nil {
log.Debugf("Not there yet %d/%d, error: %s", i, 60, err)
time.Sleep(2 * time.Second)
continue
}
if ip != "" {
log.Debugf("Got an ip: %s", ip)
break
}
}
if ip == "" {
return fmt.Errorf("Machine didn't return an IP after 120 seconds, aborting")
}
// we got an IP, let's copy ssh keys over
d.IPAddress = ip
return nil
}
func (d *Driver) Start() error {
uuid := d.UUID
vmlinuz := d.ResolveStorePath("vmlinuz64")
initrd := d.ResolveStorePath("initrd.img")
iso := d.ResolveStorePath(isoFilename)
img := d.ResolveStorePath(d.MachineName + ".img")
bootcmd := d.BootCmd
cmd := exec.Command("goxhyve",
fmt.Sprintf("%s", uuid),
fmt.Sprintf("%d", d.CPU),
fmt.Sprintf("%d", d.Memory),
fmt.Sprintf("%s", iso),
fmt.Sprintf("%s", img),
fmt.Sprintf("kexec,%s,%s,%s", vmlinuz, initrd, bootcmd),
"-d", //TODO fix daemonize flag
)
log.Debug(cmd)
go func() {
err := cmd.Run()
if err != nil {
log.Error(err, cmd.Stdout)
}
}()
return nil
}
func (d *Driver) Stop() error { // TODO
// xhyve("controlvm", d.MachineName, "acpipowerbutton")
for {
s, err := d.GetState()
if err != nil {
return err
}
if s == state.Running {
time.Sleep(1 * time.Second)
} else {
break
}
}
d.IPAddress = ""
return nil
}
func (d *Driver) Remove() error { // TODO
s, err := d.GetState()
if err != nil {
if err == ErrMachineNotExist {
log.Infof("machine does not exist, assuming it has been removed already")
return nil
}
return err
}
if s == state.Running {
if err := d.Stop(); err != nil {
return err
}
}
//return xhyve("unregistervm", "--delete", d.MachineName)
return nil
}
func (d *Driver) Restart() error { // TODO
s, err := d.GetState()
if err != nil {
return err
}
if s == state.Running {
if err := d.Stop(); err != nil {
return err
}
}
return d.Start()
}
func (d *Driver) Kill() error { // TODO
//return xhyve("controlvm", d.MachineName, "poweroff")
return nil
}
func (d *Driver) setMachineNameIfNotSet() {
if d.MachineName == "" {
d.MachineName = fmt.Sprintf("docker-machine-unknown")
}
}
func (d *Driver) imgPath() string {
return d.ResolveStorePath(fmt.Sprintf("%s.img", d.MachineName))
}
func (d *Driver) userdataPath() string {
return d.ResolveStorePath("userdata.tar")
}
func (d *Driver) getIPfromDHCPLease() (string, error) {
currentip, err := vmnet.GetIPAddressByMACAddress(d.MacAddr)
log.Debugf(currentip)
if currentip == "" {
return "", fmt.Errorf("IP not found for MAC %s in DHCP leases", d.MacAddr)
}
log.Debugf("IP found in DHCP lease table: %s", currentip)
return currentip, err
}
func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
}
func (d *Driver) extractKernelImages() error {
var vmlinuz64 = "/Volumes/Boot2Docker-v1.8/boot/vmlinuz64" // TODO Do not hardcode boot2docker version
var initrd = "/Volumes/Boot2Docker-v1.8/boot/initrd.img" // TODO Do not hardcode boot2docker version
log.Debugf("Mounting %s", isoFilename)
hdiutil("attach", d.ResolveStorePath(isoFilename)) // TODO need parse attached disk identifier.
log.Debugf("Extract vmlinuz64")
if err := mcnutils.CopyFile(vmlinuz64, d.ResolveStorePath("vmlinuz64")); err != nil {
return err
}
log.Debugf("Extract initrd.img")
if err := mcnutils.CopyFile(initrd, d.ResolveStorePath("initrd.img")); err != nil {
return err
}
log.Debugf("Unmounting %s", isoFilename)
if err := hdiutil("detach", "/Volumes/Boot2Docker-v1.8/"); err != nil { // TODO Do not hardcode boot2docker version
return err
}
return nil
}
func (d *Driver) generateBlankDiskImage(count int64) error {
output := d.ResolveStorePath(d.MachineName + ".img")
g, errno := guestfs.Create()
if errno != nil {
panic(errno)
}
defer g.Close()
/* Set $LIBGUESTFS_PATH(libguestfs appliance path) to root user */
p := toPtr("/usr/local/lib/guestfs")
g.Set_path(p)
/* Set the trace flag so that we can see each libguestfs call. */
if log.IsDebug == true {
g.Set_trace(true)
}
/* Create the disk image to libguestfs. */
optargsDiskCreate := guestfs.OptargsDisk_create{
Backingfile_is_set: false,
Backingformat_is_set: false,
Preallocation_is_set: false,
Compat_is_set: false,
Clustersize_is_set: false,
}
/* Create a raw-format sparse disk image, d.DiskSize MB in size. */
if err := g.Disk_create(output, "raw", int64(d.DiskSize*1024*1024), &optargsDiskCreate); err != nil {
panic(err)
}
/* Attach the disk image to libguestfs. */
optargsAdd_drive := guestfs.OptargsAdd_drive{
Format_is_set: true,
Format: "raw",
Readonly_is_set: true,
Readonly: false,
}
if err := g.Add_drive(output, &optargsAdd_drive); err != nil {
panic(err)
}
/* Run the libguestfs back-end. */
if err := g.Launch(); err != nil {
panic(err)
}
/* Get the list of devices. Because we only added one drive
* above, we expect that this list should contain a single
* element.
*/
devices, err := g.List_devices()
if err != nil {
panic(err)
}
if len(devices) != 1 {
panic("expected a single device from list-devices")
}
/* Partition the disk as one single MBR partition. */
err = g.Part_disk(devices[0], "mbr")
if err != nil {
panic(err)
}
/* Get the list of partitions. We expect a single element, which
* is the partition we have just created.
*/
partitions, err := g.List_partitions()
if err != nil {
panic(err)
}
if len(partitions) != 1 {
panic("expected a single partition from list-partitions")
}
/* Create a filesystem on the partition. */
err = g.Mkfs("ext4", partitions[0], nil)
if err != nil {
panic(err)
}
/* Now mount the filesystem so that we can add files. */
err = g.Mount(partitions[0], "/")
if err != nil {
panic(err)
}
/* Mkdir -p place of userdata.tar */
err = g.Mkdir_p("/var/lib/boot2docker")
if err != nil {
panic(err)
}
/* Uploads the local userdata.tar file into /var/lib/boot2docker */
err = g.Upload(d.ResolveStorePath("userdata.tar"), "/var/lib/boot2docker/userdata.tar")
if err != nil {
panic(err)
}
/* Because we wrote to the disk and we want to detect write
* errors, call g:shutdown. You don't need to do this:
* g.Close will do it implicitly.
*/
if err = g.Shutdown(); err != nil {
panic(fmt.Sprintf("write to disk failed: %s", err))
}
return nil
}
// Make a boot2docker userdata.tar key bundle
func (d *Driver) generateKeyBundle() error { // TODO
magicString := "boot2docker, this is xhyve speaking"
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 := buf.Bytes()
if err := ioutil.WriteFile(d.userdataPath(), raw, 0644); err != nil {
return err
}
return nil
}