Control VM created with Virtualization Framework from different processes

I'm trying to control a virtual machine that has been created from two different processes, but fail to create a VZVirtualMachine object that refers to the same VM.

For example, the first process creates and starts the virtual machine:

let vmConfiguration = getFixedVMConfiguration()
let vm = VZVirtualMachine(vmConfiguration: vmConfiguration)
vm.start {
  // completion handler code ...
}

and another process tries to stop the virtual machine:

let vmConfiguration = getFixedVMConfiguration()
let vm = VZVirtualMachine(vmConfiguration: vmConfiguration)
vm.stop {
  // completion handler code ...
}

The vm in the second thread will refer to a separte VM even when both have been created with the same configuration. Stopping it here will not change the state of the vm in the other process. Is there a way to access created VMs and use them from other processes?

A workaround would be to handle VMs in a daemon, but I failed to pass a VZVirtualMachine object through XPC to another process, because it does not implement NSSecureCoding. How could that be achieved?

I'm trying to control a virtual machine that has been created from two different processes

Yeah, I wouldn’t expect that to work.

Can you explain more about your setup here? For example, you wrote “A workaround would be to handle VMs in a daemon” but it’s a long path from an app to a daemon with a lot of intermediate steps in between.

Share and Enjoy

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

we currently use an XPC Service along with an app

So you have an app and an XPC Service.

One use-case that requires to pass a VZVirtualMachine object directly between App and Daemon

But where does this daemon come into it?

Share and Enjoy

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

Control VM created with Virtualization Framework from different processes
 
 
Q