diff --git a/pkg/vf/virtio.go b/pkg/vf/virtio.go index b33b38bd..81ffa377 100644 --- a/pkg/vf/virtio.go +++ b/pkg/vf/virtio.go @@ -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) } @@ -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) @@ -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 } diff --git a/pkg/vf/vm.go b/pkg/vf/vm.go index 7abba298..9e74612a 100644 --- a/pkg/vf/vm.go +++ b/pkg/vf/vm.go @@ -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) { @@ -129,6 +130,15 @@ func (cfg *VirtualMachineConfiguration) toVz() (*vz.VirtualMachineConfiguration, cfg.SetNetworkDevicesVirtualMachineConfiguration(cfg.networkDevicesConfiguration) cfg.SetEntropyDevicesVirtualMachineConfiguration(cfg.entropyDevicesConfiguration) cfg.SetSerialPortsVirtualMachineConfiguration(cfg.serialPortsConfiguration) + 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)