Skip to content

Commit 1ad4cc8

Browse files
authored
Merge pull request #3437 from songponssw/feat-edit-disk
Fix: resize GuestOS disk
2 parents 9d17a5d + f87c880 commit 1ad4cc8

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

cmd/limactl/edit.go

+8
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func editAction(cmd *cobra.Command, args []string) error {
133133
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
134134
return err
135135
}
136+
136137
if inst != nil {
137138
logrus.Infof("Instance %q configuration edited", inst.Name)
138139
}
@@ -157,6 +158,13 @@ func editAction(cmd *cobra.Command, args []string) error {
157158
if err != nil {
158159
return err
159160
}
161+
162+
// store.Inspect() syncs values between inst.YAML and the store.
163+
// This call applies the validated template to the store.
164+
inst, err = store.Inspect(inst.Name)
165+
if err != nil {
166+
return err
167+
}
160168
return instance.Start(ctx, inst, "", false)
161169
}
162170

pkg/instance/start.go

+62
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ import (
1616
"text/template"
1717
"time"
1818

19+
"github.com/docker/go-units"
20+
"github.com/lima-vm/go-qcow2reader"
1921
"github.com/lima-vm/lima/pkg/driver"
2022
"github.com/lima-vm/lima/pkg/driverutil"
2123
"github.com/lima-vm/lima/pkg/executil"
24+
"github.com/lima-vm/lima/pkg/nativeimgutil"
2225
"github.com/lima-vm/lima/pkg/osutil"
26+
"github.com/lima-vm/lima/pkg/qemu/imgutil"
2327
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
2428
"github.com/mattn/go-isatty"
2529

@@ -107,6 +111,12 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
107111
if err := limaDriver.CreateDisk(ctx); err != nil {
108112
return nil, err
109113
}
114+
115+
// Ensure diffDisk size matches the store
116+
if err := prepareDiffDisk(inst); err != nil {
117+
return nil, err
118+
}
119+
110120
nerdctlArchiveCache, err := ensureNerdctlArchiveCache(ctx, inst.Config, created)
111121
if err != nil {
112122
return nil, err
@@ -377,3 +387,55 @@ func ShowMessage(inst *store.Instance) error {
377387
}
378388
return scanner.Err()
379389
}
390+
391+
// prepareDiffDisk checks the disk size difference between inst.Disk and yaml.Disk.
392+
// If there is no diffDisk, return nil (the instance has not been initialized or started yet).
393+
func prepareDiffDisk(inst *store.Instance) error {
394+
diffDisk := filepath.Join(inst.Dir, filenames.DiffDisk)
395+
396+
// Handle the instance initialization
397+
_, err := os.Stat(diffDisk)
398+
if err != nil {
399+
if os.IsNotExist(err) {
400+
return nil
401+
}
402+
return err
403+
}
404+
405+
f, err := os.Open(diffDisk)
406+
if err != nil {
407+
return err
408+
}
409+
defer f.Close()
410+
411+
img, err := qcow2reader.Open(f)
412+
if err != nil {
413+
return err
414+
}
415+
416+
diskSize := img.Size()
417+
format := string(img.Type())
418+
419+
if inst.Disk == diskSize {
420+
return nil
421+
}
422+
423+
logrus.Infof("Resize instance %s's disk from %s to %s", inst.Name, units.BytesSize(float64(diskSize)), units.BytesSize(float64(inst.Disk)))
424+
425+
if inst.Disk < diskSize {
426+
inst.Disk = diskSize
427+
return errors.New("diffDisk: Shrinking is currently unavailable")
428+
}
429+
430+
if format == "raw" {
431+
err = nativeimgutil.ResizeRawDisk(diffDisk, int(inst.Disk))
432+
} else {
433+
err = imgutil.ResizeDisk(diffDisk, format, int(inst.Disk))
434+
}
435+
436+
if err != nil {
437+
return err
438+
}
439+
440+
return nil
441+
}

0 commit comments

Comments
 (0)