Skip to content

Commit

Permalink
Use VirtioConsoleDevice
Browse files Browse the repository at this point in the history
VirtioConsoleDevice is used to configure serial interface, also 'isConsole' for port configuration is set to 'true'.

Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Feb 18, 2025
1 parent 33b62fd commit 59e344b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
62 changes: 37 additions & 25 deletions pkg/vf/virtio.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,26 +255,6 @@ func (dev *VirtioSerial) toVz() (*vz.VirtioConsoleDeviceSerialPortConfiguration,
return nil, err
}
serialPortAttachment, retErr = vz.NewFileHandleSerialPortAttachment(os.Stdin, os.Stdout)
case dev.UsesPty:
master, slave, err := termios.Pty()
if err != nil {
return nil, err
}
// as far as I can tell, we have no use for the slave fd in the
// vfkit process, the user will open minicom/screen/... /dev/ttys00?
// when needed
defer slave.Close()

// the master fd must stay open for vfkit's lifetime
gocleanup.Register(func() { _ = master.Close() })

dev.PtyName = slave.Name()

if err := setRawMode(master); err != nil {
return nil, err
}
serialPortAttachment, retErr = vz.NewFileHandleSerialPortAttachment(master, master)

default:
serialPortAttachment, retErr = vz.NewFileSerialPortAttachment(dev.LogFile, false)
}
Expand All @@ -285,6 +265,32 @@ func (dev *VirtioSerial) toVz() (*vz.VirtioConsoleDeviceSerialPortConfiguration,
return vz.NewVirtioConsoleDeviceSerialPortConfiguration(serialPortAttachment)
}

func (dev *VirtioSerial) toVzConsole() (*vz.VirtioConsolePortConfiguration, error) {
master, slave, err := termios.Pty()
if err != nil {
return nil, err
}

// the master fd and slave fd must stay open for vfkit's lifetime
gocleanup.Register(func() {
_ = master.Close()
_ = slave.Close()
})

dev.PtyName = slave.Name()

if err := setRawMode(master); err != nil {
return nil, err
}
serialPortAttachment, retErr := vz.NewFileHandleSerialPortAttachment(master, master)
if retErr != nil {
return nil, retErr
}
return vz.NewVirtioConsolePortConfiguration(
vz.WithVirtioConsolePortConfigurationAttachment(serialPortAttachment),
vz.WithVirtioConsolePortConfigurationIsConsole(true))
}

func (dev *VirtioSerial) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
if dev.LogFile != "" {
log.Infof("Adding virtio-serial device (logFile: %s)", dev.LogFile)
Expand All @@ -296,14 +302,20 @@ func (dev *VirtioSerial) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfi
return fmt.Errorf("VirtioSerial.PtyName must be empty (current value: %s)", dev.PtyName)
}

consoleConfig, err := dev.toVz()
if err != nil {
return err
}
if dev.UsesPty {
consolePortConfig, err := dev.toVzConsole()
if err != nil {
return err
}
vmConfig.consolePortsConfiguration = append(vmConfig.consolePortsConfiguration, consolePortConfig)
log.Infof("Using PTY (pty path: %s)", dev.PtyName)
} else {
consoleConfig, err := dev.toVz()
if err != nil {
return err
}
vmConfig.serialPortsConfiguration = append(vmConfig.serialPortsConfiguration, consoleConfig)
}
vmConfig.serialPortsConfiguration = append(vmConfig.serialPortsConfiguration, consoleConfig)

return nil
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/vf/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type VirtualMachineConfiguration struct {
entropyDevicesConfiguration []*vz.VirtioEntropyDeviceConfiguration
serialPortsConfiguration []*vz.VirtioConsoleDeviceSerialPortConfiguration
socketDevicesConfiguration []vz.SocketDeviceConfiguration
consolePortsConfiguration []*vz.VirtioConsolePortConfiguration
}

func NewVirtualMachineConfiguration(vmConfig *config.VirtualMachine) (*VirtualMachineConfiguration, error) {
Expand Down Expand Up @@ -129,6 +130,18 @@ func (cfg *VirtualMachineConfiguration) toVz() (*vz.VirtualMachineConfiguration,
cfg.SetNetworkDevicesVirtualMachineConfiguration(cfg.networkDevicesConfiguration)
cfg.SetEntropyDevicesVirtualMachineConfiguration(cfg.entropyDevicesConfiguration)
cfg.SetSerialPortsVirtualMachineConfiguration(cfg.serialPortsConfiguration)

if len(cfg.consolePortsConfiguration) > 0 {
consoleDeviceConfiguration, err := vz.NewVirtioConsoleDeviceConfiguration()
if err != nil {
return nil, err
}
for i, portCfg := range cfg.consolePortsConfiguration {
consoleDeviceConfiguration.SetVirtioConsolePortConfiguration(i, portCfg)
}
cfg.SetConsoleDevicesVirtualMachineConfiguration([]vz.ConsoleDeviceConfiguration{consoleDeviceConfiguration})
}

// len(cfg.socketDevicesConfiguration should be 0 or 1
// https://developer.apple.com/documentation/virtualization/vzvirtiosocketdeviceconfiguration?language=objc
cfg.SetSocketDevicesVirtualMachineConfiguration(cfg.socketDevicesConfiguration)
Expand Down

0 comments on commit 59e344b

Please sign in to comment.