Skip to content

Commit d57d50d

Browse files
committed
Support for display with vz driver
Signed-off-by: Balaji Vijayakumar <[email protected]>
1 parent 01dbd4d commit d57d50d

File tree

6 files changed

+52
-20
lines changed

6 files changed

+52
-20
lines changed

pkg/driver/driver.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ type Driver interface {
1313

1414
CreateDisk() error
1515

16-
Start(_ context.Context) (chan error, error)
16+
Start(_ context.Context) (bool, chan error, error)
17+
18+
StartGUI() error
1719

1820
Stop(_ context.Context) error
1921

@@ -45,8 +47,12 @@ func (d *BaseDriver) CreateDisk() error {
4547
return nil
4648
}
4749

48-
func (d *BaseDriver) Start(_ context.Context) (chan error, error) {
49-
return nil, nil
50+
func (d *BaseDriver) Start(_ context.Context) (bool, chan error, error) {
51+
return false, nil, nil
52+
}
53+
54+
func (d *BaseDriver) StartGUI() error {
55+
return nil
5056
}
5157

5258
func (d *BaseDriver) Stop(_ context.Context) error {

pkg/hostagent/hostagent.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func (a *HostAgent) Run(ctx context.Context) error {
286286
}
287287
defer dnsServer.Shutdown()
288288
}
289-
errCh, err := a.driver.Start(ctx)
289+
startGui, errCh, err := a.driver.Start(ctx)
290290
if err != nil {
291291
return err
292292
}
@@ -335,12 +335,22 @@ func (a *HostAgent) Run(ctx context.Context) error {
335335
logrus.Infof("VNC Password: `%s`", vncpwdfile)
336336
}
337337

338+
if startGui {
339+
go func() {
340+
err = a.startRoutinesAndWait(ctx, errCh)
341+
logrus.Error(err)
342+
}()
343+
return a.driver.StartGUI()
344+
}
345+
return a.startRoutinesAndWait(ctx, errCh)
346+
}
347+
348+
func (a *HostAgent) startRoutinesAndWait(ctx context.Context, errCh chan error) error {
338349
stBase := events.Status{
339350
SSHLocalPort: a.sshLocalPort,
340351
}
341352
stBooting := stBase
342353
a.emitEvent(ctx, events.Event{Status: stBooting})
343-
344354
ctxHA, cancelHA := context.WithCancel(ctx)
345355
go func() {
346356
stRunning := stBase
@@ -351,7 +361,6 @@ func (a *HostAgent) Run(ctx context.Context) error {
351361
stRunning.Running = true
352362
a.emitEvent(ctx, events.Event{Status: stRunning})
353363
}()
354-
355364
for {
356365
select {
357366
case driverErr := <-errCh:

pkg/qemu/qemu_driver.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (l *LimaQemuDriver) CreateDisk() error {
5858
return nil
5959
}
6060

61-
func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
61+
func (l *LimaQemuDriver) Start(ctx context.Context) (bool, chan error, error) {
6262
qCfg := Config{
6363
Name: l.Instance.Name,
6464
InstanceDir: l.Instance.Dir,
@@ -67,35 +67,35 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
6767
}
6868
qExe, qArgs, err := Cmdline(qCfg)
6969
if err != nil {
70-
return nil, err
70+
return false, nil, err
7171
}
7272

7373
var qArgsFinal []string
7474
applier := &qArgTemplateApplier{}
7575
for _, unapplied := range qArgs {
7676
applied, err := applier.applyTemplate(unapplied)
7777
if err != nil {
78-
return nil, err
78+
return false, nil, err
7979
}
8080
qArgsFinal = append(qArgsFinal, applied)
8181
}
8282
qCmd := exec.CommandContext(ctx, qExe, qArgsFinal...)
8383
qCmd.ExtraFiles = append(qCmd.ExtraFiles, applier.files...)
8484
qStdout, err := qCmd.StdoutPipe()
8585
if err != nil {
86-
return nil, err
86+
return false, nil, err
8787
}
8888
go logPipeRoutine(qStdout, "qemu[stdout]")
8989
qStderr, err := qCmd.StderrPipe()
9090
if err != nil {
91-
return nil, err
91+
return false, nil, err
9292
}
9393
go logPipeRoutine(qStderr, "qemu[stderr]")
9494

9595
logrus.Infof("Starting QEMU (hint: to watch the boot progress, see %q)", filepath.Join(qCfg.InstanceDir, filenames.SerialLog))
9696
logrus.Debugf("qCmd.Args: %v", qCmd.Args)
9797
if err := qCmd.Start(); err != nil {
98-
return nil, err
98+
return false, nil, err
9999
}
100100
l.qCmd = qCmd
101101
l.qWaitCh = make(chan error)
@@ -108,7 +108,7 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
108108
err = client.ResolveAndForwardSSH(limayaml.MACAddress(l.Instance.Dir), l.SSHLocalPort)
109109
}
110110
}()
111-
return l.qWaitCh, nil
111+
return false, l.qWaitCh, nil
112112
}
113113

114114
func (l *LimaQemuDriver) Stop(ctx context.Context) error {

pkg/vz/vm_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura
499499
}
500500

501501
func attachDisplay(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration) error {
502-
if driver.Yaml.Video.Display != nil {
502+
if *driver.Yaml.Video.Display == "vz" {
503503
graphicsDeviceConfiguration, err := vz.NewVirtioGraphicsDeviceConfiguration()
504504
if err != nil {
505505
return err

pkg/vz/vz_driver_darwin.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"path/filepath"
11+
"runtime"
1112
"time"
1213

1314
"github.com/lima-vm/lima/pkg/reflectutil"
@@ -29,6 +30,11 @@ type LimaVzDriver struct {
2930
machine *virtualMachineWrapper
3031
}
3132

33+
func init() {
34+
//Without this the call to StartGraphicApplication fails. This has to be called before the vz cgo loads
35+
runtime.LockOSThread()
36+
}
37+
3238
func New(driver *driver.BaseDriver) *LimaVzDriver {
3339
return &LimaVzDriver{
3440
BaseDriver: driver,
@@ -72,6 +78,7 @@ func (l *LimaVzDriver) Validate() error {
7278
"Rosetta",
7379
"AdditionalDisks",
7480
"Audio",
81+
"Video",
7582
); len(unknown) > 0 {
7683
logrus.Warnf("Ignoring: vmType %s: %+v", *l.Yaml.VMType, unknown)
7784
}
@@ -118,6 +125,11 @@ func (l *LimaVzDriver) Validate() error {
118125
if audioDevice != "" && audioDevice != "vz" {
119126
logrus.Warnf("field `audio.device` must be %q for VZ driver , got %q", "vz", audioDevice)
120127
}
128+
129+
videoDisplay := *l.Yaml.Video.Display
130+
if videoDisplay != "" && videoDisplay != "vz" {
131+
logrus.Warnf("field `video.display` must be %q for VZ driver , got %q", "vz", videoDisplay)
132+
}
121133
return nil
122134
}
123135

@@ -129,18 +141,23 @@ func (l *LimaVzDriver) CreateDisk() error {
129141
return nil
130142
}
131143

132-
func (l *LimaVzDriver) Start(ctx context.Context) (chan error, error) {
144+
func (l *LimaVzDriver) Start(ctx context.Context) (bool, chan error, error) {
133145
logrus.Infof("Starting VZ (hint: to watch the boot progress, see %q)", filepath.Join(l.Instance.Dir, filenames.SerialLog))
134146
vm, errCh, err := startVM(ctx, l.BaseDriver)
135147
if err != nil {
136148
if errors.Is(err, vz.ErrUnsupportedOSVersion) {
137-
return nil, fmt.Errorf("vz driver requires macOS 13 or higher to run: %q", err)
149+
return false, nil, fmt.Errorf("vz driver requires macOS 13 or higher to run: %q", err)
138150
}
139-
return nil, err
151+
return false, nil, err
140152
}
141153
l.machine = vm
142154

143-
return errCh, nil
155+
startGui := *l.Yaml.Video.Display == "vz"
156+
return startGui, errCh, nil
157+
}
158+
159+
func (l *LimaVzDriver) StartGUI() error {
160+
return l.machine.StartGraphicApplication(1920, 1200)
144161
}
145162

146163
func (l *LimaVzDriver) Stop(_ context.Context) error {

pkg/vz/vz_driver_others.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func (l *LimaVzDriver) CreateDisk() error {
3232
return ErrUnsupported
3333
}
3434

35-
func (l *LimaVzDriver) Start(ctx context.Context) (chan error, error) {
36-
return nil, ErrUnsupported
35+
func (l *LimaVzDriver) Start(ctx context.Context) (bool, chan error, error) {
36+
return false, nil, ErrUnsupported
3737
}
3838

3939
func (l *LimaVzDriver) Stop(_ context.Context) error {

0 commit comments

Comments
 (0)