VZDiskImageStorageDeviceAttachment lifecycle after guest shutdown

After a normal macOS guest shutdown, what is the documented lifecycle for VZDiskImageStorageDeviceAttachment? What cleanup is required before the host can safely read the backing disk image, and is there a supported completion notification for outstanding I/O?

Answered by DTS Engineer in 908592022

My initial take on this would that it’d be quite straightforward: By the time you’re notified of the guest stopping, the disk image will be free to use. And by that I mean:

  • If the guest stops of its own accord, when you’re notified of that via the guestDidStop(_:) delegate callback.
  • If you force the guest to stop by calling stop(completionHandler:), when you’re notifier via the completion handler being called.

But I’m glad I checked because it seems there are some wrinkles. Things should work as I expected on macOS 27 and later. On earlier systems it’s possible for you to receive the notification before the guess is fully done with the disk image. In that case you can avoid running into problems by trying to acquire a advisory lock (using flock) on the disk image. If the guest is still using it, that will wait (or fail if you set LOCK_NB).


Oh, and one thing to be careful of. Stopping the guest by calling stop(completionHandler:) is a bit like pulling out the power cable. The guest doesn’t get to shut down cleanly, so the file system on the disk image may not be in a consistent state.

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

My initial take on this would that it’d be quite straightforward: By the time you’re notified of the guest stopping, the disk image will be free to use. And by that I mean:

  • If the guest stops of its own accord, when you’re notified of that via the guestDidStop(_:) delegate callback.
  • If you force the guest to stop by calling stop(completionHandler:), when you’re notifier via the completion handler being called.

But I’m glad I checked because it seems there are some wrinkles. Things should work as I expected on macOS 27 and later. On earlier systems it’s possible for you to receive the notification before the guess is fully done with the disk image. In that case you can avoid running into problems by trying to acquire a advisory lock (using flock) on the disk image. If the guest is still using it, that will wait (or fail if you set LOCK_NB).


Oh, and one thing to be careful of. Stopping the guest by calling stop(completionHandler:) is a bit like pulling out the power cable. The guest doesn’t get to shut down cleanly, so the file system on the disk image may not be in a consistent state.

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