Make the disk size configurable upon creation (#26)

* Make the disk size configurable upon creation

I haven't found a way to make a VM aware of a larger disk size. So I didn't add `--disk-size` option to `set` command.

* Fixed size so it's pretty in the UI

* Hidden option to resize disk for testing
This commit is contained in:
Fedor Korotkov
2022-04-18 17:46:19 +03:00
committed by GitHub
parent 609a2530dd
commit d55e5457f9
4 changed files with 26 additions and 8 deletions
+7 -3
View File
@@ -9,7 +9,11 @@ struct Create: AsyncParsableCommand {
@Argument(help: "VM name")
var name: String
@Option(help: ArgumentHelp("Path to the IPSW file (or \"latest\") to fetch the latest appropriate IPSW", valueName: "path")) var fromIPSW: String?
@Option(help: ArgumentHelp("Path to the IPSW file (or \"latest\") to fetch the latest appropriate IPSW", valueName: "path"))
var fromIPSW: String?
@Option(help: ArgumentHelp("Disk size in Gb"))
var diskSize: UInt8 = 32
func validate() throws {
if fromIPSW == nil {
@@ -22,9 +26,9 @@ struct Create: AsyncParsableCommand {
let vmDir = try VMStorage().create(name)
if fromIPSW! == "latest" {
_ = try await VM(vmDir: vmDir, ipswURL: nil)
_ = try await VM(vmDir: vmDir, ipswURL: nil, diskSizeGB: diskSize)
} else {
_ = try await VM(vmDir: vmDir, ipswURL: URL(fileURLWithPath: fromIPSW!))
_ = try await VM(vmDir: vmDir, ipswURL: URL(fileURLWithPath: fromIPSW!), diskSizeGB: diskSize)
}
Foundation.exit(0)
+7
View File
@@ -16,6 +16,9 @@ struct Set: AsyncParsableCommand {
@Option(help: "VM display settings in a format of <width>x<height>(x<dpi>)?. For example, 1200x800 or 1200x800x72")
var display: VMDisplayConfig?
@Option(help: .hidden)
var diskSize: UInt8?
func run() async throws {
do {
let vmStorage = VMStorage()
@@ -43,6 +46,10 @@ struct Set: AsyncParsableCommand {
}
try vmConfig.save(toURL: vmDir.configURL)
if diskSize != nil {
try vmDir.resizeDisk(diskSize!)
}
Foundation.exit(0)
} catch {
+2 -5
View File
@@ -78,7 +78,7 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
return expectedIPSWLocation
}
init(vmDir: VMDirectory, ipswURL: URL?, diskSize: UInt64 = 32 * 1024 * 1024 * 1024) async throws {
init(vmDir: VMDirectory, ipswURL: URL?, diskSizeGB: UInt8) async throws {
let ipswURL = ipswURL != nil ? ipswURL! : try await VM.retrieveLatestIPSW();
// Load the restore image and try to get the requirements
@@ -97,10 +97,7 @@ class VM: NSObject, VZVirtualMachineDelegate, ObservableObject {
let auxStorage = try VZMacAuxiliaryStorage(creatingStorageAt: vmDir.nvramURL, hardwareModel: requirements.hardwareModel)
// Create disk
FileManager.default.createFile(atPath: vmDir.diskURL.path, contents: nil, attributes: nil)
let diskFileHandle = try FileHandle.init(forWritingTo: vmDir.diskURL)
try diskFileHandle.truncate(atOffset: diskSize)
try diskFileHandle.close()
try vmDir.resizeDisk(diskSizeGB)
name = vmDir.name
// Create config
+10
View File
@@ -39,4 +39,14 @@ struct VMDirectory {
throw UninitializedVMDirectoryError()
}
}
func resizeDisk(_ sizeGB: UInt8) throws {
if !FileManager.default.fileExists(atPath: diskURL.path) {
FileManager.default.createFile(atPath: diskURL.path, contents: nil, attributes: nil)
}
let diskFileHandle = try FileHandle.init(forWritingTo: diskURL)
// macOS considers kilo being 1000 and not 1024
try diskFileHandle.truncate(atOffset: UInt64(sizeGB) * 1000 * 1000 * 1000)
try diskFileHandle.close()
}
}