Skip to content

Commit

Permalink
timesync: Fix automatic vsock device creation
Browse files Browse the repository at this point in the history
Timesync uses a vsock port for guest/host time synchronization.
When timesync is in use, if the VM has no vsock device, a new one is
automatically added.

However this got broken in commit a5db53d as we are generating the
hypervisor configuration before adding this device.
The vsock device must be part of `virtualMachineConfig` before calling
`cfg.SetSocketDevicesVirtualMachineConfiguration(cfg.socketDevicesConfiguration)`,
and after this commit, the vsock device is only added to
`virtualMachineConfig` afterwards.

This means the timesync only worked if you had another vsock device
configured.

This fixes #190

Signed-off-by: Christophe Fergeau <[email protected]>
  • Loading branch information
cfergeau committed Sep 23, 2024
1 parent 15089c6 commit db36849
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions pkg/vf/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ func (cfg *VirtualMachineConfiguration) toVz() (*vz.VirtualMachineConfiguration,
return nil, err
}
}
if cfg.config.Timesync != nil && cfg.config.Timesync.VsockPort != 0 {
// automatically add the vsock device we'll need for communication over VsockPort
vsockDev := VirtioVsock{
Port: cfg.config.Timesync.VsockPort,
Listen: false,
}
if err := vsockDev.AddToVirtualMachineConfig(cfg); err != nil {
return nil, err
}
}

cfg.SetStorageDevicesVirtualMachineConfiguration(cfg.storageDevicesConfiguration)
cfg.SetDirectorySharingDevicesVirtualMachineConfiguration(cfg.directorySharingDevicesConfiguration)
cfg.SetPointingDevicesVirtualMachineConfiguration(cfg.pointingDevicesConfiguration)
Expand All @@ -115,17 +126,6 @@ func (cfg *VirtualMachineConfiguration) toVz() (*vz.VirtualMachineConfiguration,
// https://developer.apple.com/documentation/virtualization/vzvirtiosocketdeviceconfiguration?language=objc
cfg.SetSocketDevicesVirtualMachineConfiguration(cfg.socketDevicesConfiguration)

if cfg.config.Timesync != nil && cfg.config.Timesync.VsockPort != 0 {
// automatically add the vsock device we'll need for communication over VsockPort
vsockDev := VirtioVsock{
Port: cfg.config.Timesync.VsockPort,
Listen: false,
}
if err := vsockDev.AddToVirtualMachineConfig(cfg); err != nil {
return nil, err
}
}

valid, err := cfg.Validate()
if err != nil {
return nil, err
Expand Down

0 comments on commit db36849

Please sign in to comment.