Skip to content

scripting: cannot create macOS VM #6982

@naveenrajm7

Description

@naveenrajm7

Creating VM using Apple backend , will result in Linux (operating system) VM by default, which is fine.
However, the field Boot->Operating System is not exposed to config , which means we cannot update the the OS field in Boot section to macOS and hence we will not be able to create a macOS VM via script.

Default VM creation issue

 let config = UTMAppleConfiguration()
 // Creates Linux VM by default
 config.system.boot = try UTMAppleConfigurationBoot(for: .linux)

 // But the config.system.boot is not exposed in config to be updated, Hence you cannot create macOS VM via script

Originally requested in naveenrajm7/packer-plugin-utm#12

Suggested Fix:

        // Get os via config record
        guard let osString = record["operatingSystem"] as? String  else {
            throw ScriptingError.osNotSpecified
        }
        let config = UTMAppleConfiguration()
        // validate os. Creating MacOS requires you to have a valid ipsw file, so enforce it
        switch osString {
        case "macos":
            guard let ipswURL = record["ipsw"] as? URL else {
                throw ScriptingError.ipswNotSpecified
            }
            let image = try await VZMacOSRestoreImage.image(from: ipswURL)
            guard let model = image.mostFeaturefulSupportedConfiguration?.hardwareModel else {
                throw ScriptingError.ipswNotSupported
            }
            config.system.macPlatform = UTMAppleConfigurationMacPlatform(newHardware: model)
            config.system.boot = try UTMAppleConfigurationBoot(for: .macOS)
            config.system.boot.macRecoveryIpswURL = ipswURL
        case "linux":
            config.system.boot = try UTMAppleConfigurationBoot(for: .linux)
        default:
            throw ScriptingError.osNotSupported
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions