diff --git a/Sources/tart/Commands/Run.swift b/Sources/tart/Commands/Run.swift index 3f5e6db2..4f3f5bf6 100644 --- a/Sources/tart/Commands/Run.swift +++ b/Sources/tart/Commands/Run.swift @@ -108,6 +108,12 @@ struct Run: AsyncParsableCommand { if graphics && noGraphics { throw ValidationError("--graphics and --no-graphics are mutually exclusive") } + + let localStorage = VMStorageLocal() + let vmDir = try localStorage.open(name) + if try vmDir.state() == "suspended" { + suspendable = true + } } @MainActor diff --git a/Sources/tart/Commands/Stop.swift b/Sources/tart/Commands/Stop.swift index 1ecc0d05..9c97c1e6 100644 --- a/Sources/tart/Commands/Stop.swift +++ b/Sources/tart/Commands/Stop.swift @@ -14,6 +14,21 @@ struct Stop: AsyncParsableCommand { func run() async throws { let vmDir = try VMStorageLocal().open(name) + switch try vmDir.state() { + case "suspended": + try stopSuspended(vmDir) + case "running": + try await stopRunning(vmDir) + default: + return + } + } + + func stopSuspended(_ vmDir: VMDirectory) throws { + try? FileManager.default.removeItem(at: vmDir.stateURL) + } + + func stopRunning(_ vmDir: VMDirectory) async throws { let lock = try PIDLock(lockURL: vmDir.configURL) // Find the VM's PID