mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 02:21:28 +08:00
606 lines
21 KiB
Swift
606 lines
21 KiB
Swift
//
|
|
// GuestAdditionsDiskImage.swift
|
|
// VirtualCore
|
|
//
|
|
// Created by Guilherme Rambo on 07/03/23.
|
|
//
|
|
|
|
import Foundation
|
|
import ManagedPreferencesKit
|
|
import Virtualization
|
|
import CryptoKit
|
|
import UniformTypeIdentifiers
|
|
import OSLog
|
|
import Combine
|
|
import BuddyFoundation
|
|
|
|
public final class GuestAdditionsDiskImage: ObservableObject {
|
|
|
|
private let logger: Logger
|
|
|
|
public static let `default` = GuestAdditionsDiskImage(source: .embedded)
|
|
|
|
/// The disk image that provides the guest app from the specified source.
|
|
///
|
|
/// There's a single image for the embedded guest app, so every client that needs it gets ``default``,
|
|
/// which makes them all observe and wait for the same installation.
|
|
public static func image(for source: Source) -> GuestAdditionsDiskImage {
|
|
switch source {
|
|
case .embedded: .default
|
|
case .catalog: GuestAdditionsDiskImage(source: source)
|
|
}
|
|
}
|
|
|
|
public enum State: CustomStringConvertible {
|
|
case ready
|
|
case downloading
|
|
case installing
|
|
case installFailed(Error)
|
|
|
|
public var description: String {
|
|
switch self {
|
|
case .ready: "Ready"
|
|
case .downloading: "Downloading"
|
|
case .installing: "Installing"
|
|
case .installFailed(let error): "Failed: \(error)"
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum Source {
|
|
case embedded
|
|
case catalog(_ id: CatalogLegacyGuestAppVersion.ID)
|
|
|
|
var imageBaseName: String {
|
|
switch self {
|
|
case .embedded: "VirtualBuddyGuest_\(Bundle.embeddedGuestApp.vbShortVersionString)"
|
|
case .catalog(let id): id
|
|
}
|
|
}
|
|
|
|
var loggerName: String {
|
|
switch self {
|
|
case .embedded: "Embedded"
|
|
case .catalog(let id): id
|
|
}
|
|
}
|
|
|
|
var initialState: State {
|
|
switch self {
|
|
case .embedded: .ready
|
|
case .catalog: .downloading
|
|
}
|
|
}
|
|
|
|
/// Whether a successful installation remains valid for as long as the app is running.
|
|
///
|
|
/// That's the case for the embedded guest app, which can't change without the app itself being replaced,
|
|
/// whereas an image from the catalog has to be checked against the catalog every time.
|
|
var installsOncePerLaunch: Bool {
|
|
switch self {
|
|
case .embedded: true
|
|
case .catalog: false
|
|
}
|
|
}
|
|
}
|
|
|
|
private let source: Source
|
|
private var imageBaseName: String { source.imageBaseName }
|
|
|
|
@Published public private(set) var state: State
|
|
|
|
public init(source: Source) {
|
|
self.source = source
|
|
self.logger = Logger(subsystem: VirtualCoreConstants.subsystemName, category: "\(Self.self)\(source.loggerName)")
|
|
self.state = source.initialState
|
|
}
|
|
|
|
private let installationLock = NSLock()
|
|
|
|
/// The installation that's currently in progress, or the one that has succeeded for sources that only install once per launch.
|
|
private var installation: Task<Void, Error>?
|
|
|
|
/// Makes sure that the image is installed and up to date, returning once it's safe to attach to a virtual machine.
|
|
///
|
|
/// Concurrent calls join the installation that's already in progress instead of starting another one.
|
|
/// This matters because generating the image rewrites it, so a virtual machine must never be started while that's happening.
|
|
public func installIfNeeded() async throws {
|
|
let installation = installationLock.withLock {
|
|
if let installation { return installation }
|
|
|
|
let installation = Task { try await self.runInstallation() }
|
|
self.installation = installation
|
|
|
|
return installation
|
|
}
|
|
|
|
try await installation.value
|
|
}
|
|
|
|
private func runInstallation() async throws {
|
|
do {
|
|
try await performInstallIfNeeded()
|
|
} catch {
|
|
/// Allows the next call to try again.
|
|
installationLock.withLock { installation = nil }
|
|
|
|
throw error
|
|
}
|
|
|
|
guard !source.installsOncePerLaunch else { return }
|
|
|
|
installationLock.withLock { installation = nil }
|
|
}
|
|
|
|
private func performInstallIfNeeded() async throws {
|
|
#if DEBUG
|
|
if await simulateInstall() { return }
|
|
#endif
|
|
|
|
switch source {
|
|
case .embedded: try await generateAndInstallEmbeddedGuestDiskImage()
|
|
case .catalog(let id): do {
|
|
try await installCatalogDiskImage(id)
|
|
await MainActor.run { self.state = .ready }
|
|
} catch {
|
|
await MainActor.run { self.state = .installFailed(error) }
|
|
}
|
|
}
|
|
}
|
|
|
|
private func installCatalogDiskImage(_ id: CatalogLegacyGuestAppVersion.ID) async throws {
|
|
let app = try await SoftwareCatalog.currentMacCatalog.legacyGuestAppVersions
|
|
.first(where: { $0.id == id })
|
|
.require("Guest app image not found: \(id.quoted).")
|
|
|
|
let imagePath = FilePath(installedImageURL)
|
|
|
|
if imagePath.exists {
|
|
logger.debug("Catalog disk image already installed at \(imagePath)")
|
|
|
|
if let digest = try? imagePath.sha384Digest {
|
|
guard digest.hexString.caseInsensitiveCompare(app.sha384) != .orderedSame else {
|
|
await MainActor.run { self.state = .ready }
|
|
return
|
|
}
|
|
|
|
logger.debug("Local disk image digest doesn't match catalog, will redownload")
|
|
|
|
do {
|
|
try imagePath.delete()
|
|
} catch {
|
|
logger.error("Error removing cached local disk image: \(error, privacy: .public)")
|
|
}
|
|
}
|
|
}
|
|
|
|
logger.debug("Downloading image from \(app.url, privacy: .public)")
|
|
|
|
await MainActor.run { self.state = .downloading }
|
|
|
|
let request = URLRequest(url: app.url)
|
|
let (fileURL, response) = try await URLSession.shared.download(for: request)
|
|
|
|
let status = (response as! HTTPURLResponse).statusCode
|
|
try (status == 200).require("HTTP \(status).")
|
|
|
|
await MainActor.run { self.state = .installing }
|
|
|
|
logger.debug("Copying image to \(imagePath)")
|
|
|
|
try FilePath(fileURL).copy(imagePath)
|
|
|
|
logger.notice("Image installed for \(id, privacy: .public): \(imagePath, privacy: .public)")
|
|
}
|
|
|
|
private func generateAndInstallEmbeddedGuestDiskImage() async throws {
|
|
do {
|
|
logger.debug(#function)
|
|
|
|
func performInstall(with digest: String) async throws {
|
|
await MainActor.run { state = .installing }
|
|
|
|
try await writeGuestImage(with: digest)
|
|
|
|
await MainActor.run { state = .ready }
|
|
}
|
|
|
|
let digest = try computeGuestDigest()
|
|
|
|
/// Migrate installed image digest file from hidden file to visible file (changed in version 2.2 beta 3).
|
|
if FileManager.default.fileExists(atPath: _legacyInstalledImageDigestURL.path(percentEncoded: false)) {
|
|
do {
|
|
try FileManager.default.moveItem(at: _legacyInstalledImageDigestURL, to: installedImageDigestURL)
|
|
} catch {
|
|
logger.warning("Error moving legacy installed image digest file - \(error, privacy: .public)")
|
|
}
|
|
}
|
|
|
|
if let currentlyInstalledGuestImageDigest {
|
|
logger.debug("Guest app digest: \(digest, privacy: .public) / Library guest app digest: \(currentlyInstalledGuestImageDigest, privacy: .public)")
|
|
|
|
guard digest != currentlyInstalledGuestImageDigest else {
|
|
logger.debug("Guest digests match, skipping guest image generation")
|
|
|
|
await MainActor.run { state = .ready }
|
|
|
|
return
|
|
}
|
|
|
|
logger.debug("Guest digests don't match, generating new guest image")
|
|
|
|
try await performInstall(with: digest)
|
|
} else {
|
|
logger.debug("No digest for currently installed image, assuming not installed. Guest app digest: \(digest, privacy: .public)")
|
|
|
|
try await performInstall(with: digest)
|
|
}
|
|
} catch {
|
|
logger.error("Guest disk image installation failed. \(error, privacy: .public)")
|
|
|
|
await MainActor.run { state = .installFailed(error) }
|
|
|
|
throw error
|
|
}
|
|
}
|
|
|
|
// MARK: File Paths
|
|
|
|
private var generatorScriptURL: URL {
|
|
get throws {
|
|
guard let url = Bundle.virtualCore.url(forResource: "CreateGuestImage", withExtension: "sh") else {
|
|
throw Failure("Couldn't get CreateGuestImage.sh URL within VirtualCore bundle")
|
|
}
|
|
|
|
guard FileManager.default.fileExists(atPath: url.path) else {
|
|
throw Failure("CreateGuestImage.sj doesn't exist at \(url.path)")
|
|
}
|
|
|
|
return url
|
|
}
|
|
}
|
|
|
|
private var imageName: String {
|
|
if let suffix = VBBuildType.current.guestAdditionsImageSuffix {
|
|
imageBaseName + suffix
|
|
} else {
|
|
imageBaseName
|
|
}
|
|
}
|
|
|
|
static let imagesRootURL: URL = URL.defaultVirtualBuddyLibraryURL.appendingPathComponent("_GuestImage")
|
|
|
|
private var imagesRootURL: URL { Self.imagesRootURL }
|
|
|
|
/// Before version 2.2 beta 3, guest image digests were hidden files, but this was changed to make
|
|
/// it easier for users and developers to delete all existing guest images alongside their digests in Finder.
|
|
private var _legacyInstalledImageDigestURL: URL {
|
|
imagesRootURL
|
|
.appendingPathComponent("." + imageName)
|
|
.appendingPathExtension("digest")
|
|
}
|
|
|
|
private var installedImageDigestURL: URL {
|
|
imagesRootURL
|
|
.appendingPathComponent(imageName)
|
|
.appendingPathExtension("digest")
|
|
}
|
|
|
|
public var installedImageURL: URL {
|
|
switch source {
|
|
case .embedded:
|
|
imagesRootURL
|
|
.appendingPathComponent(imageName)
|
|
.appendingPathExtension("dmg")
|
|
case .catalog(let id):
|
|
imagesRootURL
|
|
.appendingPathComponent(id)
|
|
.appendingPathExtension("dmg")
|
|
}
|
|
}
|
|
|
|
// MARK: Digest
|
|
|
|
private var currentlyInstalledGuestImageDigest: String? {
|
|
/// Ignore installed image digest unless the image itself exists.
|
|
guard FileManager.default.fileExists(atPath: installedImageURL.path(percentEncoded: false)) else {
|
|
logger.notice("Ignoring installed guest image digest because the guest image itself doesn't exist")
|
|
return nil
|
|
}
|
|
|
|
guard FileManager.default.fileExists(atPath: installedImageDigestURL.path(percentEncoded: false)) else {
|
|
return nil
|
|
}
|
|
do {
|
|
return try String(contentsOf: installedImageDigestURL)
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
} catch {
|
|
logger.error("Failed to read installed image digest at \(self.installedImageDigestURL.path): \(error, privacy: .public)")
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
private func computeGuestDigest() throws -> String {
|
|
guard let enumerator = FileManager.default.enumerator(at: Bundle.embeddedGuestApp.bundleURL, includingPropertiesForKeys: [.contentTypeKey]) else {
|
|
throw Failure("Couldn't instantiate file enumerator for computing guest app bundle digest")
|
|
}
|
|
|
|
var hash = SHA256()
|
|
|
|
while let url = enumerator.nextObject() as? URL {
|
|
guard let values = try? url.resourceValues(forKeys: [.contentTypeKey]),
|
|
let contentType = values.contentType
|
|
else { continue }
|
|
|
|
guard contentType.conforms(to: .executable),
|
|
!contentType.conforms(to: .directory)
|
|
else { continue }
|
|
|
|
#if DEBUG
|
|
logger.debug("Computing hash for \(url.lastPathComponent)")
|
|
#endif
|
|
|
|
do {
|
|
let data = try Data(contentsOf: url, options: .mappedIfSafe)
|
|
|
|
hash.update(data: data)
|
|
} catch {
|
|
logger.warning("Couldn't compute hash for \(url.lastPathComponent): \(error, privacy: .public)")
|
|
}
|
|
}
|
|
|
|
let digest = hash.finalize()
|
|
let hashStr = digest.map { String(format: "%02x", $0) }.joined()
|
|
|
|
return hashStr
|
|
}
|
|
|
|
// MARK: Installation
|
|
|
|
private func writeGuestImage(with digest: String) async throws {
|
|
let scriptPath = try generatorScriptURL.path
|
|
let guestPath = Bundle.embeddedGuestApp.bundlePath
|
|
let size = computeImageSizeInMB(guestAppURL: Bundle.embeddedGuestApp.bundleURL)
|
|
|
|
let args: [String] = [
|
|
scriptPath,
|
|
guestPath,
|
|
digest,
|
|
"\(size)MB",
|
|
imageName
|
|
]
|
|
|
|
let p = Process()
|
|
p.executableURL = URL(fileURLWithPath: "/bin/sh")
|
|
p.arguments = args
|
|
let outPipe = Pipe()
|
|
let errPipe = Pipe()
|
|
p.standardOutput = outPipe
|
|
p.standardError = errPipe
|
|
|
|
try p.run()
|
|
p.waitUntilExit()
|
|
|
|
let outData = try outPipe.fileHandleForReading.readToEnd()
|
|
let errData = try errPipe.fileHandleForReading.readToEnd()
|
|
|
|
#if DEBUG
|
|
if let outData, !outData.isEmpty {
|
|
logger.debug("#### Generator script output (stdout): ####")
|
|
logger.debug("\(String(decoding: outData, as: UTF8.self), privacy: .public)")
|
|
}
|
|
if let errData, !errData.isEmpty {
|
|
logger.debug("#### Generator script output (stderr): ####")
|
|
logger.debug("\(String(decoding: errData, as: UTF8.self), privacy: .public)")
|
|
}
|
|
#endif
|
|
|
|
guard p.terminationStatus == 0 else {
|
|
if let message = errData.flatMap({ String(decoding: $0, as: UTF8.self) }) {
|
|
throw Failure(message)
|
|
} else {
|
|
throw Failure("Guest additions disk image generator failed with exit code \(p.terminationStatus)")
|
|
}
|
|
}
|
|
|
|
logger.notice("Guest additions disk image generated at \(self.installedImageURL.path, privacy: .public)")
|
|
}
|
|
|
|
}
|
|
|
|
public extension Bundle {
|
|
/// Bundle of the VirtualBuddyGuest app embedded in the app's main bundle.
|
|
static let embeddedGuestApp: Bundle = {
|
|
#if DEBUG
|
|
/// Allow using SwiftUI previews with VirtualUI target selected without having to embed VirtualBuddyGuest.app inside VirtualUI.
|
|
guard !ProcessInfo.isSwiftUIPreview else { return Bundle.main }
|
|
#endif
|
|
do {
|
|
guard let url = Bundle.main.sharedSupportURL?.appendingPathComponent("VirtualBuddyGuest.app") else {
|
|
throw Failure("Couldn't get VirtualBuddyGuest.app URL within main app bundle")
|
|
}
|
|
|
|
guard FileManager.default.fileExists(atPath: url.path) else {
|
|
throw Failure("VirtualBuddyGuest.app doesn't exist at \(url.path)")
|
|
}
|
|
|
|
guard let bundle = Bundle(url: url) else {
|
|
throw Failure("Failed to construct bundle for embedded guest app at \(url.path(percentEncoded: false)).")
|
|
}
|
|
|
|
return bundle
|
|
} catch {
|
|
preconditionFailure("\(error)")
|
|
}
|
|
}()
|
|
|
|
var minimumSystemVersion: SoftwareVersion {
|
|
guard let versionString: String = self.infoPlistValue(for: "LSMinimumSystemVersion") else { return .empty }
|
|
return SoftwareVersion(string: versionString) ?? .empty
|
|
}
|
|
}
|
|
|
|
public extension SoftwareVersion {
|
|
/// Version of the VirtualBuddyGuest app embedded in the app's main bundle.
|
|
static let embeddedGuestApp = Bundle.embeddedGuestApp.softwareVersion
|
|
}
|
|
|
|
// MARK: - Virtualization Extensions
|
|
|
|
extension VZVirtioBlockDeviceConfiguration {
|
|
|
|
static func guestAdditionsDisk(for configuration: VBMacConfiguration, preferences: ManagedPreferenceReader<VirtualBuddyManagedPreferences> = VirtualBuddyManagedPreferences.schema.reader()) async throws -> VZVirtioBlockDeviceConfiguration? {
|
|
guard let url = guestAdditionsImageURL(for: configuration, preferences: preferences) else { return nil }
|
|
|
|
return try guestAdditionsDisk(imageURL: url)
|
|
}
|
|
|
|
/// The installed guest additions image that should be attached for the configuration,
|
|
/// or `nil` if the policy forbids it or the image isn't available.
|
|
static func guestAdditionsImageURL(for configuration: VBMacConfiguration, preferences: ManagedPreferenceReader<VirtualBuddyManagedPreferences> = VirtualBuddyManagedPreferences.schema.reader()) -> URL? {
|
|
guard !preferences.value(for: .disableGuestApp, default: false) else {
|
|
VirtualBuddyManagedPreferences.logger.notice("Guest app installer disk omitted by DisableGuestApp")
|
|
return nil
|
|
}
|
|
let image = GuestAdditionsDiskImage(source: configuration.guestAppDiskImageSource)
|
|
|
|
let guestImagePath = FilePath(image.installedImageURL)
|
|
|
|
guard guestImagePath.exists else { return nil }
|
|
|
|
return guestImagePath.url
|
|
}
|
|
|
|
static func guestAdditionsDisk(imageURL: URL) throws -> VZVirtioBlockDeviceConfiguration {
|
|
let guestAttachment = try VZDiskImageStorageDeviceAttachment(url: imageURL, readOnly: true)
|
|
|
|
return VZVirtioBlockDeviceConfiguration(attachment: guestAttachment)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Image Size Calculation
|
|
|
|
private extension GuestAdditionsDiskImage {
|
|
/// Fallback size in case image size can't be calculated.
|
|
static let defaultImageSizeInMB = 32
|
|
|
|
/// Just being paranoid in case size computation goes haywire and ends up computing a huge image size.
|
|
static let maxImageSizeInMB = 128
|
|
|
|
/// Increase image size slightly when compared to guest app size to account for extra space needed for disk image.
|
|
static let imageSizeMultiplier: Double = 1.1
|
|
|
|
func computeImageSizeInMB(guestAppURL: URL) -> Int {
|
|
do {
|
|
guard let enumerator = FileManager.default.enumerator(at: guestAppURL, includingPropertiesForKeys: [.totalFileAllocatedSizeKey, .contentTypeKey], options: [], errorHandler: { url, error in
|
|
self.logger.warning("Error enumerating guest app contents at \(url.lastPathComponent, privacy: .public) - \(error, privacy: .public)")
|
|
return true
|
|
}) else {
|
|
throw Failure("Failed to create directory enumerator.")
|
|
}
|
|
|
|
var totalSize: Int = 0
|
|
|
|
while let file = enumerator.nextObject() as? URL {
|
|
let values = try file.resourceValues(forKeys: [.contentTypeKey, .totalFileAllocatedSizeKey])
|
|
|
|
guard let type = values.contentType else {
|
|
throw Failure("Content type not available for \(file.lastPathComponent)")
|
|
}
|
|
|
|
guard !type.conforms(to: .directory) else { continue }
|
|
|
|
guard let size = values.totalFileAllocatedSize else {
|
|
throw Failure("File size not available for \(file.lastPathComponent)")
|
|
}
|
|
|
|
totalSize += size
|
|
}
|
|
|
|
let totalSizeMB = Int(ceil(Double(totalSize) * Self.imageSizeMultiplier)) / 1000 / 1000
|
|
|
|
logger.info("Calculated guest disk image size: \(totalSizeMB, privacy: .public)MB")
|
|
|
|
guard totalSizeMB <= Self.maxImageSizeInMB else {
|
|
assertionFailure("\(#function) calculated a size that's larger than the maximum allowed size. Calculated size in MB: \(totalSizeMB), max size in MB: \(Self.maxImageSizeInMB)")
|
|
return Self.maxImageSizeInMB
|
|
}
|
|
|
|
return totalSizeMB
|
|
} catch {
|
|
logger.fault("Error computing total guest disk image size. \(error, privacy: .public)")
|
|
return Self.defaultImageSizeInMB
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension VBBuildType {
|
|
var guestAdditionsImageSuffix: String? {
|
|
switch self {
|
|
case .debug: "_Debug"
|
|
case .betaDebug: "_Beta_Debug"
|
|
case .release: nil
|
|
case .betaRelease: "_Beta"
|
|
case .devRelease: "_Dev"
|
|
}
|
|
}
|
|
}
|
|
|
|
extension FilePath {
|
|
var sha384Digest: Data {
|
|
get throws { try Data(contentsOf: url, options: .mappedIfSafe).sha384Digest }
|
|
}
|
|
}
|
|
|
|
extension Data {
|
|
var sha384Digest: Data { Data(SHA384.hash(data: self)) }
|
|
}
|
|
|
|
// MARK: - Debug Simulation
|
|
|
|
#if DEBUG
|
|
private extension GuestAdditionsDiskImage {
|
|
func simulateInstall() async -> Bool {
|
|
guard UserDefaults.standard.bool(forKey: "VBSimulateGuestDiskImageGeneration") else {
|
|
return false
|
|
}
|
|
|
|
logger.debug("Guest disk image will not be generated because VBSimulateGuestDiskImageGeneration is enabled.")
|
|
|
|
await MainActor.run {
|
|
state = .installing
|
|
}
|
|
|
|
let delaySeconds = UserDefaults.standard.integer(forKey: "VBDelayGuestDiskImageGenerationBySeconds")
|
|
if delaySeconds > 0 {
|
|
logger.debug("Simulating guest disk image install with custom delay of \(delaySeconds) seconds")
|
|
|
|
try? await Task.sleep(for: .seconds(delaySeconds))
|
|
} else {
|
|
logger.debug("Simulating guest disk image install with default delay")
|
|
|
|
try? await Task.sleep(for: .seconds(3))
|
|
}
|
|
|
|
guard !UserDefaults.standard.bool(forKey: "VBSimulateGuestDiskImageGenerationError") else {
|
|
logger.debug("Simulating guest disk image install error.")
|
|
await MainActor.run {
|
|
state = .installFailed("This is a simulated error for debugging.")
|
|
}
|
|
return true
|
|
}
|
|
|
|
logger.debug("Simulated guest disk image install completed")
|
|
|
|
await MainActor.run {
|
|
state = .ready
|
|
}
|
|
|
|
return true
|
|
}
|
|
}
|
|
#endif
|