From 1afaa7ec7af10ebfceac52c26d691bc5f55b2ebe Mon Sep 17 00:00:00 2001 From: Fedor Korotkov Date: Tue, 11 Jul 2023 11:49:43 -0400 Subject: [PATCH] Reclaim disk space only in the very end of `tart clone` (#553) * Reclaim disk space only in the very end of `tart clone` Please check comments in code for reasoning. * Phrasing * Removed hidden argument --- Sources/tart/Commands/Clone.swift | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/tart/Commands/Clone.swift b/Sources/tart/Commands/Clone.swift index 8ac1d69..8b4016c 100644 --- a/Sources/tart/Commands/Clone.swift +++ b/Sources/tart/Commands/Clone.swift @@ -31,8 +31,6 @@ struct Clone: AsyncParsableCommand { } let sourceVM = try VMStorageHelper.open(sourceName) - try Prune.reclaimIfNeeded(UInt64(sourceVM.sizeBytes())) - let tmpVMDir = try VMDirectory.temporary() // Lock the temporary VM directory to prevent it's garbage collection @@ -44,13 +42,18 @@ struct Clone: AsyncParsableCommand { let lock = try FileLock(lockURL: Config().tartHomeDir) try lock.lock() - let generateMAC = try localStorage.hasVMsWithMACAddress(macAddress: sourceVM.macAddress()) + let generateMAC = try localStorage.hasVMsWithMACAddress(macAddress: sourceVM.macAddress()) && sourceVM.state() != "suspended" try sourceVM.clone(to: tmpVMDir, generateMAC: generateMAC) try localStorage.move(newName, from: tmpVMDir) try lock.unlock() + + // APFS is doing copy-on-write so the above cloning operation (just copying files on disk) + // is not actually claiming new space until the VM is started and it writes something to disk. + // So once we clone the VM let's try to claim a little bit of space for the VM to run. + try Prune.reclaimIfNeeded(UInt64(sourceVM.sizeBytes())) }, onCancel: { try? FileManager.default.removeItem(at: tmpVMDir.baseURL) })