Skip to content

Commit

Permalink
Reclaim disk space only in the very end of tart clone (#553)
Browse files Browse the repository at this point in the history
* Reclaim disk space only in the very end of `tart clone`

Please check comments in code for reasoning.

* Phrasing

* Removed hidden argument
  • Loading branch information
fkorotkov authored Jul 11, 2023
1 parent 826e508 commit 1afaa7e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Sources/tart/Commands/Clone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
})
Expand Down

0 comments on commit 1afaa7e

Please sign in to comment.