Virtualization API - Share folders when VM running

I want to expose a directory to MacOS VM when it is running.

Apple's documentation mentions this (here) :

The VZDirectoryShare on the configuration defines the host directories to expose to the guest. To limit or expose new directories to the guest while the VM is running, you can update the directory share with VZVirtioFileSystemDevice.

But I tried the following when VM is running:

  1. Changing the original configuration of directory (saved the config applied, then changed it)
  2. Added new directory share in the original configuration
  3. Restarted running VM after (1) and (2)

None of them are working. Anyone has any idea how to make this work ? Or this is not currently supported ?

Accepted Reply

The VZVirtualMachineConfiguration is used by the initializer of VZVirtualMachine to define the virtual hardware. It is not used after that by the VZVirtualMachine object created from it.

To update a state of a VZVirtualMachine, you will want to use the runtime APIs. In the case of sharing directories, VZVirtualMachine.directorySharingDevices contains the runtime counterparts of the VZVirtualMachineConfiguration.directorySharingDevices.

Let's say we have a single Virtio File System device. The code would look something like:

guard let virtioFileSystemDevice = virtualMachine.directorySharingDevices[0] as? VZVirtioFileSystemDevice else {
    return
}

virtioFileSystemDevice.share = myNewShare

For changing shared directories at runtime, the VZMultipleDirectoryShare is generally preferred as it is more flexible.

Replies

The VZVirtualMachineConfiguration is used by the initializer of VZVirtualMachine to define the virtual hardware. It is not used after that by the VZVirtualMachine object created from it.

To update a state of a VZVirtualMachine, you will want to use the runtime APIs. In the case of sharing directories, VZVirtualMachine.directorySharingDevices contains the runtime counterparts of the VZVirtualMachineConfiguration.directorySharingDevices.

Let's say we have a single Virtio File System device. The code would look something like:

guard let virtioFileSystemDevice = virtualMachine.directorySharingDevices[0] as? VZVirtioFileSystemDevice else {
    return
}

virtioFileSystemDevice.share = myNewShare

For changing shared directories at runtime, the VZMultipleDirectoryShare is generally preferred as it is more flexible.