Skip to content

Commit c74201c

Browse files
anjannathpraveenkumar
authored andcommitted
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
1 parent 868d96c commit c74201c

1 file changed

Lines changed: 36 additions & 5 deletions

File tree

pkg/drivers/hyperv/hyperv_windows.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"fmt"
77
"os/exec"
8+
"strconv"
9+
"strings"
810
"time"
911

1012
log "github.com/crc-org/crc/pkg/crc/logging"
@@ -71,9 +73,7 @@ func (d *Driver) UpdateConfigRaw(rawConfig []byte) error {
7173
}
7274
}
7375
if newDriver.DiskCapacity != d.DiskCapacity {
74-
log.Debugf("Resizing disk from %d bytes to %d bytes", d.DiskCapacity, newDriver.DiskCapacity)
75-
err := cmd("Hyper-V\\Resize-VHD", "-Path", quote(d.getDiskPath()), "-SizeBytes", fmt.Sprintf("%d", newDriver.DiskCapacity))
76-
if err != nil {
76+
if err := d.resizeDisk(int64(newDriver.DiskCapacity)); err != nil {
7777
log.Warnf("Failed to set disk size to %d", newDriver.DiskCapacity)
7878
return err
7979
}
@@ -145,6 +145,32 @@ func (d *Driver) getDiskPath() string {
145145
return d.ResolveStorePath(fmt.Sprintf("%s.%s", d.MachineName, d.ImageFormat))
146146
}
147147

148+
func (d *Driver) resizeDisk(newSize int64) error {
149+
diskPath := d.getDiskPath()
150+
out, err := cmdOut(fmt.Sprintf("@(Get-VHD -Path %s).Size", quote(diskPath)))
151+
if err != nil {
152+
return fmt.Errorf("unable to get current size of crc.vhdx: %w", err)
153+
}
154+
currentSize, err := strconv.ParseInt(strings.TrimSpace(out), 10, 64)
155+
if err != nil {
156+
return fmt.Errorf("unable to convert disk size to int: %w", err)
157+
}
158+
if newSize == currentSize {
159+
log.Debugf("%s is already %d bytes", diskPath, newSize)
160+
return nil
161+
}
162+
if newSize < currentSize {
163+
return fmt.Errorf("current disk image capacity is bigger than the requested size (%d > %d)", currentSize, newSize)
164+
}
165+
166+
log.Debugf("Resizing disk from %d bytes to %d bytes", currentSize, newSize)
167+
return cmd("Hyper-V\\Resize-VHD",
168+
"-Path",
169+
quote(diskPath),
170+
"-SizeBytes",
171+
fmt.Sprintf("%d", newSize))
172+
}
173+
148174
func (d *Driver) Create() error {
149175
if err := crcos.CopyFile(d.ImageSourcePath, d.getDiskPath()); err != nil {
150176
return err
@@ -210,9 +236,14 @@ func (d *Driver) Create() error {
210236
return err
211237
}
212238

213-
return cmd("Hyper-V\\Add-VMHardDiskDrive",
239+
if err := cmd("Hyper-V\\Add-VMHardDiskDrive",
214240
"-VMName", d.MachineName,
215-
"-Path", quote(d.getDiskPath()))
241+
"-Path", quote(d.getDiskPath())); err != nil {
242+
return err
243+
}
244+
245+
return d.resizeDisk(int64(d.DiskCapacity))
246+
216247
}
217248

218249
func (d *Driver) chooseVirtualSwitch() (string, error) {

0 commit comments

Comments
 (0)