Skip to content

Commit

Permalink
Fixes #3496 resize disk also during creation of the VM when using hyp…
Browse files Browse the repository at this point in the history
…er-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
  • Loading branch information
anjannath authored and praveenkumar committed Feb 14, 2023
1 parent 868d96c commit c74201c
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions pkg/drivers/hyperv/hyperv_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"
"os/exec"
"strconv"
"strings"
"time"

log "github.com/crc-org/crc/pkg/crc/logging"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c74201c

Please sign in to comment.