Running macOS in a virtual machine on Apple silicon
At a glance
| Item | Summary |
|---|---|
| Purpose | Install and run macOS in a virtual machine using the Virtualization framework. |
| App architecture | A C/Objective-C header, Objective-C, Swift sample bundle with entry-bearing project variants Objective-C, Swift, each leading to Virtualization APIs. |
| Main patterns | Delegate or data-source callbacks |
| Project style | 21 scanned source file(s) across C/Objective-C header, Objective-C, Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: DispatchQueue.main.async; none alone proves a background thread. |
| State/event model | No structured observation or publisher-scheduling marker indexed. |
| Key frameworks/packages | Foundation, Virtualization, Cocoa, AppKit, sys; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── Swift/
│ ├── macOSVirtualMachineSampleApp/
│ │ └── AppDelegate.swift
│ ├── InstallationTool/
│ │ └── main.swift
│ └── Common/
│ └── MacOSVirtualMachineDelegate.swift
└── Objective-C/
├── InstallationTool/
│ ├── main.m
│ └── MacOSRestoreImage.h
├── macOSVirtualMachineSampleApp/
│ ├── main.m
│ ├── AppDelegate.h
│ └── AppDelegate.m
└── Common/
├── MacOSVirtualMachineDelegate.h
├── MacOSVirtualMachineDelegate.m
├── MacOSVirtualMachineConfigurationHelper.h
└── MacOSVirtualMachineConfigurationHelper.m
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: C/Objective-C header, Objective-C, Swift.
- The verified tree contains 9 project/configuration file(s) and 10 source declaration(s).
Overall architecture
flowchart LR
Bundle["Sample bundle"]
V1["Objective-C"]
V2["Swift"]
Boundary["Virtualization APIs"]
Bundle --> V1
V1 --> Boundary
Bundle --> V2
V2 --> Boundary
Reference code
Swift/macOSVirtualMachineSampleApp/AppDelegate.swift:12 — architecture anchor
@main
class AppDelegate: NSObject, NSApplicationDelegate {
// ...
@IBOutlet var window: NSWindow!
// ...
}Interpretation
The branches represent separate entry-bearing project variants in the downloaded bundle, not runtime calls between those variants. Each branch is intentionally collapsed at the documented framework boundary; the detailed target-local flow remains in the cited files. Ownership is claimed only where the next section cites a stored property or assignment.
Ownership and state
classDiagram
AppDelegate o-- NSWindow : window
AppDelegate o-- VZVirtualMachineView : virtualMachineView
AppDelegate o-- MacOSVirtualMachineDelegate : virtualMachineResponder
AppDelegate o-- VZVirtualMachine : virtualMachine
Ownership evidence
Swift/macOSVirtualMachineSampleApp/AppDelegate.swift:15 — stored dependency or nearest verified ownership anchor
@main
class AppDelegate: NSObject, NSApplicationDelegate {
// ...
@IBOutlet var window: NSWindow!
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
AppDelegate |
NSWindow (window) |
stores or receives | App/module collaborators |
AppDelegate |
VZVirtualMachineView (virtualMachineView) |
holds a non-owning reference | The referenced object’s lifecycle is owned elsewhere |
AppDelegate |
MacOSVirtualMachineDelegate (virtualMachineResponder) |
stores or receives | Owning lexical scope |
AppDelegate |
VZVirtualMachine (virtualMachine) |
stores or receives | Owning lexical scope |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Queue scheduling | DispatchQueue.main.async |
The source addresses the main dispatch queue. | Swift/InstallationTool/MacOSVirtualMachineInstaller.swift:49 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
Swift/InstallationTool/MacOSVirtualMachineInstaller.swift:49 — representative execution boundary
#if arch(arm64)
DispatchQueue.main.async { [self] in
setupVirtualMachine(macOSConfiguration: macOSConfiguration)
startInstallation(restoreImageURL: restoreImage.url)
}
#endifState propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| Source import | Foundation |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/Common/Error.h:8 |
| Source import | Virtualization |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/Common/MacOSVirtualMachineConfigurationHelper.h:11 |
| Source import | Cocoa |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/macOSVirtualMachineSampleApp/AppDelegate.h:8 |
| Source import | AppKit |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/Common/MacOSVirtualMachineDelegate.m:10 |
| Source import | sys |
The cited file imports this module; runtime use and architectural role are not inferred. | Objective-C/InstallationTool/MacOSVirtualMachineInstaller.m:18 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
Swift/macOSVirtualMachineSampleApp/AppDelegate.swift:13 — representative type boundary
@main
class AppDelegate: NSObject, NSApplicationDelegate {
// ...
@IBOutlet var window: NSWindow!
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AppDelegate |
Receives callback-driven events | NSObject, NSApplicationDelegate |
AppDelegate |
Receives callback-driven events | NSObject, NSApplicationDelegate |
MacOSVirtualMachineDelegate |
Receives callback-driven events | NSObject, VZVirtualMachineDelegate |
MacOSVirtualMachineDelegate |
Receives callback-driven events | NSObject, VZVirtualMachineDelegate |
MacOSVirtualMachineConfigurationHelper |
Defines a feature-specific type boundary | NSObject |
MacOSRestoreImage |
Defines a feature-specific type boundary | NSObject |
MacOSVirtualMachineInstaller |
Defines a feature-specific type boundary | NSObject |
MacOSVirtualMachineConfigurationHelper |
Represents a feature value or composable behavior | Concrete collaborators/imported frameworks |
MacOSRestoreImage |
Owns feature behavior and collaborator lifecycle | NSObject |
MacOSVirtualMachineInstaller |
Owns feature behavior and collaborator lifecycle | NSObject |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
virtualMachineView (Objective-C/macOSVirtualMachineSampleApp/AppDelegate.m:19) |
implementation |
Visibility follows header/implementation and language linkage rules. | Inference: keep the declaration in the Objective-C implementation boundary. |
window (Objective-C/macOSVirtualMachineSampleApp/AppDelegate.m:21) |
implementation |
Visibility follows header/implementation and language linkage rules. | Inference: keep the declaration in the Objective-C implementation boundary. |
downloadObserver (Swift/InstallationTool/MacOSRestoreImage.swift:14) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
download (Swift/InstallationTool/MacOSRestoreImage.swift:18) |
public |
The symbol is visible to importing modules. | Inference: make the declaration available across a module or target boundary. |
Reference code
Objective-C/macOSVirtualMachineSampleApp/AppDelegate.m:19 — representative boundary
@interface AppDelegate ()
// ...
@property (weak) IBOutlet VZVirtualMachineView *virtualMachineView;
// ...
@endSwift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Receives callback-driven events | AppDelegate, MacOSVirtualMachineDelegate |
The source’s Delegate suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| Delegate or data-source callbacks | Objective-C/Common/MacOSVirtualMachineDelegate.h:13 |
Callback protocols invert event delivery back into the sample’s owner. |
Main application flow
sequenceDiagram
participant MacOSRestoreImage
participant VZMacOSRestoreImage
participant NSString
participant NSURLSession
participant NSFileManager
participant downloadTaskprogress
participant downloadTask
MacOSRestoreImage->>VZMacOSRestoreImage: fetchLatestSupportedWithCompletionHandler()
MacOSRestoreImage->>NSString: stringWithFormat()
MacOSRestoreImage->>NSURLSession: downloadTaskWithURL()
MacOSRestoreImage->>NSString: stringWithFormat()
MacOSRestoreImage->>NSFileManager: moveItemAtURL()
MacOSRestoreImage->>downloadTaskprogress: addObserver()
MacOSRestoreImage->>downloadTask: resume()
Reference code
Objective-C/InstallationTool/MacOSRestoreImage.m:32 — download()
- (void)download:(void (^)(void))completionHandler
{
[VZMacOSRestoreImage fetchLatestSupportedWithCompletionHandler:^(VZMacOSRestoreImage *restoreImage, NSError *error) {
if (error) {
abortWithErrorMessage([NSString stringWithFormat:@"Failed to fetch latest supported restore image catalog. %@", error.localizedDescription]);
}
NSLog(@"Attempting to download the latest available restore image.");
NSURLSessionDownloadTask *downloadTask = [[NSURLSession sharedSession] downloadTaskWithURL:restoreImage.URL
completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
if (error) {
abortWithErrorMessage([NSString stringWithFormat:@"Failed to download restore image. %@", error.localizedDescription]);
}
if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:getRestoreImageURL() error:&error]) {
abortWithErrorMessage(error.localizedDescription);
}
completionHandler();
}];
[downloadTask.progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:nil];
[downloadTask resume];
}];
}Naming conventions
- Types: Delegate: AppDelegate, MacOSVirtualMachineDelegate.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
createMacPlaform,createVirtualMachine,startVirtualMachine,resumeVirtualMachine,restoreVirtualMachine,applicationDidFinishLaunching,applicationShouldTerminateAfterLastWindowClosed,saveVirtualMachine. - Files:
Swift/macOSVirtualMachineSampleApp/AppDelegate.swift,Objective-C/macOSVirtualMachineSampleApp/AppDelegate.h,Objective-C/macOSVirtualMachineSampleApp/AppDelegate.m,Objective-C/Common/MacOSVirtualMachineDelegate.h,Objective-C/Common/MacOSVirtualMachineDelegate.m,Swift/Common/MacOSVirtualMachineDelegate.swift.
Architecture takeaways
AppDelegateis the main source-visible entry or composition anchor for this sample.- Framework work reaches Virtualization, Cocoa, AppKit, sys through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
Swift/macOSVirtualMachineSampleApp/AppDelegate.swift |
Cited implementation, AppDelegate |
Objective-C/macOSVirtualMachineSampleApp/AppDelegate.m |
virtualMachineView, window, AppDelegate |
Swift/InstallationTool/MacOSRestoreImage.swift |
Cited implementation, MacOSRestoreImage |
Objective-C/Common/MacOSVirtualMachineDelegate.h |
Cited implementation, MacOSVirtualMachineDelegate |
Swift/InstallationTool/MacOSVirtualMachineInstaller.swift |
DispatchQueue.main.async, MacOSVirtualMachineInstaller |
Objective-C/Common/Error.h |
Foundation, Feature implementation |
Objective-C/Common/MacOSVirtualMachineConfigurationHelper.h |
Virtualization, MacOSVirtualMachineConfigurationHelper |
Objective-C/macOSVirtualMachineSampleApp/AppDelegate.h |
Cocoa, AppDelegate |
Objective-C/Common/MacOSVirtualMachineDelegate.m |
AppKit, MacOSVirtualMachineDelegate |
Objective-C/InstallationTool/MacOSVirtualMachineInstaller.m |
sys, MacOSVirtualMachineInstaller |
Objective-C/InstallationTool/main.m |
Feature implementation |
Objective-C/macOSVirtualMachineSampleApp/main.m |
Feature implementation |
Swift/InstallationTool/main.swift |
Feature implementation |
Swift/Common/MacOSVirtualMachineDelegate.swift |
MacOSVirtualMachineDelegate |
Objective-C/Common/MacOSVirtualMachineConfigurationHelper.m |
MacOSVirtualMachineConfigurationHelper |
Objective-C/InstallationTool/MacOSRestoreImage.h |
MacOSRestoreImage |
Objective-C/InstallationTool/MacOSRestoreImage.m |
download |