-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMachineMethods.cs
More file actions
1048 lines (944 loc) · 45.7 KB
/
MachineMethods.cs
File metadata and controls
1048 lines (944 loc) · 45.7 KB
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Linq;
using System.Management;
using System.Security;
/*
* The default WMI method class includes:
* 1. MachineMethods.cs - A base class for WMI operations on local and remote machines
*/
namespace TheFlightSims.HyperVDDA.WMIProperties
{
/*
* Machine Method class
* It is used to connect and perform WMI tasks to local and remote machines through WMI
*
* Critial Note: There are two states of the PCI device:
* 1. PCI device in root\cimv2. These PCI devices are unmounted and still under control of host OS
* 2. PCI device in root\virtualization\V2. These PCI devices are MOUNTED into PCIP and under the control of hypervisor
*
* Security Note:
* 1. When connecting to remote machines, ensure that the credentials are handled securely.
* 2. Use SecureString for passwords to enhance security.
* 3. The application should run with appropriate permissions to access WMI on the target machines.
* For example, locally it may require administrative privileges and do not run with external credentials.
*/
public class MachineMethods
{
////////////////////////////////////////////////////////////////
/// Global Properties and Constructors Region
/// This region contains global properties and constructors
/// for the MachineMethods class.
////////////////////////////////////////////////////////////////
/*
* Global properties
* userCredential: A tuple to store computer name, username, and password
* credential: A ConnectionOptions object to store WMI connection options
* scope: A ManagementScope object to define the WMI scope
* searcher: A ManagementObjectSearcher object to perform WMI queries
* scopePath: A string to store the WMI scope path
* isLocal: A boolean to indicate whether the connection is local or remote
*/
protected (string computerName, string username, SecureString password) userCredential;
protected ConnectionOptions credential;
protected ManagementScope scope;
protected ManagementObjectSearcher searcher;
protected string scopePath;
protected bool isLocal;
/*
* Constructor for local connection
* Note: Local connections do not require username and password.
* It uses current user context.
*/
public MachineMethods()
{
userCredential.computerName = "localhost";
isLocal = true;
credential = new ConnectionOptions
{
Impersonation = ImpersonationLevel.Impersonate,
Authentication = AuthenticationLevel.PacketPrivacy,
EnablePrivileges = true
};
}
/*
* Constructor for remote connection
* Note: Remote connections require username and password.
*/
public MachineMethods((string computerName, string username, SecureString password) userCredential)
{
this.userCredential.computerName = userCredential.computerName;
this.userCredential.username = userCredential.username;
this.userCredential.password = userCredential.password;
isLocal = false;
credential = new ConnectionOptions
{
Username = userCredential.username,
SecurePassword = userCredential.password,
Impersonation = ImpersonationLevel.Impersonate,
Authentication = AuthenticationLevel.PacketPrivacy,
EnablePrivileges = true
};
}
////////////////////////////////////////////////////////////////
/// Connect to a name space region
/// This region contains methods to connect to a WMI name space.
/// For example, "root\\cimv2".
////////////////////////////////////////////////////////////////
public void Connect(string nameSpace)
{
scopePath = $"\\\\{userCredential.computerName}\\{nameSpace}";
scope = (isLocal) ? new ManagementScope(scopePath) : new ManagementScope(scopePath, credential);
scope.Connect();
}
////////////////////////////////////////////////////////////////
/// Get methods region (connection setting, not the WMI data)
/// Various get methods to retrieve information about the machine
/// For example, computer name, current user name, etc.
////////////////////////////////////////////////////////////////
/*
* Get methods for computer name
*/
public string GetComputerName()
{
return userCredential.computerName;
}
/*
* Get methods for current user name
*/
public string GetUsername()
{
return userCredential.username;
}
////////////////////////////////////////////////////////////////
/// Get methods region (for WMI data)
/// This region contains methods to retrieve WMI objects from the connected scope.
////////////////////////////////////////////////////////////////
public ManagementObjectCollection GetObjects(string className, string fields)
{
ObjectQuery query = new ObjectQuery($"SELECT {fields} FROM {className}");
searcher = new ManagementObjectSearcher(scope, query);
return (searcher?.Get());
}
////////////////////////////////////////////////////////////////
/// Set methods region (for WMI object)
/// This region contains methods to modify WMI objects in the connected scope.
////////////////////////////////////////////////////////////////
/*
* Change Guest Cache Type Method
* This method modifies the GuestControlledCacheTypes property of a virtual machine's settings.
*
* How does it work?
* 1. It connects to the Hyper-V WMI namespace (root\virtualization\v2)
* 2. From variable vmName, get the VM Management Object. Note: Check if the VM exists
* after assignment
* 3. From the virtualization service, it invokes the ModifySystemSettings method to change the
* GuestControlledCacheTypes property. See in the references for more details.
* 4. From the out parameter, check the return value and compare it into Success or Failure state
*
* Note:
* - For each value of the get ManagementObject, it is required to check for null value
* - It is required to get the out parameter to check the status of the operation.
* - No matter how the service operate with the parameter, the virtual machine instance must
* be disposed
*
* References:
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-virtualsystemsettingdata
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-virtualsystemmanagementservice
*
* Limitations:
* - The method assumes that the virtual machine with the specified name exists.
* - Windows 8/Windows Server 2012 or later is required for this method to work.
* - WMI Filtering may affect the operation.
*/
public void ChangeGuestCacheType(string vmName, bool isEnableGuestControlCache)
{
// 1. Connect to the namespace
Connect("root\\virtualization\\v2");
// Set the property
uint outParams = 32768;
ManagementObject vm = null;
// 2. Get the target ManagementObject from the vmName
using (ManagementObjectCollection vmSettingDataList = GetObjects("Msvm_VirtualSystemSettingData", "*"))
{
foreach (ManagementObject obj in vmSettingDataList.Cast<ManagementObject>())
{
/*
* In this loop, it first check if the target VM setting is the actual setting, and not the definition
* After that, if the ElementName of the ManagementObject matches with the vmName param, set the target
* object to that ManagementObject
*
* Note: Always dispose on null values
*/
string objCaption = obj["Caption"]?.ToString();
string objInstanceId = obj["InstanceID"]?.ToString();
if (objCaption == null || objInstanceId == null)
{
obj.Dispose();
continue;
}
if (objCaption.Equals("Virtual Machine Settings") && !objInstanceId.Contains("Microsoft:Definition"))
{
string hostName = obj["ElementName"]?.ToString();
if (hostName == null)
{
obj.Dispose();
continue;
}
if (hostName.Equals(vmName))
{
vm = obj;
break;
}
}
obj.Dispose();
}
}
if (vm == null)
{
throw new ManagementException("ChangeGuestCacheType: Unable to get the virtual machine setting");
}
// 3. Modify for the value
try
{
vm["GuestControlledCacheTypes"] = isEnableGuestControlCache;
// Get the service object
ManagementObject srv = new ManagementClass(
scope,
new ManagementPath("Msvm_VirtualSystemManagementService"),
null
).GetInstances().Cast<ManagementObject>().FirstOrDefault();
// If the service is null, it is crashed
if (srv == null)
{
throw new ManagementException(
"ChangeGuestCacheType: Assignment service is either not running or crashed"
);
}
// Casting and get the return value of the Msvm_VirtualSystemManagementService
outParams = (uint)srv.InvokeMethod("ModifySystemSettings", new object[]
{
vm.GetText(TextFormat.WmiDtd20)
});
}
finally
{
vm?.Dispose();
}
// 4. Check for the out param. Returns 0 in case success, other numbers as failure, and failback as unknown
switch (outParams)
{
case 0:
break;
case 1:
throw new ManagementException("ChangeGuestCacheType: Not Supported");
case 2:
throw new ManagementException("ChangeGuestCacheType: Failed");
case 3:
throw new ManagementException("ChangeGuestCacheType: Timed out");
case 4:
throw new ManagementException("ChangeGuestCacheType: Invalid Parameter");
case 5:
throw new ManagementException("ChangeGuestCacheType: Invalid State");
case 6:
throw new ManagementException("ChangeGuestCacheType: Incompatible Parameters");
case 4096:
throw new ManagementException("ChangeGuestCacheType: Method Parameters Checked - Job Started");
default:
throw new ManagementException($"ChangeGuestCacheType: Unknown error ({outParams})");
}
}
/*
* Change Memory Allocation
* This method changes the low memory allocation and high memory allocation
*
* How does it work?
* 1. It connects to the Hyper-V WMI namespace (root\virtualization\v2)
* 2. From variable vmName, get the VM Management Object. Note: Check if the VM exists
* after assignment
* 3. From the virtualization service, it invokes the ModifySystemSettings method to change the
* LowMmioGapSize and HighMmioGapSize properties. See in the references for more details.
* 4. From the out parameter, check the return value and compare it into Success or Failure state
*
* Note:
* - For each value of the get ManagementObject, it is required to check for null value
* - It is required to get the out parameter to check the status of the operation.
* - No matter how the service operate with the parameter, the virtual machine instance must
* be disposed
*
* References:
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-virtualsystemsettingdata
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-virtualsystemmanagementservice
*
* Limitations:
* - The method assumes that the virtual machine with the specified name exists.
* - Windows 8/Windows Server 2012 or later is required for this method to work.
* - WMI Filtering may affect the operation.
*/
public void ChangeMemAllocate(string vmName, ulong lowMem, ulong highMem)
{
// 1. Connect to the namespace
Connect("root\\virtualization\\v2");
// Set the property
uint outParams = 32768;
ManagementObject vm = null;
// 2. Get the target ManagementObject from the vmName
using (ManagementObjectCollection vmSettingDataList = GetObjects("Msvm_VirtualSystemSettingData", "*"))
{
foreach (ManagementObject obj in vmSettingDataList.Cast<ManagementObject>())
{
/*
* In this loop, it first check if the target VM setting is the actual setting, and not the definition
* After that, if the ElementName of the ManagementObject matches with the vmName param, set the target
* object to that ManagementObject
*
* Note: Always dispose on null values
*/
string objCaption = obj["Caption"]?.ToString();
string objInstanceId = obj["InstanceID"]?.ToString();
if (objCaption == null || objInstanceId == null)
{
obj.Dispose();
continue;
}
if (objCaption.Equals("Virtual Machine Settings") && !objInstanceId.Contains("Microsoft:Definition"))
{
string hostName = obj["ElementName"]?.ToString();
if (hostName == null)
{
obj.Dispose();
continue;
}
if (hostName.Equals(vmName))
{
vm = obj;
}
}
obj.Dispose();
}
}
if (vm == null)
{
throw new ManagementException("ChangeMemAllocate: Unable to get the virtual machine setting");
}
// 3. Modify for the value
try
{
vm["LowMmioGapSize"] = lowMem;
vm["HighMmioGapSize"] = highMem;
// Get the management service
ManagementObject srv = new ManagementClass(
scope,
new ManagementPath("Msvm_VirtualSystemManagementService"), null
).GetInstances().Cast<ManagementObject>().FirstOrDefault();
// If null, the service is crashed
if (srv == null)
{
throw new ManagementException("ChangeMemAllocate: Assignment service is either not running or crashed");
}
// Casting and get the return value of the Msvm_VirtualSystemManagementService
outParams = (uint)srv.InvokeMethod("ModifySystemSettings", new object[]
{
vm.GetText(TextFormat.WmiDtd20)
});
}
finally
{
vm?.Dispose();
}
// 4. Check for the out param. Returns 0 in case success, other numbers as failure, and failback as unknown
switch (outParams)
{
case 0:
break;
case 1:
throw new ManagementException("ChangeMemAllocate: Not Supported");
case 2:
throw new ManagementException("ChangeMemAllocate: Failed");
case 3:
throw new ManagementException("ChangeMemAllocate: Timed out");
case 4:
throw new ManagementException("ChangeMemAllocate: Invalid Parameter");
case 5:
throw new ManagementException("ChangeMemAllocate: Invalid State");
case 6:
throw new ManagementException("ChangeMemAllocate: Incompatible Parameters");
case 4096:
throw new ManagementException("ChangeMemAllocate: Method Parameters Checked - Job Started");
default:
throw new ManagementException($"ChangeMemAllocate: Unknown error ({outParams})");
}
}
/*
* Change the PCI device behaviour
* This method enable/disable the target PCI device on the host machine
*
* How does it work?
* 1. It connects to the CIMv2 namespace (root\cimv2)
* 2. From the provided the deviceID (of PCI, not PCIP), try to invoke method "Disable" or
* "Enable", depends on the input parameter isDisable. Note: the device will report
* IndexOutOfRangeException, just ignore it.
*
* Note:
* - For each value of the get ManagementObject, it is required to check for null value
* - No matter how the service operate with the parameter, any WMI object must be disposed
*
* References:
* - https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-pnpentity#methods
*
* Limitations:
* - Windows Vista/Windows Server 2008 or later is required for this method to work.
* - WMI Filtering may affect the operation.
*/
public void ChangePnpDeviceBehaviour(string deviceID, bool isDisable)
{
// 1. Connect to the root name space
Connect("root\\cimv2");
// 2. From the device list, try to find the device that matches the deviceID
using (ManagementObjectCollection pnpEntityList = GetObjects("Win32_PnPEntity", "DeviceId"))
{
foreach (ManagementObject deviceSearcher in pnpEntityList.Cast<ManagementObject>())
{
// Get the device ID
string devSearcherId = deviceSearcher["DeviceId"]?.ToString();
// Dispose the device when find null
if (devSearcherId == null)
{
deviceSearcher.Dispose();
continue;
}
// Check if the target device is
if (devSearcherId.Equals(deviceID))
{
try
{
_ = (uint)deviceSearcher.InvokeMethod(
(isDisable) ? "Disable" : "Enable",
new object[] { }
);
}
catch (IndexOutOfRangeException) { }
finally
{
deviceSearcher.Dispose();
}
break;
}
deviceSearcher.Dispose();
}
}
}
/*
* Mount PnP Device into PCIP
* This method mounts a device from PCI (root\cimv2) into PCIP (root\virtualization\v2)
*
* How does it work?
* 1. It connects to the Hyper-V namespace (root\virtualization\v2)
* 2. From the device ID, try to mount the device into the PCIP using the service Msvm_AssignableDeviceService
* 3. From the out parameter, check the return value and compare it into Success or Failure state
*
* Note:
* - For each value of the get ManagementObject, it is required to check for null value
* - No matter how the service operate with the parameter, any WMI object must be disposed
*
* References:
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-assignabledevicedismountsettingdata
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-assignabledeviceservice
*
* Limitations:
* - Windows 10 1703/Windows Server 2016 or later is required for this method to work.
* - WMI Filtering may affect the operation.
*/
public void MountPnPDeviceToPcip(string deviceID)
{
// 1. Connect to the Hyper-V WMI namespace
Connect("root\\virtualization\\v2");
// Set for the out param
uint outObj = 32768;
// Create new object as the input param from the Msvm_AssignableDeviceDismountSettingData for the WMI method
ManagementObject setting = new ManagementClass(
scope,
new ManagementPath("Msvm_AssignableDeviceDismountSettingData"),
null
)?.CreateInstance();
if (setting == null)
{
throw new ManagementException("MountPnPDeviceToPcip: Unable to get the setting class");
}
// Get the Assignable Device Service WMI object
ManagementObject srv = new ManagementClass(scope, new ManagementPath("Msvm_AssignableDeviceService"), null).GetInstances().Cast<ManagementObject>().FirstOrDefault();
if (srv == null)
{
throw new ManagementException("MountPnPDeviceToPcip: Assignment service is either not running or crashed");
}
// 2. Mount the device into the PCIP.
try
{
setting["DeviceInstancePath"] = deviceID;
setting["DeviceLocationPath"] = string.Empty;
setting["RequireAcsSupport"] = false;
setting["RequireDeviceMitigations"] = false;
// Call for service method
outObj = (uint)srv.InvokeMethod(
"DismountAssignableDevice",
new object[] {
setting.GetText(TextFormat.WmiDtd20)
});
}
finally
{
srv?.Dispose();
setting?.Dispose();
}
// 3. Check for the out param. Returns 0 in case success, other numbers as failure, and failback as unknown
switch (outObj)
{
case 0:
break;
case 4096:
throw new ManagementException("MountPnPDeviceToPcip: Method Parameters Checked - Job Started");
case 32768:
throw new ManagementException("MountPnPDeviceToPcip: Access Denied");
case 32770:
throw new ManagementException("MountPnPDeviceToPcip: Not Supported");
case 32771:
throw new ManagementException("MountPnPDeviceToPcip: Status is unknown");
case 32772:
throw new ManagementException("MountPnPDeviceToPcip: Timeout");
case 32773:
throw new ManagementException("MountPnPDeviceToPcip: Invalid parameter");
case 32774:
throw new ManagementException("MountPnPDeviceToPcip: System is in use");
case 32775:
throw new ManagementException("MountPnPDeviceToPcip: Invalid state for this operation");
case 32776:
throw new ManagementException("MountPnPDeviceToPcip: Incorrect data type");
case 32777:
throw new ManagementException("MountPnPDeviceToPcip: System is not available");
case 32778:
throw new ManagementException("MountPnPDeviceToPcip: Out of memory");
case 32779:
throw new ManagementException("MountPnPDeviceToPcip: File not found");
default:
throw new ManagementException($"Unknow error in method MountPnPDeviceToPcip ({outObj})");
}
}
/*
* Dismount PnP Device from PCIP
* This method dismounts a device from PCIP (root\virtualization\v2)
*
* How does it work?
* 1. It connects to the Hyper-V namespace (root\virtualization\v2)
* 2. From devicePath variable, get the location path
* 3. From the devicePath and PCIP device location path, try to dismount the
* device using Msvm_AssignableDeviceService and get the out param
* 4. From the out parameter, check the return value and compare it into Success or
* Failure state
*
* Note:
* - For each value of the get ManagementObject, it is required to check for null value
* - No matter how the service operate with the parameter, any WMI object must be disposed
*
* References:
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-assignabledeviceservice
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-pciexpress
*
* Limitations:
* - Windows 10 1703/Windows Server 2016 or later is required for this method to work.
* - WMI Filtering may affect the operation.
*/
public void DismountPnPDeviceFromPcip(string devicePath)
{
// 1. Connect to Hyper-V WMI and
Connect("root\\virtualization\\v2");
// Set the out param and device location
uint outObj = 32768;
string deviceLocation = string.Empty;
// 2. Get the device location path
using (ManagementObjectCollection pciExpressList = GetObjects("Msvm_PciExpress", "*"))
{
foreach (ManagementObject obj in pciExpressList.Cast<ManagementObject>())
{
string devInstancePath = obj["DeviceInstancePath"]?.ToString();
if (devInstancePath == null)
{
obj.Dispose();
continue;
}
if (devInstancePath.Equals(devicePath))
{
string devLoc = obj["LocationPath"]?.ToString();
if (devLoc != null)
{
deviceLocation = devLoc;
obj.Dispose();
break;
}
}
obj.Dispose();
}
}
// In case not found, throw exception
if (deviceLocation == string.Empty)
{
throw new ManagementException("DismountPnPDeviceFromPcip: Unable to get the device location");
}
// 3. Call the service
ManagementObject srv = new ManagementClass(
scope,
new ManagementPath("Msvm_AssignableDeviceService"),
null
).GetInstances().Cast<ManagementObject>().FirstOrDefault();
if (srv == null)
{
throw new ManagementException(
"DismountPnPDeviceFromPcip: Assignment service is either not running or crashed"
);
}
try
{
outObj = (uint)srv.InvokeMethod("MountAssignableDevice", new object[]
{
devicePath,
deviceLocation
});
}
finally
{
srv?.Dispose();
}
// 4. Check for the out param. Returns 0 in case success, other numbers as failure, and failback as unknown
switch (outObj)
{
case 0:
break;
case 4096:
throw new ManagementException("DismountPnPDeviceFromPcip: Method Parameters Checked - Job Started");
case 32768:
throw new ManagementException("DismountPnPDeviceFromPcip: Failed");
case 32769:
throw new ManagementException("DismountPnPDeviceFromPcip: Access Denied");
case 32770:
throw new ManagementException("DismountPnPDeviceFromPcip: Not Supported");
case 32771:
throw new ManagementException("DismountPnPDeviceFromPcip: Status is unknown");
case 32772:
throw new ManagementException("DismountPnPDeviceFromPcip: Timeout");
case 32773:
throw new ManagementException("DismountPnPDeviceFromPcip: Invalid parameter");
case 32774:
throw new ManagementException("DismountPnPDeviceFromPcip: System is in use");
case 32775:
throw new ManagementException("DismountPnPDeviceFromPcip: Invalid state for this operation");
case 32776:
throw new ManagementException("DismountPnPDeviceFromPcip: Incorrect data type");
case 32777:
throw new ManagementException("DismountPnPDeviceFromPcip: System is not available");
case 32778:
throw new ManagementException("DismountPnPDeviceFromPcip: Out of memory");
case 32779:
throw new ManagementException("DismountPnPDeviceFromPcip: File not found");
default:
throw new ManagementException($"Unknow error in method DismountPnPDeviceFromPcip ({outObj})");
}
}
/*
* Mount PCIP device into the VM (Must be invoked when it is still in PCI)
*
* How does it work?
* 1. It connects to the Hyper-V namespace (root\virtualization\v2)
* 2. Then, creates a new instance of PCI Express Setting Data from Msvm_PciExpressSettingData
* 3. From the PCIP device list, find if there is any device matches the PCI that has mounted
* 4. Get the VM setting to mount new device into the VM
* 5. Now try to mount the VM using vmCurrentSetting and setting
*
* Note:
* - For each value of the get ManagementObject, it is required to check for null value
* - No matter how the service operate with the parameter, any WMI object must be disposed
* - The function must be invoked from the PCI, that means the when the device is still with
*
* References:
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-pciexpresssettingdata
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-virtualsystemmanagementservice
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-pciexpress
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-virtualsystemsettingdata
* - https://learn.microsoft.com/en-us/previous-versions/cc136911(v=vs.85)
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/cim-resourceallocationsettingdata
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-pciexpresssettingdata
*
* Limitations:
* - Windows 10 1703/Windows Server 2016 or later is required for this method to work.
* - Changing host device name can cause counter effects to the Hyper-V system
* - WMI Filtering may affect the operation.
*/
public void MountIntoVM(string vmName, string deviceId)
{
// 1. Connect to Hyper-V WMI
Connect("root\\virtualization\\v2");
uint outParams = 32768;
string hostRes = string.Empty, vmRes = string.Empty;
ManagementObject vmCurrentSetting = null;
// 2. Create new instance for the vm setting
ManagementObject setting = new ManagementClass(
scope,
new ManagementPath("Msvm_PciExpressSettingData"),
null
).CreateInstance();
// Get the service instance
ManagementObject srv = new ManagementClass(
scope,
new ManagementPath("Msvm_VirtualSystemManagementService"),
null
).GetInstances().Cast<ManagementObject>().FirstOrDefault();
if (srv == null)
{
throw new ManagementException(
"MountIntoVM: Assignment service is either not running or crashed"
);
}
// 3. From the PCIP device list, find if there is any device matches the PCI that has mounted
// From that, set the host resources (hostRes)
using (ManagementObjectCollection pciExpressList = GetObjects("Msvm_PciExpress", "*"))
{
foreach (ManagementObject device in pciExpressList.Cast<ManagementObject>())
{
// Get the device instance path (starts with "PCIP\")
string devInstancePath = device["DeviceInstancePath"]?.ToString();
if (devInstancePath == null)
{
device.Dispose();
continue;
}
// If matches, make sure the
if (devInstancePath.Contains(deviceId.Replace("PCI\\", "PCIP\\")))
{
// Get the host name and device PnP
string hostName = device["SystemName"]?.ToString();
string deviceIdPnP = device["DeviceId"]?.ToString();
// Set the hostRes
/*
* Note it has a structure of this (in a single line, just seperate into lines so can see it easily):
* \\{hostName}\root\virtualization\v2:
* Msvm_PciExpress.CreationClassName="Msvm_PciExpress",
* DeviceID="{deviceIdPnP}",
* SystemCreationClassName="Msvm_ComputerSystem",
* SystemName="{hostName}"
*
* where "hostName" is the device name, and "deviceIdPnP" is the device ID in GUIDv4
*/
if (hostName != null || deviceIdPnP != null)
{
deviceIdPnP = deviceIdPnP.Replace("\\", "\\\\");
hostRes = $"\\\\{hostName}\\root\\virtualization\\v2:Msvm_PciExpress.CreationClassName=\"Msvm_PciExpress\",DeviceID=\"{deviceIdPnP}\",SystemCreationClassName=\"Msvm_ComputerSystem\",SystemName=\"{hostName}\"";
device.Dispose();
break;
}
device.Dispose();
}
}
}
// If the hostRes is empty, throw new exception
if (hostRes == string.Empty)
{
throw new ManagementException("MountIntoVM: Unable to get HostResource");
}
// 4. Get the VM setting to mount new device into the VM
using (ManagementObjectCollection vmSettingDataList = GetObjects("Msvm_VirtualSystemSettingData", "*"))
{
foreach (ManagementObject vmSetting in vmSettingDataList.Cast<ManagementObject>())
{
// Get the Caption and Instance ID
string vmCaption = vmSetting["Caption"]?.ToString();
string vmInstanceID = vmSetting["InstanceID"]?.ToString();
// Check if the vmCaption is null
if (vmCaption == null || vmInstanceID == null)
{
vmSetting.Dispose();
continue;
}
if (vmCaption.Equals("Virtual Machine Settings") && !vmInstanceID.Contains("Microsoft:Definition"))
{
// Get the VM name
string hostName = vmSetting["ElementName"]?.ToString();
if (hostName == null)
{
vmSetting.Dispose();
continue;
}
// If the VM name matches with the name wants to set the target, set the vmCurrentSetting and vmRes to it
if (hostName.Equals(vmName))
{
vmRes = $"{vmSetting["InstanceID"]}\\{Guid.NewGuid().ToString().ToUpper()}";
vmCurrentSetting = vmSetting;
vmSetting.Dispose();
break;
}
}
}
}
if (vmRes == string.Empty)
{
throw new ManagementException("MountIntoVM: Unable to generate InstanceID");
}
if (vmCurrentSetting == null)
{
throw new ManagementException("MountIntoVM: Unable to get setting for VM");
}
// 5. Now try to mount the VM using vmCurrentSetting and setting
try
{
// CIM_SettingData
setting["Caption"] = "PCI Express Port";
setting["Description"] = "Microsoft Virtual PCI Express Port Setting Data";
setting["InstanceID"] = vmRes;
setting["ElementName"] = "PCI Express Port";
// CIM_ResourceAllocationSettingData
setting["ResourceType"] = (ushort)32769;
setting["OtherResourceType"] = null;
setting["ResourceSubType"] = "Microsoft:Hyper-V:Virtual Pci Express Port";
setting["PoolID"] = string.Empty;
setting["ConsumerVisibility"] = (ushort)3;
setting["HostResource"] = new string[] { hostRes };
setting["AllocationUnits"] = "count";
setting["VirtualQuantity"] = (ulong)1;
setting["Reservation"] = (ulong)1;
setting["Limit"] = (ulong)1;
setting["Weight"] = (uint)0;
setting["AutomaticAllocation"] = true;
setting["AutomaticDeallocation"] = true;
setting["Parent"] = null;
setting["Connection"] = null;
setting["Address"] = string.Empty;
setting["MappingBehavior"] = null;
setting["AddressOnParent"] = string.Empty;
setting["VirtualQuantityUnits"] = "count";
// Msvm_PciExpressSettingData
setting["VirtualFunctions"] = new ushort[] { 0 };
setting["VirtualSystemIdentifiers"] = new string[] { "{" + Guid.NewGuid() + "}" };
// Call for the service
outParams = (uint)srv.InvokeMethod("AddResourceSettings", new object[]
{
vmCurrentSetting,
new string[] {
setting.GetText(TextFormat.WmiDtd20)
}
});
}
finally
{
setting?.Dispose();
srv?.Dispose();
}
// 6. Check for the out param. Returns 0 in case success, other numbers as failure, and failback as unknown
switch (outParams)
{
case 0:
break;
case 1:
throw new ManagementException("MountIntoVM: Not Supported");
case 2:
throw new ManagementException("MountIntoVM: Failed");
case 3:
throw new ManagementException("MountIntoVM: Timed out");
case 4:
throw new ManagementException("MountIntoVM: Invalid Parameter");
case 4096:
throw new ManagementException("MountIntoVM: Method Parameters Checked - Job Started");
case 4097:
throw new ManagementException("MountIntoVM: The function may not be called or is reserved for vendor");
default:
throw new ManagementException($"MountIntoVM: Unknown error ({outParams})");
}
}
/*
* Dismount PCIP device into the VM (Must be invoked when it is still in PCIP)
*
* How does it work?
* 1. It connects to the Hyper-V namespace (root\virtualization\v2)
* 2. From the provided variable deviceId, try to get the PCIP device that match deviceID
* in Msvm_PciExpressSettingData
* 3. Call the service Msvm_PciExpressSettingData to dismount the device from the VM
* 4. From the out parameter, check the return value and compare it into Success or
* Failure state
*
* Note:
* - For each value of the get ManagementObject, it is required to check for null value
* - No matter how the service operate with the parameter, any WMI object must be disposed
* - The function must be invoked from the PCI, that means the when the device is still with
*
* References:
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-virtualsystemmanagementservice
* - https://learn.microsoft.com/en-us/windows/win32/hyperv_v2/msvm-pciexpresssettingdata
*
* Limitations:
* - Windows 10 1703/Windows Server 2016 or later is required for this method to work.
* - WMI Filtering may affect the operation.
*/
public void DismountFromVM(string deviceId)
{
// 1. Connect to Hyper-V WMI
Connect("root\\virtualization\\v2");
uint outParams = 32768;
ManagementObject[] deviceObj = new ManagementObject[1];
// Get the service
ManagementObject srv = new ManagementClass(scope, new ManagementPath("Msvm_VirtualSystemManagementService"), null).GetInstances().Cast<ManagementObject>().FirstOrDefault();
if (srv == null)
{
throw new ManagementException("DismountFromVM: Assignment service is either not running or crashed");
}
// 2. Get the device instance from deviceId
using (ManagementObjectCollection pciExpressSettingList = GetObjects("Msvm_PciExpressSettingData", "*"))
{
foreach (ManagementObject obj in pciExpressSettingList.Cast<ManagementObject>())
{
string objInstanceId = obj["InstanceID"]?.ToString();
if (objInstanceId != null)
{
if (objInstanceId.Equals(deviceId))
{
deviceObj[0] = obj;
obj.Dispose();
break;