Skip to content

Commit

Permalink
libhvee: Use libhvee APIs for disk resize
Browse files Browse the repository at this point in the history
Disk handling API was added to libhvee since the first iteration of the
libhvee support, we can now make use of it.

Signed-off-by: Christophe Fergeau <[email protected]>
  • Loading branch information
cfergeau authored and praveenkumar committed Sep 23, 2024
1 parent e5e2fde commit d6c6e90
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/cavaliergopher/grab/v3 v3.0.1
github.com/cheggaaa/pb/v3 v3.1.5
github.com/containers/common v0.59.1
github.com/containers/gvisor-tap-vsock v0.7.5
github.com/containers/image/v5 v5.32.2
github.com/containers/libhvee v0.7.1
Expand Down Expand Up @@ -77,7 +78,6 @@ require (
github.com/areYouLazy/libhosty v1.1.0 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/containers/common v0.59.1 // indirect
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/ocicrypt v1.2.0 // indirect
github.com/containers/storage v1.55.0 // indirect
Expand Down
31 changes: 13 additions & 18 deletions pkg/drivers/libhvee/libhvee_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"

"github.com/containers/libhvee/pkg/hypervctl"
log "github.com/crc-org/crc/v2/pkg/crc/logging"
crcos "github.com/crc-org/crc/v2/pkg/os"
"github.com/crc-org/machine/libmachine/drivers"
"github.com/crc-org/machine/libmachine/state"

"github.com/containers/common/pkg/strongunits"
"github.com/containers/libhvee/pkg/hypervctl"
)

type Driver struct {
Expand Down Expand Up @@ -283,28 +283,23 @@ func (d *Driver) getDiskPath() string {
return d.ResolveStorePath(fmt.Sprintf("%s.%s", d.MachineName, d.ImageFormat))
}

func (d *Driver) resizeDisk(newSize int64) error {
func (d *Driver) resizeDisk(newSizeBytes int64) error {

newSize := strongunits.B(newSizeBytes)

diskPath := d.getDiskPath()
out, err := cmdOut(fmt.Sprintf("@(Get-VHD -Path %s).Size", quote(diskPath)))
currentSize, err := hypervctl.GetDiskSize(diskPath)
if err != nil {
return fmt.Errorf("unable to get current size of crc.vhdx: %w", err)
}
currentSize, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
if err != nil {
return fmt.Errorf("unable to convert disk size to int: %w", err)
}
if newSize == currentSize {
if newSize == currentSize.ToBytes() {
log.Debugf("%s is already %d bytes", diskPath, newSize)
return nil
}
if newSize < currentSize {
return fmt.Errorf("current disk image capacity is bigger than the requested size (%d > %d)", currentSize, newSize)
if newSize < currentSize.ToBytes() {
return fmt.Errorf("current disk image capacity is bigger than the requested size (%d > %d)", currentSize.ToBytes(), newSize)
}

log.Debugf("Resizing disk from %d bytes to %d bytes", currentSize, newSize)
return cmd("Hyper-V\\Resize-VHD",
"-Path",
quote(diskPath),
"-SizeBytes",
fmt.Sprintf("%d", newSize))
log.Debugf("Resizing disk from %d bytes to %d bytes", currentSize.ToBytes(), newSize.ToBytes())
return hypervctl.ResizeDisk(diskPath, strongunits.ToGiB(newSize))
}
4 changes: 0 additions & 4 deletions pkg/drivers/libhvee/powershell_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ func isWindowsAdministrator() (bool, error) {
return resp == "True", nil
}

func quote(text string) string {
return fmt.Sprintf("'%s'", text)
}

func smbShareExists(name string) bool {
if err := cmd(fmt.Sprintf("Get-SmbShare -Name %s", name)); err != nil {
return false
Expand Down

0 comments on commit d6c6e90

Please sign in to comment.