From e8dbb86fc097eb2e2712a1391983703285fc24b6 Mon Sep 17 00:00:00 2001 From: Fedor Korotkov Date: Fri, 7 Jul 2023 05:38:34 -0400 Subject: [PATCH] Support stopping of suspended VMs (#541) --- Sources/tart/Commands/Stop.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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