Skip to content

Commit 634e3f5

Browse files
committed
Switch to the official go hyperkit bindings.
1 parent 64ff2aa commit 634e3f5

File tree

2 files changed

+46
-102
lines changed

2 files changed

+46
-102
lines changed

main.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,10 @@
55
package main
66

77
import (
8-
"fmt"
9-
"os"
10-
118
"github.com/docker/machine/libmachine/drivers/plugin"
129
"github.com/zchee/docker-machine-driver-xhyve/xhyve"
13-
hyperkit "github.com/zchee/libhyperkit"
1410
)
1511

1612
func main() {
17-
if len(os.Args) >= 2 && os.Args[1] == "xhyve" {
18-
runXhyve()
19-
} else {
20-
plugin.RegisterDriver(xhyve.NewDriver("", ""))
21-
}
22-
}
23-
24-
func runXhyve() {
25-
done := make(chan bool)
26-
ptyCh := make(chan string)
27-
28-
args := os.Args[1:] // Technically we only need 2:, but a bug in hooklift/xhyve requires one arg in the beginning
29-
go func() {
30-
if err := hyperkit.Run(args, ptyCh); err != nil {
31-
fmt.Println(err)
32-
}
33-
done <- true
34-
}()
35-
36-
if os.Args[len(os.Args)-1] != "-M" {
37-
fmt.Printf("Waiting on a pseudo-terminal to be ready... ")
38-
pty := <-ptyCh
39-
fmt.Printf("done\n")
40-
fmt.Printf("Hook up your terminal emulator to %s in order to connect to your VM\n", pty)
41-
}
42-
43-
<-done
13+
plugin.RegisterDriver(xhyve.NewDriver("", ""))
4414
}

xhyve/xhyve.go

Lines changed: 45 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ import (
3232
"github.com/docker/machine/libmachine/state"
3333
"github.com/johanneswuerbach/nfsexports"
3434
ps "github.com/mitchellh/go-ps"
35+
hyperkit "github.com/moby/hyperkit/go"
3536
"github.com/zchee/docker-machine-driver-xhyve/b2d"
3637
"github.com/zchee/docker-machine-driver-xhyve/vmnet"
3738
qcow2 "github.com/zchee/go-qcow2"
39+
govmnet "github.com/zchee/go-vmnet"
3840
)
3941

4042
const (
@@ -339,7 +341,8 @@ func (d *Driver) GetState() (state.State, error) {
339341
return state.Error, err
340342
}
341343
// process name is truncated to 'docker-machine-d'
342-
if !strings.Contains(psproc.Executable(), "docker-machine") {
344+
exe := psproc.Executable()
345+
if !(strings.Contains(exe, "docker-machine") || strings.Contains(exe, "hyperkit")) {
343346
return state.Error, fmt.Errorf("Unable to find 'xhyve' process by PID: %d", pid)
344347
}
345348

@@ -422,7 +425,7 @@ func (d *Driver) Create() error {
422425
return err
423426
}
424427

425-
log.Infof("Generating %dMB disk image...", d.DiskSize)
428+
log.Infof("Generating %dMB disk image. QCOW: %s, RAW: %s", d.DiskSize, d.Qcow2, d.RawDisk)
426429

427430
if d.Qcow2 {
428431
if err := d.generateQcow2Image(d.DiskSize); err != nil {
@@ -485,30 +488,48 @@ func (d *Driver) Start() error {
485488

486489
d.attachDiskImage()
487490

488-
args := d.xhyveArgs()
489-
args = append(args, "-F", fmt.Sprintf("%s", pid))
490-
if d.Virtio9p {
491-
args = append(args, "-s", fmt.Sprintf("5,virtio-9p,host=%s", d.Virtio9pFolder))
491+
h, err := hyperkit.New("", "auto", d.ResolveStorePath(""))
492+
if err != nil {
493+
return err
492494
}
495+
h.Console = hyperkit.ConsoleFile
493496

494-
log.Debug(args)
497+
h.Kernel = d.ResolveStorePath(d.Vmlinuz)
498+
h.Initrd = d.ResolveStorePath(d.Initrd)
495499

496-
cmd := exec.Command(os.Args[0], args...)
497-
cmd.Stdout = os.Stdout
498-
cmd.Stderr = os.Stderr
500+
if d.Qcow2 {
501+
h.Disks = []hyperkit.DiskConfig{
502+
{
503+
Path: fmt.Sprintf("file://%s", filepath.Join(d.ResolveStorePath("."), d.MachineName+".qcow2")),
504+
Format: "qcow",
505+
Driver: "virtio-blk",
506+
},
507+
}
508+
} else if d.RawDisk {
509+
h.Disks = []hyperkit.DiskConfig{
510+
{
511+
Path: filepath.Join(d.ResolveStorePath("."), d.MachineName+".rawdisk"),
512+
Driver: "virtio-blk",
513+
},
514+
}
515+
} else {
516+
h.Disks = []hyperkit.DiskConfig{
517+
{
518+
Path: fmt.Sprintf("/dev/rdisk%d", d.DiskNumber),
519+
Driver: "ahci-hd",
520+
},
521+
}
522+
}
499523

500-
err := cmd.Start()
501-
if err != nil {
524+
h.UUID = d.UUID
525+
h.CPUs = d.CPU
526+
h.Memory = d.Memory
527+
h.VMNet = true
528+
529+
if err := h.Start(d.BootCmd); err != nil {
502530
return err
503531
}
504532

505-
go func() {
506-
err := cmd.Wait()
507-
if err != nil {
508-
log.Error(err, cmd.Stdout, cmd.Stderr)
509-
}
510-
}()
511-
512533
if err := d.waitForIP(); err != nil {
513534
return err
514535
}
@@ -851,7 +872,7 @@ func zeroFill(w io.Writer, n int64) error {
851872
}
852873

853874
func (d *Driver) generateSparseBundleDiskImage(count int64) error {
854-
diskPath := d.ResolveStorePath(rootVolumeName)
875+
diskPath := d.ResolveStorePath(rootVolumeName + ".sparsebundle")
855876

856877
if err := hdiutil("create", "-megabytes", fmt.Sprintf("%d", count), "-type", "SPARSEBUNDLE", diskPath); err != nil {
857878
return err
@@ -1029,7 +1050,7 @@ func (d *Driver) nfsExportIdentifier() string {
10291050
}
10301051

10311052
func (d *Driver) GetPid() (int, error) {
1032-
p, err := ioutil.ReadFile(d.ResolveStorePath(d.MachineName + ".pid"))
1053+
p, err := ioutil.ReadFile(d.ResolveStorePath("hyperkit.pid"))
10331054
if err != nil {
10341055
return 0, err
10351056
}
@@ -1068,60 +1089,13 @@ func trimMacAddress(rawUUID string) string {
10681089
return mac
10691090
}
10701091

1071-
func (d *Driver) xhyveArgs() []string {
1072-
iso := d.ResolveStorePath(isoFilename)
1073-
1074-
var diskImage string
1075-
if d.Qcow2 {
1076-
imgPath := fmt.Sprintf("file://%s", filepath.Join(d.ResolveStorePath("."), d.MachineName+".qcow2"))
1077-
diskImage = fmt.Sprintf("4:0,virtio-blk,%s,format=qcow", imgPath)
1078-
} else if d.RawDisk {
1079-
imgPath := fmt.Sprintf("%s", filepath.Join(d.ResolveStorePath("."), d.MachineName+".rawdisk"))
1080-
diskImage = fmt.Sprintf("4:0,virtio-blk,%s", imgPath)
1081-
} else {
1082-
imgPath := fmt.Sprintf("/dev/rdisk%d", d.DiskNumber)
1083-
diskImage = fmt.Sprintf("4:0,ahci-hd,%s", imgPath)
1084-
}
1085-
1086-
vmlinuz := d.ResolveStorePath(d.Vmlinuz)
1087-
initrd := d.ResolveStorePath(d.Initrd)
1088-
1089-
return []string{
1090-
"xhyve",
1091-
"-A",
1092-
"-U", fmt.Sprintf("%s", d.UUID),
1093-
"-c", fmt.Sprintf("%d", d.CPU),
1094-
"-m", fmt.Sprintf("%dM", d.Memory),
1095-
"-l", "com1,autopty",
1096-
"-s", "0:0,hostbridge",
1097-
"-s", "31,lpc",
1098-
"-s", "2:0,virtio-net",
1099-
"-s", fmt.Sprintf("3:0,ahci-cd,%s", iso),
1100-
"-s", diskImage,
1101-
"-f", fmt.Sprintf("kexec,%s,%s,%s", vmlinuz, initrd, d.BootCmd),
1102-
}
1103-
}
1104-
11051092
func (d *Driver) getMACAdress() (string, error) {
1106-
args := append(d.xhyveArgs(), "-M")
1107-
1108-
stdout := bytes.Buffer{}
1109-
1110-
cmd := exec.Command(os.Args[0], args...) // TODO: Should be possible without exec
1111-
log.Debugf("Running command: %s %s", os.Args[0], args)
1112-
1113-
cmd.Stdout = &stdout
1114-
if err := cmd.Run(); err != nil {
1115-
if exitErr, ok := err.(*exec.ExitError); ok {
1116-
log.Debugf("Stderr: %s", exitErr.Stderr)
1117-
}
1093+
m, err := govmnet.GetMACAddressFromUUID(d.UUID)
1094+
if err != nil {
11181095
return "", err
11191096
}
11201097

1121-
mac := bytes.TrimPrefix(stdout.Bytes(), []byte("MAC: "))
1122-
mac = bytes.TrimSpace(mac)
1123-
1124-
hw, err := net.ParseMAC(string(mac))
1098+
hw, err := net.ParseMAC(string(m))
11251099
if err != nil {
11261100
return "", err
11271101
}

0 commit comments

Comments
 (0)