Skip to content

Commit cfc71c0

Browse files
committed
core: make qemu an optional dependency
Signed-off-by: Abiola Ibrahim <git@abiosoft.com>
1 parent 0d1a6ae commit cfc71c0

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

cmd/start.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ func setConfigDefaults(conf *config.Config) {
290290
// handle macOS virtualization.framework transition
291291
if conf.VMType == "" {
292292
conf.VMType = defaultVMType
293+
// if on macOS with no qemu, use vz
294+
if err := util.AssertQemuImg(); err != nil && util.MacOS13OrNewer() {
295+
conf.VMType = "vz"
296+
}
293297
}
294298

295299
if conf.MountType == "" {

config/configmanager/configmanager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func ValidateConfig(c config.Config) error {
7171
if _, ok := validVMTypes[c.VMType]; !ok {
7272
return fmt.Errorf("invalid vmType: '%s'", c.VMType)
7373
}
74+
if c.VMType == "qemu" {
75+
if err := util.AssertQemuImg(); err != nil {
76+
return fmt.Errorf("cannot use vmType: '%s', error: %w", c.VMType, err)
77+
}
78+
}
7479

7580
return nil
7681
}

environment/vm/lima/lima.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ func (l *limaVM) syncDiskSize(ctx context.Context, conf config.Config) config.Co
319319
return false
320320
}
321321

322+
if err := util.AssertQemuImg(); err != nil {
323+
log.Warnln(fmt.Errorf("unable to resize disk: %w", err))
324+
return false
325+
}
326+
322327
sizeStr := fmt.Sprintf("%dG", conf.Disk)
323328
args := []string{"qemu-img", "resize"}
324329
disk := limautil.ColimaDiffDisk(config.CurrentProfile().ID)

environment/vm/lima/limautil/image.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/abiosoft/colima/environment"
1313
"github.com/abiosoft/colima/environment/host"
1414
"github.com/abiosoft/colima/environment/vm/lima/limaconfig"
15+
"github.com/abiosoft/colima/util"
1516
"github.com/abiosoft/colima/util/downloader"
1617
"github.com/sirupsen/logrus"
1718
)
@@ -32,7 +33,7 @@ func ImageCached(arch environment.Arch, runtime string) (limaconfig.File, bool)
3233

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

35-
img.Location = image.Raw()
36+
img.Location = image.Location()
3637
img.Digest = ""
3738

3839
return img, image.Generated()
@@ -65,8 +66,18 @@ func DownloadImage(arch environment.Arch, runtime string) (f limaconfig.File, er
6566
if err != nil {
6667
return f, err
6768
}
69+
70+
diskImage := diskImageFile(qcow2)
71+
72+
// if qemu-img is missing, ignore raw conversion
73+
if err := util.AssertQemuImg(); err != nil {
74+
img.Location = diskImage.String()
75+
img.Digest = "" // remove digest
76+
return img, nil
77+
}
78+
6879
// convert from qcow2 to raw
69-
raw, err := qcow2ToRaw(host, diskImageFile(qcow2))
80+
raw, err := qcow2ToRaw(host, diskImage)
7081
if err != nil {
7182
return f, err
7283
}
@@ -160,6 +171,14 @@ type diskImageFile string
160171
func (d diskImageFile) String() string { return strings.TrimSuffix(string(d), ".raw") }
161172
func (d diskImageFile) Raw() string { return d.String() + ".raw" }
162173
func (d diskImageFile) Generated() bool {
163-
stat, err := os.Stat(d.Raw())
174+
stat, err := os.Stat(d.Location())
164175
return err == nil && !stat.IsDir()
165176
}
177+
178+
// Location returns the expected location of the image based on availability of qemu.
179+
func (d diskImageFile) Location() string {
180+
if err := util.AssertQemuImg(); err == nil {
181+
return d.Raw()
182+
}
183+
return d.String()
184+
}

util/qemu.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
)
7+
8+
// AssertQemuImg checks if qemu-img is available.
9+
func AssertQemuImg() error {
10+
cmd := "qemu-img"
11+
if _, err := exec.LookPath(cmd); err != nil {
12+
return fmt.Errorf("%s not found, run 'brew install %s' to install", cmd, "qemu")
13+
}
14+
15+
return nil
16+
}

0 commit comments

Comments
 (0)