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 qemu additional arguments #6741

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions Scripting/UTM.sdef
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@
description="List of serial configuration.">
<type type="qemu serial configuration" list="yes"/>
</property>

<property name="qemu additional arguments" code="QeAd"
description="List of qemu arguments.">
<type type="qemu argument" list="yes"/>
</property>
</record-type>

<enumeration name="qemu directory share mode" code="QeSm" description="Method for sharing directory in QEMU.">
Expand Down Expand Up @@ -534,6 +539,14 @@
description="The port number to listen on when the interface is a TCP server."/>
</record-type>

<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" type="file" list="yes"
naveenrajm7 marked this conversation as resolved.
Show resolved Hide resolved
description="Optional URLs associated with this argument."/>
</record-type>

<record-type name="apple configuration" code="ApCf" description="Apple virtual machine configuration.">
<property name="name" code="pnam" type="text"
description="Virtual machine name."/>
Expand Down
35 changes: 35 additions & 0 deletions Scripting/UTMScriptingConfigImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ extension UTMScriptingConfigImpl {
"drives": config.drives.map({ serializeQemuDriveExisting($0) }),
"networkInterfaces": config.networks.enumerated().map({ serializeQemuNetwork($1, index: $0) }),
"serialPorts": config.serials.enumerated().map({ serializeQemuSerial($1, index: $0) }),
"qemuAdditionalArguments": config.qemu.additionalArguments.map({ serializeQemuAdditionalArgument($0)}),
]
}

Expand Down Expand Up @@ -188,6 +189,19 @@ extension UTMScriptingConfigImpl {
]
}

private func serializeQemuAdditionalArgument(_ argument: QEMUArgument) -> [AnyHashable: Any] {
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
}

private func serializeAppleConfiguration(_ config: UTMAppleConfiguration) -> [AnyHashable : Any] {
[
"name": config.information.name,
Expand Down Expand Up @@ -338,6 +352,9 @@ extension UTMScriptingConfigImpl {
if let serialPorts = record["serialPorts"] as? [[AnyHashable : Any]] {
try updateQemuSerials(from: serialPorts)
}
if let qemuAdditionalArguments = record["qemuAdditionalArguments"] as? [[AnyHashable: Any]] {
try updateQemuAdditionalArguments(from: qemuAdditionalArguments)
}
}

private func parseQemuDriveInterface(_ value: AEKeyword?) -> QEMUDriveInterface? {
Expand Down Expand Up @@ -500,6 +517,24 @@ extension UTMScriptingConfigImpl {
}
}

private func updateQemuAdditionalArguments(from records: [[AnyHashable: Any]]) throws {
let config = config as! UTMQemuConfiguration
let additionalArguments = records.compactMap { record -> QEMUArgument? in
guard let argumentString = record["argumentString"] as? String else { return nil }
var argument = QEMUArgument(argumentString)
// Qemu Additional Arguments in UI, only takes strings
// So, fileUrls of arguments will never be used
// This is here if they support in future
if let fileUrls = record["fileUrls"] as? [URL] {
argument.fileUrls = fileUrls
}
return argument
}
// Update entire additional arguments with new one.
config.qemu.additionalArguments = additionalArguments
}


private func updateAppleConfiguration(from record: [AnyHashable : Any]) throws {
let config = config as! UTMAppleConfiguration
if let name = record["name"] as? String, !name.isEmpty {
Expand Down