Skip to content

Commit e06bdc5

Browse files
committed
Support for Virt driver
This is a driver based on libvirt.org, for remote hypervisors. Requires "virsh" to be installed. Currently only shows version. Signed-off-by: Anders F Björklund <[email protected]>
1 parent b453ded commit e06bdc5

File tree

9 files changed

+143
-1
lines changed

9 files changed

+143
-1
lines changed

examples/experimental/virt.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# A template to run ubuntu using vmType: virt instead of qemu (Default)
2+
vmType: "virt"
3+
4+
images:
5+
- location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
6+
arch: "x86_64"
7+
- location: "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img"
8+
arch: "aarch64"
9+
10+
mounts:
11+
- location: "~"
12+
- location: "/tmp/lima"
13+
writable: true

pkg/driverutil/driverutil.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ package driverutil
22

33
import (
44
"github.com/lima-vm/lima/pkg/limayaml"
5+
"github.com/lima-vm/lima/pkg/virt"
56
"github.com/lima-vm/lima/pkg/vz"
67
"github.com/lima-vm/lima/pkg/wsl2"
78
)
89

910
// Drivers returns the available drivers.
1011
func Drivers() []string {
1112
drivers := []string{limayaml.QEMU}
13+
if virt.Enabled {
14+
drivers = append(drivers, limayaml.VIRT)
15+
}
1216
if vz.Enabled {
1317
drivers = append(drivers, limayaml.VZ)
1418
}

pkg/driverutil/instance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import (
44
"github.com/lima-vm/lima/pkg/driver"
55
"github.com/lima-vm/lima/pkg/limayaml"
66
"github.com/lima-vm/lima/pkg/qemu"
7+
"github.com/lima-vm/lima/pkg/virt"
78
"github.com/lima-vm/lima/pkg/vz"
89
"github.com/lima-vm/lima/pkg/wsl2"
910
)
1011

1112
func CreateTargetDriverInstance(base *driver.BaseDriver) driver.Driver {
1213
limaDriver := base.Yaml.VMType
14+
if *limaDriver == limayaml.VIRT {
15+
return virt.New(base)
16+
}
1317
if *limaDriver == limayaml.VZ {
1418
return vz.New(base)
1519
}

pkg/limayaml/defaults.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,8 @@ func NewArch(arch string) Arch {
848848

849849
func NewVMType(driver string) VMType {
850850
switch driver {
851+
case "virt":
852+
return VIRT
851853
case "vz":
852854
return VZ
853855
case "qemu":

pkg/limayaml/limayaml.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const (
6262
WSLMount MountType = "wsl2"
6363

6464
QEMU VMType = "qemu"
65+
VIRT VMType = "virt"
6566
VZ VMType = "vz"
6667
WSL2 VMType = "wsl2"
6768
)

pkg/limayaml/validate.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ func Validate(y LimaYAML, warn bool) error {
6161
if !IsNativeArch(*y.Arch) {
6262
return fmt.Errorf("field `arch` must be %q for VZ; got %q", NewArch(runtime.GOARCH), *y.Arch)
6363
}
64+
case VIRT:
65+
// NOP
6466
default:
65-
return fmt.Errorf("field `vmType` must be %q, %q, %q; got %q", QEMU, VZ, WSL2, *y.VMType)
67+
return fmt.Errorf("field `vmType` must be %q, %q, %q, %q; got %q", QEMU, VIRT, VZ, WSL2, *y.VMType)
6668
}
6769

6870
if len(y.Images) == 0 {

pkg/virt/disk.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package virt
2+
3+
import (
4+
"errors"
5+
"os"
6+
"path/filepath"
7+
8+
"github.com/docker/go-units"
9+
"github.com/lima-vm/lima/pkg/driver"
10+
"github.com/lima-vm/lima/pkg/fileutils"
11+
"github.com/lima-vm/lima/pkg/iso9660util"
12+
"github.com/lima-vm/lima/pkg/store/filenames"
13+
)
14+
15+
func EnsureDisk(driver *driver.BaseDriver) error {
16+
diffDisk := filepath.Join(driver.Instance.Dir, filenames.DiffDisk)
17+
if _, err := os.Stat(diffDisk); err == nil || !errors.Is(err, os.ErrNotExist) {
18+
// disk is already ensured
19+
return err
20+
}
21+
22+
baseDisk := filepath.Join(driver.Instance.Dir, filenames.BaseDisk)
23+
if _, err := os.Stat(baseDisk); errors.Is(err, os.ErrNotExist) {
24+
var ensuredBaseDisk bool
25+
errs := make([]error, len(driver.Yaml.Images))
26+
for i, f := range driver.Yaml.Images {
27+
if _, err := fileutils.DownloadFile(baseDisk, f.File, true, "the image", *driver.Yaml.Arch); err != nil {
28+
errs[i] = err
29+
continue
30+
}
31+
ensuredBaseDisk = true
32+
break
33+
}
34+
if !ensuredBaseDisk {
35+
return fileutils.Errors(errs)
36+
}
37+
}
38+
diskSize, _ := units.RAMInBytes(*driver.Yaml.Disk)
39+
if diskSize == 0 {
40+
return nil
41+
}
42+
isBaseDiskISO, err := iso9660util.IsISO9660(baseDisk)
43+
if err != nil {
44+
return err
45+
}
46+
_ = isBaseDiskISO
47+
// TODO
48+
return nil
49+
}

pkg/virt/virt.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package virt
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"regexp"
7+
)
8+
9+
func Version() (string, error) {
10+
out, err := exec.Command("virsh", "version").Output()
11+
if err != nil {
12+
return "", err
13+
}
14+
re := regexp.MustCompile("Using library: libvirt (.*)")
15+
m := re.FindStringSubmatch(string(out))
16+
if m == nil {
17+
return "", fmt.Errorf("can't find libvirt version")
18+
}
19+
return m[1], nil
20+
}

pkg/virt/virt_driver.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package virt
2+
3+
import (
4+
"context"
5+
"os/exec"
6+
7+
"github.com/lima-vm/lima/pkg/driver"
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
const Enabled = true
12+
13+
type LimaVirtDriver struct {
14+
*driver.BaseDriver
15+
}
16+
17+
func New(driver *driver.BaseDriver) *LimaVirtDriver {
18+
return &LimaVirtDriver{
19+
BaseDriver: driver,
20+
}
21+
}
22+
23+
func (l *LimaVirtDriver) Validate() error {
24+
if _, err := exec.LookPath("virsh"); err != nil {
25+
return err
26+
}
27+
28+
v, err := Version()
29+
if err != nil {
30+
return err
31+
}
32+
logrus.Infof("Version: %s", v)
33+
34+
return nil
35+
}
36+
37+
func (l *LimaVirtDriver) CreateDisk() error {
38+
return EnsureDisk(l.BaseDriver)
39+
}
40+
41+
func (l *LimaVirtDriver) Start(_ context.Context) (chan error, error) {
42+
return nil, errors.New("TODO")
43+
}
44+
45+
func (l *LimaVirtDriver) Stop(_ context.Context) error {
46+
return nil
47+
}

0 commit comments

Comments
 (0)