@@ -16,10 +16,14 @@ import (
16
16
"text/template"
17
17
"time"
18
18
19
+ "github.com/docker/go-units"
20
+ "github.com/lima-vm/go-qcow2reader"
19
21
"github.com/lima-vm/lima/pkg/driver"
20
22
"github.com/lima-vm/lima/pkg/driverutil"
21
23
"github.com/lima-vm/lima/pkg/executil"
24
+ "github.com/lima-vm/lima/pkg/nativeimgutil"
22
25
"github.com/lima-vm/lima/pkg/osutil"
26
+ "github.com/lima-vm/lima/pkg/qemu/imgutil"
23
27
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
24
28
"github.com/mattn/go-isatty"
25
29
@@ -107,6 +111,12 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
107
111
if err := limaDriver .CreateDisk (ctx ); err != nil {
108
112
return nil , err
109
113
}
114
+
115
+ // Ensure diffDisk size matches the store
116
+ if err := prepareDiffDisk (inst ); err != nil {
117
+ return nil , err
118
+ }
119
+
110
120
nerdctlArchiveCache , err := ensureNerdctlArchiveCache (ctx , inst .Config , created )
111
121
if err != nil {
112
122
return nil , err
@@ -377,3 +387,45 @@ func ShowMessage(inst *store.Instance) error {
377
387
}
378
388
return scanner .Err ()
379
389
}
390
+
391
+ // prepareDiffDisk checks the disk size difference between inst.Disk and yaml.Disk.
392
+ func prepareDiffDisk (inst * store.Instance ) error {
393
+ diffDisk := filepath .Join (inst .Dir , filenames .DiffDisk )
394
+
395
+ f , err := os .Open (diffDisk )
396
+ if err != nil {
397
+ return err
398
+ }
399
+ defer f .Close ()
400
+
401
+ img , err := qcow2reader .Open (f )
402
+ if err != nil {
403
+ return err
404
+ }
405
+
406
+ diskSize := img .Size ()
407
+ format := string (img .Type ())
408
+
409
+ if inst .Disk == diskSize {
410
+ return nil
411
+ }
412
+
413
+ logrus .Infof ("Resize instance %s's disk from %s to %s" , inst .Name , units .BytesSize (float64 (diskSize )), units .BytesSize (float64 (inst .Disk )))
414
+
415
+ if inst .Disk < diskSize {
416
+ inst .Disk = diskSize
417
+ return errors .New ("diffDisk: Shrinking is currently unavailable" )
418
+ }
419
+
420
+ if format == "raw" {
421
+ err = nativeimgutil .ResizeRawDisk (diffDisk , int (inst .Disk ))
422
+ } else {
423
+ err = imgutil .ResizeDisk (diffDisk , format , int (inst .Disk ))
424
+ }
425
+
426
+ if err != nil {
427
+ return err
428
+ }
429
+
430
+ return err
431
+ }
0 commit comments