Skip to content

Commit 1d0ac3f

Browse files
committed
pkg/imgutil: Consolidate ConvertToRaw() and ConvertToASIF() to Convert()
Signed-off-by: Norio Nomura <[email protected]>
1 parent 70ed498 commit 1d0ac3f

File tree

7 files changed

+33
-42
lines changed

7 files changed

+33
-42
lines changed

pkg/driver/krunkit/krunkit_darwin_arm64.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strconv"
1515

1616
"github.com/docker/go-units"
17+
"github.com/lima-vm/go-qcow2reader/image/raw"
1718
"github.com/sirupsen/logrus"
1819

1920
"github.com/lima-vm/lima/v2/pkg/driver/vz"
@@ -65,7 +66,7 @@ func Cmdline(inst *limatype.Instance) (*exec.Cmd, error) {
6566
}
6667
extraDiskPath := filepath.Join(disk.Dir, filenames.DataDisk)
6768
logrus.Infof("Mounting disk %q on %q", disk.Name, disk.MountPoint)
68-
if cerr := diskUtil.ConvertToRaw(ctx, extraDiskPath, extraDiskPath, nil, true); cerr != nil {
69+
if cerr := diskUtil.Convert(ctx, raw.Type, extraDiskPath, extraDiskPath, nil, true); cerr != nil {
6970
return nil, fmt.Errorf("failed to convert extra disk %q to raw: %w", extraDiskPath, cerr)
7071
}
7172
args = append(args, "--device", fmt.Sprintf("virtio-blk,path=%s,format=raw", extraDiskPath))

pkg/driver/vz/vm_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ func attachDisks(ctx context.Context, inst *limatype.Instance, vmConfig *vz.Virt
520520
// ConvertToRaw is a NOP if no conversion is needed
521521
logrus.Debugf("Converting extra disk %q to a raw disk (if it is not a raw)", extraDiskPath)
522522

523-
if err = diskUtil.ConvertToRaw(ctx, extraDiskPath, extraDiskPath, nil, true); err != nil {
523+
if err = diskUtil.Convert(ctx, raw.Type, extraDiskPath, extraDiskPath, nil, true); err != nil {
524524
return fmt.Errorf("failed to convert extra disk %q to a raw disk: %w", extraDiskPath, err)
525525
}
526526
extraDiskPathAttachment, err := vz.NewDiskImageStorageDeviceAttachmentWithCacheAndSync(extraDiskPath, false, diskImageCachingMode, vz.DiskImageSynchronizationModeFsync)

pkg/driverutil/disk.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/docker/go-units"
1414
"github.com/lima-vm/go-qcow2reader/image"
15-
"github.com/lima-vm/go-qcow2reader/image/asif"
1615

1716
"github.com/lima-vm/lima/v2/pkg/imgutil/proxyimgutil"
1817
"github.com/lima-vm/lima/v2/pkg/iso9660util"
@@ -55,11 +54,7 @@ func EnsureDisk(ctx context.Context, instDir, diskSize string, diskImageFormat i
5554
}
5655
// Check whether to use ASIF format
5756

58-
converter := diskUtil.ConvertToASIF
59-
if diskImageFormat != asif.Type {
60-
converter = diskUtil.ConvertToRaw
61-
}
62-
if err = converter(ctx, baseDisk, diffDisk, &diskSizeInBytes, false); err != nil {
57+
if err = diskUtil.Convert(ctx, diskImageFormat, baseDisk, diffDisk, &diskSizeInBytes, false); err != nil {
6358
return fmt.Errorf("failed to convert %q to a disk %q: %w", baseDisk, diffDisk, err)
6459
}
6560
return err

pkg/imgutil/manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package imgutil
66
import (
77
"context"
88
"os"
9+
10+
"github.com/lima-vm/go-qcow2reader/image"
911
)
1012

1113
// ImageDiskManager defines the common operations for disk image utilities.
@@ -16,12 +18,10 @@ type ImageDiskManager interface {
1618
// ResizeDisk resizes an existing disk image to the specified size.
1719
ResizeDisk(ctx context.Context, disk string, size int64) error
1820

19-
// ConvertToRaw converts a disk image to raw format.
20-
ConvertToRaw(ctx context.Context, source, dest string, size *int64, allowSourceWithBackingFile bool) error
21+
// Convert converts a disk image to the specified format.
22+
// Currently supported formats are raw.Type and asif.Type.
23+
Convert(ctx context.Context, imageType image.Type, source, dest string, size *int64, allowSourceWithBackingFile bool) error
2124

2225
// MakeSparse makes a file sparse, starting from the specified offset.
2326
MakeSparse(ctx context.Context, f *os.File, offset int64) error
24-
25-
// ConvertToASIF converts a disk image to ASIF format.
26-
ConvertToASIF(ctx context.Context, source, dest string, size *int64, allowSourceWithBackingFile bool) error
2727
}

pkg/imgutil/nativeimgutil/nativeimgutil.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ func (n *NativeImageUtil) CreateDisk(_ context.Context, disk string, size int64)
230230
return f.Truncate(roundedSize)
231231
}
232232

233-
// ConvertToRaw converts a disk image to raw format.
234-
func (n *NativeImageUtil) ConvertToRaw(_ context.Context, source, dest string, size *int64, allowSourceWithBackingFile bool) error {
235-
return convertTo(raw.Type, source, dest, size, allowSourceWithBackingFile)
233+
// Convert converts a disk image to the specified format.
234+
// Currently supported formats are raw.Type and asif.Type.
235+
func (n *NativeImageUtil) Convert(_ context.Context, imageType image.Type, source, dest string, size *int64, allowSourceWithBackingFile bool) error {
236+
return convertTo(imageType, source, dest, size, allowSourceWithBackingFile)
236237
}
237238

238239
// ResizeDisk resizes an existing disk image to the specified size.
@@ -245,8 +246,3 @@ func (n *NativeImageUtil) ResizeDisk(_ context.Context, disk string, size int64)
245246
func (n *NativeImageUtil) MakeSparse(_ context.Context, f *os.File, offset int64) error {
246247
return makeSparse(f, offset)
247248
}
248-
249-
// ConvertToASIF converts a disk image to ASIF format.
250-
func (n *NativeImageUtil) ConvertToASIF(_ context.Context, source, dest string, size *int64, allowSourceWithBackingFile bool) error {
251-
return convertTo(asif.Type, source, dest, size, allowSourceWithBackingFile)
252-
}

pkg/imgutil/proxyimgutil/proxyimgutil.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"os"
1010
"os/exec"
1111

12+
"github.com/lima-vm/go-qcow2reader/image"
13+
"github.com/lima-vm/go-qcow2reader/image/raw"
14+
1215
"github.com/lima-vm/lima/v2/pkg/imgutil"
1316
"github.com/lima-vm/lima/v2/pkg/imgutil/nativeimgutil"
1417
"github.com/lima-vm/lima/v2/pkg/qemuimgutil"
@@ -52,16 +55,20 @@ func (p *ImageDiskManager) ResizeDisk(ctx context.Context, disk string, size int
5255
return err
5356
}
5457

55-
// ConvertToRaw converts a disk image to raw format.
56-
func (p *ImageDiskManager) ConvertToRaw(ctx context.Context, source, dest string, size *int64, allowSourceWithBackingFile bool) error {
57-
err := p.qemu.ConvertToRaw(ctx, source, dest, size, allowSourceWithBackingFile)
58-
if err == nil {
59-
return nil
60-
}
61-
if errors.Is(err, exec.ErrNotFound) {
62-
return p.native.ConvertToRaw(ctx, source, dest, size, allowSourceWithBackingFile)
58+
// Convert converts a disk image to the specified format.
59+
// Currently supported formats are raw.Type and asif.Type.
60+
func (p *ImageDiskManager) Convert(ctx context.Context, imageType image.Type, source, dest string, size *int64, allowSourceWithBackingFile bool) error {
61+
if imageType == raw.Type {
62+
err := p.qemu.Convert(ctx, imageType, source, dest, size, allowSourceWithBackingFile)
63+
if err == nil {
64+
return nil
65+
}
66+
if errors.Is(err, exec.ErrNotFound) {
67+
return p.native.Convert(ctx, imageType, source, dest, size, allowSourceWithBackingFile)
68+
}
69+
return err
6370
}
64-
return err
71+
return p.native.Convert(ctx, imageType, source, dest, size, allowSourceWithBackingFile)
6572
}
6673

6774
func (p *ImageDiskManager) MakeSparse(ctx context.Context, f *os.File, offset int64) error {
@@ -74,8 +81,3 @@ func (p *ImageDiskManager) MakeSparse(ctx context.Context, f *os.File, offset in
7481
}
7582
return err
7683
}
77-
78-
func (p *ImageDiskManager) ConvertToASIF(ctx context.Context, source, dest string, size *int64, allowSourceWithBackingFile bool) error {
79-
// ASIF conversion is only supported by the native image utility.
80-
return p.native.ConvertToASIF(ctx, source, dest, size, allowSourceWithBackingFile)
81-
}

pkg/qemuimgutil/qemuimgutil.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"os/exec"
1515
"strconv"
1616

17+
"github.com/lima-vm/go-qcow2reader/image"
1718
"github.com/sirupsen/logrus"
1819
)
1920

@@ -199,8 +200,9 @@ func GetInfo(ctx context.Context, path string) (*Info, error) {
199200
return qemuInfo, nil
200201
}
201202

202-
// ConvertToRaw converts a disk image to raw format.
203-
func (q *QemuImageUtil) ConvertToRaw(ctx context.Context, source, dest string, size *int64, allowSourceWithBackingFile bool) error {
203+
// Convert converts a disk image to raw format.
204+
// Specified imageType is ignored.
205+
func (q *QemuImageUtil) Convert(ctx context.Context, _ image.Type, source, dest string, size *int64, allowSourceWithBackingFile bool) error {
204206
if !allowSourceWithBackingFile {
205207
info, err := getInfo(ctx, source)
206208
if err != nil {
@@ -269,8 +271,3 @@ func AcceptableAsBaseDisk(info *Info) error {
269271
}
270272
return nil
271273
}
272-
273-
func (q *QemuImageUtil) ConvertToASIF(_ context.Context, _, _ string, _ *int64, _ bool) error {
274-
// Should never be called because ASIF is not supported by qemu-img.
275-
return errors.New("unimplemented")
276-
}

0 commit comments

Comments
 (0)