Skip to content

Commit 56a5d9d

Browse files
authored
Merge pull request #1597 from balajiv113/vz-display
Support for display with vz driver
2 parents 01dbd4d + 2e70032 commit 56a5d9d

File tree

6 files changed

+72
-3
lines changed

6 files changed

+72
-3
lines changed

cmd/limactl/hostagent.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"os/signal"
11+
"runtime"
1112
"strconv"
1213

1314
"github.com/gorilla/mux"
@@ -27,6 +28,7 @@ func newHostagentCommand() *cobra.Command {
2728
}
2829
hostagentCommand.Flags().StringP("pidfile", "p", "", "write pid to file")
2930
hostagentCommand.Flags().String("socket", "", "hostagent socket")
31+
hostagentCommand.Flags().Bool("run-gui", false, "run gui synchronously within hostagent")
3032
hostagentCommand.Flags().String("nerdctl-archive", "", "local file path (not URL) of nerdctl-full-VERSION-linux-GOARCH.tar.gz")
3133
return hostagentCommand
3234
}
@@ -55,6 +57,16 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
5557

5658
instName := args[0]
5759

60+
runGUI, err := cmd.Flags().GetBool("run-gui")
61+
if err != nil {
62+
return err
63+
}
64+
if runGUI {
65+
//Without this the call to vz.RunGUI fails. Adding it here, as this has to be called before the vz cgo loads.
66+
runtime.LockOSThread()
67+
defer runtime.UnlockOSThread()
68+
}
69+
5870
sigintCh := make(chan os.Signal, 1)
5971
signal.Notify(sigintCh, os.Interrupt)
6072

pkg/driver/driver.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,31 @@ import (
88
"github.com/lima-vm/lima/pkg/store"
99
)
1010

11+
// Driver interface is used by hostagent for managing vm.
12+
//
13+
// This interface is extended by BaseDriver which provides default implementation.
14+
// All other driver definition must extend BaseDriver
1115
type Driver interface {
16+
// Validate returns error if the current driver isn't support for given config
1217
Validate() error
1318

19+
// CreateDisk returns error if the current driver fails in creating disk
1420
CreateDisk() error
1521

22+
// Start is used for booting the vm using driver instance
23+
// It returns a chan error on successful boot
24+
// The second argument may contain error occurred while starting driver
1625
Start(_ context.Context) (chan error, error)
1726

27+
// CanRunGUI returns bool to indicate if the hostagent need to run GUI synchronously
28+
CanRunGUI() bool
29+
30+
// RunGUI is for starting GUI synchronously by hostagent. This method should be wait and return only after vm terminates
31+
// It returns error if there are any failures
32+
RunGUI() error
33+
34+
// Stop will terminate the running vm instance.
35+
// It returns error if there are any errors during Stop
1836
Stop(_ context.Context) error
1937

2038
ChangeDisplayPassword(_ context.Context, password string) error
@@ -49,6 +67,14 @@ func (d *BaseDriver) Start(_ context.Context) (chan error, error) {
4967
return nil, nil
5068
}
5169

70+
func (d *BaseDriver) CanRunGUI() bool {
71+
return false
72+
}
73+
74+
func (d *BaseDriver) RunGUI() error {
75+
return nil
76+
}
77+
5278
func (d *BaseDriver) Stop(_ context.Context) error {
5379
return nil
5480
}

pkg/hostagent/hostagent.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,24 @@ func (a *HostAgent) Run(ctx context.Context) error {
335335
logrus.Infof("VNC Password: `%s`", vncpwdfile)
336336
}
337337

338+
if a.driver.CanRunGUI() {
339+
go func() {
340+
err = a.startRoutinesAndWait(ctx, errCh)
341+
if err != nil {
342+
logrus.Error(err)
343+
}
344+
}()
345+
return a.driver.RunGUI()
346+
}
347+
return a.startRoutinesAndWait(ctx, errCh)
348+
}
349+
350+
func (a *HostAgent) startRoutinesAndWait(ctx context.Context, errCh chan error) error {
338351
stBase := events.Status{
339352
SSHLocalPort: a.sshLocalPort,
340353
}
341354
stBooting := stBase
342355
a.emitEvent(ctx, events.Event{Status: stBooting})
343-
344356
ctxHA, cancelHA := context.WithCancel(ctx)
345357
go func() {
346358
stRunning := stBase
@@ -351,7 +363,6 @@ func (a *HostAgent) Run(ctx context.Context) error {
351363
stRunning.Running = true
352364
a.emitEvent(ctx, events.Event{Status: stRunning})
353365
}()
354-
355366
for {
356367
select {
357368
case driverErr := <-errCh:

pkg/start/start.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ func Start(ctx context.Context, inst *store.Instance) error {
117117
"hostagent",
118118
"--pidfile", haPIDPath,
119119
"--socket", haSockPath)
120+
if limaDriver.CanRunGUI() {
121+
args = append(args, "--run-gui")
122+
}
120123
if nerdctlArchiveCache != "" {
121124
args = append(args, "--nerdctl-archive", nerdctlArchiveCache)
122125
}

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func (l *LimaVzDriver) Validate() error {
7272
"Rosetta",
7373
"AdditionalDisks",
7474
"Audio",
75+
"Video",
7576
); len(unknown) > 0 {
7677
logrus.Warnf("Ignoring: vmType %s: %+v", *l.Yaml.VMType, unknown)
7778
}
@@ -118,6 +119,11 @@ func (l *LimaVzDriver) Validate() error {
118119
if audioDevice != "" && audioDevice != "vz" {
119120
logrus.Warnf("field `audio.device` must be %q for VZ driver , got %q", "vz", audioDevice)
120121
}
122+
123+
videoDisplay := *l.Yaml.Video.Display
124+
if videoDisplay != "" && videoDisplay != "vz" {
125+
logrus.Warnf("field `video.display` must be %q for VZ driver , got %q", "vz", videoDisplay)
126+
}
121127
return nil
122128
}
123129

@@ -143,6 +149,17 @@ func (l *LimaVzDriver) Start(ctx context.Context) (chan error, error) {
143149
return errCh, nil
144150
}
145151

152+
func (l *LimaVzDriver) CanRunGUI() bool {
153+
return *l.Yaml.Video.Display == "vz"
154+
}
155+
156+
func (l *LimaVzDriver) RunGUI() error {
157+
if l.CanRunGUI() {
158+
return l.machine.StartGraphicApplication(1920, 1200)
159+
}
160+
return fmt.Errorf("RunGUI is not support for the given driver '%s' and diplay '%s'", "vz", *l.Yaml.Video.Display)
161+
}
162+
146163
func (l *LimaVzDriver) Stop(_ context.Context) error {
147164
logrus.Info("Shutting down VZ")
148165
canStop := l.machine.CanRequestStop()

0 commit comments

Comments
 (0)