Skip to content

Commit

Permalink
Move drivers to pkg/drivers, share utils
Browse files Browse the repository at this point in the history
Share most of the disk image setup between hyperkit and kvm drivers.
Move and remove a lot of shared configuration between all the in-tree
drivers: kvm, hyperkit, none.
  • Loading branch information
r2d4 committed Sep 10, 2017
1 parent d6049c1 commit 703bf81
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 305 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
110 changes: 110 additions & 0 deletions pkg/drivers/drivers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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 GetDiskFilename(machineName string) string {
return machineName + ".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
}
file.Close()

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, shouldFixPermissions bool) (string, 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 := filepath.Join(d.ResolveStorePath("."), GetDiskFilename(d.MachineName))
if _, err := os.Stat(diskPath); os.IsNotExist(err) {
if err := createRawDiskImage(publicSSHKeyPath(d), diskPath, diskSize); err != nil {
return "", err
}
if shouldFixPermissions {
if err := fixPermissions(d.ResolveStorePath(".")); err != nil {
return "", err
}
}
}
return diskPath, 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())
log.Debugf(fp)
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,11 @@ 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
}

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 +76,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 +117,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 +131,18 @@ 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
}
diskPath, err := pkgdrivers.MakeDiskImage(d.BaseDriver, d.Boot2DockerURL, d.DiskSize, true)
if err != nil {
return errors.Wrap(err, "making disk image")
}

h, err := hyperkit.New("", "", filepath.Join(d.StorePath, "machines", d.MachineName))
if err != nil {
return err
Expand Down Expand Up @@ -256,10 +218,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.
58 changes: 7 additions & 51 deletions pkg/drivers/kvm/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ 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/state"
libvirt "github.com/libvirt/libvirt-go"
"github.com/pkg/errors"
pkgdrivers "k8s.io/minikube/pkg/drivers"
)

const (
Expand All @@ -42,6 +41,7 @@ const (

type Driver struct {
*drivers.BaseDriver
*pkgdrivers.CommonDriver

// How much memory, in MB, to allocate to the VM
Memory int
Expand Down Expand Up @@ -73,28 +73,20 @@ func NewDriver(hostName, storePath string) *Driver {
BaseDriver: &drivers.BaseDriver{
MachineName: hostName,
StorePath: storePath,
SSHUser: "docker",
},
CommonDriver: &pkgdrivers.CommonDriver{},
Boot2DockerURL: constants.DefaultIsoUrl,
CPU: constants.DefaultCPUS,
PrivateNetwork: defaultPrivateNetworkName,
DiskSize: util.CalculateDiskSizeInMB(constants.DefaultDiskSize),
Memory: constants.DefaultMemory,
PrivateNetwork: defaultPrivateNetworkName,
Network: defaultNetworkName,
DiskPath: filepath.Join(constants.GetMinipath(), "machines", config.GetMachineName(), fmt.Sprintf("%s.img", config.GetMachineName())),
ISO: filepath.Join(constants.GetMinipath(), "machines", config.GetMachineName(), "boot2docker.iso"),
}
}

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

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

func (d *Driver) PreCommandCheck() error {
conn, err := getConnection()
if err != nil {
Expand Down Expand Up @@ -171,30 +163,10 @@ func (d *Driver) GetIP() (string, error) {
return ip, nil
}

func (d *Driver) GetMachineName() string {
return d.MachineName
}

func (d *Driver) GetSSHHostname() (string, error) {
return d.GetIP()
}

func (d *Driver) GetSSHUsername() string {
return "docker"
}

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) DriverName() string {
return "kvm"
}
Expand All @@ -210,16 +182,7 @@ func (d *Driver) Kill() error {
}

func (d *Driver) Restart() error {
dom, conn, err := d.getDomain()
if err != nil {
return errors.Wrap(err, "getting connection")
}
defer closeDomain(dom, conn)

if err := d.Stop(); err != nil {
return errors.Wrap(err, "stopping VM:")
}
return d.Start()
return pkgdrivers.Restart(d)
}

func (d *Driver) Start() error {
Expand Down Expand Up @@ -269,13 +232,6 @@ func (d *Driver) Start() error {

func (d *Driver) Create() error {
log.Info("Creating machine...")

//TODO(r2d4): rewrite this, not using b2dutils
b2dutils := mcnutils.NewB2dUtils(d.StorePath)
if err := b2dutils.CopyIsoToMachineDir(d.Boot2DockerURL, d.MachineName); err != nil {
return errors.Wrap(err, "Error copying ISO to machine dir")
}

log.Info("Creating network...")
err := d.createNetwork()
if err != nil {
Expand All @@ -301,7 +257,7 @@ func (d *Driver) Create() error {
}

log.Info("Building disk image...")
err = d.buildDiskImage()
d.DiskPath, err = pkgdrivers.MakeDiskImage(d.BaseDriver, d.Boot2DockerURL, d.DiskSize, false)
if err != nil {
return errors.Wrap(err, "Error creating disk")
}
Expand Down
Loading

0 comments on commit 703bf81

Please sign in to comment.