-
Notifications
You must be signed in to change notification settings - Fork 654
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
base: master
Are you sure you want to change the base?
Fix: resize GuestOS disk #3437
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 This implementation tries to reuse |
||
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 | ||
} |
There was a problem hiding this comment.
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