forked from kholia/OSX-KVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli-driver.js
522 lines (434 loc) · 16.2 KB
/
cli-driver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
const { copySync } = require('fs-extra');
const { program } = require('commander');
const { input, select, confirm } = require('@inquirer/prompts');
const {AppleBootable} = require("./node-src/Foundation/AppleBootable");
const {logger} = require("./node-src/logger");
const fs = require("node:fs");
const {LevelupDatabase} = require("./node-src/Database/Levelup");
const _ = require("lodash");
const {AppleVirtualDrive} = require("./node-src/Foundation/AppleVirtualDrive");
const {AppleBaseSystemHub} = require("./node-src/Foundation/AppleBasesystemHub");
const {UnixVirtualization} = require("./node-src/Foundation/UnixVirtualization");
const {AppleComputer} = require("./node-src/Foundation/AppleComputer");
const {createFolderIfNotExists, checkFileExists} = require("./node-src/boot");
const {config} = require("./node-src/config");
const {AppleDisk} = require("./node-src/Foundation/AppleDisk");
const {PhantomFile} = require("./node-src/helpers/PhantomFile");
const {AppleBootableHub} = require("./node-src/Foundation/AppleBootableHub");
const path = require("node:path");
createFolderIfNotExists('data')
createFolderIfNotExists('data/generated')
createFolderIfNotExists('data/bootable')
createFolderIfNotExists('data/hdd')
createFolderIfNotExists('prebuilt')
createFolderIfNotExists('prebuilt/hotdata')
createFolderIfNotExists('prebuilt/developerKit')
checkFileExists(process.cwd() + "/OVMF_VARS-1024x768.fd")
checkFileExists(process.cwd() + "/OVMF_CODE.fd")
checkFileExists(process.cwd() + "/.vnc-port")
checkFileExists(process.cwd() + "/hypervisor.json")
checkFileExists(process.cwd() + "/app.json")
for (const plist of _.get(config, 'plists', [])) {
checkFileExists(process.cwd() + `/osx-serial-generator/${plist}`)
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
async function generateWizardData() {
const dataStorageSize = parseInt(await select({
message: 'Select data storage size in GB',
choices: _.range(64, 1000, 32).map(size => ({
name: `${size} GB`,
value: size,
// description: '',
}))
}))
const operationSystemVersion = await select({
message: 'Select a operation system',
choices: [
{
name: 'sonoma',
value: 'sonoma',
description: 'macOS Sonoma 14.0',
},
{
name: 'ventura',
value: 'ventura',
description: 'macOS Ventura 13.0',
},
],
})
const ram = parseInt(await select({
message: 'Select the amount of RAM',
choices: _.range(1024, 1024 * 32, 1024).map(size => ({
name: `${size} MB`,
value: size,
// description: '',
}))
}))
const graphic = parseInt(await select({
message: 'How graphics work',
choices: [
{
name: `VNC`,
value: 'vnc',
description: 'Use VNC',
},
{
name: `Default graphic`,
value: 'graphic',
description: 'QEMU default',
}
]
}))
return {
dataStorageSize,
operationSystemVersion,
ram,
graphic
}
}
program
.option("--size <size>", "Size of disk")
.option("--force")
.command("create-disk <name>")
.description('Create a hard drive')
.action(async (name) => {
const opts = program.opts()
const disk =
await AppleDisk
.of(name)
.spawnImage(opts.force)
console.log(disk.dump())
})
program
.command("spawn-random-bootable-data")
.description('Create a JSON file to generate a MacOS Bootable disk')
.action(async () => {
const {AppleBootable} = require("./node-src/Foundation/AppleBootable");
console.log(JSON.stringify(AppleBootable.spawnData(), null, 2))
})
program
.command("spawn-random-bootable-disk <name>")
.description('Create a hard drive for the OpenCore bootloader. Uses a random set of data to identify a computer')
.action(async (name) => {
const {AppleBootable} = require("./node-src/Foundation/AppleBootable");
if (!fs.existsSync(`${process.cwd()}/data/generated/`)) {
fs.mkdirSync(`${process.cwd()}/data/generated/`, {
recursive: true
})
}
const data = Object.assign(AppleBootable.spawnData(), {
OUTPUT_QCOW: `${process.cwd()}/data/generated/${name}.qcow2`
})
logger.debug(JSON.stringify(data, null, 2))
AppleBootable
.spawnDisk(data)
.then(logger.info)
.catch(logger.error)
.finally(() => process.exit(0))
})
program
.command("spawn-vm <name>")
.description('Utility for creating MacOS VMs. Simplified version')
.action(async name => {
const { LevelupDatabase } = require("./node-src/Database/Levelup");
const {
dataStorageSize,
operationSystemVersion,
ram,
graphic
} = await generateWizardData()
LevelupDatabase.get('vms', async function (err, value) {
LevelupDatabase.put('vms', JSON.stringify({
vms: _.get(JSON.parse(String(value || "{}")), 'vms', []).concat([
{ name, link: `vm/${name}` }
])
}))
LevelupDatabase.put(`vm/${name}`, JSON.stringify({
name,
label: await input({ message: 'Enter label of VM', default: name }),
wizard: {
dataStorageSize,
operationSystemVersion,
ram,
graphic
}
}))
})
})
program
.command("show-vms")
.description('Show all virtual machines')
.action(async () => {
const { LevelupDatabase } = require("./node-src/Database/Levelup");
LevelupDatabase.get('vms', function (e, data) {
const objects = JSON.parse(String(data))
console.table(_.get(objects, 'vms', []))
})
})
program
.command("killall-qemu-instances")
.description('Kill all instances of QEMU')
.action(async () => {
console.log(require('kill-process-by-name')("qemu-system-x86_64"))
})
program
.command("show-vm <name>")
.description('Show virtual machine')
.action(async (name) => {
const { LevelupDatabase } = require("./node-src/Database/Levelup");
LevelupDatabase.get(`vm/${name}`, function (e, data) {
const object = JSON.parse(String(data || '{}'))
console.info(object)
})
})
program
.command("create-vm <name>")
.description('Create virtual machine record. To create a virtual machine in simple mode, use the spawn-vm utility')
.action(async (name) => {
const banner = ('CuKWiOKWiOKVl-KWkeKWkeKWiOKWiOKVl-KWiOKWiOKVl-KWkeKWkeKWkeKWiOKWiOKVl-KWiOKWiOKWiOKWiOKWiOKWiOKVl-KWkeKWiOKWiOKWiOKWiOKWiOKWiOKWiOKVl-KWiOKWiOKWiOKWiOKWiOKWiOKVl-KWkeKWiOKWiOKVl-KWkeKWkeKWkeKWiOKWiOKVl-KWiOKWiOKVl-KWkeKWiOKWiOKWiOKWiOKWiOKWiOKVl-KWkeKWiOKWiOKWiOKWiOKWiOKVl-KWkeKWiOKWiOKWiOKWiOKWiOKWiOKVl-KWkQrilojilojilZHilpHilpHilojilojilZHilZrilojilojilZfilpHilojilojilZTilZ3ilojilojilZTilZDilZDilojilojilZfilojilojilZTilZDilZDilZDilZDilZ3ilojilojilZTilZDilZDilojilojilZfilojilojilZHilpHilpHilpHilojilojilZHilojilojilZHilojilojilZTilZDilZDilZDilZDilZ3ilojilojilZTilZDilZDilojilojilZfilojilojilZTilZDilZDilojilojilZcK4paI4paI4paI4paI4paI4paI4paI4pWR4paR4pWa4paI4paI4paI4paI4pWU4pWd4paR4paI4paI4paI4paI4paI4paI4pWU4pWd4paI4paI4paI4paI4paI4pWX4paR4paR4paI4paI4paI4paI4paI4paI4pWU4pWd4pWa4paI4paI4pWX4paR4paI4paI4pWU4pWd4paI4paI4pWR4pWa4paI4paI4paI4paI4paI4pWX4paR4paI4paI4pWR4paR4paR4paI4paI4pWR4paI4paI4paI4paI4paI4paI4pWU4pWdCuKWiOKWiOKVlOKVkOKVkOKWiOKWiOKVkeKWkeKWkeKVmuKWiOKWiOKVlOKVneKWkeKWkeKWiOKWiOKVlOKVkOKVkOKVkOKVneKWkeKWiOKWiOKVlOKVkOKVkOKVneKWkeKWkeKWiOKWiOKVlOKVkOKVkOKWiOKWiOKVl-KWkeKVmuKWiOKWiOKWiOKWiOKVlOKVneKWkeKWiOKWiOKVkeKWkeKVmuKVkOKVkOKVkOKWiOKWiOKVl-KWiOKWiOKVkeKWkeKWkeKWiOKWiOKVkeKWiOKWiOKVlOKVkOKVkOKWiOKWiOKVlwrilojilojilZHilpHilpHilojilojilZHilpHilpHilpHilojilojilZHilpHilpHilpHilojilojilZHilpHilpHilpHilpHilpHilojilojilojilojilojilojilojilZfilojilojilZHilpHilpHilojilojilZHilpHilpHilZrilojilojilZTilZ3ilpHilpHilojilojilZHilojilojilojilojilojilojilZTilZ3ilZrilojilojilojilojilojilZTilZ3ilojilojilZHilpHilpHilojilojilZEK4pWa4pWQ4pWd4paR4paR4pWa4pWQ4pWd4paR4paR4paR4pWa4pWQ4pWd4paR4paR4paR4pWa4pWQ4pWd4paR4paR4paR4paR4paR4pWa4pWQ4pWQ4pWQ4pWQ4pWQ4pWQ4pWd4pWa4pWQ4pWd4paR4paR4pWa4pWQ4pWd4paR4paR4paR4pWa4pWQ4pWd4paR4paR4paR4pWa4pWQ4pWd4pWa4pWQ4pWQ4pWQ4pWQ4pWQ4pWd4paR4paR4pWa4pWQ4pWQ4pWQ4pWQ4pWd4paR4pWa4pWQ4pWd4paR4paR4pWa4pWQ4pWd')
console.log(Buffer.from(banner, 'base64').toString())
console.log('')
console.log(' Welcome to the Hypervisor for MacOS Images')
console.log(' Attention! THIS IS NOT A STABLE BUILD. USE IT FOR EXPERIMENTATION ONLY')
console.log('')
console.log(' Credits (without them this fork would not exist)')
console.log(' - https://github.com/kholia/OSX-KVM')
console.log(' - https://github.com/sickcodes/Docker-OSX')
console.log(' - https://github.com/sickcodes/osx-serial-generator')
console.log('')
const {AppleOVMF} = require("./node-src/Foundation/AppleOVMF");
const {AppleDisk} = require("./node-src/Foundation/AppleDisk");
const {AppleComputer} = require("./node-src/Foundation/AppleComputer");
const {AppleBaseSystemHub} = require("./node-src/Foundation/AppleBasesystemHub");
const {AppleBootableHub} = require("./node-src/Foundation/AppleBootableHub");
const computer = new AppleComputer()
if (AppleDisk.isExistsDisk(name) && await confirm({ message: "Skip install" })) {
} else {
}
let {
dataStorageSize,
operationSystemVersion,
ram,
// graphic
} = await generateWizardData()
const operationSystemDrive = {
sonoma: AppleBaseSystemHub.Sonoma,
ventura: AppleBaseSystemHub.Ventura,
}[operationSystemVersion]
const dataVirtualDrive = await AppleDisk
.of(name)
.setSize(dataStorageSize)
.spawnImage(false, true)
let dvdVenturaInstallerDrive = undefined
if (operationSystemVersion === 'ventura') {
dvdVenturaInstallerDrive = AppleDisk.ofDvDIso(
`${process.cwd()}/prebuilt/InstallAssistant/InstallAssistantVentura.iso`,
'AppleVenturaIsoDVD'
)
}
logger.debug('Running...')
computer
.setRam(ram)
.setVersionSystem(operationSystemVersion)
.setDrive(AppleBootableHub.prebuilt)
.setDrive(operationSystemDrive)
.setDrive(dvdVenturaInstallerDrive, 5)
.setDataDrive(dataVirtualDrive.toVirtualDrive(true, "MacHDD")) // should be MacHDD
.setDisplay('sdl')
// .useAppleKvm()
.setEnableGraphic(false)
.setHypervisorVmxConfig(true, true)
.setEnableVnc('127.0.0.1', 1)
.setOvmf(AppleOVMF.ovmfCodeFile)
.setOvmf(AppleOVMF.ovmfVars1024x768File)
try {
computer.spawnAndRunComputer()
} catch (e) {
console.error(e)
}
// const argv = computer.spawnQemuRunnable().getArgs()
//
// const unix = new UnixVirtualization()
//
// console.log(computer.spawnQemuRunnable().getArgs(false))
//
// console.log(unix.run(argv))
})
program
.command("hotspawn-vm <name>")
.description('Create virtual machine with prebuild data')
.action(async (name) => {
const {AppleOVMF} = require("./node-src/Foundation/AppleOVMF");
const {AppleDisk} = require("./node-src/Foundation/AppleDisk");
const {AppleComputer} = require("./node-src/Foundation/AppleComputer");
const {AppleBaseSystemHub} = require("./node-src/Foundation/AppleBasesystemHub");
const {AppleBootableHub} = require("./node-src/Foundation/AppleBootableHub");
const computer = new AppleComputer()
if (_.size(fs.readdirSync(`${process.cwd()}/prebuilt/hotdata`)) === 0) {
logger.error("We were unable to find any HotSpawn prepared images in your configuration")
process.exit(1)
}
const ram = parseInt(await select({
message: 'Select the amount of RAM',
choices: _.range(1024, 1024 * 32, 1024).map(size => ({
name: `${size} MB`,
value: size,
// description: '',
}))
}))
const operationSystemVersion = await select({
message: 'Select a operation system',
choices: [
fs.existsSync(`${process.cwd()}/prebuilt/hotdata/Sonoma.img`) ? {
name: 'sonoma',
value: 'sonoma',
description: 'macOS Sonoma 14.0',
} : null,
fs.existsSync(`${process.cwd()}/prebuilt/hotdata/Ventura.img`) ? {
name: 'ventura',
value: 'ventura',
description: 'macOS Ventura 13.0',
} : null,
].filter(i => i != null),
})
const vncHost = await select({
message: 'Select a VNC-host',
choices: [
{
name: 'localhost (127.0.0.1)',
value: '127.0.0.1',
description: "VNC is ONLY available on YOUR computer"
},
{
name: 'shared (0.0.0.0)',
value: '0.0.0.0',
description: 'VNC is available to EVERYONE who is on the local and global network',
},
],
})
const dataDrive = `${process.cwd()}/prebuilt/hotdata/${capitalizeFirstLetter(String(operationSystemVersion))}.img`
const hotspawnDiskType = await select({
message: 'Please select the data disk type that suits you best',
choices: [
{
name: 'Storage',
value: 'storage',
description: "We will copy the HotSpawn image to the internal memory of the AppleComputer in the folder data/hdd/HOTSPAW_* or load this"
},
{
name: 'Use exist',
value: 'useExists',
description: 'Use a ready-made HotSpawn image on a remote storage from a folder',
},
{
name: 'External',
value: 'external',
description: 'Generate the image in the path you specify. Provide the full route with the .qcow file format. Example: /absolute/url/to/file/disk.qcow',
},
],
})
const dataDisk = new AppleDisk()
let dataDriveReadyUri = `${process.cwd()}/data/hdd/HOTSPAWN_${name}`
if (hotspawnDiskType === 'useExists') {
while (true) {
const pathToDisk = await input({message: "Path"})
if (path.isAbsolute(pathToDisk) && fs.existsSync(pathToDisk)) {
dataDriveReadyUri = path.normalize(pathToDisk)
break
} else {
logger.debug("Disk not found at this path or path is non absolute")
}
}
} else if (hotspawnDiskType === 'external') {
while (true) {
const pathToDisk = await input({message: "Path (override: on)"})
if (path.isAbsolute(pathToDisk)) {
if (!fs.existsSync(pathToDisk)) {
logger.debug('We are preparing the files. Please note that HotSwap will require more preparation time than installation of the system itself.')
copySync(
String(dataDrive),
pathToDisk
)
}
fs.accessSync(pathToDisk)
dataDriveReadyUri = pathToDisk
break
} else {
logger.debug("Disk already exists at this path or path is non absolute")
}
}
} else if (hotspawnDiskType === 'storage') {
dataDriveReadyUri = `${process.cwd()}/data/hdd/HOTSPAWN_${name}`
if (!fs.existsSync(dataDriveReadyUri)) {
logger.debug('We are preparing the files. Please note that HotSwap will require more preparation time than installation of the system itself.')
copySync(
String(dataDrive),
dataDriveReadyUri
)
}
}
dataDisk.useExistDisk(dataDriveReadyUri)
let prebuiltBootableDiskUri
const qcowUri = `${process.cwd()}/data/bootable/HOTSPAWN_${name}.qcow2`
if (!fs.existsSync(qcowUri)) {
if (await confirm({ message: "Generate random MacOS data?", default: true })) {
const data = Object.assign(AppleBootable.spawnData(), {
OUTPUT_QCOW: qcowUri,
MASTER_PLIST: './config-custom.plist'
})
logger.debug(JSON.stringify(data, null, 2))
await AppleBootable.spawnDisk(data)
prebuiltBootableDiskUri = (new AppleDisk()).useExistDisk(qcowUri).toVirtualDrive(true, "OpenCoreBoot")
} else {
prebuiltBootableDiskUri = AppleBootableHub.prebuilt.makeIOSafe()
}
} else {
prebuiltBootableDiskUri = (new AppleDisk()).useExistDisk(qcowUri).toVirtualDrive(true, "OpenCoreBoot")
}
const operationSystemDrive = {
sonoma: AppleBaseSystemHub.Sonoma,
ventura: AppleBaseSystemHub.Ventura,
}[operationSystemVersion]
let developerKit = undefined
const developerKitVersions = fs.readdirSync(`${process.cwd()}/prebuilt/developerKit`).map(path => {
return `${process.cwd()}/prebuilt/developerKit/${path}`
})
if (!_.isEmpty(developerKitVersions)) {
const connectDeveloperKit = await select({
message: "Select DeveloperKit revision",
choices: developerKitVersions.map(path => ({
name: path,
value: path
}))
})
developerKit = AppleDisk.ofDvDIso(
connectDeveloperKit,
'DeveloperKitIso'
)
}
if (await confirm({message: "Save the current virtual machine configuration?"})) {
}
logger.debug('Running...')
computer
.setRam(ram)
.setVersionSystem(operationSystemVersion)
.setDrive(prebuiltBootableDiskUri)
.setDrive(operationSystemDrive)
.setDrive(developerKit, 5)
.setDataDrive(dataDisk.toVirtualDrive(true, "MacHDD")) // should be MacHDD
.setDisplay('sdl')
// .useAppleKvm()
.setEnableGraphic(false)
.setHypervisorVmxConfig(true, true)
.setEnableVnc(vncHost, 1)
.setOvmf(AppleOVMF.ovmfCodeFile)
.setOvmf(AppleOVMF.ovmfVars1024x768File)
try {
computer.spawnAndRunComputer()
} catch (e) {
console.error(e)
}
})
program.parse();