-
Notifications
You must be signed in to change notification settings - Fork 673
defined a common interface for nativeimgutil & qemuimgutil #3650
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?
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// SPDX-FileCopyrightText: Copyright The Lima Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package imgutil | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
) | ||
|
||
// ImageDiskManager defines the common operations for disk image utilities. | ||
type ImageDiskManager interface { | ||
// CreateDisk creates a new disk image with the specified size. | ||
CreateDisk(disk string, size int) error | ||
|
||
// ResizeDisk resizes an existing disk image to the specified size. | ||
ResizeDisk(disk string, size int) error | ||
|
||
// ConvertToRaw converts a disk image to raw format. | ||
ConvertToRaw(source, dest string, size *int64, allowSourceWithBackingFile bool) error | ||
|
||
// MakeSparse makes a file sparse, starting from the specified offset. | ||
MakeSparse(f *os.File, offset int64) error | ||
} | ||
|
||
type InfoChild struct { | ||
Name string `json:"name,omitempty"` // since QEMU 8.0 | ||
Info Info `json:"info,omitempty"` // since QEMU 8.0 | ||
} | ||
|
||
type InfoFormatSpecific struct { | ||
Type string `json:"type,omitempty"` // since QEMU 1.7 | ||
Data json.RawMessage `json:"data,omitempty"` // since QEMU 1.7 | ||
} | ||
|
||
func (sp *InfoFormatSpecific) Qcow2() *InfoFormatSpecificDataQcow2 { | ||
if sp.Type != "qcow2" { | ||
return nil | ||
} | ||
var x InfoFormatSpecificDataQcow2 | ||
if err := json.Unmarshal(sp.Data, &x); err != nil { | ||
panic(err) | ||
} | ||
return &x | ||
} | ||
|
||
func (sp *InfoFormatSpecific) Vmdk() *InfoFormatSpecificDataVmdk { | ||
if sp.Type != "vmdk" { | ||
return nil | ||
} | ||
var x InfoFormatSpecificDataVmdk | ||
if err := json.Unmarshal(sp.Data, &x); err != nil { | ||
panic(err) | ||
} | ||
return &x | ||
} | ||
|
||
type InfoFormatSpecificDataQcow2 struct { | ||
Compat string `json:"compat,omitempty"` // since QEMU 1.7 | ||
LazyRefcounts bool `json:"lazy-refcounts,omitempty"` // since QEMU 1.7 | ||
Corrupt bool `json:"corrupt,omitempty"` // since QEMU 2.2 | ||
RefcountBits int `json:"refcount-bits,omitempty"` // since QEMU 2.3 | ||
CompressionType string `json:"compression-type,omitempty"` // since QEMU 5.1 | ||
ExtendedL2 bool `json:"extended-l2,omitempty"` // since QEMU 5.2 | ||
} | ||
|
||
type InfoFormatSpecificDataVmdk struct { | ||
CreateType string `json:"create-type,omitempty"` // since QEMU 1.7 | ||
CID int `json:"cid,omitempty"` // since QEMU 1.7 | ||
ParentCID int `json:"parent-cid,omitempty"` // since QEMU 1.7 | ||
Extents []InfoFormatSpecificDataVmdkExtent `json:"extents,omitempty"` // since QEMU 1.7 | ||
} | ||
|
||
type InfoFormatSpecificDataVmdkExtent struct { | ||
Filename string `json:"filename,omitempty"` // since QEMU 1.7 | ||
Format string `json:"format,omitempty"` // since QEMU 1.7 | ||
VSize int64 `json:"virtual-size,omitempty"` // since QEMU 1.7 | ||
ClusterSize int `json:"cluster-size,omitempty"` // since QEMU 1.7 | ||
} | ||
|
||
// Info corresponds to the output of `qemu-img info --output=json FILE`. | ||
type Info struct { | ||
Filename string `json:"filename,omitempty"` // since QEMU 1.3 | ||
Format string `json:"format,omitempty"` // since QEMU 1.3 | ||
VSize int64 `json:"virtual-size,omitempty"` // since QEMU 1.3 | ||
ActualSize int64 `json:"actual-size,omitempty"` // since QEMU 1.3 | ||
DirtyFlag bool `json:"dirty-flag,omitempty"` // since QEMU 5.2 | ||
ClusterSize int `json:"cluster-size,omitempty"` // since QEMU 1.3 | ||
BackingFilename string `json:"backing-filename,omitempty"` // since QEMU 1.3 | ||
FullBackingFilename string `json:"full-backing-filename,omitempty"` // since QEMU 1.3 | ||
BackingFilenameFormat string `json:"backing-filename-format,omitempty"` // since QEMU 1.3 | ||
FormatSpecific *InfoFormatSpecific `json:"format-specific,omitempty"` // since QEMU 1.7 | ||
Children []InfoChild `json:"children,omitempty"` // since QEMU 8.0 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-FileCopyrightText: Copyright The Lima Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package nativeimgutil | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// NewNativeImageUtil returns a new NativeImageUtil instance. | ||
func NewNativeImageUtil() *NativeImageUtil { | ||
return &NativeImageUtil{} | ||
} | ||
|
||
// CreateDisk creates a new raw disk image with the specified size. | ||
func (n *NativeImageUtil) CreateDisk(disk string, size int) error { | ||
return createRawDisk(disk, size) | ||
} | ||
|
||
// ResizeDisk resizes an existing raw disk image to the specified size. | ||
func (n *NativeImageUtil) ResizeDisk(disk string, size int) error { | ||
return resizeRawDisk(disk, size) | ||
} | ||
|
||
// ConvertToRaw converts a disk image to raw format. | ||
func (n *NativeImageUtil) ConvertToRaw(source, dest string, size *int64, allowSourceWithBackingFile bool) error { | ||
return convertToRaw(source, dest, size, allowSourceWithBackingFile) | ||
} | ||
|
||
func (n *NativeImageUtil) MakeSparse(f *os.File, offset int64) error { | ||
return makeSparse(f, offset) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// SPDX-FileCopyrightText: Copyright The Lima Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package proxyimgutil | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/lima-vm/lima/pkg/imgutil" | ||
"github.com/lima-vm/lima/pkg/imgutil/nativeimgutil" | ||
"github.com/lima-vm/lima/pkg/imgutil/qemuimgutil" | ||
) | ||
|
||
type ProxyImageDiskManager struct { | ||
qemu imgutil.ImageDiskManager | ||
native imgutil.ImageDiskManager | ||
} | ||
|
||
func NewProxyImageUtil() (imgutil.ImageDiskManager, *qemuimgutil.QemuInfoProvider) { | ||
return &ProxyImageDiskManager{ | ||
qemu: qemuimgutil.NewQemuImageUtil(), | ||
native: nativeimgutil.NewNativeImageUtil(), | ||
}, qemuimgutil.NewQemuInfoProvider() | ||
} | ||
|
||
func (p *ProxyImageDiskManager) CreateDisk(disk string, size int) error { | ||
if err := p.qemu.CreateDisk(disk, size); err != nil { | ||
if errors.Is(err, exec.ErrNotFound) { | ||
return p.native.CreateDisk(disk, size) | ||
} | ||
return err | ||
} | ||
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. When 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. Same applies to other functions too 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.
How does this happen? 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. Ok heres the explanation , see in go
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. How is sh relevant here? 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. I used The In this case, yeah the failure scenario I described doesn't really apply here. I included it more as an extra precaution. |
||
return nil | ||
} | ||
|
||
func (p *ProxyImageDiskManager) ResizeDisk(disk string, size int) error { | ||
if err := p.qemu.ResizeDisk(disk, size); err != nil { | ||
if errors.Is(err, exec.ErrNotFound) { | ||
return p.native.ResizeDisk(disk, size) | ||
} | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (p *ProxyImageDiskManager) ConvertToRaw(source, dest string, size *int64, allowSourceWithBackingFile bool) error { | ||
if err := p.qemu.ConvertToRaw(source, dest, size, allowSourceWithBackingFile); err != nil { | ||
if errors.Is(err, exec.ErrNotFound) { | ||
return p.native.ConvertToRaw(source, dest, size, allowSourceWithBackingFile) | ||
} | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (p *ProxyImageDiskManager) MakeSparse(f *os.File, offset int64) error { | ||
if err := p.qemu.MakeSparse(f, offset); err != nil { | ||
if errors.Is(err, exec.ErrNotFound) { | ||
return p.native.MakeSparse(f, offset) | ||
} | ||
return err | ||
} | ||
return nil | ||
} |
Uh oh!
There was an error while loading. Please reload this page.