diff --git a/go.mod b/go.mod index 686630ee22..96284c2512 100644 --- a/go.mod +++ b/go.mod @@ -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.1 github.com/containers/libhvee v0.7.1 @@ -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 diff --git a/pkg/drivers/libhvee/libhvee_windows.go b/pkg/drivers/libhvee/libhvee_windows.go index 1ef024e650..7028c819a6 100644 --- a/pkg/drivers/libhvee/libhvee_windows.go +++ b/pkg/drivers/libhvee/libhvee_windows.go @@ -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 { @@ -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)) } diff --git a/pkg/drivers/libhvee/powershell_windows.go b/pkg/drivers/libhvee/powershell_windows.go index 7741ea1b54..6c8f5c7ab3 100644 --- a/pkg/drivers/libhvee/powershell_windows.go +++ b/pkg/drivers/libhvee/powershell_windows.go @@ -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