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

Introduce --random-mac and --random-serial flags for tart set #809

Merged
merged 2 commits into from
May 2, 2024
Merged
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
20 changes: 20 additions & 0 deletions Sources/tart/Commands/Set.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ArgumentParser
import Foundation
import Virtualization

struct Set: AsyncParsableCommand {
static var configuration = CommandConfiguration(commandName: "set", abstract: "Modify VM's configuration")
Expand All @@ -16,6 +17,14 @@ struct Set: AsyncParsableCommand {
@Option(help: "VM display resolution in a format of <width>x<height>. For example, 1200x800")
var display: VMDisplayConfig?

@Flag(help: ArgumentHelp("Generate a new random MAC address for the VM."))
var randomMAC: Bool = false

#if arch(arm64)
@Flag(help: ArgumentHelp("Generate a new random serial number for the macOS VM."))
#endif
var randomSerial: Bool = false

@Option(help: ArgumentHelp("Resize the VMs disk to the specified size in GB (note that the disk size can only be increased to avoid losing data)",
discussion: """
Disk resizing works on most cloud-ready Linux distributions out-of-the box (e.g. Ubuntu Cloud Images
Expand Down Expand Up @@ -51,6 +60,17 @@ struct Set: AsyncParsableCommand {
}
}

if randomMAC {
vmConfig.macAddress = VZMACAddress.randomLocallyAdministered()
}

#if arch(arm64)
if randomSerial {
let oldPlatform = vmConfig.platform as! Darwin
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not assume that it's always Darwin and do a proper type check & throw an error if it's Linux?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though about it but was too lazy and the error message will be self-explanatory. Plus help mentions macOS.

vmConfig.platform = Darwin(ecid: VZMacMachineIdentifier(), hardwareModel: oldPlatform.hardwareModel)
}
#endif

try vmConfig.save(toURL: vmDir.configURL)

if diskSize != nil {
Expand Down