Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Guest Agent transport setting #3387

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -127,15 +128,52 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
virtioPort := ""
if *inst.Config.VMType == limayaml.VZ {
vSockPort = 2222
if inst.Config.GuestAgentTransportType != nil {
switch *inst.Config.GuestAgentTransportType {
case limayaml.VSOCKGA:
case limayaml.HOSTAGENTGA:
vSockPort = 0
default:
logrus.Warnf("Ignoring invalid for %s type GA transport %s", *inst.Config.VMType, *inst.Config.GuestAgentTransportType)
}
}
} else if *inst.Config.VMType == limayaml.WSL2 {
port, err := freeport.VSock()
if err != nil {
logrus.WithError(err).Error("failed to get free VSock port")
}
vSockPort = port
if inst.Config.GuestAgentTransportType != nil {
switch *inst.Config.GuestAgentTransportType {
case limayaml.VSOCKGA:
// TODO support hostagent forwarder https://github.com/lima-vm/lima/issues/3386
default:
logrus.Warnf("Ignoring invalid for %s type GA transport %s", *inst.Config.VMType, *inst.Config.GuestAgentTransportType)
}
}
} else if *inst.Config.VMType == limayaml.QEMU {
// virtserialport doesn't seem to work reliably: https://github.com/lima-vm/lima/issues/2064
virtioPort = "" // filenames.VirtioPort
if runtime.GOOS != "windows" {
// virtserialport doesn't seem to work reliably: https://github.com/lima-vm/lima/issues/2064
virtioPort = ""
} else {
// On Windows it is a more reasonable default
virtioPort = filenames.VirtioPort
}
if inst.Config.GuestAgentTransportType != nil {
switch *inst.Config.GuestAgentTransportType {
case limayaml.VIRTIOGA:
virtioPort = filenames.VirtioPort
case limayaml.HOSTAGENTGA:
if runtime.GOOS != "windows" {
virtioPort = ""
} else {
// TODO support hostagent forwarder https://github.com/lima-vm/lima/issues/3386
logrus.Warnf("Ignoring invalid for %s type GA transport %s", *inst.Config.VMType, *inst.Config.GuestAgentTransportType)
}
default:
logrus.Warnf("Ignoring invalid for %s type GA transport %s", *inst.Config.VMType, *inst.Config.GuestAgentTransportType)
}
}
}

if err := cidata.GenerateCloudConfig(inst.Dir, instName, inst.Config); err != nil {
Expand Down
28 changes: 17 additions & 11 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ type LimaYAML struct {
DNS []net.IP `yaml:"dns,omitempty" json:"dns,omitempty"`
HostResolver HostResolver `yaml:"hostResolver,omitempty" json:"hostResolver,omitempty"`
// `useHostResolver` was deprecated in Lima v0.8.1, removed in Lima v0.14.0. Use `hostResolver.enabled` instead.
PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty" jsonschema:"nullable"`
CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"`
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
Plain *bool `yaml:"plain,omitempty" json:"plain,omitempty" jsonschema:"nullable"`
TimeZone *string `yaml:"timezone,omitempty" json:"timezone,omitempty" jsonschema:"nullable"`
NestedVirtualization *bool `yaml:"nestedVirtualization,omitempty" json:"nestedVirtualization,omitempty" jsonschema:"nullable"`
User User `yaml:"user,omitempty" json:"user,omitempty"`
PropagateProxyEnv *bool `yaml:"propagateProxyEnv,omitempty" json:"propagateProxyEnv,omitempty" jsonschema:"nullable"`
CACertificates CACertificates `yaml:"caCerts,omitempty" json:"caCerts,omitempty"`
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
GuestAgentTransportType *GATransportType `yaml:"guestAgentTransportType,omitempty" json:"guestAgentTransportType,omitempty" jsonschema:"nullable"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a new struct GuestAgent?

GuestInstallPrefix can be moved there too. (The old form should be supported too for a while)

Plain *bool `yaml:"plain,omitempty" json:"plain,omitempty" jsonschema:"nullable"`
TimeZone *string `yaml:"timezone,omitempty" json:"timezone,omitempty" jsonschema:"nullable"`
NestedVirtualization *bool `yaml:"nestedVirtualization,omitempty" json:"nestedVirtualization,omitempty" jsonschema:"nullable"`
User User `yaml:"user,omitempty" json:"user,omitempty"`
}

type BaseTemplates []LocatorWithDigest
Expand All @@ -62,10 +63,11 @@ type LocatorWithDigest struct {
}

type (
OS = string
Arch = string
MountType = string
VMType = string
OS = string
Arch = string
MountType = string
VMType = string
GATransportType = string
)

type CPUType = map[Arch]string
Expand All @@ -87,6 +89,10 @@ const (
QEMU VMType = "qemu"
VZ VMType = "vz"
WSL2 VMType = "wsl2"

VIRTIOGA GATransportType = "virtio-ga"
VSOCKGA GATransportType = "vsock-ga"
HOSTAGENTGA GATransportType = "hostagent-ga"
)

var (
Expand Down
13 changes: 8 additions & 5 deletions pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Config struct {
LimaYAML *limayaml.LimaYAML
SSHLocalPort int
SSHAddress string
VirtioGA bool
}

// MinimumQemuVersion is the minimum supported QEMU version.
Expand Down Expand Up @@ -987,11 +988,13 @@ func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err er
args = append(args, "-chardev", fmt.Sprintf("socket,id=%s,path=%s,server=on,wait=off", qmpChardev, qmpSock))
args = append(args, "-qmp", "chardev:"+qmpChardev)

// Guest agent via serialport
guestSock := filepath.Join(cfg.InstanceDir, filenames.GuestAgentSock)
args = append(args, "-chardev", fmt.Sprintf("socket,path=%s,server=on,wait=off,id=qga0", guestSock))
args = append(args, "-device", "virtio-serial")
args = append(args, "-device", "virtserialport,chardev=qga0,name="+filenames.VirtioPort)
if cfg.VirtioGA {
// Guest agent via serialport
guestSock := filepath.Join(cfg.InstanceDir, filenames.GuestAgentSock)
args = append(args, "-chardev", fmt.Sprintf("socket,path=%s,server=on,wait=off,id=qga0", guestSock))
args = append(args, "-device", "virtio-serial")
args = append(args, "-device", "virtserialport,chardev=qga0,name="+filenames.VirtioPort)
}

// QEMU process
args = append(args, "-name", "lima-"+cfg.Name)
Expand Down
1 change: 1 addition & 0 deletions pkg/qemu/qemu_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
LimaYAML: l.Instance.Config,
SSHLocalPort: l.SSHLocalPort,
SSHAddress: l.Instance.SSHAddress,
VirtioGA: l.VirtioPort != "",
}
qExe, qArgs, err := Cmdline(ctx, qCfg)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion pkg/vz/vz_driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/reflectutil"
"github.com/lima-vm/lima/pkg/store/filenames"
)

var knownYamlProperties = []string{
Expand All @@ -36,6 +37,7 @@ var knownYamlProperties = []string{
"Env",
"Firmware",
"GuestInstallPrefix",
"GuestAgentTransportType",
"HostResolver",
"Images",
"Memory",
Expand Down Expand Up @@ -225,7 +227,12 @@ func (l *LimaVzDriver) Stop(_ context.Context) error {
return errors.New("vz: CanRequestStop is not supported")
}

func (l *LimaVzDriver) GuestAgentConn(_ context.Context) (net.Conn, error) {
func (l *LimaVzDriver) GuestAgentConn(ctx context.Context) (net.Conn, error) {
if l.VSockPort == 0 {
var d net.Dialer
dialContext, err := d.DialContext(ctx, "unix", filepath.Join(l.Instance.Dir, filenames.GuestAgentSock))
return dialContext, err
}
for _, socket := range l.machine.SocketDevices() {
connect, err := socket.Connect(uint32(l.VSockPort))
if err == nil && connect.SourcePort() != 0 {
Expand Down
7 changes: 7 additions & 0 deletions templates/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,13 @@ rosetta:
# 🟢 Builtin default: false
binfmt: null

# Specify communication transport to connect to GuestAgent.
# Supported values are "virtio-ga", "vsock-ga" and "hostagent-ga".
# If selected option will not be supported for VMType and Host OS a Warning will be shown and the
# default mode will be selected.
# 🟢 Builtin default: not set which results in VMType and Host OS specific behavior
guestAgentTransportType: null

# Specify the timezone name (as used by the zoneinfo database). Specify the empty string
# to not set a timezone in the instance.
# 🟢 Builtin default: use name from /etc/timezone or deduce from symlink target of /etc/localtime
Expand Down
Loading