forked from Code-Hex/vz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualization.m
689 lines (607 loc) · 25.5 KB
/
virtualization.m
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//
// virtualization.m
//
// Created by codehex.
//
#import "virtualization.h"
char *copyCString(NSString *nss)
{
const char *cc = [nss UTF8String];
char *c = calloc([nss length]+1, 1);
strncpy(c, cc, [nss length]);
return c;
}
@implementation Observer
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
@autoreleasepool {
if ([keyPath isEqualToString:@"state"]) {
int newState = (int)[change[NSKeyValueChangeNewKey] integerValue];
char *vmid = copyCString((NSString *)context);
changeStateOnObserver(newState, vmid);
free(vmid);
} else {
// bool canVal = (bool)[change[NSKeyValueChangeNewKey] boolValue];
// char *vmid = copyCString((NSString *)context);
// char *key = copyCString(keyPath);
// changeCanPropertyOnObserver(canVal, vmid, key);
// free(vmid);
// free(key);
}
}
}
@end
@implementation VZVirtioSocketListenerDelegateImpl
- (BOOL)listener:(VZVirtioSocketListener *)listener shouldAcceptNewConnection:(VZVirtioSocketConnection *)connection fromSocketDevice:(VZVirtioSocketDevice *)socketDevice;
{
return (BOOL)shouldAcceptNewConnectionHandler(listener, connection, socketDevice);
}
@end
/*!
@abstract Create a VZLinuxBootLoader with the Linux kernel passed as URL.
@param kernelPath Path of Linux kernel on the local file system.
*/
void *newVZLinuxBootLoader(const char *kernelPath)
{
VZLinuxBootLoader *ret;
@autoreleasepool {
NSString *kernelPathNSString = [NSString stringWithUTF8String:kernelPath];
NSURL *kernelURL = [NSURL fileURLWithPath:kernelPathNSString];
ret = [[VZLinuxBootLoader alloc] initWithKernelURL:kernelURL];
}
return ret;
}
/*!
@abstract Set the command-line parameters.
@param bootLoader VZLinuxBootLoader
@param commandLine The command-line parameters passed to the kernel on boot.
@link https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
*/
void setCommandLineVZLinuxBootLoader(void *bootLoaderPtr, const char *commandLine)
{
VZLinuxBootLoader *bootLoader = (VZLinuxBootLoader *)bootLoaderPtr;
@autoreleasepool {
NSString *commandLineNSString = [NSString stringWithUTF8String:commandLine];
[bootLoader setCommandLine:commandLineNSString];
}
}
/*!
@abstract Set the optional initial RAM disk.
@param bootLoader VZLinuxBootLoader
@param ramdiskPath The RAM disk is mapped into memory before booting the kernel.
@link https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
*/
void setInitialRamdiskURLVZLinuxBootLoader(void *bootLoaderPtr, const char *ramdiskPath)
{
VZLinuxBootLoader *bootLoader = (VZLinuxBootLoader *)bootLoaderPtr;
@autoreleasepool {
NSString *ramdiskPathNSString = [NSString stringWithUTF8String:ramdiskPath];
NSURL *ramdiskURL = [NSURL fileURLWithPath:ramdiskPathNSString];
[bootLoader setInitialRamdiskURL:ramdiskURL];
}
}
/*!
@abstract Validate the configuration.
@param config Virtual machine configuration.
@param error If not nil, assigned with the validation error if the validation failed.
@return true if the configuration is valid.
*/
bool validateVZVirtualMachineConfiguration(void *config, void **error)
{
return (bool)[(VZVirtualMachineConfiguration *)config
validateWithError:(NSError * _Nullable * _Nullable)error];
}
/*!
@abstract Create a new Virtual machine configuration.
@param bootLoader Boot loader used when the virtual machine starts.
@param CPUCount Number of CPUs.
@discussion
The number of CPUs must be a value between VZVirtualMachineConfiguration.minimumAllowedCPUCount
and VZVirtualMachineConfiguration.maximumAllowedCPUCount.
@see VZVirtualMachineConfiguration.minimumAllowedCPUCount
@see VZVirtualMachineConfiguration.maximumAllowedCPUCount
@param memorySize Virtual machine memory size in bytes.
@discussion
The memory size must be a multiple of a 1 megabyte (1024 * 1024 bytes) between VZVirtualMachineConfiguration.minimumAllowedMemorySize
and VZVirtualMachineConfiguration.maximumAllowedMemorySize.
The memorySize represents the total physical memory seen by a guest OS running in the virtual machine.
Not all memory is allocated on start, the virtual machine allocates memory on demand.
@see VZVirtualMachineConfiguration.minimumAllowedMemorySize
@see VZVirtualMachineConfiguration.maximumAllowedMemorySize
*/
void *newVZVirtualMachineConfiguration(void *bootLoaderPtr,
unsigned int CPUCount,
unsigned long long memorySize)
{
VZVirtualMachineConfiguration *config = [[VZVirtualMachineConfiguration alloc] init];
[config setBootLoader:(VZLinuxBootLoader *)bootLoaderPtr];
[config setCPUCount:(NSUInteger)CPUCount];
[config setMemorySize:memorySize];
return config;
}
/*!
@abstract List of entropy devices. Empty by default.
@see VZVirtioEntropyDeviceConfiguration
*/
void setEntropyDevicesVZVirtualMachineConfiguration(void *config,
void *entropyDevices)
{
[(VZVirtualMachineConfiguration *)config setEntropyDevices:[(NSMutableArray *)entropyDevices copy]];
}
/*!
@abstract List of memory balloon devices. Empty by default.
@see VZVirtioTraditionalMemoryBalloonDeviceConfiguration
*/
void setMemoryBalloonDevicesVZVirtualMachineConfiguration(void *config,
void *memoryBalloonDevices)
{
[(VZVirtualMachineConfiguration *)config setMemoryBalloonDevices:[(NSMutableArray *)memoryBalloonDevices copy]];
}
/*!
@abstract List of network adapters. Empty by default.
@see VZVirtioNetworkDeviceConfiguration
*/
void setNetworkDevicesVZVirtualMachineConfiguration(void *config,
void *networkDevices)
{
[(VZVirtualMachineConfiguration *)config setNetworkDevices:[(NSMutableArray *)networkDevices copy]];
}
/*!
@abstract List of serial ports. Empty by default.
@see VZVirtioConsoleDeviceSerialPortConfiguration
*/
void setSerialPortsVZVirtualMachineConfiguration(void *config,
void *serialPorts)
{
[(VZVirtualMachineConfiguration *)config setSerialPorts:[(NSMutableArray *)serialPorts copy]];
}
/*!
@abstract List of socket devices. Empty by default.
@see VZVirtioSocketDeviceConfiguration
*/
void setSocketDevicesVZVirtualMachineConfiguration(void *config,
void *socketDevices)
{
[(VZVirtualMachineConfiguration *)config setSocketDevices:[(NSMutableArray *)socketDevices copy]];
}
/*!
@abstract List of disk devices. Empty by default.
@see VZVirtioBlockDeviceConfiguration
*/
void setStorageDevicesVZVirtualMachineConfiguration(void *config,
void *storageDevices)
{
[(VZVirtualMachineConfiguration *)config setStorageDevices:[(NSMutableArray *)storageDevices copy]];
}
void setDirectorySharingVZVirtualMachineConfiguration(void *config,
void *directoryConfigs)
{
[(VZVirtualMachineConfiguration *)config setDirectorySharingDevices:[(NSMutableArray *)directoryConfigs copy]];
}
/*!
@abstract Intialize the VZFileHandleSerialPortAttachment from file descriptors.
@param readFileDescriptor File descriptor for reading from the file.
@param writeFileDescriptor File descriptor for writing to the file.
@discussion
Each file descriptor must a valid.
*/
void *newVZFileHandleSerialPortAttachment(int readFileDescriptor, int writeFileDescriptor)
{
VZFileHandleSerialPortAttachment *ret;
@autoreleasepool {
NSFileHandle *fileHandleForReading = [[NSFileHandle alloc] initWithFileDescriptor:readFileDescriptor];
NSFileHandle *fileHandleForWriting = [[NSFileHandle alloc] initWithFileDescriptor:writeFileDescriptor];
ret = [[VZFileHandleSerialPortAttachment alloc]
initWithFileHandleForReading:fileHandleForReading
fileHandleForWriting:fileHandleForWriting];
}
return ret;
}
/*!
@abstract Initialize the VZFileSerialPortAttachment from a URL of a file.
@param filePath The path of the file for the attachment on the local file system.
@param shouldAppend True if the file should be opened in append mode, false otherwise.
When a file is opened in append mode, writing to that file will append to the end of it.
@param error If not nil, used to report errors if intialization fails.
@return A VZFileSerialPortAttachment on success. Nil otherwise and the error parameter is populated if set.
*/
void *newVZFileSerialPortAttachment(const char *filePath, bool shouldAppend, void **error)
{
VZFileSerialPortAttachment *ret;
@autoreleasepool {
NSString *filePathNSString = [NSString stringWithUTF8String:filePath];
NSURL *fileURL = [NSURL fileURLWithPath:filePathNSString];
ret = [[VZFileSerialPortAttachment alloc]
initWithURL:fileURL append:(BOOL)shouldAppend error:(NSError * _Nullable * _Nullable)error];
}
return ret;
}
/*!
@abstract Create a new Virtio Console Serial Port Device configuration
@param attachment Base class for a serial port attachment.
@discussion
The device creates a console which enables communication between the host and the guest through the Virtio interface.
The device sets up a single port on the Virtio console device.
*/
void *newVZVirtioConsoleDeviceSerialPortConfiguration(void *attachment)
{
VZVirtioConsoleDeviceSerialPortConfiguration *config = [[VZVirtioConsoleDeviceSerialPortConfiguration alloc] init];
[config setAttachment:(VZSerialPortAttachment *)attachment];
return config;
}
/*!
@abstract Create a new Network device attachment bridging a host physical interface with a virtual network device.
@param networkInterface a network interface that bridges a physical interface.
@discussion
A bridged network allows the virtual machine to use the same physical interface as the host. Both host and virtual machine
send and receive packets on the same physical interface but have distinct network layers.
The bridge network device attachment is used with a VZNetworkDeviceConfiguration to define a virtual network device.
Using a VZBridgedNetworkDeviceAttachment requires the app to have the "com.apple.vm.networking" entitlement.
@see VZBridgedNetworkInterface
@see VZNetworkDeviceConfiguration
@see VZVirtioNetworkDeviceConfiguration
*/
void *newVZBridgedNetworkDeviceAttachment(void *networkInterface)
{
return [[VZBridgedNetworkDeviceAttachment alloc] initWithInterface:(VZBridgedNetworkInterface *)networkInterface];
}
/*!
@abstract Create a new Network device attachment using network address translation (NAT) with outside networks.
@discussion
Using the NAT attachment type, the host serves as router and performs network address translation for accesses to outside networks.
@see VZNetworkDeviceConfiguration
@see VZVirtioNetworkDeviceConfiguration
*/
void *newVZNATNetworkDeviceAttachment()
{
return [[VZNATNetworkDeviceAttachment alloc] init];
}
/*!
@abstract Create a new Network device attachment sending raw network packets over a file handle.
@discussion
The file handle attachment transmits the raw packets/frames between the virtual network interface and a file handle.
The data transmitted through this attachment is at the level of the data link layer.
The file handle must hold a connected datagram socket.
@see VZNetworkDeviceConfiguration
@see VZVirtioNetworkDeviceConfiguration
*/
void *newVZFileHandleNetworkDeviceAttachment(int fileDescriptor)
{
VZFileHandleNetworkDeviceAttachment *ret;
@autoreleasepool {
NSFileHandle *fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fileDescriptor];
ret = [[VZFileHandleNetworkDeviceAttachment alloc] initWithFileHandle:fileHandle];
}
return ret;
}
/*!
@abstract Create a new Configuration of a paravirtualized network device of type Virtio Network Device.
@discussion
The communication channel used on the host is defined through the attachment. It is set with the VZNetworkDeviceConfiguration.attachment property.
The configuration is only valid with valid MACAddress and attachment.
@see VZVirtualMachineConfiguration.networkDevices
@param attachment Base class for a network device attachment.
@discussion
A network device attachment defines how a virtual network device interfaces with the host system.
VZNetworkDeviceAttachment should not be instantiated directly. One of its subclasses should be used instead.
Common attachment types include:
- VZNATNetworkDeviceAttachment
- VZFileHandleNetworkDeviceAttachment
@see VZBridgedNetworkDeviceAttachment
@see VZFileHandleNetworkDeviceAttachment
@see VZNATNetworkDeviceAttachment
*/
void *newVZVirtioNetworkDeviceConfiguration(void *attachment)
{
VZVirtioNetworkDeviceConfiguration *config = [[VZVirtioNetworkDeviceConfiguration alloc] init];
[config setAttachment:(VZNetworkDeviceAttachment *)attachment];
return config;
}
/*!
@abstract Create a new Virtio Entropy Device confiuration
@discussion The device exposes a source of entropy for the guest's random number generator.
*/
void *newVZVirtioEntropyDeviceConfiguration()
{
return [[VZVirtioEntropyDeviceConfiguration alloc] init];
}
/*!
@abstract Initialize a VZVirtioBlockDeviceConfiguration with a device attachment.
@param attachment The storage device attachment. This defines how the virtualized device operates on the host side.
@see VZDiskImageStorageDeviceAttachment
*/
void *newVZVirtioBlockDeviceConfiguration(void *attachment)
{
return [[VZVirtioBlockDeviceConfiguration alloc] initWithAttachment:(VZStorageDeviceAttachment *)attachment];
}
/*!
@abstract Initialize the attachment from a local file url.
@param diskPath Local file path to the disk image in RAW format.
@param readOnly If YES, the device attachment is read-only, otherwise the device can write data to the disk image.
@param error If not nil, assigned with the error if the initialization failed.
@return A VZDiskImageStorageDeviceAttachment on success. Nil otherwise and the error parameter is populated if set.
*/
void *newVZDiskImageStorageDeviceAttachment(const char *diskPath, bool readOnly, void **error)
{
NSString *diskPathNSString = [NSString stringWithUTF8String:diskPath];
NSURL *diskURL = [NSURL fileURLWithPath:diskPathNSString];
return [[VZDiskImageStorageDeviceAttachment alloc]
initWithURL:diskURL
readOnly:(BOOL)readOnly
cachingMode:(VZDiskImageCachingMode)0
synchronizationMode:(VZDiskImageSynchronizationMode)VZDiskImageSynchronizationModeFsync
error:(NSError * _Nullable * _Nullable)error];
}
void *newVZVirtioDirectorySharingDeviceConfiguration(const char *tagName, const char *folderPath, bool readOnly)
{
NSString *tagNameNSString = [NSString stringWithUTF8String:tagName];
NSString *folderPathNSString = [NSString stringWithUTF8String:folderPath];
NSURL *folderPathURL = [NSURL fileURLWithPath:folderPathNSString];
VZSharedDirectory *directory = [[VZSharedDirectory alloc] initWithURL:folderPathURL readOnly:(BOOL)readOnly];
VZVirtioFileSystemDeviceConfiguration *share_conf = [[VZVirtioFileSystemDeviceConfiguration alloc] initWithTag:tagNameNSString];
share_conf.share = [[VZSingleDirectoryShare alloc] initWithDirectory:directory];
return share_conf;
}
/*!
@abstract Create a configuration of the Virtio traditional memory balloon device.
@discussion
This configuration creates a Virtio traditional memory balloon device which allows for managing guest memory.
Only one Virtio traditional memory balloon device can be used per virtual machine.
@see VZVirtioTraditionalMemoryBalloonDevice
*/
void *newVZVirtioTraditionalMemoryBalloonDeviceConfiguration()
{
return [[VZVirtioTraditionalMemoryBalloonDeviceConfiguration alloc] init];
}
/*!
@abstract Create a configuration of the Virtio socket device.
@discussion
This configuration creates a Virtio socket device for the guest which communicates with the host through the Virtio interface.
Only one Virtio socket device can be used per virtual machine.
@see VZVirtioSocketDevice
*/
void *newVZVirtioSocketDeviceConfiguration()
{
return [[VZVirtioSocketDeviceConfiguration alloc] init];
}
/*!
@abstract The VZVirtioSocketListener object represents a listener for the Virtio socket device.
@discussion
The listener encompasses a VZVirtioSocketListenerDelegate object.
VZVirtioSocketListener is used with VZVirtioSocketDevice to listen to a particular port.
The delegate is used when a guest connects to a port associated with the listener.
@see VZVirtioSocketDevice
@see VZVirtioSocketListenerDelegate
*/
void *newVZVirtioSocketListener()
{
VZVirtioSocketListener *ret = [[VZVirtioSocketListener alloc] init];
[ret setDelegate:[[VZVirtioSocketListenerDelegateImpl alloc] init]];
return ret;
}
/*!
@abstract Sets a listener at a specified port.
@discussion
There is only one listener per port, any existing listener will be removed, and the specified listener here will be set instead.
The same listener can be registered on multiple ports.
The listener's delegate will be called whenever the guest connects to that port.
@param listener The VZVirtioSocketListener object to be set.
@param port The port number to set the listener at.
*/
void VZVirtioSocketDevice_setSocketListenerForPort(void *socketDevice, void *vmQueue, void *listener, uint32_t port)
{
dispatch_sync((dispatch_queue_t)vmQueue, ^{
[(VZVirtioSocketDevice *)socketDevice setSocketListener:(VZVirtioSocketListener *)listener forPort:port];
});
}
/*!
@abstract Removes the listener at a specfied port.
@discussion Does nothing if the port had no listener.
@param port The port number at which the listener is to be removed.
*/
void VZVirtioSocketDevice_removeSocketListenerForPort(void *socketDevice, void *vmQueue, uint32_t port)
{
dispatch_sync((dispatch_queue_t)vmQueue, ^{
[(VZVirtioSocketDevice *)socketDevice removeSocketListenerForPort:port];
});
}
typedef void (^connection_handler_t)(VZVirtioSocketConnection *, NSError *);
connection_handler_t generateConnectionHandler(const char *socketDeviceID, void handler(void *, void *, char *))
{
connection_handler_t ret;
@autoreleasepool {
NSString *str = [NSString stringWithUTF8String:socketDeviceID];
ret = Block_copy(^(VZVirtioSocketConnection *connection, NSError *err){
handler(connection, err, copyCString(str));
});
}
return ret;
}
/*!
@abstract Connects to a specified port.
@discussion Does nothing if the guest does not listen on that port.
@param port The port number to connect to.
@param completionHandler Block called after the connection has been successfully established or on error.
The error parameter passed to the block is nil if the connection was successful.
*/
void VZVirtioSocketDevice_connectToPort(void *socketDevice, void *vmQueue, uint32_t port, const char *socketDeviceID)
{
connection_handler_t handler = generateConnectionHandler(socketDeviceID, connectionHandler);
dispatch_sync((dispatch_queue_t)vmQueue, ^{
[(VZVirtioSocketDevice *)socketDevice connectToPort:port completionHandler:handler];
});
Block_release(handler);
}
VZVirtioSocketConnectionFlat convertVZVirtioSocketConnection2Flat(void *connection)
{
VZVirtioSocketConnectionFlat ret;
ret.sourcePort = [(VZVirtioSocketConnection *)connection sourcePort];
ret.destinationPort = [(VZVirtioSocketConnection *)connection destinationPort];
ret.fileDescriptor = [(VZVirtioSocketConnection *)connection fileDescriptor];
return ret;
}
/*!
@abstract Initialize the virtual machine.
@param config The configuration of the virtual machine.
The configuration must be valid. Validation can be performed at runtime with [VZVirtualMachineConfiguration validateWithError:].
The configuration is copied by the initializer.
@param queue The serial queue on which the virtual machine operates.
Every operation on the virtual machine must be done on that queue. The callbacks and delegate methods are invoked on that queue.
If the queue is not serial, the behavior is undefined.
*/
void *newVZVirtualMachineWithDispatchQueue(void *config, void *queue, const char *vmid)
{
VZVirtualMachine *vm = [[VZVirtualMachine alloc]
initWithConfiguration:(VZVirtualMachineConfiguration *)config
queue:(dispatch_queue_t)queue];
@autoreleasepool {
Observer *o = [[Observer alloc] init];
NSString *str = [NSString stringWithUTF8String:vmid];
[vm addObserver:o forKeyPath:@"state"
options:NSKeyValueObservingOptionNew
context:[str copy]];
}
return vm;
}
/*!
@abstract Return the list of socket devices configured on this virtual machine. Return an empty array if no socket device is configured.
@see VZVirtioSocketDeviceConfiguration
@see VZVirtualMachineConfiguration
*/
void *VZVirtualMachine_socketDevices(void *machine)
{
return [(VZVirtualMachine *)machine socketDevices]; // NSArray<VZSocketDevice *>
}
/*!
@abstract Initialize the VZMACAddress from a string representation of a MAC address.
@param string
The string should be formatted representing the 6 bytes in hexadecimal separated by a colon character.
e.g. "01:23:45:ab:cd:ef"
The alphabetical characters can appear lowercase or uppercase.
@return A VZMACAddress or nil if the string is not formatted correctly.
*/
void *newVZMACAddress(const char *macAddress)
{
VZMACAddress *ret;
@autoreleasepool {
NSString *str = [NSString stringWithUTF8String:macAddress];
ret = [[VZMACAddress alloc] initWithString:str];
}
return ret;
}
/*!
@abstract Create a valid, random, unicast, locally administered address.
@discussion The generated address is not guaranteed to be unique.
*/
void *newRandomLocallyAdministeredVZMACAddress()
{
return [VZMACAddress randomLocallyAdministeredAddress];
}
/*!
@abstract Sets the media access control address of the device.
*/
void setNetworkDevicesVZMACAddress(void *config, void *macAddress)
{
[(VZNetworkDeviceConfiguration *)config setMACAddress:[(VZMACAddress *)macAddress copy]];
}
/*!
@abstract The address represented as a string.
@discussion
The 6 bytes are represented in hexadecimal form, separated by a colon character.
Alphabetical characters are lowercase.
The address is compatible with the parameter of -[VZMACAddress initWithString:].
*/
const char *getVZMACAddressString(void *macAddress)
{
return [[(VZMACAddress *)macAddress string] UTF8String];
}
/*!
@abstract Request that the guest turns itself off.
@param error If not nil, assigned with the error if the request failed.
@return YES if the request was made successfully.
*/
bool requestStopVirtualMachine(void *machine, void *queue, void **error)
{
__block BOOL ret;
dispatch_sync((dispatch_queue_t)queue, ^{
ret = [(VZVirtualMachine *)machine requestStopWithError:(NSError * _Nullable *_Nullable)error];
});
return (bool)ret;
}
void *makeDispatchQueue(const char *label)
{
//dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0);
dispatch_queue_t queue = dispatch_queue_create(label, DISPATCH_QUEUE_SERIAL);
//dispatch_retain(queue);
return queue;
}
typedef void (^handler_t)(NSError *);
handler_t generateHandler(const char *vmid, void handler(void *, char *))
{
handler_t ret;
@autoreleasepool {
NSString *str = [NSString stringWithUTF8String:vmid];
ret = Block_copy(^(NSError *err){
handler(err, copyCString(str));
});
}
return ret;
}
void startWithCompletionHandler(void *machine, void *queue, const char *vmid)
{
handler_t handler = generateHandler(vmid, startHandler);
dispatch_sync((dispatch_queue_t)queue, ^{
[(VZVirtualMachine *)machine startWithCompletionHandler:handler];
});
Block_release(handler);
}
void pauseWithCompletionHandler(void *machine, void *queue, const char *vmid)
{
handler_t handler = generateHandler(vmid, pauseHandler);
dispatch_sync((dispatch_queue_t)queue, ^{
[(VZVirtualMachine *)machine pauseWithCompletionHandler:handler];
});
Block_release(handler);
}
void resumeWithCompletionHandler(void *machine, void *queue, const char *vmid)
{
handler_t handler = generateHandler(vmid, pauseHandler);
dispatch_sync((dispatch_queue_t)queue, ^{
[(VZVirtualMachine *)machine resumeWithCompletionHandler:handler];
});
Block_release(handler);
}
// TODO(codehex): use KVO
bool vmCanStart(void *machine, void *queue)
{
__block BOOL result;
dispatch_sync((dispatch_queue_t)queue, ^{
result = ((VZVirtualMachine *)machine).canStart;
});
return (bool)result;
}
bool vmCanPause(void *machine, void *queue)
{
__block BOOL result;
dispatch_sync((dispatch_queue_t)queue, ^{
result = ((VZVirtualMachine *)machine).canPause;
});
return (bool)result;
}
bool vmCanResume(void *machine, void *queue)
{
__block BOOL result;
dispatch_sync((dispatch_queue_t)queue, ^{
result = ((VZVirtualMachine *)machine).canResume;
});
return (bool)result;
}
bool vmCanRequestStop(void *machine, void *queue)
{
__block BOOL result;
dispatch_sync((dispatch_queue_t)queue, ^{
result = ((VZVirtualMachine *)machine).canRequestStop;
});
return (bool)result;
}
// --- TODO end