Virtual serial ports

Hello,

I want to add virtual serial ports to a macOS VM (host = Sonoma, guest = Sonoma).

Here is what I tried so far:

option 1

I create a VZVirtioConsoleDeviceConfiguration and I add a port to it, with a VZFileHandleSerialPortAttachment connected to two pipes:

let consoleDeviceConfiguration = VZVirtioConsoleDeviceConfiguration()
let guestPort = VZVirtioConsolePortConfiguration()
guestPort.isConsole = false
guestPort.name = "myserialport"

let guestToHostPipe = Pipe()
let hostToGuestPipe = Pipe()
guestPort.attachment = VZFileHandleSerialPortAttachment(
    fileHandleForReading: hostToGuestPipe.fileHandleForReading,
    fileHandleForWriting: guestToHostPipe.fileHandleForWriting
)

consoleDeviceConfiguration.ports[0] = guestPort

// config is my VZVirtualMachineConfiguration
config.consoleDevices = [consoleDeviceConfiguration]

Then after the VZVirtualMachine is instantiated, I get the runtime VZVirtioConsoleDevice for my device and I set my service as its delegate.

In the guest system, the device is created with the expected name (/dev/cu.myserialport)

When I open it (e.g. screen /dev/cu.myserialport 9600), the consoleDevice(_:,didOpen:) delegate method is called as expected. Then I start sending data to hostToGuestPipe.fileHandleForWriting, but I get nothing in my guest system.

option 2

Instead of using config.consoleDevices, I add a VZVirtioConsoleDeviceSerialPortConfiguration to config.serialPorts and I use the same scheme as above with a VZFileHandleSerialPortAttachment and two pipes.

It works, but:

  • I can't find a way to rename the devices (/dev/cu.virtio, /dev/cu.virtio1, etc)
  • I can't detect when the endpoint is opened/closed.

Am I missing something?

I want to add virtual serial ports to a macOS VM

To what end?

This matters because the best path forward depends on your final goal. Option 2 makes the most sense if you want the guest to have a general serial port, whereas option 1 is focused primarily on providing a virtual serial console, which is kinda obscure on the Mac.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

To what end?

My goal is to make serial or serial-like devices (connected to the host) available to the VM. Option 2 works, but I'd like to be able to change the names of the devices.

As for option 1: when VZVirtioConsolePortConfiguration.isConsole is false, isn't it the same thing as a serial port?

Thanks!

isn't it the same thing as a serial port?

No. Well, also yes (-: They share a some internal infrastructure but there’s also a lot of internal differences.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Virtual serial ports
 
 
Q