Skip to content

limactl disk: Do not use qemu-img for raw disks #3180

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

Merged
merged 1 commit into from
Mar 14, 2025
Merged
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
19 changes: 17 additions & 2 deletions cmd/limactl/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"text/tabwriter"

"github.com/docker/go-units"
"github.com/lima-vm/lima/pkg/nativeimgutil"
"github.com/lima-vm/lima/pkg/qemu"
"github.com/lima-vm/lima/pkg/store"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -101,7 +102,13 @@ func diskCreateAction(cmd *cobra.Command, args []string) error {
return err
}

if err := qemu.CreateDataDisk(diskDir, format, int(diskSize)); err != nil {
// qemu may not be available, use it only if needed.
if format == "raw" {
err = nativeimgutil.CreateRawDataDisk(diskDir, int(diskSize))
} else {
err = qemu.CreateDataDisk(diskDir, format, int(diskSize))
}
if err != nil {
rerr := os.RemoveAll(diskDir)
if rerr != nil {
err = errors.Join(err, fmt.Errorf("failed to remove a directory %q: %w", diskDir, rerr))
Expand Down Expand Up @@ -390,9 +397,17 @@ func diskResizeAction(cmd *cobra.Command, args []string) error {
}
}
}
if err := qemu.ResizeDataDisk(disk.Dir, disk.Format, int(diskSize)); err != nil {

// qemu may not be available, use it only if needed.
if disk.Format == "raw" {
err = nativeimgutil.ResizeRawDataDisk(disk.Dir, int(diskSize))
} else {
err = qemu.ResizeDataDisk(disk.Dir, disk.Format, int(diskSize))
}
if err != nil {
return fmt.Errorf("failed to resize disk %q: %w", diskName, err)
}

logrus.Infof("Resized disk %q (%q)", diskName, disk.Dir)
return nil
}
Expand Down
27 changes: 25 additions & 2 deletions pkg/nativeimgutil/nativeimgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,44 @@
package nativeimgutil

import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"

"github.com/containerd/continuity/fs"
containerdfs "github.com/containerd/continuity/fs"
"github.com/docker/go-units"
"github.com/lima-vm/go-qcow2reader"
"github.com/lima-vm/go-qcow2reader/convert"
"github.com/lima-vm/go-qcow2reader/image/qcow2"
"github.com/lima-vm/go-qcow2reader/image/raw"
"github.com/lima-vm/lima/pkg/progressbar"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
)

// CreateRawDataDisk creates an empty raw data disk.
func CreateRawDataDisk(dir string, size int) error {
dataDisk := filepath.Join(dir, filenames.DataDisk)
if _, err := os.Stat(dataDisk); err == nil || !errors.Is(err, fs.ErrNotExist) {
return err
}
f, err := os.Create(dataDisk)
if err != nil {
return err
}
defer f.Close()
return f.Truncate(int64(size))
}

// ResizeRawDataDisk resizes a raw data disk.
func ResizeRawDataDisk(dir string, size int) error {
dataDisk := filepath.Join(dir, filenames.DataDisk)
return os.Truncate(dataDisk, int64(size))
}

// ConvertToRaw converts a source disk into a raw disk.
// source and dest may be same.
// ConvertToRaw is a NOP if source == dest, and no resizing is needed.
Expand Down Expand Up @@ -106,7 +129,7 @@ func ConvertToRaw(source, dest string, size *int64, allowSourceWithBackingFile b
func convertRawToRaw(source, dest string, size *int64) error {
if source != dest {
// continuity attempts clonefile
if err := fs.CopyFile(dest, source); err != nil {
if err := containerdfs.CopyFile(dest, source); err != nil {
return fmt.Errorf("failed to copy %q into %q: %w", source, dest, err)
}
}
Expand Down
Loading