Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripting : add vm registry suite #6977

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Scripting/UTM.sdef
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,10 @@
<record-type name="qemu argument" code="QeAr" description="QEMU argument configuration.">
<property name="argument string" code="ArSt" type="text"
description="The QEMU argument as a string."/>
<property name="file urls" code="FlUr"
description="Optional URLs associated with this argument.">
<type type="file" list="yes"/>
</property>
</record-type>

<record-type name="apple configuration" code="ApCf" description="Apple virtual machine configuration.">
Expand Down Expand Up @@ -708,4 +712,27 @@
<direct-parameter description="The USB device to disconnect." type="usb device"/>
</command>
</suite>

<suite name="UTM Registry Suite" code="UTMr" description="UTM virtual machine registry suite. Use this to update virtual machine registry.">
<access-group identifier="com.utmapp.UTM.vm-access" />

<class-extension extends="virtual machine" description="Virtual machine registry.">
<property name="registry" code="ReGs" access="r"
description="The registry of the virtual machine.">
<type type="file" list="yes"/>
</property>

<responds-to command="update registry">
<cocoa method="updateRegistry:"/>
</responds-to>
</class-extension>

<command name="update registry" code="UTMrUpDt" description="Update the registry of the virtual machine.">
<direct-parameter description="Virtual machine to update." type="virtual machine"/>
<parameter name="with" code="UpRg" description="The registry to update the virtual machine. Currently you can only change the shared directory with this!">
<cocoa key="newRegistry"/>
<type type="file" list="yes"/>
</parameter>
</command>
</suite>
</dictionary>
9 changes: 8 additions & 1 deletion Scripting/UTMScriptingConfigImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ extension UTMScriptingConfigImpl {
var serializedArgument: [AnyHashable: Any] = [
"argumentString": argument.string
]
// Only add fileUrls if it is not nil and contains URLs
if let fileUrls = argument.fileUrls, !fileUrls.isEmpty {
serializedArgument["fileUrls"] = fileUrls.map({ $0 as AnyHashable })
}

return serializedArgument
}
Expand Down Expand Up @@ -517,7 +521,10 @@ extension UTMScriptingConfigImpl {
let additionalArguments = records.compactMap { record -> QEMUArgument? in
guard let argumentString = record["argumentString"] as? String else { return nil }
var argument = QEMUArgument(argumentString)

// fileUrls are used as required resources by QEMU.
if let fileUrls = record["fileUrls"] as? [URL] {
argument.fileUrls = fileUrls
}
return argument
}
// Update entire additional arguments with new one.
Expand Down
77 changes: 77 additions & 0 deletions Scripting/UTMScriptingRegistryEntryImpl.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// Copyright © 2025 naveenrajm7. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation

@objc extension UTMScriptingVirtualMachineImpl {
@objc var registry: [URL] {
let wrapper = UTMScriptingRegistryEntryImpl(vm.registryEntry)
return wrapper.serializeRegistry()
}

@objc func updateRegistry(_ command: NSScriptCommand) {
let newRegistry = command.evaluatedArguments?["newRegistry"] as? [URL]
withScriptCommand(command) { [self] in
guard let newRegistry = newRegistry else {
throw ScriptingError.invalidParameter
}
let wrapper = UTMScriptingRegistryEntryImpl(vm.registryEntry)
try await wrapper.updateRegistry(from: newRegistry, qemuProcess)
}
}
}

@MainActor
class UTMScriptingRegistryEntryImpl {
private(set) var registry: UTMRegistryEntry

init(_ registry: UTMRegistryEntry) {
self.registry = registry
}

func serializeRegistry() -> [URL] {
return registry.sharedDirectories.compactMap { $0.url }
}

func updateRegistry(from fileUrls: [URL], _ system: UTMQemuSystem?) async throws {
// Clear all shared directories, we add all directories here
registry.removeAllSharedDirectories()

// Add urls to the registry
for url in fileUrls {
// Start scoped access
let isScopedAccess = url.startAccessingSecurityScopedResource()
defer {
if isScopedAccess {
url.stopAccessingSecurityScopedResource()
}
}

// Get bookmark from UTM process
let standardBookmark = try url.bookmarkData()
let system = system ?? UTMProcess()
let (success, bookmark, path) = await system.accessData(withBookmark: standardBookmark, securityScoped: false)
guard let bookmark = bookmark, let _ = path, success else {
throw UTMQemuVirtualMachineError.accessDriveImageFailed
}

// Store bookmark in registry
let file = UTMRegistryEntry.File(dummyFromPath: url.path, remoteBookmark: bookmark)
registry.appendSharedDirectory(file)
}

}
}
6 changes: 6 additions & 0 deletions Scripting/UTMScriptingVirtualMachineImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class UTMScriptingVirtualMachineImpl: NSObject, UTMScriptable {
}
}

var qemuProcess: UTMQemuSystem? {
get async {
await (vm as? UTMQemuVirtualMachine)?.system
}
}

override var objectSpecifier: NSScriptObjectSpecifier? {
let appDescription = NSApplication.classDescription() as! NSScriptClassDescription
return NSUniqueIDSpecifier(containerClassDescription: appDescription,
Expand Down
3 changes: 2 additions & 1 deletion Services/UTMQemuVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ final class UTMQemuVirtualMachine: UTMSpiceVirtualMachine {

private let qemuVM = QEMUVirtualMachine()

private var system: UTMQemuSystem? {
/// QEMU Process interface
var system: UTMQemuSystem? {
get async {
await qemuVM.launcher as? UTMQemuSystem
}
Expand Down
4 changes: 4 additions & 0 deletions Services/UTMRegistryEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ extension UTMRegistryEntry: UTMRegistryEntryDecodable {}
}
}

func appendSharedDirectory(_ file: File) {
sharedDirectories.append(file)
}

func removeAllSharedDirectories() {
sharedDirectories = []
}
Expand Down
4 changes: 4 additions & 0 deletions UTM.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
B3DDF57226E9BBA300CE47F0 /* AltKit in Frameworks */ = {isa = PBXBuildFile; productRef = B3DDF57126E9BBA300CE47F0 /* AltKit */; };
CD77BE422CAB51B40074ADD2 /* UTMScriptingExportCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD77BE412CAB519F0074ADD2 /* UTMScriptingExportCommand.swift */; };
CD77BE442CB38F060074ADD2 /* UTMScriptingImportCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD77BE432CB38F060074ADD2 /* UTMScriptingImportCommand.swift */; };
CD84C2092D3B446D00829850 /* UTMScriptingRegistryEntryImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD84C2082D3B446D00829850 /* UTMScriptingRegistryEntryImpl.swift */; };
CE020BA324AEDC7C00B44AB6 /* UTMData.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE020BA224AEDC7C00B44AB6 /* UTMData.swift */; };
CE020BA424AEDC7C00B44AB6 /* UTMData.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE020BA224AEDC7C00B44AB6 /* UTMData.swift */; };
CE020BA724AEDEF000B44AB6 /* Logging in Frameworks */ = {isa = PBXBuildFile; productRef = CE020BA624AEDEF000B44AB6 /* Logging */; };
Expand Down Expand Up @@ -1779,6 +1780,7 @@
C8958B6D243634DA002D86B4 /* ko */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ko; path = ko.lproj/Localizable.strings; sourceTree = "<group>"; };
CD77BE412CAB519F0074ADD2 /* UTMScriptingExportCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptingExportCommand.swift; sourceTree = "<group>"; };
CD77BE432CB38F060074ADD2 /* UTMScriptingImportCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptingImportCommand.swift; sourceTree = "<group>"; };
CD84C2082D3B446D00829850 /* UTMScriptingRegistryEntryImpl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMScriptingRegistryEntryImpl.swift; sourceTree = "<group>"; };
CE020BA224AEDC7C00B44AB6 /* UTMData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMData.swift; sourceTree = "<group>"; };
CE020BAA24AEE00000B44AB6 /* UTMLoggingSwift.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMLoggingSwift.swift; sourceTree = "<group>"; };
CE020BB524B14F8400B44AB6 /* UTMVirtualMachine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTMVirtualMachine.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3027,6 +3029,7 @@
CEC794B9294924E300121A9F /* UTMScriptingSerialPortImpl.swift */,
CE25124829BFDBA6000790AB /* UTMScriptingGuestFileImpl.swift */,
CE25124629BFDB87000790AB /* UTMScriptingGuestProcessImpl.swift */,
CD84C2082D3B446D00829850 /* UTMScriptingRegistryEntryImpl.swift */,
CE25124C29C55816000790AB /* UTMScriptingConfigImpl.swift */,
CE25125429C80CD4000790AB /* UTMScriptingCreateCommand.swift */,
CD77BE432CB38F060074ADD2 /* UTMScriptingImportCommand.swift */,
Expand Down Expand Up @@ -3724,6 +3727,7 @@
CEBE820D26A4C8E0007AAB12 /* VMWizardSummaryView.swift in Sources */,
8401FDB2269E602000265F0D /* VMConfigAppleDriveDetailsView.swift in Sources */,
841619B428431DA5000034B2 /* UTMQemuConfigurationQEMU.swift in Sources */,
CD84C2092D3B446D00829850 /* UTMScriptingRegistryEntryImpl.swift in Sources */,
CEF0307626A2B40B00667B63 /* VMWizardHardwareView.swift in Sources */,
CED234EE254796E500ED0A57 /* NumberTextField.swift in Sources */,
841619B028431952000034B2 /* UTMQemuConfigurationSystem.swift in Sources */,
Expand Down
Loading