Skip to content

Commit

Permalink
Merge pull request #1941 from r2d4/shared-drivers
Browse files Browse the repository at this point in the history
Move hyperkit and none drivers to pkg/drivers, share utils
  • Loading branch information
r2d4 authored and aaron-prindle committed Sep 13, 2017
2 parents 3180e2e + ebbc34d commit 0861725
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 130 deletions.
2 changes: 1 addition & 1 deletion cmd/drivers/hyperkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package main

import (
"github.com/docker/machine/libmachine/drivers/plugin"
"k8s.io/minikube/pkg/minikube/drivers/hyperkit"
"k8s.io/minikube/pkg/drivers/hyperkit"
)

func main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ ConditionVirtualization=microsoft


[Service]
Restart=always
RestartSec=3

Type=simple
ExecStart=/usr/sbin/hv_kvp_daemon -n

Expand Down
125 changes: 125 additions & 0 deletions pkg/drivers/drivers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
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 drivers

import (
"io/ioutil"
"os"
"path/filepath"
"syscall"

"github.com/cloudflare/cfssl/log"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/mcnflag"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/pkg/errors"
)

func GetDiskPath(d *drivers.BaseDriver) string {
return filepath.Join(d.ResolveStorePath("."), d.GetMachineName()+".rawdisk")
}

type CommonDriver struct{}

//Not implemented yet
func (d *CommonDriver) GetCreateFlags() []mcnflag.Flag {
return nil
}

//Not implemented yet
func (d *CommonDriver) SetConfigFromFlags(flags drivers.DriverOptions) error {
return nil
}

func createRawDiskImage(sshKeyPath, diskPath string, diskSizeMb int) error {
tarBuf, err := mcnutils.MakeDiskImage(sshKeyPath)
if err != nil {
return err
}

file, err := os.OpenFile(diskPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
file.Seek(0, os.SEEK_SET)

if _, err := file.Write(tarBuf.Bytes()); err != nil {
return err
}
if err := file.Close(); err != nil {
return errors.Wrapf(err, "closing file %s", diskPath)
}

if err := os.Truncate(diskPath, int64(diskSizeMb*1000000)); err != nil {
return err
}
return nil
}

func publicSSHKeyPath(d *drivers.BaseDriver) string {
return d.GetSSHKeyPath() + ".pub"
}

// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
func Restart(d drivers.Driver) error {
for _, f := range []func() error{d.Stop, d.Start} {
if err := f(); err != nil {
return err
}
}
return nil
}

func MakeDiskImage(d *drivers.BaseDriver, boot2dockerURL string, diskSize int) error {
//TODO(r2d4): rewrite this, not using b2dutils
b2dutils := mcnutils.NewB2dUtils(d.StorePath)
if err := b2dutils.CopyIsoToMachineDir(boot2dockerURL, d.MachineName); err != nil {
return errors.Wrap(err, "Error copying ISO to machine dir")
}

log.Info("Creating ssh key...")
if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
}

log.Info("Creating raw disk image...")
diskPath := GetDiskPath(d)
if _, err := os.Stat(diskPath); os.IsNotExist(err) {
if err := createRawDiskImage(publicSSHKeyPath(d), diskPath, diskSize); err != nil {
return err
}
if err := fixPermissions(d.ResolveStorePath(".")); err != nil {
return err
}
}
return nil
}

func fixPermissions(path string) error {
os.Chown(path, syscall.Getuid(), syscall.Getegid())
files, _ := ioutil.ReadDir(path)
for _, f := range files {
fp := filepath.Join(path, f.Name())
if err := os.Chown(fp, syscall.Getuid(), syscall.Getegid()); err != nil {
return err
}
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package hyperkit
package drivers

import (
"io/ioutil"
Expand All @@ -35,7 +35,7 @@ func Test_createDiskImage(t *testing.T) {

sizeInMb := 100
sizeInBytes := int64(sizeInMb) * 1000000
if err := createDiskImage(sshPath, diskPath, sizeInMb); err != nil {
if err := createRawDiskImage(sshPath, diskPath, sizeInMb); err != nil {
t.Errorf("createDiskImage() error = %v", err)
}
fi, err := os.Lstat(diskPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ import (

"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"
hyperkit "github.com/moby/hyperkit/go"
"github.com/pborman/uuid"
"github.com/pkg/errors"
vmnet "github.com/zchee/go-vmnet"
pkgdrivers "k8s.io/minikube/pkg/drivers"
commonutil "k8s.io/minikube/pkg/util"
)

Expand All @@ -46,6 +45,7 @@ const (

type Driver struct {
*drivers.BaseDriver
*pkgdrivers.CommonDriver
Boot2DockerURL string
DiskSize int
CPU int
Expand All @@ -58,19 +58,16 @@ func NewDriver(hostName, storePath string) *Driver {
BaseDriver: &drivers.BaseDriver{
SSHUser: "docker",
},
CommonDriver: &pkgdrivers.CommonDriver{},
}
}

func (d *Driver) Create() error {
b2dutils := mcnutils.NewB2dUtils(d.StorePath)

if err := ssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil {
return err
// TODO: handle different disk types.
if err := pkgdrivers.MakeDiskImage(d.BaseDriver, d.Boot2DockerURL, d.DiskSize); err != nil {
return errors.Wrap(err, "making disk image")
}

if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, d.MachineName); err != nil {
return err
}
isoPath := d.ResolveStorePath(isoFilename)
if err := d.extractKernel(isoPath); err != nil {
return err
Expand All @@ -84,12 +81,6 @@ func (d *Driver) DriverName() string {
return "hyperkit"
}

// GetCreateFlags returns the mcnflag.Flag slice representing the flags
// that can be set, their descriptions and defaults.
func (d *Driver) GetCreateFlags() []mcnflag.Flag {
return nil
}

// GetSSHHostname returns hostname for use with ssh
func (d *Driver) GetSSHHostname() (string, error) {
return d.IPAddress, nil
Expand Down Expand Up @@ -131,11 +122,6 @@ func (d *Driver) Kill() error {
return d.sendSignal(syscall.SIGKILL)
}

// PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation
func (d *Driver) PreCreateCheck() error {
return nil
}

// Remove a host
func (d *Driver) Remove() error {
s, err := d.GetState()
Expand All @@ -150,37 +136,12 @@ func (d *Driver) Remove() error {
return nil
}

// Restart a host. This may just call Stop(); Start() if the provider does not
// have any special restart behaviour.
func (d *Driver) Restart() error {
for _, f := range []func() error{d.Stop, d.Start} {
if err := f(); err != nil {
return err
}
}
return nil
}

// SetConfigFromFlags configures the driver with the object that was returned
// by RegisterCreateFlags
func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
return nil
return pkgdrivers.Restart(d)
}

// Start a host
func (d *Driver) Start() error {

// TODO: handle different disk types.
diskPath := filepath.Join(d.ResolveStorePath("."), d.MachineName+".rawdisk")
if _, err := os.Stat(diskPath); os.IsNotExist(err) {
if err := createDiskImage(d.publicSSHKeyPath(), diskPath, d.DiskSize); err != nil {
return err
}
if err := fixPermissions(d.ResolveStorePath(".")); err != nil {
return err
}
}

h, err := hyperkit.New("", "", filepath.Join(d.StorePath, "machines", d.MachineName))
if err != nil {
return err
Expand All @@ -206,10 +167,9 @@ func (d *Driver) Start() error {
// Need to strip 0's
mac = trimMacAddress(mac)
log.Infof("Generated MAC %s", mac)

h.Disks = []hyperkit.DiskConfig{
{
Path: diskPath,
Path: pkgdrivers.GetDiskPath(d.BaseDriver),
Size: d.DiskSize,
Driver: "virtio-blk",
},
Expand Down Expand Up @@ -256,10 +216,6 @@ func (d *Driver) extractKernel(isoPath string) error {
return nil
}

func (d *Driver) publicSSHKeyPath() string {
return d.GetSSHKeyPath() + ".pub"
}

func (d *Driver) sendSignal(s os.Signal) error {
pid := d.getPid()
proc, err := os.FindProcess(pid)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 0861725

Please sign in to comment.