Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phatblat/runningguilinuxinavirtualmachineonamac
Experimenting with Linux on Apple's Virtualization framework
https://github.com/phatblat/runningguilinuxinavirtualmachineonamac
apple linux virtualization-framework vm
Last synced: about 1 month ago
JSON representation
Experimenting with Linux on Apple's Virtualization framework
- Host: GitHub
- URL: https://github.com/phatblat/runningguilinuxinavirtualmachineonamac
- Owner: phatblat
- Created: 2023-12-02T18:39:42.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2023-12-02T18:51:44.000Z (12 months ago)
- Last Synced: 2024-05-01T22:22:15.103Z (7 months ago)
- Topics: apple, linux, virtualization-framework, vm
- Language: Swift
- Homepage: https://developer.apple.com/documentation/virtualization/running_gui_linux_in_a_virtual_machine_on_a_mac
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE/LICENSE.txt
Awesome Lists containing this project
README
# Running GUI Linux in a virtual machine on a Mac
Install and run GUI Linux in a virtual machine using the Virtualization framework.
## Overview
This sample code project demonstrates how to install and run GUI Linux virtual machines (VMs) on a Mac.
The Xcode project includes a single target, `GUILinuxVirtualMachineSampleApp`, which is a macOS app that installs a Linux distribution from an ISO image into a VM, and subsequently runs the installed Linux VM.
[class_VZVirtualMachineConfiguration]:https://developer.apple.com/documentation/virtualization/vzvirtualmachineconfiguration
[class_VZLinuxBootLoader]:https://developer.apple.com/documentation/virtualization/vzlinuxbootloader
[class_VZVirtualMachine]:https://developer.apple.com/documentation/virtualization/vzvirtualmachine
[property_bootLoader]:https://developer.apple.com/documentation/virtualization/vzvirtualmachineconfiguration/3656716-bootloader
[method_start]:https://developer.apple.com/documentation/virtualization/vzvirtualmachine/3656826-start
[method_guestDidStop]:https://developer.apple.com/documentation/virtualization/vzvirtualmachinedelegate/3656730-guestdidstop## Download a Linux installation image
Before you run the sample program, you need to download an ISO installation image from a Linux distribution website. Some common Linux distributions include:
- [Debian](https://www.debian.org/distrib/)
- [Fedora](https://getfedora.org/en/workstation/download/)
- [Ubuntu](https://ubuntu.com/download/desktop)- Important: The Virtualization framework can run Linux VMs on a Mac with Apple silicon, and on an Intel-based Mac. The Linux ISO image you download must support the CPU architecture of your Mac. For a Mac with Apple silicon, download a Linux ISO image for ARM, which is usually indicated by `aarch64` or `arm64` in the image filename. For an Intel-based Mac, download a Linux ISO image for Intel-compatible CPUs, which is usually indicated by `x86_64` or `amd64` in the image filename.
- Note: If you need to run Intel Linux binaries in ARM Linux on a Mac with Apple silicon, the Virtualization framework supports this capability using the Rosetta translation environment. For more information, see [Running Intel Binaries in Linux VMs with Rosetta](https://developer.apple.com/documentation/virtualization/running_intel_binaries_in_linux_vms_with_rosetta).
## Configure the sample code project
1. Launch Xcode and open `GUILinuxVirtualMachineSampleApp.xcodeproj`.
2. Navigate to the Signing & Capabilities panel and select your team ID.
3. Build and run GUILinuxVirtualMachineSampleApp. The sample app starts the VM and configures a graphical view that you interact with. The Linux VM continues running until you shut it down from the guest OS, or when you quit the app.
When you run the app for the first time, it displays a file picker so you can choose the Linux installation ISO image to use for installing your Linux VM. Navigate to the ISO image that you downloaded, select the file, and click Open. The VM boots into the OS installer, and the installer's user interface appears in the app's window. Follow the installation instructions. When the installation finishes, the Linux VM is ready to use.
As part of the installation process, the Virtualization framework creates a `GUI Linux VM.bundle` package in your home directory. The sample app only supports running one VM at a time, however, the Virtualization framework supports running multiple VMs simultaneously. Running multiple VMs requires an app to manage the execution and artifacts of each individual VM.
The contents of the bundle represent the state of the Linux guest, and contain the following:* `Disk.img` — The main disk image of the installed Linux OS.
* `MachineIdentifier` — The data representation of the `VZGenericMachineIdentifier` object.
* `NVRAM` — The EFI variable store.Subsequent launches of GUILinuxVirtualMachineSampleApp run the installed Linux VM. To reinstall the VM, delete the `GUI Linux VM.bundle` package and run the app again.
## Install GUI Linux from an ISO image
The sample app configures a `VZDiskImageStorageDeviceAttachment` object with the downloaded ISO image attached, and creates a `VZUSBMassStorageDeviceConfiguration` with it to emulate a USB thumb drive that's plugged in to the VM.
``` swift
private func createUSBMassStorageDeviceConfiguration() -> VZUSBMassStorageDeviceConfiguration {
guard let intallerDiskAttachment = try? VZDiskImageStorageDeviceAttachment(url: installerISOPath!, readOnly: true) else {
fatalError("Failed to create installer's disk attachment.")
}return VZUSBMassStorageDeviceConfiguration(attachment: intallerDiskAttachment)
}
```## Set up the VM
The sample app uses a [`VZVirtualMachineConfiguration`][class_VZVirtualMachineConfiguration] object to configure the basic characteristics of the VM, such as the CPU count, memory size, various device configurations, and a `VZEFIBootloader` to load the Linux operating system into the VM.
``` swift
let virtualMachineConfiguration = VZVirtualMachineConfiguration()virtualMachineConfiguration.cpuCount = computeCPUCount()
virtualMachineConfiguration.memorySize = computeMemorySize()let platform = VZGenericPlatformConfiguration()
let bootloader = VZEFIBootLoader()
let disksArray = NSMutableArray()if needsInstall {
// This is a fresh install: Create a new machine identifier and EFI variable store,
// and configure a USB mass storage device to boot the ISO image.
platform.machineIdentifier = createAndSaveMachineIdentifier()
bootloader.variableStore = createEFIVariableStore()
disksArray.add(createUSBMassStorageDeviceConfiguration())
} else {
// The VM is booting from a disk image that already has the OS installed.
// Retrieve the machine identifier and EFI variable store that were saved to
// disk during installation.
platform.machineIdentifier = retrieveMachineIdentifier()
bootloader.variableStore = retrieveEFIVariableStore()
}virtualMachineConfiguration.platform = platform
virtualMachineConfiguration.bootLoader = bootloaderdisksArray.add(createBlockDeviceConfiguration())
guard let disks = disksArray as? [VZStorageDeviceConfiguration] else {
fatalError("Invalid disksArray.")
}
virtualMachineConfiguration.storageDevices = disksvirtualMachineConfiguration.networkDevices = [createNetworkDeviceConfiguration()]
virtualMachineConfiguration.graphicsDevices = [createGraphicsDeviceConfiguration()]
virtualMachineConfiguration.audioDevices = [createInputAudioDeviceConfiguration(), createOutputAudioDeviceConfiguration()]virtualMachineConfiguration.keyboards = [VZUSBKeyboardConfiguration()]
virtualMachineConfiguration.pointingDevices = [VZUSBScreenCoordinatePointingDeviceConfiguration()]
virtualMachineConfiguration.consoleDevices = [createSpiceAgentConsoleDeviceConfiguration()]try! virtualMachineConfiguration.validate()
virtualMachine = VZVirtualMachine(configuration: virtualMachineConfiguration)
```## Enable copy-and-paste support between the host and the guest
In macOS 13 and later, the Virtualization framework supports copy-and-paste of text and images between the Mac host and Linux guests through the SPICE agent clipboard-sharing capability. The example below shows the steps for configuring `VZVirtioConsoleDeviceConfiguration` and `VZSpiceAgentPortAttachment` to enable this capability:
``` swift
private func createSpiceAgentConsoleDeviceConfiguration() -> VZVirtioConsoleDeviceConfiguration {
let consoleDevice = VZVirtioConsoleDeviceConfiguration()let spiceAgentPort = VZVirtioConsolePortConfiguration()
spiceAgentPort.name = VZSpiceAgentPortAttachment.spiceAgentPortName
spiceAgentPort.attachment = VZSpiceAgentPortAttachment()
consoleDevice.ports[0] = spiceAgentPortreturn consoleDevice
}
```- Important: To use the copy-and-paste capability in Linux, the user needs to install the spice-vdagent package, which is available through most Linux package managers. Developers need to communicate this requirement to users of their apps.
## Start the VM
After building the configuration data for the VM, the sample app uses the `VZVirtualMachine` object to start the execution of the Linux guest operating system.
Before calling the VM's [`start`][method_start] method, the sample app configures a delegate object to receive messages about the state of the virtual machine. When the Linux operating system shuts down, the VM calls the delegate's [`guestDidStop`][method_guestDidStop] method. In response, the delegate method prints a message and exits the sample.
``` swift
self.virtualMachineView.virtualMachine = self.virtualMachine
self.virtualMachine.delegate = self
self.virtualMachine.start(completionHandler: { (result) in
switch result {
case let .failure(error):
fatalError("Virtual machine failed to start with error: \(error)")default:
print("Virtual machine successfully started.")
}
})
```