forked from lstoll/libvirt-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomain.java
More file actions
1075 lines (1001 loc) · 38.8 KB
/
Copy pathDomain.java
File metadata and controls
1075 lines (1001 loc) · 38.8 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
package org.libvirt;
import org.libvirt.jna.DomainPointer;
import org.libvirt.jna.DomainSnapshotPointer;
import org.libvirt.jna.Libvirt;
import org.libvirt.jna.virDomainBlockInfo;
import org.libvirt.jna.virDomainBlockStats;
import org.libvirt.jna.virDomainInfo;
import org.libvirt.jna.virDomainInterfaceStats;
import org.libvirt.jna.virDomainJobInfo;
import org.libvirt.jna.virDomainMemoryStats;
import org.libvirt.jna.virSchedParameter;
import org.libvirt.jna.virVcpuInfo;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.IntByReference;
/**
* A virtual machine defined within libvirt.
*/
public class Domain {
static final class CreateFlags {
static final int VIR_DOMAIN_NONE = 0;
}
static final class MigrateFlags {
/**
* live migration
*/
static final int VIR_MIGRATE_LIVE = 1;
}
static final class XMLFlags {
/**
* dump security sensitive information too
*/
static final int VIR_DOMAIN_XML_SECURE = 1;
/**
* dump inactive domain information
*/
static final int VIR_DOMAIN_XML_INACTIVE = 2;
}
/**
* the native virDomainPtr.
*/
DomainPointer VDP;
/**
* The Connect Object that represents the Hypervisor of this Domain
*/
private Connect virConnect;
/**
* The libvirt connection from the hypervisor
*/
protected Libvirt libvirt;
/**
* Constructs a Domain object from a known native DomainPointer, and a
* Connect object.
*
* @param virConnect
* the Domain's hypervisor
* @param VDP
* the native virDomainPtr
*/
Domain(Connect virConnect, DomainPointer VDP) {
this.virConnect = virConnect;
this.VDP = VDP;
libvirt = virConnect.libvirt;
}
/**
* Requests that the current background job be aborted at the soonest
* opportunity. This will block until the job has either completed, or
* aborted.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainAbortJob">Libvirt
* Documentation</a>
* @return 0 in case of success and -1 in case of failure.
* @throws LibvirtException
*/
public int abortJob() throws LibvirtException {
int returnValue = libvirt.virDomainAbortJob(VDP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Creates a virtual device attachment to backend.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainAttachDevice">Libvirt
* Documentation</a>
* @param xmlDesc
* XML description of one device
* @throws LibvirtException
*/
public void attachDevice(String xmlDesc) throws LibvirtException {
int result = libvirt.virDomainAttachDevice(VDP, xmlDesc);
ErrorHandler.processError(libvirt, result);
}
/**
* Creates a virtual device attachment to backend.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainAttachDeviceFlags">Libvirt
* Documentation</a>
* @param xmlDesc
* XML description of one device
* @param flags
* the an OR'ed set of virDomainDeviceModifyFlags
* @throws LibvirtException
*/
public void attachDeviceFlags(String xmlDesc, int flags) throws LibvirtException {
int result = libvirt.virDomainAttachDeviceFlags(VDP, xmlDesc, flags);
ErrorHandler.processError(libvirt, result);
}
/**
* This function returns block device (disk) stats for block devices
* attached to the domain.
*
* @param path
* the path to the block device
* @return the info, or null if an error
* @throws LibvirtException
*/
public DomainBlockInfo blockInfo(String path) throws LibvirtException {
virDomainBlockInfo info = new virDomainBlockInfo();
int success = libvirt.virDomainGetBlockInfo(VDP, path, info, 0);
ErrorHandler.processError(libvirt, success);
return success == 0 ? new DomainBlockInfo(info) : null;
}
/**
* Returns block device (disk) stats for block devices attached to this
* domain. The path parameter is the name of the block device. Get this by
* calling virDomainGetXMLDesc and finding the <target dev='...'> attribute
* within //domain/devices/disk. (For example, "xvda"). Domains may have
* more than one block device. To get stats for each you should make
* multiple calls to this function. Individual fields within the
* DomainBlockStats object may be returned as -1, which indicates that the
* hypervisor does not support that particular statistic.
*
* @param path
* path to the block device
* @return the statistics in a DomainBlockStats object
* @throws LibvirtException
*/
public DomainBlockStats blockStats(String path) throws LibvirtException {
virDomainBlockStats stats = new virDomainBlockStats();
int success = libvirt.virDomainBlockStats(VDP, path, stats, stats.size());
ErrorHandler.processError(libvirt, success);
return success == 0 ? new DomainBlockStats(stats) : null;
}
/**
* Dumps the core of this domain on a given file for analysis. Note that for
* remote Xen Daemon the file path will be interpreted in the remote host.
*
* @param to
* path for the core file
* @param flags
* extra flags, currently unused
* @throws LibvirtException
*/
public void coreDump(String to, int flags) throws LibvirtException {
int result = libvirt.virDomainCoreDump(VDP, to, flags);
ErrorHandler.processError(libvirt, result);
}
/**
* It returns the length (in bytes) required to store the complete CPU map
* between a single virtual & all physical CPUs of a domain.
*/
public int cpuMapLength(int maxCpus) {
return (((maxCpus) + 7) / 8);
}
/**
* Launches this defined domain. If the call succeed the domain moves from
* the defined to the running domains pools.
*
* @throws LibvirtException
*/
public int create() throws LibvirtException {
int returnValue = libvirt.virDomainCreate(VDP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Launches this defined domain with the provide flags.
* If the call succeed the domain moves from
* the defined to the running domains pools.
*
* @throws LibvirtException
*/
public int create(int flags) throws LibvirtException {
int returnValue = libvirt.virDomainCreateWithFlags(VDP, flags);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Destroys this domain object. The running instance is shutdown if not down
* already and all resources used by it are given back to the hypervisor.
* The data structure is freed and should not be used thereafter if the call
* does not return an error. This function may requires priviledged access
*
* @throws LibvirtException
*/
public void destroy() throws LibvirtException {
int result = libvirt.virDomainDestroy(VDP);
ErrorHandler.processError(libvirt, result);
}
/**
* Destroys a virtual device attachment to backend.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainDetachDevice">Libvirt
* Documentation</a>
* @param xmlDesc
* XML description of one device
* @throws LibvirtException
*/
public void detachDevice(String xmlDesc) throws LibvirtException {
int result = libvirt.virDomainDetachDevice(VDP, xmlDesc);
ErrorHandler.processError(libvirt, result);
}
/**
* Destroys a virtual device attachment to backend.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainDetachDeviceFlags">Libvirt
* Documentation</a>
* @param xmlDesc
* XML description of one device
* @throws LibvirtException
*/
public void detachDeviceFlags(String xmlDesc, int flags) throws LibvirtException {
int result = libvirt.virDomainDetachDeviceFlags(VDP, xmlDesc, flags);
ErrorHandler.processError(libvirt, result);
}
@Override
public void finalize() throws LibvirtException {
free();
}
/**
* Frees this domain object. The running instance is kept alive. The data
* structure is freed and should not be used thereafter.
*
* @throws LibvirtException
* @return number of references left (>= 0) for success, -1 for failure.
*/
public int free() throws LibvirtException {
int success = 0;
if (VDP != null) {
success = libvirt.virDomainFree(VDP);
ErrorHandler.processError(libvirt, success);
VDP = null;
}
return success;
}
/**
* Provides a boolean value indicating whether the network is configured to
* be automatically started when the host machine boots.
*
* @return the result
* @throws LibvirtException
*/
public boolean getAutostart() throws LibvirtException {
IntByReference autoStart = new IntByReference();
int result = libvirt.virDomainGetAutostart(VDP, autoStart);
ErrorHandler.processError(libvirt, result);
return autoStart.getValue() != 0 ? true : false;
}
/**
* Provides the connection object associated with a domain.
*
* @return the Connect object
*/
public Connect getConnect() {
return virConnect;
}
/**
* Gets the hypervisor ID number for the domain
*
* @return the hypervisor ID
* @throws LibvirtException
*/
public int getID() throws LibvirtException {
int returnValue = libvirt.virDomainGetID(VDP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Extract information about a domain. Note that if the connection used to
* get the domain is limited only a partial set of the information can be
* extracted.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainGetInfo">Libvirt
* Documentation</a>
*
* @return a DomainInfo object describing this domain
* @throws LibvirtException
*/
public DomainInfo getInfo() throws LibvirtException {
DomainInfo returnValue = null;
virDomainInfo vInfo = new virDomainInfo();
int success = libvirt.virDomainGetInfo(VDP, vInfo);
ErrorHandler.processError(libvirt, success);
if (success == 0) {
returnValue = new DomainInfo(vInfo);
}
return returnValue;
}
/**
* Extract information about progress of a background job on a domain. Will
* return an error if the domain is not active.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainGetJobInfo">Libvirt
* Documentation</a>
* @return a DomainJobInfo object describing this domain
* @throws LibvirtException
*/
public DomainJobInfo getJobInfo() throws LibvirtException {
DomainJobInfo returnValue = null;
virDomainJobInfo vInfo = new virDomainJobInfo();
int success = libvirt.virDomainGetJobInfo(VDP, vInfo);
ErrorHandler.processError(libvirt, success);
if (success == 0) {
returnValue = new DomainJobInfo(vInfo);
}
return returnValue;
}
/**
* Retrieve the maximum amount of physical memory allocated to a domain.
*
* @return the memory in kilobytes
* @throws LibvirtException
*/
public long getMaxMemory() throws LibvirtException {
NativeLong returnValue = libvirt.virDomainGetMaxMemory(VDP);
ErrorHandler.processError(libvirt, returnValue.longValue() - 1); // 0 is an error for this call
return returnValue.longValue();
}
/**
* Provides the maximum number of virtual CPUs supported for the guest VM.
* If the guest is inactive, this is basically the same as
* virConnectGetMaxVcpus. If the guest is running this will reflect the
* maximum number of virtual CPUs the guest was booted with.
*
* @return the number of VCPUs
* @throws LibvirtException
*/
public int getMaxVcpus() throws LibvirtException {
int returnValue = libvirt.virDomainGetMaxVcpus(VDP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Gets the public name for this domain
*
* @return the name
* @throws LibvirtException
*/
public String getName() throws LibvirtException {
String returnValue = libvirt.virDomainGetName(VDP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Gets the type of domain operation system.
*
* @return the type
* @throws LibvirtException
*/
public String getOSType() throws LibvirtException {
String returnValue = libvirt.virDomainGetOSType(VDP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Gets the scheduler parameters.
*
* @return an array of SchedParameter objects
* @throws LibvirtException
*/
public SchedParameter[] getSchedulerParameters() throws LibvirtException {
IntByReference nParams = new IntByReference();
SchedParameter[] returnValue = new SchedParameter[0];
String scheduler = libvirt.virDomainGetSchedulerType(VDP, nParams);
ErrorHandler.processError(libvirt, scheduler);
if (scheduler != null) {
virSchedParameter[] nativeParams = new virSchedParameter[nParams.getValue()];
returnValue = new SchedParameter[nParams.getValue()];
int result = libvirt.virDomainGetSchedulerParameters(VDP, nativeParams, nParams);
ErrorHandler.processError(libvirt, result);
for (int x = 0; x < nParams.getValue(); x++) {
returnValue[x] = SchedParameter.create(nativeParams[x]);
}
}
return returnValue;
}
// getSchedulerType
// We don't expose the nparams return value, it's only needed for the
// SchedulerParameters allocations,
// but we handle that in getSchedulerParameters internally.
/**
* Gets the scheduler type.
*
* @return the type of the scheduler
* @throws LibvirtException
*/
public String[] getSchedulerType() throws LibvirtException {
IntByReference nParams = new IntByReference();
String returnValue = libvirt.virDomainGetSchedulerType(VDP, nParams);
ErrorHandler.processError(libvirt, returnValue);
String[] array = new String[1];
array[0] = returnValue;
return array;
}
/**
* Get the UUID for this domain.
*
* @return the UUID as an unpacked int array
* @throws LibvirtException
* @see <a href="http://www.ietf.org/rfc/rfc4122.txt">rfc4122</a>
*/
public int[] getUUID() throws LibvirtException {
byte[] bytes = new byte[Libvirt.VIR_UUID_BUFLEN];
int success = libvirt.virDomainGetUUID(VDP, bytes);
ErrorHandler.processError(libvirt, success);
int[] returnValue = new int[0];
if (success == 0) {
returnValue = Connect.convertUUIDBytes(bytes);
}
return returnValue;
}
/**
* Gets the UUID for this domain as string.
*
* @return the UUID in canonical String format
* @throws LibvirtException
* @see <a href="http://www.ietf.org/rfc/rfc4122.txt">rfc4122</a>
*/
public String getUUIDString() throws LibvirtException {
byte[] bytes = new byte[Libvirt.VIR_UUID_STRING_BUFLEN];
int success = libvirt.virDomainGetUUIDString(VDP, bytes);
ErrorHandler.processError(libvirt, success);
String returnValue = null;
if (success == 0) {
returnValue = Native.toString(bytes);
}
return returnValue;
}
/**
* Returns the cpumaps for this domain Only the lower 8 bits of each int in
* the array contain information.
*
* @return a bitmap of real CPUs for all vcpus of this domain
* @throws LibvirtException
*/
public int[] getVcpusCpuMaps() throws LibvirtException {
int[] returnValue = new int[0];
int cpuCount = getMaxVcpus();
if (cpuCount > 0) {
NodeInfo nodeInfo = virConnect.nodeInfo();
int maplength = cpuMapLength(nodeInfo.maxCpus());
virVcpuInfo[] infos = new virVcpuInfo[cpuCount];
returnValue = new int[cpuCount * maplength];
byte[] cpumaps = new byte[cpuCount * maplength];
int result = libvirt.virDomainGetVcpus(VDP, infos, cpuCount, cpumaps, maplength);
ErrorHandler.processError(libvirt, result);
for (int x = 0; x < cpuCount * maplength; x++) {
returnValue[x] = cpumaps[x];
}
}
return returnValue;
}
/**
* Extracts information about virtual CPUs of this domain
*
* @return an array of VcpuInfo object describing the VCPUs
* @throws LibvirtException
*/
public VcpuInfo[] getVcpusInfo() throws LibvirtException {
int cpuCount = getMaxVcpus();
VcpuInfo[] returnValue = new VcpuInfo[cpuCount];
virVcpuInfo[] infos = new virVcpuInfo[cpuCount];
int result = libvirt.virDomainGetVcpus(VDP, infos, cpuCount, null, 0);
ErrorHandler.processError(libvirt, result);
for (int x = 0; x < cpuCount; x++) {
returnValue[x] = new VcpuInfo(infos[x]);
}
return returnValue;
}
/**
* Provides an XML description of the domain. The description may be reused
* later to relaunch the domain with createLinux().
*
* @param flags
* not used
* @return the XML description String
* @throws LibvirtException
* @see <a href="http://libvirt.org/format.html#Normal1" >The XML
* Description format </a>
*/
public String getXMLDesc(int flags) throws LibvirtException {
String returnValue = libvirt.virDomainGetXMLDesc(VDP, flags);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Determine if the domain has a snapshot
*
* @see <a href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainHasCurrentSnapshot>Libvi
* r t Documentation</a>
* @return 1 if running, 0 if inactive, -1 on error
* @throws LibvirtException
*/
public int hasCurrentSnapshot() throws LibvirtException {
int returnValue = libvirt.virDomainHasCurrentSnapshot(VDP, 0);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Determine if the domain has a managed save image
*
* @see <a href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainHasManagedSaveImage>Libvi
* r t Documentation</a>
* @return 0 if no image is present, 1 if an image is present, and -1 in
* case of error
* @throws LibvirtException
*/
public int hasManagedSaveImage() throws LibvirtException {
int returnValue = libvirt.virDomainHasManagedSaveImage(VDP, 0);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Returns network interface stats for interfaces attached to this domain.
* The path parameter is the name of the network interface. Domains may have
* more than network interface. To get stats for each you should make
* multiple calls to this function. Individual fields within the
* DomainInterfaceStats object may be returned as -1, which indicates that
* the hypervisor does not support that particular statistic.
*
* @param path
* path to the interface
* @return the statistics in a DomainInterfaceStats object
* @throws LibvirtException
*/
public DomainInterfaceStats interfaceStats(String path) throws LibvirtException {
virDomainInterfaceStats stats = new virDomainInterfaceStats();
int result = libvirt.virDomainInterfaceStats(VDP, path, stats, stats.size());
ErrorHandler.processError(libvirt, result);
return new DomainInterfaceStats(stats);
}
/**
* Determine if the domain is currently running
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainIsActive">Libvirt
* Documentation</a>
* @return 1 if running, 0 if inactive, -1 on error
* @throws LibvirtException
*/
public int isActive() throws LibvirtException {
int returnValue = libvirt.virDomainIsActive(VDP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Determine if the domain has a persistent configuration which means it
* will still exist after shutting down
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainIsPersistent">Libvirt
* Documentation</a>
* @return 1 if persistent, 0 if transient, -1 on error
* @throws LibvirtException
*/
public int isPersistent() throws LibvirtException {
int returnValue = libvirt.virDomainIsPersistent(VDP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* suspend a domain and save its memory contents to a file on disk.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainManagedSave">Libvirt
* Documentation</a>
* @return 0 in case of success or -1 in case of failure
* @throws LibvirtException
*/
public int managedSave() throws LibvirtException {
int returnValue = libvirt.virDomainManagedSave(VDP, 0);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Remove any managed save images from the domain
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainManagedSaveRemove">Libvirt
* Documentation</a>
* @return
* @throws LibvirtException
*/
public int managedSaveRemote() throws LibvirtException {
int returnValue = libvirt.virDomainManagedSaveRemove(VDP, 0);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* This function provides memory statistics for the domain.
*
* @param number
* the number of stats to retrieve
* @return the collection of stats, or null if an error occurs.
* @throws LibvirtException
*/
public MemoryStatistic[] memoryStats(int number) throws LibvirtException {
virDomainMemoryStats[] stats = new virDomainMemoryStats[number];
MemoryStatistic[] returnStats = null;
int result = libvirt.virDomainMemoryStats(VDP, stats, number, 0);
ErrorHandler.processError(libvirt, result);
if (result >= 0) {
returnStats = new MemoryStatistic[result];
for (int x = 0; x < result; x++) {
returnStats[x] = new MemoryStatistic(stats[x]);
}
}
return returnStats;
}
/**
* Migrate this domain object from its current host to the destination host
* given by dconn (a connection to the destination host). Flags may be one
* of more of the following: Domain.VIR_MIGRATE_LIVE Attempt a live
* migration. If a hypervisor supports renaming domains during migration,
* then you may set the dname parameter to the new name (otherwise it keeps
* the same name). If this is not supported by the hypervisor, dname must be
* NULL or else you will get an error. Since typically the two hypervisors
* connect directly to each other in order to perform the migration, you may
* need to specify a path from the source to the destination. This is the
* purpose of the uri parameter.If uri is NULL, then libvirt will try to
* find the best method. Uri may specify the hostname or IP address of the
* destination host as seen from the source, or uri may be a URI giving
* transport, hostname, user, port, etc. in the usual form. Uri should only
* be specified if you want to migrate over a specific interface on the
* remote host. For Qemu/KVM, the uri should be of the form
* "tcp://hostname[:port]". This does not require TCP auth to be setup
* between the connections, since migrate uses a straight TCP connection
* (unless using the PEER2PEER flag, in which case URI should be a full
* fledged libvirt URI). Refer also to driver documentation for the
* particular URIs supported. If set to 0, libvirt will choose a suitable
* default. Some hypervisors do not support this feature and will return an
* error if bandwidth is not 0. To see which features are supported by the
* current hypervisor, see Connect.getCapabilities,
* /capabilities/host/migration_features. There are many limitations on
* migration imposed by the underlying technology - for example it may not
* be possible to migrate between different processors even with the same
* architecture, or between different types of hypervisor.
*
* @param dconn
* destination host (a Connect object)
* @param flags
* flags
* @param dname
* (optional) rename domain to this at destination
* @param uri
* (optional) dest hostname/URI as seen from the source host
* @param bandwidth
* optional) specify migration bandwidth limit in Mbps
* @return the new domain object if the migration was successful, or NULL in
* case of error. Note that the new domain object exists in the
* scope of the destination connection (dconn).
* @throws LibvirtException
*/
public Domain migrate(Connect dconn, long flags, String dname, String uri, long bandwidth) throws LibvirtException {
DomainPointer newPtr = libvirt.virDomainMigrate(VDP, dconn.VCP, new NativeLong(flags), dname, uri, new NativeLong(bandwidth));
ErrorHandler.processError(libvirt, newPtr);
return new Domain(dconn, newPtr);
}
/**
* Sets maximum tolerable time for which the domain is allowed to be paused
* at the end of live migration.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainMigrateSetMaxDowntime">LIbvirt
* Documentation</a>
* @param downtime
* the time to be down
* @return 0 in case of success, -1 otherwise.
* @throws LibvirtException
*/
public int migrateSetMaxDowntime(long downtime) throws LibvirtException {
int returnValue = libvirt.virDomainMigrateSetMaxDowntime(VDP, downtime, 0);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Migrate the domain object from its current host to the destination host
* given by duri.
*
* @see http
* ://www.libvirt.org/html/libvirt-libvirt.html#virDomainMigrateToURI
*
* @param uri
* The destination URI
* @param flags
* Controls the migrate
* @param dname
* The name at the destnation
* @param bandwidth
* Specify the migration bandwidth
* @return 0 if successful, -1 if not
* @throws LibvirtException
*/
public int migrateToURI(String uri, long flags, String dname, long bandwidth) throws LibvirtException {
int returnValue = libvirt.virDomainMigrateToURI(VDP, uri, new NativeLong(flags), dname, new NativeLong(bandwidth));
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Dynamically changes the real CPUs which can be allocated to a virtual
* CPU. This function requires priviledged access to the hypervisor.
*
* @param vcpu
* virtual cpu number
* @param cpumap
* bit map of real CPUs represented by the the lower 8 bits of
* each int in the array. Each bit set to 1 means that
* corresponding CPU is usable. Bytes are stored in little-endian
* order: CPU0-7, 8-15... In each byte, lowest CPU number is
* least significant bit.
* @throws LibvirtException
*/
public void pinVcpu(int vcpu, int[] cpumap) throws LibvirtException {
byte[] packedMap = new byte[cpumap.length];
for (int x = 0; x < cpumap.length; x++) {
packedMap[x] = (byte) cpumap[x];
}
int result = libvirt.virDomainPinVcpu(VDP, vcpu, packedMap, cpumap.length);
ErrorHandler.processError(libvirt, result);
}
/**
* Reboot this domain, the domain object is still usable there after but the
* domain OS is being stopped for a restart. Note that the guest OS may
* ignore the request.
*
* @param flags
* extra flags for the reboot operation, not used yet
* @throws LibvirtException
*/
public void reboot(int flags) throws LibvirtException {
int result = libvirt.virDomainReboot(VDP, flags);
ErrorHandler.processError(libvirt, result);
}
/**
* Resume this suspended domain, the process is restarted from the state
* where it was frozen by calling virSuspendDomain(). This function may
* requires privileged access
*
* @throws LibvirtException
*/
public void resume() throws LibvirtException {
int result = libvirt.virDomainResume(VDP);
ErrorHandler.processError(libvirt, result);
}
/**
* Revert the domain to a given snapshot.
*
* @see <a href=
* "http://www.libvirt.org/html/libvirt-libvirt.html#virDomainRevertToSnapshot"
* >Libvirt Documentation</>
* @param snapshot
* the snapshot to revert to
* @return 0 if the creation is successful, -1 on error.
* @throws LibvirtException
*/
public int revertToSnapshot(DomainSnapshot snapshot) throws LibvirtException {
int returnCode = libvirt.virDomainRevertToSnapshot(snapshot.VDSP, 0);
ErrorHandler.processError(libvirt, returnCode);
return returnCode;
}
/**
* Suspends this domain and saves its memory contents to a file on disk.
* After the call, if successful, the domain is not listed as running
* anymore (this may be a problem). Use Connect.virDomainRestore() to
* restore a domain after saving.
*
* @param to
* path for the output file
* @throws LibvirtException
*/
public void save(String to) throws LibvirtException {
int result = libvirt.virDomainSave(VDP, to);
ErrorHandler.processError(libvirt, result);
}
/**
* Configures the network to be automatically started when the host machine
* boots.
*
* @param autostart
* @throws LibvirtException
*/
public void setAutostart(boolean autostart) throws LibvirtException {
int autoValue = autostart ? 1 : 0;
int result = libvirt.virDomainSetAutostart(VDP, autoValue);
ErrorHandler.processError(libvirt, result);
}
/**
* * Dynamically change the maximum amount of physical memory allocated to a
* domain. This function requires priviledged access to the hypervisor.
*
* @param memory
* the amount memory in kilobytes
* @throws LibvirtException
*/
public void setMaxMemory(long memory) throws LibvirtException {
int result = libvirt.virDomainSetMaxMemory(VDP, new NativeLong(memory));
ErrorHandler.processError(libvirt, result);
}
/**
* Dynamically changes the target amount of physical memory allocated to
* this domain. This function may requires priviledged access to the
* hypervisor.
*
* @param memory
* in kilobytes
* @throws LibvirtException
*/
public void setMemory(long memory) throws LibvirtException {
int result = libvirt.virDomainSetMemory(VDP, new NativeLong(memory));
ErrorHandler.processError(libvirt, result);
}
/**
* Changes the scheduler parameters
*
* @param params
* an array of SchedParameter objects to be changed
* @throws LibvirtException
*/
public void setSchedulerParameters(SchedParameter[] params) throws LibvirtException {
virSchedParameter[] input = new virSchedParameter[params.length];
for (int x = 0; x < params.length; x++) {
input[x] = SchedParameter.toNative(params[x]);
}
int result = libvirt.virDomainSetSchedulerParameters(VDP, input, params.length);
ErrorHandler.processError(libvirt, result);
}
/**
* Dynamically changes the number of virtual CPUs used by this domain. Note
* that this call may fail if the underlying virtualization hypervisor does
* not support it or if growing the number is arbitrary limited. This
* function requires priviledged access to the hypervisor.
*
* @param nvcpus
* the new number of virtual CPUs for this domain
* @throws LibvirtException
*/
public void setVcpus(int nvcpus) throws LibvirtException {
int result = libvirt.virDomainSetVcpus(VDP, nvcpus);
ErrorHandler.processError(libvirt, result);
}
/**
* Shuts down this domain, the domain object is still usable there after but
* the domain OS is being stopped. Note that the guest OS may ignore the
* request. TODO: should we add an option for reboot, knowing it may not be
* doable in the general case ?
*
* @throws LibvirtException
*/
public void shutdown() throws LibvirtException {
int result = libvirt.virDomainShutdown(VDP);
ErrorHandler.processError(libvirt, result);
}
/**
* Creates a new snapshot of a domain based on the snapshot xml contained in
* xmlDesc.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainSnapshotCreateXML">Libvirt
* Documentation</a>
* @param xmlDesc
* string containing an XML description of the domain
* @return the snapshot, or null on Error
* @throws LibvirtException
*/
public DomainSnapshot snapshotCreateXML(String xmlDesc) throws LibvirtException {
DomainSnapshotPointer ptr = libvirt.virDomainSnapshotCreateXML(VDP, xmlDesc, 0);
ErrorHandler.processError(libvirt, ptr);
DomainSnapshot returnValue = null;
if (ptr != null) {
returnValue = new DomainSnapshot(virConnect, ptr);
}
return returnValue;
}
/**
* Get the current snapshot for a domain, if any.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainSnapshotCurrent">Libvirt
* Documentation</a>
* @return the snapshot, or null on Error
* @throws LibvirtException
*/
public DomainSnapshot snapshotCurrent() throws LibvirtException {
DomainSnapshotPointer ptr = libvirt.virDomainSnapshotCurrent(VDP, 0);
ErrorHandler.processError(libvirt, ptr);
DomainSnapshot returnValue = null;
if (ptr != null) {
returnValue = new DomainSnapshot(virConnect, ptr);
}
return returnValue;
}
/**
* Collect the list of domain snapshots for the given domain.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virDomainSnapshotListNames">Libvirt
* Documentation</a>
* @return The list of names, or null if an error
* @throws LibvirtException
*/
public String[] snapshotListNames() throws LibvirtException {
String[] returnValue = null;
int num = snapshotNum();
if (num >= 0) {
returnValue = new String[num];
if (num > 0) {
int result = libvirt.virDomainSnapshotListNames(VDP, returnValue, num, 0);
ErrorHandler.processError(libvirt, result);
}
}
return returnValue;
}
/**
* Retrieve a snapshot by name