Skip to content

Fix: resize GuestOS disk #3437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/limactl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func editAction(cmd *cobra.Command, args []string) error {
if err := os.WriteFile(filePath, yBytes, 0o644); err != nil {
return err
}

if inst != nil {
logrus.Infof("Instance %q configuration edited", inst.Name)
}
Expand All @@ -157,6 +158,13 @@ func editAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

// store.Inspect() syncs values between inst.YAML and the store.
// This call applies the validated template to the store.
inst, err = store.Inspect(inst.Name)
if err != nil {
return err
}
return instance.Start(ctx, inst, "", false)
}

Expand Down
62 changes: 62 additions & 0 deletions pkg/instance/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ import (
"text/template"
"time"

"github.com/docker/go-units"
"github.com/lima-vm/go-qcow2reader"
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/driverutil"
"github.com/lima-vm/lima/pkg/executil"
"github.com/lima-vm/lima/pkg/nativeimgutil"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/qemu/imgutil"
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
"github.com/mattn/go-isatty"

Expand Down Expand Up @@ -107,6 +111,12 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
if err := limaDriver.CreateDisk(ctx); err != nil {
return nil, err
}

// Ensure diffDisk size matches the store
if err := prepareDiffDisk(inst); err != nil {
return nil, err
}

nerdctlArchiveCache, err := ensureNerdctlArchiveCache(ctx, inst.Config, created)
if err != nil {
return nil, err
Expand Down Expand Up @@ -377,3 +387,55 @@ func ShowMessage(inst *store.Instance) error {
}
return scanner.Err()
}

// prepareDiffDisk checks the disk size difference between inst.Disk and yaml.Disk.
// If there is no diffDisk, return nil (the instance has not been initialized or started yet).
func prepareDiffDisk(inst *store.Instance) error {
diffDisk := filepath.Join(inst.Dir, filenames.DiffDisk)

// Handle the instance initialization
_, err := os.Stat(diffDisk)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}

f, err := os.Open(diffDisk)
if err != nil {
return err
}
defer f.Close()

img, err := qcow2reader.Open(f)
if err != nil {
return err
}

diskSize := img.Size()
format := string(img.Type())

if inst.Disk == diskSize {
return nil
}

logrus.Infof("Resize instance %s's disk from %s to %s", inst.Name, units.BytesSize(float64(diskSize)), units.BytesSize(float64(inst.Disk)))

if inst.Disk < diskSize {
inst.Disk = diskSize
return errors.New("diffDisk: Shrinking is currently unavailable")
}

if format == "raw" {
err = nativeimgutil.ResizeRawDisk(diffDisk, int(inst.Disk))
} else {
err = imgutil.ResizeDisk(diffDisk, format, int(inst.Disk))
}

if err != nil {
return err
}

return err
}