|
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | 7 | "os/exec" |
| 8 | + "strconv" |
| 9 | + "strings" |
8 | 10 | "time" |
9 | 11 |
|
10 | 12 | log "github.com/crc-org/crc/pkg/crc/logging" |
@@ -71,9 +73,7 @@ func (d *Driver) UpdateConfigRaw(rawConfig []byte) error { |
71 | 73 | } |
72 | 74 | } |
73 | 75 | 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 { |
77 | 77 | log.Warnf("Failed to set disk size to %d", newDriver.DiskCapacity) |
78 | 78 | return err |
79 | 79 | } |
@@ -145,6 +145,32 @@ func (d *Driver) getDiskPath() string { |
145 | 145 | return d.ResolveStorePath(fmt.Sprintf("%s.%s", d.MachineName, d.ImageFormat)) |
146 | 146 | } |
147 | 147 |
|
| 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 | + |
148 | 174 | func (d *Driver) Create() error { |
149 | 175 | if err := crcos.CopyFile(d.ImageSourcePath, d.getDiskPath()); err != nil { |
150 | 176 | return err |
@@ -210,9 +236,14 @@ func (d *Driver) Create() error { |
210 | 236 | return err |
211 | 237 | } |
212 | 238 |
|
213 | | - return cmd("Hyper-V\\Add-VMHardDiskDrive", |
| 239 | + if err := cmd("Hyper-V\\Add-VMHardDiskDrive", |
214 | 240 | "-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 | + |
216 | 247 | } |
217 | 248 |
|
218 | 249 | func (d *Driver) chooseVirtualSwitch() (string, error) { |
|
0 commit comments