Skip to content

Commit

Permalink
core: make qemu an optional dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
abiosoft committed Nov 7, 2024
1 parent 0d1a6ae commit cfc71c0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ func setConfigDefaults(conf *config.Config) {
// handle macOS virtualization.framework transition
if conf.VMType == "" {
conf.VMType = defaultVMType
// if on macOS with no qemu, use vz
if err := util.AssertQemuImg(); err != nil && util.MacOS13OrNewer() {
conf.VMType = "vz"
}
}

if conf.MountType == "" {
Expand Down
5 changes: 5 additions & 0 deletions config/configmanager/configmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func ValidateConfig(c config.Config) error {
if _, ok := validVMTypes[c.VMType]; !ok {
return fmt.Errorf("invalid vmType: '%s'", c.VMType)
}
if c.VMType == "qemu" {
if err := util.AssertQemuImg(); err != nil {
return fmt.Errorf("cannot use vmType: '%s', error: %w", c.VMType, err)
}
}

return nil
}
Expand Down
5 changes: 5 additions & 0 deletions environment/vm/lima/lima.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ func (l *limaVM) syncDiskSize(ctx context.Context, conf config.Config) config.Co
return false
}

if err := util.AssertQemuImg(); err != nil {
log.Warnln(fmt.Errorf("unable to resize disk: %w", err))
return false
}

sizeStr := fmt.Sprintf("%dG", conf.Disk)
args := []string{"qemu-img", "resize"}
disk := limautil.ColimaDiffDisk(config.CurrentProfile().ID)
Expand Down
25 changes: 22 additions & 3 deletions environment/vm/lima/limautil/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/abiosoft/colima/environment"
"github.com/abiosoft/colima/environment/host"
"github.com/abiosoft/colima/environment/vm/lima/limaconfig"
"github.com/abiosoft/colima/util"
"github.com/abiosoft/colima/util/downloader"
"github.com/sirupsen/logrus"
)
Expand All @@ -32,7 +33,7 @@ func ImageCached(arch environment.Arch, runtime string) (limaconfig.File, bool)

image := diskImageFile(downloader.CacheFilename(img.Location))

img.Location = image.Raw()
img.Location = image.Location()
img.Digest = ""

return img, image.Generated()
Expand Down Expand Up @@ -65,8 +66,18 @@ func DownloadImage(arch environment.Arch, runtime string) (f limaconfig.File, er
if err != nil {
return f, err
}

diskImage := diskImageFile(qcow2)

// if qemu-img is missing, ignore raw conversion
if err := util.AssertQemuImg(); err != nil {
img.Location = diskImage.String()
img.Digest = "" // remove digest
return img, nil
}

// convert from qcow2 to raw
raw, err := qcow2ToRaw(host, diskImageFile(qcow2))
raw, err := qcow2ToRaw(host, diskImage)
if err != nil {
return f, err
}
Expand Down Expand Up @@ -160,6 +171,14 @@ type diskImageFile string
func (d diskImageFile) String() string { return strings.TrimSuffix(string(d), ".raw") }
func (d diskImageFile) Raw() string { return d.String() + ".raw" }
func (d diskImageFile) Generated() bool {
stat, err := os.Stat(d.Raw())
stat, err := os.Stat(d.Location())
return err == nil && !stat.IsDir()
}

// Location returns the expected location of the image based on availability of qemu.
func (d diskImageFile) Location() string {
if err := util.AssertQemuImg(); err == nil {
return d.Raw()
}
return d.String()
}
16 changes: 16 additions & 0 deletions util/qemu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package util

import (
"fmt"
"os/exec"
)

// AssertQemuImg checks if qemu-img is available.
func AssertQemuImg() error {
cmd := "qemu-img"
if _, err := exec.LookPath(cmd); err != nil {
return fmt.Errorf("%s not found, run 'brew install %s' to install", cmd, "qemu")
}

return nil
}

0 comments on commit cfc71c0

Please sign in to comment.