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 <cfergeau@redhat.com>
  • Loading branch information
cfergeau committed Jun 18, 2024
1 parent a4680b5 commit 3def960
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.4-0.20240320091526-a0238e52b61f
github.com/containers/image/v5 v5.31.0
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.1.10 // indirect
github.com/containers/storage v1.54.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 3def960

Please sign in to comment.