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 Apr 24, 2024
1 parent b314da6 commit a9c936a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 26 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.58.1
github.com/containers/gvisor-tap-vsock v0.7.4-0.20240320091526-a0238e52b61f
github.com/containers/image/v5 v5.30.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.57.1-0.20240205132223-de5cb00e891c // indirect
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/ocicrypt v1.1.9 // indirect
github.com/containers/storage v1.53.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/cheggaaa/pb/v3 v3.1.5/go.mod h1:CrxkeghYTXi1lQBEI7jSn+3svI3cuc19haAj6
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
github.com/containers/common v0.57.1-0.20240205132223-de5cb00e891c h1:Xzo9t4eIalkeilcmYTz0YEgL7hMrGQ12GK6UlSHrEsU=
github.com/containers/common v0.57.1-0.20240205132223-de5cb00e891c/go.mod h1:s1gEyucR3ryIex1aDMo1KzbfpvRl0CaGER6s5jqXRkI=
github.com/containers/common v0.58.1 h1:E1DN9Lr7kgMVQy7AXLv1CYQCiqnweklMiYWbf0KOnqY=
github.com/containers/common v0.58.1/go.mod h1:l3vMqanJGj7tZ3W/i76gEJ128VXgFUO1tLaohJXPvdk=
github.com/containers/gvisor-tap-vsock v0.7.4-0.20240320091526-a0238e52b61f h1:NOq4UwN3M4rvN44CPznCqQlOvim7Ja1RZ082ORAJjVQ=
github.com/containers/gvisor-tap-vsock v0.7.4-0.20240320091526-a0238e52b61f/go.mod h1:hZrvqbYhTIUQCREov+M8u7sMhzGbB6umiDuVpnwtJcI=
github.com/containers/image/v5 v5.30.0 h1:CmHeSwI6W2kTRWnUsxATDFY5TEX4b58gPkaQcEyrLIA=
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
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ github.com/cloudflare/circl/math/mlsbset
github.com/cloudflare/circl/sign
github.com/cloudflare/circl/sign/ed25519
github.com/cloudflare/circl/sign/ed448
# github.com/containers/common v0.57.1-0.20240205132223-de5cb00e891c
# github.com/containers/common v0.58.1
## explicit; go 1.20
github.com/containers/common/pkg/strongunits
# github.com/containers/gvisor-tap-vsock v0.7.4-0.20240320091526-a0238e52b61f
Expand Down

0 comments on commit a9c936a

Please sign in to comment.