From 1f6df1f3514a21c7f395af56fdb04a60b78cc6e4 Mon Sep 17 00:00:00 2001 From: Michael Ryan Dempsey Date: Wed, 5 Aug 2020 01:53:02 -0700 Subject: [PATCH 1/2] use MiB for raw disk allocations --- pkg/drivers/common.go | 9 ++++++++- pkg/drivers/common_test.go | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/drivers/common.go b/pkg/drivers/common.go index 2fde2ef..c62484d 100644 --- a/pkg/drivers/common.go +++ b/pkg/drivers/common.go @@ -23,6 +23,7 @@ import ( "path/filepath" "syscall" + "github.com/docker/go-units" "github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/mcnflag" "github.com/docker/machine/libmachine/mcnutils" @@ -52,6 +53,12 @@ func (d *CommonDriver) SetConfigFromFlags(flags drivers.DriverOptions) error { return nil } +func diskSizeInMiB(diskSizeMB int) int64 { + result := int64(diskSizeMB) + result *= units.MiB + return result +} + func createRawDiskImage(sshKeyPath, diskPath string, diskSizeMb int) error { tarBuf, err := mcnutils.MakeDiskImage(sshKeyPath) if err != nil { @@ -74,7 +81,7 @@ func createRawDiskImage(sshKeyPath, diskPath string, diskSizeMb int) error { return errors.Wrapf(err, "closing file %s", diskPath) } - if err := os.Truncate(diskPath, int64(diskSizeMb*1000000)); err != nil { + if err := os.Truncate(diskPath, diskSizeInMiB(diskSizeMb)); err != nil { return errors.Wrap(err, "truncate") } return nil diff --git a/pkg/drivers/common_test.go b/pkg/drivers/common_test.go index cc250e0..14cc631 100644 --- a/pkg/drivers/common_test.go +++ b/pkg/drivers/common_test.go @@ -36,7 +36,7 @@ func Test_createDiskImage(t *testing.T) { diskPath := filepath.Join(tmpdir, "disk") sizeInMb := 100 - sizeInBytes := int64(sizeInMb) * 1000000 + sizeInBytes := int64(104857600) if err := createRawDiskImage(sshPath, diskPath, sizeInMb); err != nil { t.Errorf("createDiskImage() error = %v", err) } From 37cad57a4494213680a38536fc2099268ad0197e Mon Sep 17 00:00:00 2001 From: Michael Ryan Dempsey Date: Wed, 12 Aug 2020 10:57:23 -0700 Subject: [PATCH 2/2] refactor conversion functions into util --- pkg/drivers/common.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pkg/drivers/common.go b/pkg/drivers/common.go index c62484d..8c5c98b 100644 --- a/pkg/drivers/common.go +++ b/pkg/drivers/common.go @@ -23,13 +23,13 @@ import ( "path/filepath" "syscall" - "github.com/docker/go-units" "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/golang/glog" "github.com/pkg/errors" + "k8s.io/minikube/pkg/util" ) // This file is for common code shared among internal machine drivers @@ -53,12 +53,6 @@ func (d *CommonDriver) SetConfigFromFlags(flags drivers.DriverOptions) error { return nil } -func diskSizeInMiB(diskSizeMB int) int64 { - result := int64(diskSizeMB) - result *= units.MiB - return result -} - func createRawDiskImage(sshKeyPath, diskPath string, diskSizeMb int) error { tarBuf, err := mcnutils.MakeDiskImage(sshKeyPath) if err != nil { @@ -81,7 +75,7 @@ func createRawDiskImage(sshKeyPath, diskPath string, diskSizeMb int) error { return errors.Wrapf(err, "closing file %s", diskPath) } - if err := os.Truncate(diskPath, diskSizeInMiB(diskSizeMb)); err != nil { + if err := os.Truncate(diskPath, util.ConvertMBToBytes(diskSizeMb)); err != nil { return errors.Wrap(err, "truncate") } return nil