From a3aca7bf229808868715dca4184790aab887d5fc Mon Sep 17 00:00:00 2001 From: Christophe Fergeau Date: Tue, 18 Oct 2022 15:39:47 +0200 Subject: [PATCH] constants: Add DaemonVsockPort constant It's currently hardcoded in the libvirt and vfkit drivers. This centralizes its value in a single place --- pkg/crc/constants/constants.go | 1 + pkg/crc/machine/vfkit/driver_darwin.go | 2 ++ pkg/crc/preflight/preflight_checks_linux.go | 8 +++++--- pkg/drivers/vfkit/driver_darwin.go | 12 ++++++++---- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/crc/constants/constants.go b/pkg/crc/constants/constants.go index be67ad44fb..43f9c137d9 100644 --- a/pkg/crc/constants/constants.go +++ b/pkg/crc/constants/constants.go @@ -29,6 +29,7 @@ const ( CRCWindowsTrayDownloadURL = "https://github.com/crc-org/tray-electron/releases/download/%s/crc-tray-windows.zip" DefaultContext = "admin" DaemonHTTPEndpoint = "http://unix/api" + DaemonVsockPort = 1024 DefaultPodmanNamedPipe = `\\.\pipe\crc-podman` RootlessPodmanSocket = "/run/user/1000/podman/podman.sock" RootfulPodmanSocket = "/run/podman/podman.sock" diff --git a/pkg/crc/machine/vfkit/driver_darwin.go b/pkg/crc/machine/vfkit/driver_darwin.go index e0fa1fd6ae..066e4c8fe4 100644 --- a/pkg/crc/machine/vfkit/driver_darwin.go +++ b/pkg/crc/machine/vfkit/driver_darwin.go @@ -22,7 +22,9 @@ func CreateHost(machineConfig config.MachineConfig) *vfkit.Driver { vfDriver.VfkitPath = filepath.Join(constants.BinDir(), VfkitCommand) vfDriver.VirtioNet = machineConfig.NetworkMode == network.SystemNetworkingMode + vfDriver.VsockPath = constants.TapSocketPath + vfDriver.DaemonVsockPort = constants.DaemonVsockPort vfDriver.SharedDirs = configureShareDirs(machineConfig) diff --git a/pkg/crc/preflight/preflight_checks_linux.go b/pkg/crc/preflight/preflight_checks_linux.go index 16fc0e49ee..b7a293843f 100644 --- a/pkg/crc/preflight/preflight_checks_linux.go +++ b/pkg/crc/preflight/preflight_checks_linux.go @@ -283,12 +283,12 @@ func systemdUnitRunning(sd *systemd.Commander, unitName string) bool { } const ( - vsockUnitName = "crc-vsock.socket" - vsockUnit = `[Unit] + vsockUnitName = "crc-vsock.socket" + vsockUnitTemplate = `[Unit] Description=CRC vsock socket [Socket] -ListenStream=vsock::1024 +ListenStream=vsock::%d Service=crc-daemon.service [Install] @@ -322,6 +322,8 @@ ExecStart=%s daemon ` ) +var vsockUnit = fmt.Sprintf(vsockUnitTemplate, constants.DaemonVsockPort) + func checkSystemdUnit(unitName string, unitContent string, shouldBeRunning bool) error { sd := systemd.NewHostSystemdCommander().User() diff --git a/pkg/drivers/vfkit/driver_darwin.go b/pkg/drivers/vfkit/driver_darwin.go index d4ee51a504..0f6e1a3749 100644 --- a/pkg/drivers/vfkit/driver_darwin.go +++ b/pkg/drivers/vfkit/driver_darwin.go @@ -28,6 +28,7 @@ import ( "time" "github.com/Masterminds/semver/v3" + "github.com/crc-org/crc/pkg/crc/constants" "github.com/crc-org/crc/pkg/crc/logging" crcos "github.com/crc-org/crc/pkg/os" "github.com/crc-org/machine/libmachine/drivers" @@ -46,8 +47,9 @@ type Driver struct { InitrdPath string VfkitPath string VirtioNet bool - // TODO: Add vsock port(s) - VsockPath string + + VsockPath string + DaemonVsockPort uint } func NewDriver(hostName, storePath string) *Driver { @@ -62,6 +64,9 @@ func NewDriver(hostName, storePath string) *Driver { CPU: DefaultCPUs, Memory: DefaultMemory, }, + // needed when loading a VM which was created before + // DaemonVsockPort was introduced + DaemonVsockPort: constants.DaemonVsockPort, } } @@ -241,8 +246,7 @@ func (d *Driver) Start() error { } // virtio-vsock device - const vsockPort = 1024 - dev, err = client.VirtioVsockNew(vsockPort, d.VsockPath, true) + dev, err = client.VirtioVsockNew(d.DaemonVsockPort, d.VsockPath, true) if err != nil { return err }