From c74201c4a718a9d7458e68b1da1ee5de27cc8ad9 Mon Sep 17 00:00:00 2001 From: Anjan Nath Date: Mon, 13 Feb 2023 13:47:04 +0530 Subject: [PATCH] Fixes #3496 resize disk also during creation of the VM when using hyper-v driver in case of hyper-v driver the config value of disk-size was not applied to the VM disk image during creation of the instance this adds a new private helper resizeDisk and calls it during creation as well as vm config Update during start --- pkg/drivers/hyperv/hyperv_windows.go | 41 ++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/pkg/drivers/hyperv/hyperv_windows.go b/pkg/drivers/hyperv/hyperv_windows.go index afe2319426..3d3ebc1128 100644 --- a/pkg/drivers/hyperv/hyperv_windows.go +++ b/pkg/drivers/hyperv/hyperv_windows.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "os/exec" + "strconv" + "strings" "time" log "github.com/crc-org/crc/pkg/crc/logging" @@ -71,9 +73,7 @@ func (d *Driver) UpdateConfigRaw(rawConfig []byte) error { } } if newDriver.DiskCapacity != d.DiskCapacity { - log.Debugf("Resizing disk from %d bytes to %d bytes", d.DiskCapacity, newDriver.DiskCapacity) - err := cmd("Hyper-V\\Resize-VHD", "-Path", quote(d.getDiskPath()), "-SizeBytes", fmt.Sprintf("%d", newDriver.DiskCapacity)) - if err != nil { + if err := d.resizeDisk(int64(newDriver.DiskCapacity)); err != nil { log.Warnf("Failed to set disk size to %d", newDriver.DiskCapacity) return err } @@ -145,6 +145,32 @@ func (d *Driver) getDiskPath() string { return d.ResolveStorePath(fmt.Sprintf("%s.%s", d.MachineName, d.ImageFormat)) } +func (d *Driver) resizeDisk(newSize int64) error { + diskPath := d.getDiskPath() + out, err := cmdOut(fmt.Sprintf("@(Get-VHD -Path %s).Size", quote(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 { + 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) + } + + 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)) +} + func (d *Driver) Create() error { if err := crcos.CopyFile(d.ImageSourcePath, d.getDiskPath()); err != nil { return err @@ -210,9 +236,14 @@ func (d *Driver) Create() error { return err } - return cmd("Hyper-V\\Add-VMHardDiskDrive", + if err := cmd("Hyper-V\\Add-VMHardDiskDrive", "-VMName", d.MachineName, - "-Path", quote(d.getDiskPath())) + "-Path", quote(d.getDiskPath())); err != nil { + return err + } + + return d.resizeDisk(int64(d.DiskCapacity)) + } func (d *Driver) chooseVirtualSwitch() (string, error) {