Skip to content

Commit 6dd0a38

Browse files
cfergeaupraveenkumar
authored andcommitted
Implement disk resizing
This makes use of the qcow-tool binary from https://github.com/mirage/ocaml-qcow which provides qcow2 resizing from a commandline tool.
1 parent cbbb9d2 commit 6dd0a38

File tree

5 files changed

+90
-21
lines changed

5 files changed

+90
-21
lines changed

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ module github.com/code-ready/machine-driver-hyperkit
33
go 1.13
44

55
require (
6-
github.com/code-ready/machine v0.0.0-20210211073310-3b9fff90883f
7-
github.com/kr/text v0.2.0 // indirect
6+
github.com/code-ready/machine v0.0.0-20210616065635-eff475d32b9a
87
github.com/mitchellh/go-ps v1.0.0
98
github.com/moby/hyperkit v0.0.0-20171204115932-858492e3d919
109
github.com/pkg/errors v0.9.1
1110
github.com/sirupsen/logrus v1.8.1
1211
github.com/zchee/go-vmnet v0.0.0-20161021174912-97ebf9174097
1312
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 // indirect
14-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
1513
)

go.sum

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
github.com/code-ready/machine v0.0.0-20210211073310-3b9fff90883f h1:/hKaLGcZoofn40gRf1nt9Bs2slEF6tf/+bxSqphP+b0=
2-
github.com/code-ready/machine v0.0.0-20210211073310-3b9fff90883f/go.mod h1:4JgMqho7KuxFifYCIi7cKVmh4yA2l0QHOPftv9f5PNc=
3-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1+
github.com/code-ready/machine v0.0.0-20210616065635-eff475d32b9a h1:LLL1mb5Nm1bghXXK9g0qJTkV8DqlttnyllEgCa92xdI=
2+
github.com/code-ready/machine v0.0.0-20210616065635-eff475d32b9a/go.mod h1:4JgMqho7KuxFifYCIi7cKVmh4yA2l0QHOPftv9f5PNc=
43
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
54
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
65
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7-
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
8-
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
9-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
10-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
11-
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
12-
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
136
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
147
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
158
github.com/moby/hyperkit v0.0.0-20171204115932-858492e3d919 h1:qkxWSt7szDSjHR7qBuGb1e1VHvISWl487mihQF44T2k=
@@ -30,9 +23,8 @@ github.com/zchee/go-vmnet v0.0.0-20161021174912-97ebf9174097/go.mod h1:lFZSWRIpC
3023
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3124
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 h1:C+AwYEtBp/VQwoLntUmQ/yx3MS9vmZaKNdw5eOpoQe8=
3225
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3327
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
34-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
35-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
3628
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3729
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
3830
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/hyperkit/driver.go

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"fmt"
2424
"io/ioutil"
2525
"os"
26+
"os/exec"
2627
"path/filepath"
28+
"regexp"
2729
"strconv"
2830
"strings"
2931
"syscall"
@@ -94,6 +96,10 @@ func (d *Driver) Create() error {
9496
return err
9597
}
9698

99+
if _, err := d.resizeDiskImageIfNeeded(d.DiskCapacity); err != nil {
100+
return err
101+
}
102+
97103
return nil
98104
}
99105

@@ -405,19 +411,90 @@ func (d *Driver) findHyperkitProcess() (ps.Process, error) {
405411
return p, nil
406412
}
407413

414+
func (d *Driver) runQcowTool(args ...string) (string, error) {
415+
log.Debugf("Running %s %s", d.QcowToolPath, strings.Join(args, " "))
416+
out, err := exec.Command(d.QcowToolPath, args...).Output() // #nosec G204
417+
if err != nil {
418+
log.Debugf("qcow-tool error: %v", err)
419+
420+
var exitErr *exec.ExitError
421+
if errors.As(err, &exitErr) {
422+
log.Debugf("stderr: %s", exitErr.Stderr)
423+
}
424+
return "", err
425+
}
426+
427+
return string(out), nil
428+
}
429+
430+
func (d *Driver) resizeDiskImage(newCapacity uint64) error {
431+
log.Debugf("resizeDiskImage(%d)", newCapacity)
432+
433+
_, err := d.runQcowTool("resize", fmt.Sprintf("--size=%d", newCapacity), d.getDiskPath())
434+
if err != nil {
435+
return err
436+
}
437+
438+
d.DiskCapacity = newCapacity
439+
return nil
440+
}
441+
442+
func (d *Driver) checkIfResizeNeeded(newCapacity uint64) (bool, error) {
443+
if newCapacity == 0 {
444+
return false, nil
445+
}
446+
447+
out, err := d.runQcowTool("info", d.getDiskPath())
448+
if err != nil {
449+
return false, err
450+
}
451+
452+
sizeRegexp := regexp.MustCompile(`\(size ([[:digit:]]+)\)`)
453+
submatches := sizeRegexp.FindStringSubmatch(out)
454+
if len(submatches) != 2 {
455+
return false, fmt.Errorf("Could not parse current image size")
456+
}
457+
currentCapacity, err := strconv.ParseUint(submatches[1], 10, 64)
458+
if err != nil {
459+
return false, fmt.Errorf("Could not parse current image size: %w", err)
460+
}
461+
if currentCapacity == newCapacity {
462+
log.Debugf("disk image capacity is already %d bytes", currentCapacity)
463+
return false, nil
464+
}
465+
if currentCapacity > newCapacity {
466+
return false, fmt.Errorf("current disk image capacity is bigger than the requested size (%d > %d)", currentCapacity, newCapacity)
467+
}
468+
469+
return true, nil
470+
}
471+
472+
func (d *Driver) resizeDiskImageIfNeeded(newCapacity uint64) (bool, error) {
473+
resizeNeeded, err := d.checkIfResizeNeeded(newCapacity)
474+
if err != nil || !resizeNeeded {
475+
return false, err
476+
}
477+
err = d.resizeDiskImage(newCapacity)
478+
if err != nil {
479+
return false, err
480+
}
481+
482+
return true, nil
483+
}
484+
408485
func (d *Driver) UpdateConfigRaw(rawConfig []byte) error {
409486
var newDriver Driver
410487
err := json.Unmarshal(rawConfig, &newDriver)
411488
if err != nil {
412489
return err
413490
}
414491

415-
if newDriver.Memory == d.Memory && newDriver.CPU == d.CPU {
416-
/* For now only changing memory and CPU is supported/tested.
417-
* If none of these changed, we might be trying to change another
418-
* value, which is may or may not work, return ErrNotImplemented for now
419-
*/
420-
return drivers.ErrNotImplemented
492+
if newDriver.DiskCapacity != d.DiskCapacity {
493+
_, err = d.resizeDiskImageIfNeeded(newDriver.DiskCapacity)
494+
if err != nil {
495+
log.Debugf("failed to resize disk image: %v", err)
496+
return err
497+
}
421498
}
422499
*d = newDriver
423500

vendor/github.com/code-ready/machine/drivers/hyperkit/driver_darwin.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# github.com/code-ready/machine v0.0.0-20210211073310-3b9fff90883f
1+
# github.com/code-ready/machine v0.0.0-20210616065635-eff475d32b9a
22
github.com/code-ready/machine/drivers/hyperkit
33
github.com/code-ready/machine/libmachine/drivers
44
github.com/code-ready/machine/libmachine/drivers/plugin

0 commit comments

Comments
 (0)