Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

add recovery mode option

Amit Kumard0a6a1e36361
diff --git a/README.md b/README.md
index 3ec8c02e0c..e83688e933 100644
--- a/README.md
+++ b/README.md
@@ -65,12 +65,13 @@ Command Options:
--ipsw <path> Path to IPSW file or 'latest' for macOS VMs
run:
- --no-display Do not start the VNC client app
- --shared-dir <dir> Share directory with VM (format: path[:ro|rw])
- --mount <path> For Linux VMs only, attach a read-only disk image
- --registry <url> Container registry URL (default: ghcr.io)
- --organization <org> Organization to pull from (default: trycua)
- --vnc-port <port> Port to use for the VNC server (default: 0 for auto-assign)
+ --no-display Do not start the VNC client app
+ --shared-dir <dir> Share directory with VM (format: path[:ro|rw])
+ --mount <path> For Linux VMs only, attach a read-only disk image
+ --registry <url> Container registry URL (default: ghcr.io)
+ --organization <org> Organization to pull from (default: trycua)
+ --vnc-port <port> Port to use for the VNC server (default: 0 for auto-assign)
+ --recovery-mode <boolean> For MacOS VMs only, start VM in recovery mode (default: false)
set:
--cpu <cores> New number of CPU cores (e.g., 4)
diff --git a/src/Commands/Run.swift b/src/Commands/Run.swift
index a8c9f3ae74..bf606f487b 100644
--- a/src/Commands/Run.swift
+++ b/src/Commands/Run.swift
@@ -27,7 +27,10 @@ struct Run: AsyncParsableCommand {
@Option(name: [.customLong("vnc-port")], help: "Port to use for the VNC server. Defaults to 0 (auto-assign)")
var vncPort: Int = 0
-
+
+ @Option(help: "For MacOS VMs only, boot into the VM in recovery mode")
+ var recoveryMode: Bool = false
+
private var parsedSharedDirectories: [SharedDirectory] {
get throws {
try sharedDirectories.map { dirString -> SharedDirectory in
@@ -82,4 +85,4 @@ struct Run: AsyncParsableCommand {
vncPort: vncPort
)
}
-}
\ No newline at end of file
+}
diff --git a/src/LumeController.swift b/src/LumeController.swift
index b4b0410674..697ea7c856 100644
--- a/src/LumeController.swift
+++ b/src/LumeController.swift
@@ -256,7 +256,8 @@ final class LumeController {
mount: Path? = nil,
registry: String = "ghcr.io",
organization: String = "trycua",
- vncPort: Int = 0
+ vncPort: Int = 0,
+ recoveryMode: Bool = false
) async throws {
let normalizedName = normalizeVMName(name: name)
Logger.info(
@@ -267,6 +268,7 @@ final class LumeController {
"shared_directories": "\(sharedDirectories.map( { $0.string } ).joined(separator: ", "))",
"mount": mount?.path ?? "none",
"vnc_port": "\(vncPort)",
+ "recovery_mode": "\(recoveryMode)",
])
do {
diff --git a/src/VM/VM.swift b/src/VM/VM.swift
index 8f79cead47..8d736c62bd 100644
--- a/src/VM/VM.swift
+++ b/src/VM/VM.swift
@@ -88,7 +88,7 @@ class VM {
// MARK: - VM Lifecycle Management
- func run(noDisplay: Bool, sharedDirectories: [SharedDirectory], mount: Path?, vncPort: Int = 0) async throws {
+ func run(noDisplay: Bool, sharedDirectories: [SharedDirectory], mount: Path?, vncPort: Int = 0, recoveryMode: Bool = false) async throws {
guard vmDirContext.initialized else {
throw VMError.notInitialized(vmDirContext.name)
}
@@ -112,7 +112,8 @@ class VM {
"sharedDirectories": sharedDirectories.map(
{ $0.string }
).joined(separator: ", "),
- "vncPort": "\(vncPort)"
+ "vncPort": "\(vncPort)",
+ "recoveryMode": "\(recoveryMode)"
])
// Create and configure the VM
@@ -122,7 +123,8 @@ class VM {
memorySize: memorySize,
display: vmDirContext.config.display.string,
sharedDirectories: sharedDirectories,
- mount: mount
+ mount: mount,
+ recoveryMode: recoveryMode
)
virtualizationService = try virtualizationServiceFactory(config)
@@ -368,7 +370,8 @@ class VM {
memorySize: UInt64,
display: String,
sharedDirectories: [SharedDirectory] = [],
- mount: Path? = nil
+ mount: Path? = nil,
+ recoveryMode: Bool = false
) throws -> VMVirtualizationServiceContext {
return VMVirtualizationServiceContext(
cpuCount: cpuCount,
@@ -380,7 +383,8 @@ class VM {
machineIdentifier: vmDirContext.config.machineIdentifier,
macAddress: vmDirContext.config.macAddress!,
diskPath: vmDirContext.diskPath,
- nvramPath: vmDirContext.nvramPath
+ nvramPath: vmDirContext.nvramPath,
+ recoveryMode: recoveryMode
)
}
@@ -400,4 +404,4 @@ class VM {
func finalize(to name: String, home: Home) throws {
try vmDirContext.finalize(to: name)
}
-}
\ No newline at end of file
+}
diff --git a/src/Virtualization/VMVirtualizationService.swift b/src/Virtualization/VMVirtualizationService.swift
index c10e2fd16f..b0fbe88baf 100644
--- a/src/Virtualization/VMVirtualizationService.swift
+++ b/src/Virtualization/VMVirtualizationService.swift
@@ -13,6 +13,7 @@ struct VMVirtualizationServiceContext {
let macAddress: String
let diskPath: Path
let nvramPath: Path
+ let recoveryMode: Bool
}
/// Protocol defining the interface for virtualization operations
@@ -30,30 +31,52 @@ protocol VMVirtualizationService {
@MainActor
class BaseVirtualizationService: VMVirtualizationService {
let virtualMachine: VZVirtualMachine
+ let recoveryMode: Bool // Store whether we should start in recovery mode
var state: VZVirtualMachine.State {
virtualMachine.state
}
- init(virtualMachine: VZVirtualMachine) {
+ init(virtualMachine: VZVirtualMachine, recoveryMode: Bool = false) {
self.virtualMachine = virtualMachine
+ self.recoveryMode = recoveryMode
}
func start() async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
Task { @MainActor in
- virtualMachine.start { result in
- switch result {
- case .success:
- continuation.resume()
- case .failure(let error):
- continuation.resume(throwing: error)
+ // Check if it's a macOS VM and should start in recovery mode
+ if #available(macOS 14.0, *),
+ let macVM = virtualMachine as? VZMacOSVirtualMachine,
+ recoveryMode {
+
+ let startOptions = VZMacOSVirtualMachineStartOptions()
+ startOptions.startUpFrom = .useRecoveryPartition // Boot into recovery mode
+
+ Logger.info("Starting macOS VM in Recovery Mode")
+ macVM.start(options: startOptions) { result in
+ switch result {
+ case .success:
+ continuation.resume()
+ case .failure(let error):
+ continuation.resume(throwing: error)
+ }
+ }
+ } else {
+ Logger.info("Starting VM in normal mode")
+ virtualMachine.start { result in
+ switch result {
+ case .success:
+ continuation.resume()
+ case .failure(let error):
+ continuation.resume(throwing: error)
+ }
}
}
}
}
}
-
+
func stop() async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
virtualMachine.stop { error in
@@ -210,7 +233,7 @@ final class DarwinVirtualizationService: BaseVirtualizationService {
init(configuration: VMVirtualizationServiceContext) throws {
let vzConfig = try Self.createConfiguration(configuration)
- super.init(virtualMachine: VZVirtualMachine(configuration: vzConfig))
+ super.init(virtualMachine: VZVirtualMachine(configuration: vzConfig), recoveryMode: configuration.recoveryMode)
}
func installMacOS(imagePath: Path, progressHandler: (@Sendable (Double) -> Void)?) async throws {
@@ -326,4 +349,4 @@ final class LinuxVirtualizationService: BaseVirtualizationService {
let vzConfig = try Self.createConfiguration(configuration)
super.init(virtualMachine: VZVirtualMachine(configuration: vzConfig))
}
-}
\ No newline at end of file
+}