Skip to content

Commit d32847c

Browse files
authored
Merge pull request #3691 from unsuman/refactor/basedriver
Refactor: Remove driver.BaseDriver, move drivers and image-downloader to pkg/driver
2 parents 5a82148 + 5389e27 commit d32847c

33 files changed

+603
-457
lines changed

pkg/driver/driver.go

Lines changed: 2 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ package driver
55

66
import (
77
"context"
8-
"errors"
98
"net"
10-
11-
"github.com/lima-vm/lima/pkg/store"
129
)
1310

1411
// Driver interface is used by hostagent for managing vm.
15-
//
16-
// This interface is extended by BaseDriver which provides default implementation.
17-
// All other driver definition must extend BaseDriver.
1812
type Driver interface {
1913
// Validate returns error if the current driver isn't support for given config
2014
Validate() error
@@ -71,84 +65,7 @@ type Driver interface {
7165

7266
// GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh).
7367
GuestAgentConn(_ context.Context) (net.Conn, error)
74-
}
75-
76-
type BaseDriver struct {
77-
Instance *store.Instance
78-
79-
SSHLocalPort int
80-
VSockPort int
81-
VirtioPort string
82-
}
83-
84-
var _ Driver = (*BaseDriver)(nil)
85-
86-
func (d *BaseDriver) Validate() error {
87-
return nil
88-
}
89-
90-
func (d *BaseDriver) Initialize(_ context.Context) error {
91-
return nil
92-
}
93-
94-
func (d *BaseDriver) CreateDisk(_ context.Context) error {
95-
return nil
96-
}
97-
98-
func (d *BaseDriver) Start(_ context.Context) (chan error, error) {
99-
return nil, nil
100-
}
101-
102-
func (d *BaseDriver) CanRunGUI() bool {
103-
return false
104-
}
105-
106-
func (d *BaseDriver) RunGUI() error {
107-
return nil
108-
}
109-
110-
func (d *BaseDriver) Stop(_ context.Context) error {
111-
return nil
112-
}
113-
114-
func (d *BaseDriver) Register(_ context.Context) error {
115-
return nil
116-
}
117-
118-
func (d *BaseDriver) Unregister(_ context.Context) error {
119-
return nil
120-
}
121-
122-
func (d *BaseDriver) ChangeDisplayPassword(_ context.Context, _ string) error {
123-
return nil
124-
}
125-
126-
func (d *BaseDriver) GetDisplayConnection(_ context.Context) (string, error) {
127-
return "", nil
128-
}
129-
130-
func (d *BaseDriver) CreateSnapshot(_ context.Context, _ string) error {
131-
return errors.New("unimplemented")
132-
}
133-
134-
func (d *BaseDriver) ApplySnapshot(_ context.Context, _ string) error {
135-
return errors.New("unimplemented")
136-
}
137-
138-
func (d *BaseDriver) DeleteSnapshot(_ context.Context, _ string) error {
139-
return errors.New("unimplemented")
140-
}
141-
142-
func (d *BaseDriver) ListSnapshots(_ context.Context) (string, error) {
143-
return "", errors.New("unimplemented")
144-
}
145-
146-
func (d *BaseDriver) ForwardGuestAgent() bool {
147-
// if driver is not providing, use host agent
148-
return d.VSockPort == 0 && d.VirtioPort == ""
149-
}
15068

151-
func (d *BaseDriver) GuestAgentConn(_ context.Context) (net.Conn, error) {
152-
// use the unix socket forwarded by host agent
153-
return nil, nil
69+
VSockPort() int
70+
VirtioPort() string
15471
}

pkg/qemu/qemu.go renamed to pkg/driver/qemu/qemu.go

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ import (
2828
"github.com/sirupsen/logrus"
2929

3030
"github.com/lima-vm/lima/pkg/fileutils"
31-
"github.com/lima-vm/lima/pkg/imgutil/qemuimgutil"
3231
"github.com/lima-vm/lima/pkg/iso9660util"
3332
"github.com/lima-vm/lima/pkg/limayaml"
3433
"github.com/lima-vm/lima/pkg/networks"
3534
"github.com/lima-vm/lima/pkg/networks/usernet"
3635
"github.com/lima-vm/lima/pkg/osutil"
36+
"github.com/lima-vm/lima/pkg/qemuimgutil"
3737
"github.com/lima-vm/lima/pkg/store"
3838
"github.com/lima-vm/lima/pkg/store/filenames"
3939
)
@@ -85,50 +85,15 @@ func minimumQemuVersion() (hardMin, softMin semver.Version) {
8585
}
8686

8787
// EnsureDisk also ensures the kernel and the initrd.
88-
func EnsureDisk(ctx context.Context, cfg Config) error {
88+
func EnsureDisk(_ context.Context, cfg Config) error {
8989
diffDisk := filepath.Join(cfg.InstanceDir, filenames.DiffDisk)
9090
if _, err := os.Stat(diffDisk); err == nil || !errors.Is(err, os.ErrNotExist) {
9191
// disk is already ensured
9292
return err
9393
}
9494

9595
baseDisk := filepath.Join(cfg.InstanceDir, filenames.BaseDisk)
96-
kernel := filepath.Join(cfg.InstanceDir, filenames.Kernel)
97-
kernelCmdline := filepath.Join(cfg.InstanceDir, filenames.KernelCmdline)
98-
initrd := filepath.Join(cfg.InstanceDir, filenames.Initrd)
99-
if _, err := os.Stat(baseDisk); errors.Is(err, os.ErrNotExist) {
100-
var ensuredBaseDisk bool
101-
errs := make([]error, len(cfg.LimaYAML.Images))
102-
for i, f := range cfg.LimaYAML.Images {
103-
if _, err := fileutils.DownloadFile(ctx, baseDisk, f.File, true, "the image", *cfg.LimaYAML.Arch); err != nil {
104-
errs[i] = err
105-
continue
106-
}
107-
if f.Kernel != nil {
108-
if _, err := fileutils.DownloadFile(ctx, kernel, f.Kernel.File, false, "the kernel", *cfg.LimaYAML.Arch); err != nil {
109-
errs[i] = err
110-
continue
111-
}
112-
if f.Kernel.Cmdline != "" {
113-
if err := os.WriteFile(kernelCmdline, []byte(f.Kernel.Cmdline), 0o644); err != nil {
114-
errs[i] = err
115-
continue
116-
}
117-
}
118-
}
119-
if f.Initrd != nil {
120-
if _, err := fileutils.DownloadFile(ctx, initrd, *f.Initrd, false, "the initrd", *cfg.LimaYAML.Arch); err != nil {
121-
errs[i] = err
122-
continue
123-
}
124-
}
125-
ensuredBaseDisk = true
126-
break
127-
}
128-
if !ensuredBaseDisk {
129-
return fileutils.Errors(errs)
130-
}
131-
}
96+
13297
diskSize, _ := units.RAMInBytes(*cfg.LimaYAML.Disk)
13398
if diskSize == 0 {
13499
return nil

pkg/qemu/qemu_driver.go renamed to pkg/driver/qemu/qemu_driver.go

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,42 @@ import (
2727
"github.com/sirupsen/logrus"
2828

2929
"github.com/lima-vm/lima/pkg/driver"
30+
"github.com/lima-vm/lima/pkg/driver/qemu/entitlementutil"
3031
"github.com/lima-vm/lima/pkg/executil"
3132
"github.com/lima-vm/lima/pkg/limayaml"
3233
"github.com/lima-vm/lima/pkg/networks/usernet"
3334
"github.com/lima-vm/lima/pkg/osutil"
34-
"github.com/lima-vm/lima/pkg/qemu/entitlementutil"
3535
"github.com/lima-vm/lima/pkg/store"
3636
"github.com/lima-vm/lima/pkg/store/filenames"
3737
)
3838

3939
type LimaQemuDriver struct {
40-
*driver.BaseDriver
40+
Instance *store.Instance
41+
SSHLocalPort int
42+
vSockPort int
43+
virtioPort string
44+
4145
qCmd *exec.Cmd
4246
qWaitCh chan error
4347

4448
vhostCmds []*exec.Cmd
4549
}
4650

47-
func New(driver *driver.BaseDriver) *LimaQemuDriver {
48-
driver.VSockPort = 0
49-
driver.VirtioPort = filenames.VirtioPort
51+
var _ driver.Driver = (*LimaQemuDriver)(nil)
52+
53+
func New(inst *store.Instance, sshLocalPort int) *LimaQemuDriver {
5054
// virtserialport doesn't seem to work reliably: https://github.com/lima-vm/lima/issues/2064
5155
// but on Windows default Unix socket forwarding is not available
56+
var virtioPort string
57+
virtioPort = filenames.VirtioPort
5258
if runtime.GOOS != "windows" {
53-
driver.VirtioPort = ""
59+
virtioPort = ""
5460
}
5561
return &LimaQemuDriver{
56-
BaseDriver: driver,
62+
Instance: inst,
63+
vSockPort: 0,
64+
virtioPort: virtioPort,
65+
SSHLocalPort: sshLocalPort,
5766
}
5867
}
5968

@@ -94,7 +103,7 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
94103
LimaYAML: l.Instance.Config,
95104
SSHLocalPort: l.SSHLocalPort,
96105
SSHAddress: l.Instance.SSHAddress,
97-
VirtioGA: l.VirtioPort != "",
106+
VirtioGA: l.virtioPort != "",
98107
}
99108
qExe, qArgs, err := Cmdline(ctx, qCfg)
100109
if err != nil {
@@ -212,7 +221,7 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
212221
go func() {
213222
if usernetIndex := limayaml.FirstUsernetIndex(l.Instance.Config); usernetIndex != -1 {
214223
client := usernet.NewClientByName(l.Instance.Config.Networks[usernetIndex].Lima)
215-
err := client.ConfigureDriver(ctx, l.BaseDriver)
224+
err := client.ConfigureDriver(ctx, l.Instance, l.SSHLocalPort)
216225
if err != nil {
217226
l.qWaitCh <- err
218227
}
@@ -260,11 +269,11 @@ func (l *LimaQemuDriver) checkBinarySignature() error {
260269
}
261270
// The codesign --xml option is only available on macOS Monterey and later
262271
if !macOSProductVersion.LessThan(*semver.New("12.0.0")) {
263-
qExe, _, err := Exe(l.BaseDriver.Instance.Arch)
272+
qExe, _, err := Exe(l.Instance.Arch)
264273
if err != nil {
265-
return fmt.Errorf("failed to find the QEMU binary for the architecture %q: %w", l.BaseDriver.Instance.Arch, err)
274+
return fmt.Errorf("failed to find the QEMU binary for the architecture %q: %w", l.Instance.Arch, err)
266275
}
267-
if accel := Accel(l.BaseDriver.Instance.Arch); accel == "hvf" {
276+
if accel := Accel(l.Instance.Arch); accel == "hvf" {
268277
entitlementutil.AskToSignIfNotSignedProperly(qExe)
269278
}
270279
}
@@ -498,3 +507,36 @@ func (a *qArgTemplateApplier) applyTemplate(qArg string) (string, error) {
498507
}
499508
return b.String(), nil
500509
}
510+
511+
func (l *LimaQemuDriver) Initialize(_ context.Context) error {
512+
return nil
513+
}
514+
515+
func (l *LimaQemuDriver) CanRunGUI() bool {
516+
return false
517+
}
518+
519+
func (l *LimaQemuDriver) RunGUI() error {
520+
return nil
521+
}
522+
523+
func (l *LimaQemuDriver) Register(_ context.Context) error {
524+
return nil
525+
}
526+
527+
func (l *LimaQemuDriver) Unregister(_ context.Context) error {
528+
return nil
529+
}
530+
531+
func (l *LimaQemuDriver) ForwardGuestAgent() bool {
532+
// if driver is not providing, use host agent
533+
return l.vSockPort == 0 && l.virtioPort == ""
534+
}
535+
536+
func (l *LimaQemuDriver) VSockPort() int {
537+
return l.vSockPort
538+
}
539+
540+
func (l *LimaQemuDriver) VirtioPort() string {
541+
return l.virtioPort
542+
}
File renamed without changes.

pkg/driver/vz/disk.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package vz
5+
6+
import (
7+
"context"
8+
"errors"
9+
"fmt"
10+
"os"
11+
"path/filepath"
12+
13+
"github.com/docker/go-units"
14+
15+
"github.com/lima-vm/lima/pkg/imgutil/proxyimgutil"
16+
"github.com/lima-vm/lima/pkg/iso9660util"
17+
"github.com/lima-vm/lima/pkg/store"
18+
"github.com/lima-vm/lima/pkg/store/filenames"
19+
)
20+
21+
func EnsureDisk(_ context.Context, inst *store.Instance) error {
22+
diffDisk := filepath.Join(inst.Dir, filenames.DiffDisk)
23+
if _, err := os.Stat(diffDisk); err == nil || !errors.Is(err, os.ErrNotExist) {
24+
// disk is already ensured
25+
return err
26+
}
27+
28+
diskUtil := proxyimgutil.NewDiskUtil()
29+
30+
baseDisk := filepath.Join(inst.Dir, filenames.BaseDisk)
31+
32+
diskSize, _ := units.RAMInBytes(*inst.Config.Disk)
33+
if diskSize == 0 {
34+
return nil
35+
}
36+
isBaseDiskISO, err := iso9660util.IsISO9660(baseDisk)
37+
if err != nil {
38+
return err
39+
}
40+
if isBaseDiskISO {
41+
// Create an empty data volume (sparse)
42+
diffDiskF, err := os.Create(diffDisk)
43+
if err != nil {
44+
return err
45+
}
46+
47+
err = diskUtil.MakeSparse(diffDiskF, 0)
48+
if err != nil {
49+
diffDiskF.Close()
50+
return fmt.Errorf("failed to create sparse diff disk %q: %w", diffDisk, err)
51+
}
52+
return diffDiskF.Close()
53+
}
54+
if err = diskUtil.ConvertToRaw(baseDisk, diffDisk, &diskSize, false); err != nil {
55+
return fmt.Errorf("failed to convert %q to a raw disk %q: %w", baseDisk, diffDisk, err)
56+
}
57+
return err
58+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)