Support getting the MAC address, and setting an arbitrary MAC address (#1327)

* tart get: Report MAC address

* tart set: Support setting an arbitrary MAC address
This commit is contained in:
Brendan Shanks
2026-10-13 16:57:49 +00:00
committed by GitHub
parent 7d39f4cf68
commit 12501370cc
2 changed files with 17 additions and 0 deletions
+2
View File
@@ -9,6 +9,7 @@ fileprivate struct VMInfo: Encodable {
let DiskFormat: String
let Size: HumanReadableByteCount
let Display: String
let MACAddress: String
let Running: Bool
let State: String
}
@@ -38,6 +39,7 @@ struct Get: AsyncParsableCommand {
String(format: "%.3f", Float($0) / 1000 / 1000 / 1000)
},
Display: vmConfig.display.description,
MACAddress: vmConfig.macAddress.string,
Running: try vmDir.running(),
State: try vmDir.state().rawValue
)
+15
View File
@@ -20,6 +20,9 @@ struct Set: AsyncParsableCommand {
@Flag(inversion: .prefixedNo, help: ArgumentHelp("Whether to automatically reconfigure the VM's display to fit the window"))
var displayRefit: Bool? = nil
@Option(help: ArgumentHelp("MAC address to use."))
var mac: String?
@Flag(help: ArgumentHelp("Generate a new random MAC address for the VM."))
var randomMAC: Bool = false
@@ -47,6 +50,10 @@ struct Set: AsyncParsableCommand {
throw ValidationError("--disk is not supported for VMs with a stacked disk")
}
if randomMAC && mac != nil {
throw ValidationError("--random-mac and --mac are mutually exclusive")
}
var vmConfig = try VMConfig(fromURL: vmDir.configURL)
if let cpu = cpu {
@@ -71,6 +78,14 @@ struct Set: AsyncParsableCommand {
vmConfig.displayRefit = displayRefit
}
if let mac = mac {
if let macAddress = VZMACAddress.init(string: mac), macAddress.isUnicastAddress {
vmConfig.macAddress = macAddress
} else {
throw ValidationError("'\(mac)' is not a valid unicast MAC address")
}
}
if randomMAC {
vmConfig.macAddress = VZMACAddress.randomLocallyAdministered()
}