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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions cmd/limactl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ func editAction(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}

inst, err = store.Inspect(inst.Name)
Copy link
Member

@AkihiroSuda AkihiroSuda May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason to inspect the instance twice should be explained as a code comment

if err != nil {
return err
}

return instance.Start(ctx, inst, "", false)
}

Expand Down
66 changes: 66 additions & 0 deletions pkg/instance/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ import (
"time"

"github.com/coreos/go-semver/semver"
"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"
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
Expand Down Expand Up @@ -101,6 +104,11 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
if err := limaDriver.CreateDisk(ctx); err != nil {
return nil, err
}

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 @@ -386,3 +394,61 @@ func ShowMessage(inst *store.Instance) error {
}
return scanner.Err()
}

// prepareDiffDisk checks the disk size difference between inst.Disk and yaml.Disk.
// It reuses ResizeDataDisk() by mimicking diffDisk as a Lima disk.
func prepareDiffDisk(inst *store.Instance) error {
diffDisk := filepath.Join(inst.Dir, filenames.DiffDisk)

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 {
logrus.Infof("diffDisk size does not changed")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This debug message can be removed

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")
}

tmpDiskDir := filepath.Join(filenames.DisksDir, inst.Name+"-tmp")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DisksDir is dedicated for additional disks, and should not be used for the main disk

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case, it would make sense to implement a separate ResizeDiffDisk() function specifically for resizing the diffDisk in the instance directory (e.g., ~/.lima/default/diffdisk)?

This implementation tries to reuse ResizeDataDisk(), which was originally intended for additional disks.

if err := os.MkdirAll(tmpDiskDir, 0o755); err != nil {
return err
}

// Unsure whether to copy the file back and forth or use a symlink.
if err := os.Symlink(diffDisk, filepath.Join(tmpDiskDir, filenames.DataDisk)); err != nil {
return err
}

if format == "raw" {
err = nativeimgutil.ResizeRawDataDisk(tmpDiskDir, int(inst.Disk))
} else {
err = qemu.ResizeDataDisk(tmpDiskDir, format, int(inst.Disk))
}

if err != nil {
return err
}

if err := os.RemoveAll(tmpDiskDir); err != nil {
return fmt.Errorf("failed to remove %q: %w", tmpDiskDir, err)
}

return err
}
Loading