Remove hdutil

This commit is contained in:
f-trycua
2025-04-20 00:06:40 -07:00
committed by Dillon DuPont
parent a730cdc90e
commit a4c923a5cf

View File

@@ -1164,76 +1164,6 @@ class ImageContainerRegistry: @unchecked Sendable {
)
}
// Create a properly formatted disk image
Logger.info("Converting assembled data to proper disk image format...")
// Get actual disk usage of the assembled file
let assembledUsage = getActualDiskUsage(path: outputURL.path)
let bufferBytes: UInt64 = 2 * 1024 * 1024 * 1024 // 2GB buffer
let requiredSpace = assembledUsage + bufferBytes
// Check available disk space in the destination directory
let fileManager = FileManager.default
let availableSpace =
try? fileManager.attributesOfFileSystem(
forPath: outputURL.deletingLastPathComponent().path)[.systemFreeSize]
as? UInt64
if let available = availableSpace, available < requiredSpace {
Logger.error(
"Insufficient disk space to convert disk image format. Skipping conversion.",
metadata: [
"available": ByteCountFormatter.string(
fromByteCount: Int64(available), countStyle: .file),
"required": ByteCountFormatter.string(
fromByteCount: Int64(requiredSpace), countStyle: .file),
]
)
} else {
// Prioritize SPARSE format for better sparse file handling
Logger.info("Attempting conversion to SPARSE format...")
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/hdiutil")
process.arguments = [
"convert",
outputURL.path, // Source: our assembled file
"-format", "SPARSE", // Format: SPARSE (best for sparse images)
"-o", outputURL.path, // Output: overwrite with converted image
]
let errorPipe = Pipe()
process.standardError = errorPipe
process.standardOutput = errorPipe
try process.run()
process.waitUntilExit()
// Check for errors
let outputData = errorPipe.fileHandleForReading.readDataToEndOfFile()
if !outputData.isEmpty,
let outputString = String(data: outputData, encoding: .utf8)
{
Logger.info("hdiutil output: \(outputString)")
}
if process.terminationStatus == 0 {
// Find the potentially renamed formatted file
let formattedFile = findFormattedFile(tempFormatted: outputURL) ?? outputURL
// If the output path is different, remove the original and move the new one
if formattedFile.path != outputURL.path {
try? FileManager.default.removeItem(at: outputURL)
try FileManager.default.moveItem(at: formattedFile, to: outputURL)
}
Logger.info("Successfully converted disk image to proper format (SPARSE)")
} else {
Logger.error(
"Failed to convert disk image to SPARSE format. VM might not start properly."
)
// If SPARSE failed, maybe try UDRW as a last resort?
// For now, we'll just log the error.
}
}
Logger.info("Disk image reassembly completed")
} else {
// Copy single disk image if it exists
@@ -1486,74 +1416,6 @@ class ImageContainerRegistry: @unchecked Sendable {
)
}
// Create a properly formatted disk image
Logger.info("Converting assembled data to proper disk image format...")
// Get actual disk usage of the assembled file
let assembledUsage = getActualDiskUsage(path: outputURL.path)
let bufferBytes: UInt64 = 2 * 1024 * 1024 * 1024 // 2GB buffer
let requiredSpace = assembledUsage + bufferBytes
// Check available disk space in the destination directory
let fileManager = FileManager.default
let availableSpace =
try? fileManager.attributesOfFileSystem(
forPath: outputURL.deletingLastPathComponent().path)[.systemFreeSize] as? UInt64
if let available = availableSpace, available < requiredSpace {
Logger.error(
"Insufficient disk space to convert disk image format. Skipping conversion.",
metadata: [
"available": ByteCountFormatter.string(
fromByteCount: Int64(available), countStyle: .file),
"required": ByteCountFormatter.string(
fromByteCount: Int64(requiredSpace), countStyle: .file),
]
)
} else {
// Prioritize SPARSE format for better sparse file handling
Logger.info("Attempting conversion to SPARSE format...")
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/hdiutil")
process.arguments = [
"convert",
outputURL.path, // Source: our assembled file
"-format", "SPARSE", // Format: SPARSE (best for sparse images)
"-o", outputURL.path, // Output: overwrite with converted image
]
let errorPipe = Pipe()
process.standardError = errorPipe
process.standardOutput = errorPipe
try process.run()
process.waitUntilExit()
// Check for errors
let outputData = errorPipe.fileHandleForReading.readDataToEndOfFile()
if !outputData.isEmpty, let outputString = String(data: outputData, encoding: .utf8)
{
Logger.info("hdiutil output: \(outputString)")
}
if process.terminationStatus == 0 {
// Find the potentially renamed formatted file
let formattedFile = findFormattedFile(tempFormatted: outputURL) ?? outputURL
// If the output path is different, remove the original and move the new one
if formattedFile.path != outputURL.path {
try? FileManager.default.removeItem(at: outputURL)
try FileManager.default.moveItem(at: formattedFile, to: outputURL)
}
Logger.info("Successfully converted disk image to proper format (SPARSE)")
} else {
Logger.error(
"Failed to convert disk image to SPARSE format. VM might not start properly."
)
// If SPARSE failed, maybe try UDRW as a last resort?
// For now, we'll just log the error.
}
}
Logger.info("Disk image reassembly completed")
}