forked from xenserver/xenadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOVF.cs
5588 lines (5140 loc) · 242 KB
/
OVF.cs
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
/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Management;
using System.Reflection;
using System.Resources;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
using XenOvf.Definitions;
using XenOvf.Utilities;
using XenCenterLib.Archive;
using XenCenterLib.Compression;
namespace XenOvf
{
public partial class OVF
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
/// <summary>
/// Event Registration of changes in Ovf state.
/// </summary>
public static event Action<OvfEventArgs> Changed;
/// <summary>
/// Protected method call for eventing.
/// </summary>
/// <param name="e">OvfEventArgs</param>
private static void OnChanged(OvfEventArgs e)
{
if (Changed != null)
{
Changed(e);
}
}
private const long KB = 1024;
private const long MB = (KB * 1024);
private const long GB = (MB * 1024);
private static bool _promptForEula = true;
internal static ResourceManager _rm = new ResourceManager("XenOvf.Messages", Assembly.GetExecutingAssembly());
internal static ResourceManager _ovfrm = new ResourceManager("XenOvf.Content", Assembly.GetExecutingAssembly());
#region PUBLIC
public OVF()
{
UnLoad();
}
#region PROPERTIES
public static object AlgorithmMap(string key)
{
return Properties.Settings.Default[key];
}
public bool PromptForEula
{
get { return _promptForEula; }
set { _promptForEula = value; }
}
#endregion
#region LOAD OVF
/// <summary>
/// Load an OVF XML File into OVF class context
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static EnvelopeType Load(string filename)
{
return Tools.LoadOvfXml(filename);
}
#endregion
#region SAVE OVF
public static void SaveAs(EnvelopeType ovfEnv, string filename)
{
SaveAs(ToXml(ovfEnv), filename);
}
public static void SaveAs(string OvfXml, string filename)
{
log.DebugFormat("OVF.SaveAs: {0}", filename);
if (OvfXml == null)
{
log.Error("SaveAs: cannot save NULL string OvfXml");
throw new ArgumentNullException();
}
if (filename == null)
{
log.Error("SaveAs: cannot save OvfXml. Filename was NULL");
throw new ArgumentNullException();
}
string oldfile = string.Format(@"{0}_ovf.old", Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename)));
try
{
if (File.Exists(filename))
{
if (File.Exists(oldfile))
{
File.Delete(oldfile);
}
File.Move(filename, oldfile);
}
}
catch (Exception ex)
{
log.Error("File handling error. ", ex);
}
using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
using (var sw = new StreamWriter(fs))
{
sw.Write(OvfXml);
sw.Flush();
}
if (File.Exists(oldfile))
File.Delete(oldfile);
log.Debug("OVF.SaveAs completed");
}
#endregion
#region UNLOAD OVF
/// <summary>
/// Clears memory and resets to defaults.
/// </summary>
public void UnLoad()
{
mappings.Clear();
Win32_ComputerSystem = null;
Win32_Processor.Clear();
Win32_CDROMDrive.Clear();
Win32_DiskDrive.Clear();
Win32_NetworkAdapter.Clear();
Win32_IDEController.Clear();
Win32_SCSIController.Clear();
Win32_IDEControllerDevice.Clear();
Win32_SCSIControllerDevice.Clear();
Win32_DiskPartition.Clear();
Win32_DiskDriveToDiskPartition.Clear();
}
#endregion
#region CHECK FOR FILE(S) METHODs (and HAS methods)
public static bool HasDeploymentOptions(EnvelopeType ovfObj)
{
DeploymentOptionSection_Type[] dos = FindSections<DeploymentOptionSection_Type>(ovfObj);
if (dos != null && dos.Length > 0)
{
return true;
}
return false;
}
public static bool HasEula(EnvelopeType ovfObj)
{
EulaSection_Type[] eulas = FindSections<EulaSection_Type>(ovfObj);
if (eulas != null && eulas.Length > 0)
{
return true;
}
return false;
}
#endregion
#region ADDs
public string AddAnnotation(EnvelopeType ovfObj, string vsId, string info, string annotation)
{
return AddAnnotation(ovfObj, vsId, Properties.Settings.Default.Language, info, annotation);
}
public string AddAnnotation(EnvelopeType ovfObj, string vsId, string lang, string info, string annotation)
{
VirtualSystem_Type vs = FindVirtualSystemById(ovfObj, vsId);
List<Section_Type> sections = new List<Section_Type>();
sections.AddRange(vs.Items);
AnnotationSection_Type annotate = new AnnotationSection_Type();
annotate.Id = Guid.NewGuid().ToString();
annotate.Info = new Msg_Type(AddToStringSection(ovfObj, lang, info), info);
annotate.Annotation = new Msg_Type(AddToStringSection(ovfObj, lang, info), info);
sections.Add(annotate);
vs.Items = sections.ToArray();
return annotate.Id;
}
public static string AddCDROM(EnvelopeType ovfObj, string vsId, string cdId, string caption, string description)
{
return AddCDROM(ovfObj, vsId, Properties.Settings.Default.Language, cdId, caption, description);
}
/// <summary>
/// Add a CD/DVD Drive
/// </summary>
/// <param name="ovfObj">EnvelopeType</param>
/// <param name="vsId">Virtual System Identifier</param>
/// <param name="cdId">InstanceID</param>
/// <param name="caption">string short description</param>
/// <param name="description">string longer description</param>
public static string AddCDROM(EnvelopeType ovfObj, string vsId, string lang, string cdId, string caption, string description)
{
RASD_Type rasd = new RASD_Type();
rasd.required = false;
rasd.AllocationUnits = new cimString(_ovfrm.GetString("RASD_16_ALLOCATIONUNITS"));
rasd.AutomaticAllocation = new cimBoolean();
rasd.AutomaticAllocation.Value = true;
rasd.ConsumerVisibility = new ConsumerVisibility();
rasd.ConsumerVisibility.Value = 3; //From MS.
rasd.Caption = new Caption(caption);
rasd.Description = new cimString(description);
rasd.ElementName = new cimString(_ovfrm.GetString("RASD_16_ELEMENTNAME"));
rasd.InstanceID = new cimString(cdId);
rasd.Limit = new cimUnsignedLong();
rasd.Limit.Value = 1; // From MS;
rasd.MappingBehavior = new MappingBehavior();
rasd.MappingBehavior.Value = 0; // From MS.
rasd.ResourceType = new ResourceType();
rasd.ResourceType.Value = 16; // DVD Drive
rasd.VirtualQuantity = new cimUnsignedLong();
rasd.VirtualQuantity.Value = 1;
rasd.Weight = new cimUnsignedInt();
rasd.Weight.Value = 0; // From MS.
AddRasdToAllVHS(ovfObj, vsId, rasd);
log.Debug("OVF.AddCDDrive completed");
return rasd.InstanceID.Value;
}
public void AddController(EnvelopeType ovfObj, string vsId, DeviceType type, string deviceId, int iteration)
{
AddController(ovfObj, vsId, Properties.Settings.Default.Language, type, deviceId, iteration);
}
/// <summary>
/// Add a controller to the mix.
/// </summary>
/// <param name="ovfObj">object of type EnvelopeType</param>
/// <param name="vsId">System Instance ID</param>
/// <param name="type">Resource Type: 5 = IDE, 6 = SCSI</param>
/// <param name="deviceId">String identifying the device to match to the controller</param>
/// <param name="iteration">which controller 0 = first, 1, 2, 3... (per type)</param>
/// <returns>InstanceID of Controller</returns>
public void AddController(EnvelopeType ovfObj, string vsId, string lang, DeviceType type, string deviceId, int iteration)
{
VirtualHardwareSection_Type[] vhsArray = FindVirtualHardwareSection(ovfObj, vsId);
foreach (VirtualHardwareSection_Type vhs in vhsArray)
{
AddControllerToVHS(vhs, lang, type, deviceId, iteration);
}
log.Debug("OVF.AddController completed");
}
/// <summary>
/// Add a controller to the mix.
/// </summary>
/// <param name="ovfObj">object of type EnvelopeType</param>
/// <param name="vsId">System Instance ID</param>
/// <param name="type">Resource Type: 5 = IDE, 6 = SCSI</param>
/// <param name="deviceId">String identifying the device to match to the controller</param>
/// <param name="iteration">which controller 0 = first, 1, 2, 3... (per type)</param>
/// <returns>InstanceID of Controller</returns>
public void AddControllerToVHS(object vhsObj, string lang, DeviceType type, string deviceId, int iteration)
{
VirtualHardwareSection_Type vhs = (VirtualHardwareSection_Type)vhsObj;
RASD_Type rasd = new RASD_Type();
string controllername = _ovfrm.GetString("RASD_CONTROLLER_UNKNOWN");
switch (type)
{
case DeviceType.IDE: { controllername = _ovfrm.GetString("RASD_CONTROLLER_IDE"); break; }
case DeviceType.SCSI: { controllername = _ovfrm.GetString("RASD_CONTROLLER_SCSI"); break; }
default: { controllername = _ovfrm.GetString("RASD_CONTROLLER_OTHER"); break; }
}
string caption = string.Format("{0} {1}", controllername, iteration);
rasd.required = false; // as default change to FALSE, if we make a connection, it'll change to true.
rasd.Address = new cimString(Convert.ToString(iteration));
rasd.AllocationUnits = new cimString("Controllers");
rasd.Caption = new Caption(caption);
rasd.ConsumerVisibility = new ConsumerVisibility();
rasd.ConsumerVisibility.Value = 3;
rasd.Description = new cimString(string.Format(_ovfrm.GetString("RASD_CONTROLLER_DESCRIPTION"), controllername));
rasd.ElementName = new cimString(string.Format(_ovfrm.GetString("RASD_CONTROLLER_ELEMENTNAME"), controllername, iteration));
rasd.InstanceID = new cimString(deviceId);
if (type == DeviceType.SCSI)
{
rasd.ResourceSubType = new cimString(_ovfrm.GetString("RASD_CONTROLLER_SCSI_SUBTYPE"));
}
rasd.Limit = new cimUnsignedLong();
rasd.Limit.Value = 2;
rasd.ResourceType = new ResourceType();
rasd.ResourceType.Value = (ushort)type;
rasd.VirtualQuantity = new cimUnsignedLong();
rasd.VirtualQuantity.Value = 1;
rasd.Weight = new cimUnsignedInt();
rasd.Weight.Value = 0;
List<RASD_Type> rasds = new List<RASD_Type>();
rasds.Add(rasd);
if (vhs.Item != null && vhs.Item.Length > 0)
{
rasds.AddRange(vhs.Item);
}
vhs.Item = rasds.ToArray();
log.Debug("OVF.AddController completed");
}
public string AddDeploymentOption(EnvelopeType ovfObj, string label, string description, bool isdefault)
{
return AddDeploymentOption(ovfObj, Properties.Settings.Default.Language, label, description, isdefault);
}
public string AddDeploymentOption(EnvelopeType env, string lang, string label, string description, bool isdefault)
{
DeploymentOptionSection_Type dos = null;
List<Section_Type> sections = new List<Section_Type>();
Section_Type[] sectionArray = null;
if (env.Sections != null)
{
sectionArray = env.Sections;
}
foreach (Section_Type sect in sectionArray)
{
if (sect is DeploymentOptionSection_Type)
{
dos = (DeploymentOptionSection_Type)sect;
}
else
{
sections.Add(sect);
}
}
if (dos == null)
{
dos = new DeploymentOptionSection_Type();
dos.Id = Guid.NewGuid().ToString();
}
DeploymentOptionSection_TypeConfiguration conf = new DeploymentOptionSection_TypeConfiguration();
conf.@default = isdefault;
conf.Description = new Msg_Type(AddToStringSection(env, lang, description), description);
conf.Label = new Msg_Type(AddToStringSection(env, lang, label), label);
conf.id = Guid.NewGuid().ToString();
List<DeploymentOptionSection_TypeConfiguration> confs = new List<DeploymentOptionSection_TypeConfiguration>();
if (dos.Configuration != null && dos.Configuration.Length > 0)
{
confs.AddRange(dos.Configuration);
}
confs.Add(conf);
dos.Configuration = confs.ToArray();
sections.Add(dos);
env.Sections = sections.ToArray();
return conf.id;
}
public void AddDeviceToController(EnvelopeType ovfObj, string vsId, string deviceInstanceId, string controllerInstanceId, string AddressOnController)
{
AddDeviceToController(ovfObj, vsId, Properties.Settings.Default.Language, deviceInstanceId, controllerInstanceId, AddressOnController);
}
/// <summary>
/// Connect a Disk (VHD) to a Controller ie: IDE or SCSI and where on controller it should exist.
/// </summary>
/// <param name="ovfObj">EnvelopeType</param>
/// <param name="vsId">string Virtual System Identifier</param>
/// <param name="deviceInstanceId">instance ID of device</param>
/// <param name="controllerInstanceId">instance ID of controller</param>
/// <param name="AddressOnController">where on controller 0...</param>
public void AddDeviceToController(EnvelopeType ovfObj, string vsId, string lang, string deviceInstanceId, string controllerInstanceId, string AddressOnController)
{
VirtualHardwareSection_Type[] vhsArray = FindVirtualHardwareSection(ovfObj, vsId);
foreach (VirtualHardwareSection_Type vhs in vhsArray)
{
foreach (RASD_Type rasd in vhs.Item)
{
if (rasd.InstanceID.Value.Equals(deviceInstanceId))
{
rasd.Parent = new cimString(controllerInstanceId);
rasd.AddressOnParent = new cimString(AddressOnController);
}
else if (rasd.InstanceID.Value.Equals(controllerInstanceId))
{
rasd.required = true;
}
}
}
log.Debug("OVF.AddDeviceToController completed");
return;
}
public static void AddDisk(EnvelopeType ovfObj, string vsId, string diskId, string vhdFileName, bool bootable, string name, string description, ulong filesize, ulong capacity)
{
AddDisk(ovfObj, vsId, diskId, Properties.Settings.Default.Language, vhdFileName, bootable, name, description, filesize, capacity);
}
/// <summary>
/// Add a VHD to the VM
/// </summary>
/// <param name="ovfObj">EnvelopeType</param>
/// <param name="vsId">Which VM to apply to</param>
/// <param name="diskId">The RASDs InstanceID</param>
/// <param name="vhdFileName">File Name of VHD (needs to be in the same directory as OVF</param>
/// <param name="bootable">Is this disk bootable</param>
/// <param name="caption">Short string describing disk</param>
/// <param name="description">Description of VHD</param>
/// <param name="filesize">physical file size</param>
/// <param name="capacity">capacity of disk</param>
/// <param name="freespace">amount of free space</param>
/// <param name="sysIdx">System Index in OVF to set memory value (0 = first VM)</param>
/// <param name="idx">Section Index in Virtual Hardware Section (1 = VHS index)</param>
/// <returns>Instance ID of Disk</returns>
public static void AddDisk(EnvelopeType ovfEnv, string vsId, string diskId, string lang, string vhdFileName, bool bootable, string name, string description, ulong filesize, ulong capacity)
{
List<File_Type> files = new List<File_Type>();
List<Section_Type> sections = new List<Section_Type>();
List<VirtualDiskDesc_Type> disks = new List<VirtualDiskDesc_Type>();
DiskSection_Type disksection = null;
if (ovfEnv.References.File != null && ovfEnv.References.File.Length > 0)
{
foreach (File_Type fi in ovfEnv.References.File)
{
files.Add(fi);
}
}
if (ovfEnv.Sections != null && ovfEnv.Sections.Length > 0)
{
foreach (Section_Type sect in ovfEnv.Sections)
{
if (sect is DiskSection_Type)
{
DiskSection_Type ds = (DiskSection_Type)sect;
if (ds.Disk != null && ds.Disk.Length > 0)
{
foreach (VirtualDiskDesc_Type vd in ds.Disk)
{
disks.Add(vd);
}
}
}
else
{
sections.Add(sect);
}
}
}
disksection = new DiskSection_Type();
string info = _ovfrm.GetString("SECTION_DISK_INFO");
disksection.Info = new Msg_Type(AddToStringSection(ovfEnv, lang, info), info);
VirtualDiskDesc_Type vdisk = new VirtualDiskDesc_Type();
File_Type filet = new File_Type();
RASD_Type rasd = new RASD_Type();
vdisk.capacity = Convert.ToString(capacity);
vdisk.isBootable = bootable;
vdisk.format = Properties.Settings.Default.winFileFormatURI;
vdisk.fileRef = diskId;
vdisk.diskId = vdisk.fileRef;
disks.Add(vdisk);
filet.id = vdisk.diskId;
if (filesize > 0)
{
filet.size = filesize;
filet.sizeSpecified = true;
}
filet.href = string.Format(Properties.Settings.Default.FileURI, vhdFileName);
files.Add(filet);
rasd.AllocationUnits = new cimString(_ovfrm.GetString("RASD_19_ALLOCATIONUNITS"));
rasd.AutomaticAllocation = new cimBoolean();
rasd.AutomaticAllocation.Value = true;
// Other code depends on the value of caption.
rasd.Caption = new Caption(_ovfrm.GetString("RASD_19_CAPTION"));
rasd.ConsumerVisibility = new ConsumerVisibility();
rasd.ConsumerVisibility.Value = 3; //From MS.
rasd.Connection = new cimString[] { new cimString(diskId) };
rasd.HostResource = new cimString[] { new cimString(string.Format(Properties.Settings.Default.hostresource, diskId)) };
rasd.Description = new cimString(description);
rasd.ElementName = new cimString(name);
rasd.InstanceID = new cimString(diskId);
rasd.Limit = new cimUnsignedLong();
rasd.Limit.Value = 1; // From MS;
rasd.MappingBehavior = new MappingBehavior();
rasd.MappingBehavior.Value = 0; // From MS.
rasd.ResourceSubType = new cimString(_ovfrm.GetString("RASD_19_RESOURCESUBTYPE"));
rasd.ResourceType = new ResourceType();
rasd.ResourceType.Value = 19; // Hard Disk Image
rasd.VirtualQuantity = new cimUnsignedLong();
rasd.VirtualQuantity.Value = 1;
rasd.Weight = new cimUnsignedInt();
rasd.Weight.Value = 100; // From MS.
AddRasdToAllVHS(ovfEnv, vsId, rasd);
disksection.Disk = disks.ToArray();
sections.Add(disksection);
ovfEnv.Sections = sections.ToArray();
ovfEnv.References.File = files.ToArray();
log.Debug("OVF.AddDisk completed");
}
public static string AddEula(EnvelopeType ovfObj, string eulafilename)
{
return AddEula(ovfObj, Properties.Settings.Default.Language, eulafilename);
}
/// <summary>
/// Add a EULA to the OVF
/// </summary>
public static string AddEula(EnvelopeType ovfEnv, string lang, string eulafilename)
{
EulaSection_Type eulaSection = null;
if (eulafilename != null)
{
FileStream fs = null;
try
{
fs = new FileStream(eulafilename, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader sw = new StreamReader(fs);
eulaSection = new EulaSection_Type();
eulaSection.Id = Guid.NewGuid().ToString();
string info = _ovfrm.GetString("SECTION_EULA_INFO");
eulaSection.Info = new Msg_Type(AddToStringSection(ovfEnv, lang, info), info);
string agreement = sw.ReadToEnd();
eulaSection.License = new Msg_Type(AddToStringSection(ovfEnv, lang, agreement), agreement);
}
catch (Exception ex)
{
eulaSection = null;
throw new Exception("Export Halted: Cannot read EULA", ex);
}
finally
{
if (fs != null) { fs.Close(); }
}
}
if (eulaSection != null)
{
List<Section_Type> sections = new List<Section_Type>();
if (ovfEnv.Item != null)
{
if (ovfEnv.Item.Items != null)
{
sections.AddRange(ovfEnv.Item.Items);
}
}
else
{
throw new Exception("Cannot add EULA no VirtualSystem or VirtualSystemCollection available.");
}
sections.Add(eulaSection);
ovfEnv.Item.Items = sections.ToArray();
}
return eulaSection.Id;
}
/// <summary>
/// Add an external file
/// </summary>
/// <param name="ovfObj">EnvelopeType</param>
/// <param name="filename">Filename</param>
public static void AddExternalFile(EnvelopeType ovfObj, string filename, string id)
{
File_Type ft = new File_Type();
if (id == null || id.Length <= 0)
{
id = Guid.NewGuid().ToString();
}
ft.id = id;
ft.href = Path.GetFileName(filename);
List<File_Type> ftList = new List<File_Type>();
if (ovfObj.References.File != null)
{
ftList.AddRange(ovfObj.References.File);
}
ftList.Add(ft);
ovfObj.References.File = ftList.ToArray();
}
public void AddFileReference(EnvelopeType ovfObj, string filename, string id, ulong capacity, string format)
{
AddFileReference(ovfObj, Properties.Settings.Default.Language, filename, id, capacity, format);
}
/// <summary>
/// Add an Disk Reference file
/// </summary>
/// <param name="ovfObj">EnvelopeType</param>
/// <param name="filename">Filename</param>
public static void AddFileReference(EnvelopeType env, string lang, string filename, string id, ulong capacity, string format)
{
DiskSection_Type ds = null;
List<VirtualDiskDesc_Type> vdisks = new List<VirtualDiskDesc_Type>();
List<Section_Type> sections = new List<Section_Type>();
if (env.Sections != null)
{
foreach (Section_Type section in env.Sections)
{
if (section is DiskSection_Type)
{
ds = (DiskSection_Type)section;
}
else
{
sections.Add(section);
}
}
}
if (ds == null)
{
ds = new DiskSection_Type();
string info = _ovfrm.GetString("SECTION_DISK_INFO");
ds.Info = new Msg_Type(AddToStringSection(env, lang, info), info);
}
VirtualDiskDesc_Type vdisk = new VirtualDiskDesc_Type();
vdisk.format = format;
vdisk.diskId = id;
vdisk.fileRef = id;
if (capacity != 0)
{
vdisk.capacity = Convert.ToString(capacity);
}
AddExternalFile(env, filename, id);
if (ds.Disk != null)
{
foreach (VirtualDiskDesc_Type vd in ds.Disk)
{
vdisks.Add(vd);
}
}
vdisks.Add(vdisk);
ds.Disk = vdisks.ToArray();
sections.Add(ds);
env.Sections = sections.ToArray();
}
public static InstallSection_Type AddInstallSection(EnvelopeType ovfObj, string vsId, ushort bootStopDelay, string lang, string info)
{
VirtualSystem_Type vSystem = FindVirtualSystemById(ovfObj, vsId);
InstallSection_Type installSection = null;
List<Section_Type> sections = new List<Section_Type>();
foreach (Section_Type section in vSystem.Items)
{
if (section is InstallSection_Type)
{
installSection = (InstallSection_Type)section;
}
else
{
sections.Add(section);
}
}
if (installSection == null)
{
installSection = new InstallSection_Type();
installSection.Id = Guid.NewGuid().ToString();
}
installSection.initialBootStopDelay = bootStopDelay;
installSection.Info = new Msg_Type(AddToStringSection(ovfObj, lang, info), info);
sections.Add(installSection);
vSystem.Items = sections.ToArray();
return installSection;
}
public static void AddNetwork(EnvelopeType ovfObj, string vsId, string netId, string netName, string networkDescription, string macAddress)
{
AddNetwork(ovfObj, vsId, Properties.Settings.Default.Language, netId, netName, networkDescription, macAddress);
}
/// <summary>
/// Add a Network to the VM
/// </summary>
/// <param name="ovfObj">EnvelopeType</param>
/// <param name="vsId">Virtual System Identifier</param>
/// <param name="macAddress">null = unset, value sets MAC Address</param>
public static void AddNetwork(EnvelopeType ovfEnv, string vsId, string lang, string netId, string netName, string networkDescription, string macAddress)
{
List<NetworkSection_TypeNetwork> ns = new List<NetworkSection_TypeNetwork>();
NetworkSection_TypeNetwork attached = null;
List<RASD_Type> rasds = new List<RASD_Type>();
NetworkSection_Type netsection = new NetworkSection_Type();
string info = _ovfrm.GetString("SECTION_NETWORK_INFO");
netsection.Info = new Msg_Type(AddToStringSection(ovfEnv, lang, info), info);
List<Section_Type> sections = new List<Section_Type>();
if (ovfEnv.Sections != null && ovfEnv.Sections.Length > 0)
{
foreach (Section_Type sect in ovfEnv.Sections)
{
if (sect is NetworkSection_Type)
{
netsection = (NetworkSection_Type)sect;
}
else
{
sections.Add(sect);
}
}
}
if (netsection.Network != null && netsection.Network.Length > 0)
{
foreach (NetworkSection_TypeNetwork lns in netsection.Network)
{
ns.Add(lns);
if (lns.name.Equals(netId))
{
attached = lns;
}
}
}
RASD_Type rasd = new RASD_Type();
if (!string.IsNullOrEmpty(macAddress))
{
rasd.Address = new cimString(macAddress);
}
rasd.AllocationUnits = new cimString(_ovfrm.GetString("RASD_10_ALLOCATIONUNITS"));
rasd.AutomaticAllocation = new cimBoolean();
rasd.AutomaticAllocation.Value = true;
rasd.Caption = new Caption(_ovfrm.GetString("RASD_10_CAPTION"));
rasd.ConsumerVisibility = new ConsumerVisibility();
rasd.ConsumerVisibility.Value = 3;
if (!string.IsNullOrEmpty(networkDescription))
{
rasd.Description = new cimString(networkDescription);
}
else
{
rasd.Description = new cimString(_ovfrm.GetString("RASD_10_DESCRIPTION"));
}
rasd.ElementName = new cimString(netName);
rasd.InstanceID = new cimString(Guid.NewGuid().ToString());
rasd.Connection = new cimString[1];
rasd.Connection[0] = new cimString(netId);
rasd.Limit = new cimUnsignedLong();
rasd.Limit.Value = 1;
rasd.MappingBehavior = new MappingBehavior();
rasd.MappingBehavior.Value = 0;
rasd.ResourceType = new ResourceType();
rasd.ResourceType.Value = 10;
rasd.VirtualQuantity = new cimUnsignedLong();
rasd.VirtualQuantity.Value = 1;
rasd.Weight = new cimUnsignedInt();
rasd.Weight.Value = 0;
if (attached == null)
{
attached = new NetworkSection_TypeNetwork();
attached.name = netId;
attached.Description = new Msg_Type(AddToStringSection(ovfEnv, lang, rasd.Description.Value), rasd.Description.Value);
ns.Add(attached);
}
AddRasdToAllVHS(ovfEnv, vsId, rasd);
netsection.Network = ns.ToArray();
sections.Add(netsection);
ovfEnv.Sections = sections.ToArray();
log.Debug("OVF.AddNetwork completed");
}
public void AddOperatingSystemSection(EnvelopeType ovfObj, string vsId, string description, string osInfo)
{
AddOperatingSystemSection(ovfObj, vsId, Properties.Settings.Default.Language, description, osInfo, 0);
}
public static void AddOperatingSystemSection(EnvelopeType ovfObj, string vsId, string lang, string description, string osInfo)
{
AddOperatingSystemSection(ovfObj, vsId, lang, description, osInfo, 0);
}
/// <summary>
/// Add the Operating System Section
/// </summary>
/// <param name="ovfEnv">Ovf:EnvelopeType</param>
/// <param name="vsId">Virtual System Identifier</param>
/// <param name="lang">Language</param>
/// <param name="description">Description</param>
/// <param name="osInfo">OS Information</param>
/// <param name="osid">ushort identifying the OS from CIM_OperatingSystem ValueMap</param>
public void AddOperatingSystemSection(EnvelopeType ovfObj, string vsId, string description, string osInfo, ushort osid)
{
AddOperatingSystemSection(ovfObj, vsId, Properties.Settings.Default.Language, description, osInfo, osid);
}
/// <summary>
/// Add the Operating System Section
/// </summary>
/// <param name="ovfEnv">Ovf:EnvelopeType</param>
/// <param name="vsId">Virtual System Identifier</param>
/// <param name="lang">Language</param>
/// <param name="description">Description</param>
/// <param name="osInfo">OS Information</param>
/// <param name="osid">ushort identifying the OS from CIM_OperatingSystem ValueMap</param>
public static void AddOperatingSystemSection(EnvelopeType ovfEnv, string vsId, string lang, string description, string osInfo, ushort osid)
{
OperatingSystemSection_Type oss = new OperatingSystemSection_Type();
oss.id = osid;
string info = null;
if (!string.IsNullOrEmpty(description))
{
oss.Description = new Msg_Type(AddToStringSection(ovfEnv, lang, description), description);
}
else
{
info = _ovfrm.GetString("SECTION_OPERATINGSYSTEM_DESCRIPTION");
oss.Description = new Msg_Type(AddToStringSection(ovfEnv, lang, info), info);
}
if (!string.IsNullOrEmpty(osInfo))
{
oss.Info = new Msg_Type(AddToStringSection(ovfEnv, lang, osInfo), osInfo);
}
else
{
info = _ovfrm.GetString("SECTION_OPERATINGSYSTEM_INFO");
oss.Info = new Msg_Type(AddToStringSection(ovfEnv, lang, info), info);
}
if (ovfEnv.Item == null || ((VirtualSystemCollection_Type)ovfEnv.Item).Content == null)
{
throw new ArgumentNullException(Messages.FAILED_TO_ADD_OS_SECTION);
}
AddContent((VirtualSystemCollection_Type)ovfEnv.Item, vsId, oss);
log.DebugFormat("OVF.AddOperatingSystemSection completed {0}", vsId);
}
public static string AddOtherSystemSettingData(EnvelopeType ovfObj, string vsId, string name, string value, string description, bool permitMultiple=false)
{
return AddOtherSystemSettingData(ovfObj, vsId, Properties.Settings.Default.Language, name, value, description, permitMultiple);
}
/// <summary>
/// Add XEN Specific configuration Items.
/// </summary>
/// <param name="OvfObj">EnvelopeType</param>
/// <param name="vsId">Virtual System Identifier</param>
/// <param name="name">Name of Parameter: is: HVM-boot-policy (case sensitive)</param>
/// <param name="value">value for the parameter</param>
/// <param name="description">Description of parameter</param>
/// <param name="permitMultiple">Whether the setting data permit multiple values, default false</param>
public static string AddOtherSystemSettingData(EnvelopeType ovfObj, string vsId, string lang, string name, string value, string description, bool permitMultiple=false)
{
VirtualHardwareSection_Type[] vhsArray = FindVirtualHardwareSection(ovfObj, vsId);
VirtualHardwareSection_Type vhs = null;
foreach (VirtualHardwareSection_Type _vhs in vhsArray)
{
if (_vhs.System != null && _vhs.System.VirtualSystemType != null &&
!(string.IsNullOrEmpty(_vhs.System.VirtualSystemType.Value)) &&
(_vhs.System.VirtualSystemType.Value.ToLower().StartsWith("xen") ||
_vhs.System.VirtualSystemType.Value.ToLower().StartsWith("hvm")))
{
vhs = _vhs;
break;
}
}
if (vhs == null)
{
log.Warn("OVF.AddOtherSystemSettingData: could not find 'xen' or 'hvm' system type VHS, skipping.");
return null;
}
List<Xen_ConfigurationSettingData_Type> xencfg = new List<Xen_ConfigurationSettingData_Type>();
if (vhs.VirtualSystemOtherConfigurationData != null && vhs.VirtualSystemOtherConfigurationData.Length > 0)
{
foreach (Xen_ConfigurationSettingData_Type xencsd in vhs.VirtualSystemOtherConfigurationData)
{
// If permitMultiple, the old items are kept, and the new item will be added later,
// otherwise, the old item is skipped here and will be overridden by the new one
if (permitMultiple || xencsd.Name.ToLower() != name.ToLower())
{
xencfg.Add(xencsd);
}
}
}
Xen_ConfigurationSettingData_Type xenother = new Xen_ConfigurationSettingData_Type();
xenother.id = Guid.NewGuid().ToString();
xenother.Name = name;
xenother.Value = new cimString(value);
xenother.Info = new Msg_Type(AddToStringSection(ovfObj, lang, description), description);
xencfg.Add(xenother);
vhs.VirtualSystemOtherConfigurationData = xencfg.ToArray();
log.Debug("OVF.AddOtherSystemSettingData completed");
return xenother.id;
}
public static string AddPostInstallOperation(EnvelopeType ovfObj, string vsId, string lang, string message)
{
VirtualSystem_Type vSystem = FindVirtualSystemById(ovfObj, vsId);
InstallSection_Type installSection = null;
foreach (Section_Type sec in vSystem.Items)
{
if (sec is InstallSection_Type)
{
installSection = (InstallSection_Type)sec;
break;
}
}
if (installSection == null)
installSection = AddInstallSection(ovfObj, vsId, 600, Properties.Settings.Default.Language, "ConfigureForXenServer");
Xen_PostInstallOperation_Type XenPostInstall = new Xen_PostInstallOperation_Type();
XenPostInstall.required = false;
XenPostInstall.requiredSpecified = true;
XenPostInstall.id = Guid.NewGuid().ToString();
XenPostInstall.Info = new Msg_Type(AddToStringSection(ovfObj, lang, message), message);
installSection.PostInstallOperations = XenPostInstall;
return XenPostInstall.id;
}
public string AddProductSection(EnvelopeType ovfObj, string nameSpace, string info, string product, string vendor, string version, string producturl, string vendorurl)
{
return AddProductSection(ovfObj, Properties.Settings.Default.Language, nameSpace, info, product, vendor, version, producturl, vendorurl);
}
public string AddProductSection(EnvelopeType env, string lang, string nameSpace, string info, string product, string vendor, string version, string producturl, string vendorurl)
{
string psId = Guid.NewGuid().ToString();
ProductSection_Type ps = new ProductSection_Type();
ps.@class = nameSpace;
ps.instance = psId;
ps.Product = new Msg_Type(AddToStringSection(env, lang, product), product);
ps.ProductUrl = new cimString(producturl);