forked from lstoll/libvirt-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect.java
More file actions
1563 lines (1451 loc) · 52 KB
/
Copy pathConnect.java
File metadata and controls
1563 lines (1451 loc) · 52 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 java.util.UUID;
import org.libvirt.jna.ConnectionPointer;
import org.libvirt.jna.DevicePointer;
import org.libvirt.jna.DomainPointer;
import org.libvirt.jna.InterfacePointer;
import org.libvirt.jna.Libvirt;
import org.libvirt.jna.NetworkFilterPointer;
import org.libvirt.jna.NetworkPointer;
import org.libvirt.jna.SecretPointer;
import org.libvirt.jna.StoragePoolPointer;
import org.libvirt.jna.StorageVolPointer;
import org.libvirt.jna.StreamPointer;
import org.libvirt.jna.virConnectAuth;
import org.libvirt.jna.virNodeInfo;
import com.sun.jna.Memory;
import com.sun.jna.NativeLong;
import com.sun.jna.ptr.LongByReference;
/**
* The Connect object represents a connection to a local or remote
* hypervisor/driver.
*
* @author stoty
*/
public class Connect {
// Load the native part
static {
int result = Libvirt.INSTANCE.virInitialize();
try {
ErrorHandler.processError(Libvirt.INSTANCE, result);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates a new connection object from the domain. If all you want is the
* existing domain's connection, use the getConnection method directly. Thie
* method returns a new connection.
*
* @param domain
* @return a new connection
*/
public static Connect connectionForDomain(Domain domain) {
ConnectionPointer conn = Libvirt.INSTANCE.virDomainGetConnect(domain.VDP);
return new Connect(conn);
}
/**
* Creates a new connection object from the network. If all you want is the
* existing network's connection, use the getConnection method directly.
* Thie method returns a new connection.
*
* @param network
* @return a new connection
*/
public static Connect connectionForNetwork(Network network) {
ConnectionPointer conn = Libvirt.INSTANCE.virNetworkGetConnect(network.VNP);
return new Connect(conn);
}
/**
* Creates a new connection object from the network. If all you want is the
* existing network's connection, use the getConnection method directly.
* Thie method returns a new connection.
*
* @param network
* @return a new connection
*/
public static Connect connectionForSecret(Secret secret) {
ConnectionPointer conn = Libvirt.INSTANCE.virSecretGetConnect(secret.VSP);
return new Connect(conn);
}
/**
* Get the version of a connection.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectGetLibVersion">Libvirt
* Documentation</a>
* @param conn
* the connection to use.
* @return -1 in case of failure, versions have the format major * 1,000,000
* + minor * 1,000 + release.
*/
public static long connectionVersion(Connect conn) {
LongByReference libVer = new LongByReference();
int result = Libvirt.INSTANCE.virConnectGetLibVersion(conn.VCP, libVer);
return result != -1 ? libVer.getValue() : -1;
}
/**
* Helper function to convert bytes into ints for the UUID calls
*/
public static int[] convertUUIDBytes(byte bytes[]) {
int[] returnValue = new int[Libvirt.VIR_UUID_BUFLEN];
for (int x = 0; x < Libvirt.VIR_UUID_BUFLEN; x++) {
// For some reason, the higher bytes come back wierd.
// We only want the lower 2 bytes.
returnValue[x] = (bytes[x] & 255);
}
return returnValue;
}
/**
* Helper function to convert UUIDs into a stirng for the UUID calls
*/
public static byte[] createUUIDBytes(int[] UUID) {
byte[] bytes = new byte[Libvirt.VIR_UUID_BUFLEN];
for (int x = 0; x < Libvirt.VIR_UUID_BUFLEN; x++) {
bytes[x] = (byte) UUID[x];
}
return bytes;
}
/**
* Sets the error function to a user defined callback
*
* @param callback
* a Class to perform the callback
*/
public static void setErrorCallback(Libvirt.VirErrorCallback callback) throws LibvirtException {
Libvirt.INSTANCE.virSetErrorFunc(null, callback);
}
/**
* The native virConnectPtr.
*/
protected ConnectionPointer VCP;
/**
* The libvirt library
*/
Libvirt libvirt = Libvirt.INSTANCE;
/**
* Protected constructor to return a Connection with ConnectionPointer
*/
Connect(ConnectionPointer ptr) {
VCP = ptr;
}
/**
* Construct a Connect object from a known native virConnectPtr For use when
* native libvirt returns a virConnectPtr, i.e. error handling.
*
* @param VCP
* the virConnectPtr pointing to an existing native virConnect
* structure
*/
@Deprecated
Connect(long VCP) {
throw new RuntimeException("No longer supported");
}
/**
* Constructs a read-write Connect object from the supplied URI.
*
* @param uri
* The connection URI
* @throws LibvirtException
* @see <a href="http://libvirt.org/uri.html">The URI documentation</a>
*/
public Connect(String uri) throws LibvirtException {
VCP = libvirt.virConnectOpen(uri);
// Check for an error
ErrorHandler.processError(libvirt, VCP);
}
/**
* Constructs a Connect object from the supplied URI.
*
* @param uri
* The connection URI
* @param readOnly
* Whether the connection is read-only
* @throws LibvirtException
* @see <a href="http://libvirt.org/uri.html">The URI documentation</a>
*/
public Connect(String uri, boolean readOnly) throws LibvirtException {
if (readOnly) {
VCP = libvirt.virConnectOpenReadOnly(uri);
} else {
VCP = libvirt.virConnectOpen(uri);
}
// Check for an error
ErrorHandler.processError(libvirt, VCP);
}
/**
* Constructs a Connect object from the supplied URI, using the supplied
* authentication callback
*
* @param uri
* The connection URI
* @param auth
* a ConnectAuth object
* @param flags
* @throws LibvirtException
* @see <a href="http://libvirt.org/uri.html">The URI documentation</a>
*/
public Connect(String uri, ConnectAuth auth, int flags) throws LibvirtException {
virConnectAuth vAuth = new virConnectAuth();
vAuth.cb = auth;
vAuth.cbdata = null;
vAuth.ncredtype = auth.credType.length;
int[] authInts = new int[vAuth.ncredtype];
for (int x = 0; x < vAuth.ncredtype; x++) {
authInts[x] = auth.credType[x].mapToInt();
}
Memory mem = new Memory(4 * vAuth.ncredtype);
mem.write(0, authInts, 0, vAuth.ncredtype);
vAuth.credtype = mem.share(0);
VCP = libvirt.virConnectOpenAuth(uri, vAuth, flags);
ErrorHandler.processError(Libvirt.INSTANCE, VCP);
}
/**
* Computes the most feature-rich CPU which is compatible with all given
* host CPUs.
*
* @param xmlCPUs
* array of XML descriptions of host CPUs
* @return XML description of the computed CPU or NULL on error.
* @throws LibvirtException
*/
public String baselineCPU(String[] xmlCPUs) throws LibvirtException {
String returnValue = libvirt.virConnectBaselineCPU(VCP, xmlCPUs, xmlCPUs.length, 0);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Closes the connection to the hypervisor/driver. Calling any methods on
* the object after close() will result in an exception.
*
* @throws LibvirtException
* @return number of references left (>= 0) for success, -1 for failure.
*/
public int close() throws LibvirtException {
int success = 0;
if (VCP != null) {
success = libvirt.virConnectClose(VCP);
ErrorHandler.processError(libvirt, success);
// If leave an invalid pointer dangling around JVM crashes and burns
// if someone tries to call a method on us
// We rely on the underlying libvirt error handling to detect that
// it's called with a null virConnectPointer
VCP = null;
}
return success;
}
/**
* Compares the given CPU description with the host CPU
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectCompareCPU">Libvirt
* Documentation</a>
* @param xmlDesc
* @return comparison result according to enum CPUCompareResult
* @throws LibvirtException
*/
public CPUCompareResult compareCPU(String xmlDesc) throws LibvirtException {
int rawResult = libvirt.virConnectCompareCPU(VCP, xmlDesc, 0);
ErrorHandler.processError(libvirt, rawResult);
return CPUCompareResult.get(rawResult);
}
/**
* Create a new device on the VM host machine, for example, virtual HBAs
* created using vport_create.
*
* @param xmlDesc
* the device to create
* @return the Device object
* @throws LibvirtException
*/
public Device deviceCreateXML(String xmlDesc) throws LibvirtException {
Device returnValue = null;
DevicePointer ptr = libvirt.virNodeDeviceCreateXML(VCP, xmlDesc, 0);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Device(this, ptr);
}
return returnValue;
}
/**
* Fetch a device based on its unique name
*
* @param name
* name of device to fetch
* @return Device object
* @throws LibvirtException
*/
public Device deviceLookupByName(String name) throws LibvirtException {
DevicePointer ptr = libvirt.virNodeDeviceLookupByName(VCP, name);
ErrorHandler.processError(libvirt, ptr);
return new Device(this, ptr);
}
/**
* Launches a new Linux guest domain. The domain is based on an XML
* description similar to the one returned by virDomainGetXMLDesc(). This
* function may require priviledged access to the hypervisor.
*
* @param xmlDesc
* the Domain description in XML
* @param flags
* an optional set of flags (unused)
* @return the Domain object
* @throws LibvirtException
* @see <a href="http://libvirt.org/format.html#Normal1" > The XML format
* description </a>
*/
public Domain domainCreateLinux(String xmlDesc, int flags) throws LibvirtException {
Domain returnValue = null;
DomainPointer ptr = libvirt.virDomainCreateLinux(VCP, xmlDesc, flags);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Domain(this, ptr);
}
return returnValue;
}
/**
* Launch a new guest domain, based on an XML description
*
* @param xmlDesc
* @return the Domain object
* @throws LibvirtException
* @see <a href="http://libvirt.org/format.html#Normal1" > The XML format
* description </a>
*/
public Domain domainCreateXML(String xmlDesc, int flags) throws LibvirtException {
Domain returnValue = null;
DomainPointer ptr = libvirt.virDomainCreateXML(VCP, xmlDesc, flags);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Domain(this, ptr);
}
return returnValue;
}
/**
* Defines a domain, but does not start it
*
* @param xmlDesc
* @return the Domain object
* @throws LibvirtException
* @see <a href="http://libvirt.org/format.html#Normal1" > The XML format
* description </a>
*/
public Domain domainDefineXML(String xmlDesc) throws LibvirtException {
Domain returnValue = null;
DomainPointer ptr = libvirt.virDomainDefineXML(VCP, xmlDesc);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Domain(this, ptr);
}
return returnValue;
}
/**
* Removes an event callback.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectDomainEventDeregisterAny">Libvirt
* Documentation</a>
* @param callbackID
* the callback to deregister
* @return
* @throws LibvirtException
*/
public int domainEventDeregisterAny(int callbackID) throws LibvirtException {
int returnValue = libvirt.virConnectDomainEventDeregisterAny(VCP, callbackID);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Adds a callback to receive notifications of arbitrary domain events
* occurring on a domain.
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectDomainEventRegisterAny">Libvirt
* Documentation</a>
* @param domain
* option domain to limit the events monitored
* @param eventId
* the events to monitor
* @param cb
* the callback function to use.
* @return . The return value from this method is a positive integer
* identifier for the callback. -1 if an error
* @throws LibvirtException
*/
public int domainEventRegisterAny(Domain domain, int eventId, Libvirt.VirConnectDomainEventGenericCallback cb)
throws LibvirtException {
DomainPointer ptr = domain == null ? null : domain.VDP;
int returnValue = libvirt.virConnectDomainEventRegisterAny(VCP, ptr, eventId, cb, null, null);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Finds a domain based on the hypervisor ID number.
*
* @param id
* the hypervisor id
* @return the Domain object
* @throws LibvirtException
*/
public Domain domainLookupByID(int id) throws LibvirtException {
Domain returnValue = null;
DomainPointer ptr = libvirt.virDomainLookupByID(VCP, id);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Domain(this, ptr);
}
return returnValue;
}
/**
* Looks up a domain based on its name.
*
* @param name
* the name of the domain
* @return the Domain object
* @throws LibvirtException
*/
public Domain domainLookupByName(String name) throws LibvirtException {
Domain returnValue = null;
DomainPointer ptr = libvirt.virDomainLookupByName(VCP, name);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Domain(this, ptr);
}
return returnValue;
}
/**
* Looks up a domain based on its UUID in array form. The UUID Array
* contains an unpacked representation of the UUID, each int contains only
* one byte.
*
* @param UUID
* the UUID as an unpacked int array
* @return the Domain object
* @throws LibvirtException
*/
public Domain domainLookupByUUID(int[] UUID) throws LibvirtException {
byte[] uuidBytes = Connect.createUUIDBytes(UUID);
Domain returnValue = null;
DomainPointer ptr = libvirt.virDomainLookupByUUID(VCP, uuidBytes);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Domain(this, ptr);
}
return returnValue;
}
/**
* Fetch a domain based on its globally unique id
*
* @param UUID
* a java UUID
* @return a new domain object
* @throws LibvirtException
*/
public Domain domainLookupByUUID(UUID uuid) throws LibvirtException {
return domainLookupByUUIDString(uuid.toString());
}
/**
* Looks up a domain based on its UUID in String form.
*
* @param UUID
* the UUID in canonical String representation
* @return the Domain object
* @throws LibvirtException
*/
public Domain domainLookupByUUIDString(String UUID) throws LibvirtException {
Domain returnValue = null;
DomainPointer ptr = libvirt.virDomainLookupByUUIDString(VCP, UUID);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Domain(this, ptr);
}
return returnValue;
}
/**
* Reads a native XML configuration document, and generates generates a
* domain configuration file describing the domain. The format of the native
* data is hypervisor dependant.
*
* @return
* @throws LibvirtException
*/
public String domainXMLFromNative(String nativeFormat, String nativeConfig, int flags) throws LibvirtException {
String returnValue = libvirt.virConnectDomainXMLFromNative(VCP, nativeFormat, nativeConfig, 0);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Reads a domain XML configuration document, and generates generates a
* native configuration file describing the domain. The format of the native
* data is hypervisor dependant.
*
* @return
* @throws LibvirtException
*/
public String domainXMLToNative(String nativeFormat, String domainXML, int flags) throws LibvirtException {
String returnValue = libvirt.virConnectDomainXMLToNative(VCP, nativeFormat, domainXML, 0);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
@Override
public void finalize() throws LibvirtException {
close();
}
/**
* Talks to a storage backend and attempts to auto-discover the set of
* available storage pool sources. e.g. For iSCSI this would be a set of
* iSCSI targets. For NFS this would be a list of exported paths. The
* srcSpec (optional for some storage pool types, e.g. local ones) is an
* instance of the storage pool's source element specifying where to
* look for the pools. srcSpec is not required for some types (e.g., those
* querying local storage resources only)
*
* @param type
* type of storage pool to discover
* @param srcSpecs
* XML document specifying discovery sourc
* @param flags
* unused
* @return an xml document consisting of a SourceList element containing a
* source document appropriate to the given pool type for each
* discovered source.
* @throws LibvirtException
*/
public String findStoragePoolSources(String type, String srcSpecs, int flags) throws LibvirtException {
String returnValue = libvirt.virConnectFindStoragePoolSources(VCP, type, srcSpecs, flags);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Provides capabilities of the hypervisor / driver.
*
* @return an XML String describing the capabilities.
* @throws LibvirtException
* @see <a href="http://libvirt.org/format.html#Capa1" >The XML format
* description</a>
*/
public String getCapabilities() throws LibvirtException {
String returnValue = libvirt.virConnectGetCapabilities(VCP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* NUMA Support
*/
public long getCellsFreeMemory(int startCells, int maxCells) throws LibvirtException {
LongByReference returnValue = new LongByReference();
libvirt.virNodeGetCellsFreeMemory(VCP, returnValue, startCells, maxCells);
ErrorHandler.processError(libvirt, returnValue);
return returnValue.getValue();
}
/**
* Returns the free memory for the connection
*/
public long getFreeMemory() throws LibvirtException {
long returnValue = 0;
returnValue = libvirt.virNodeGetFreeMemory(VCP);
ErrorHandler.processError(libvirt, returnValue - 1); // 0 is an error for this case
return returnValue;
}
/**
* Returns the system hostname on which the hypervisor is running. (the
* result of the gethostname(2) system call) If we are connected to a remote
* system, then this returns the hostname of the remote system.
*
* @return the hostname
* @throws LibvirtException
*/
public String getHostName() throws LibvirtException {
String returnValue = libvirt.virConnectGetHostname(VCP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Returns the version of the hypervisor against which the library was
* compiled. The type parameter specified which hypervisor's version is
* returned
*
* @param type
* @return major * 1,000,000 + minor * 1,000 + release
* @throws LibvirtException
*/
public long getHypervisorVersion(String type) throws LibvirtException {
LongByReference libVer = new LongByReference();
LongByReference typeVer = new LongByReference();
int result = libvirt.virGetVersion(libVer, type, typeVer);
ErrorHandler.processError(libvirt, result);
return libVer.getValue();
}
/**
* Gets the version of the native libvirt library that the JNI part is
* linked to.
*
* @return major * 1,000,000 + minor * 1,000 + release
* @throws LibvirtException
*/
public long getLibVirVersion() throws LibvirtException {
LongByReference libVer = new LongByReference();
LongByReference typeVer = new LongByReference();
int result = libvirt.virGetVersion(libVer, null, typeVer);
ErrorHandler.processError(libvirt, result);
return libVer.getValue();
}
/**
* Provides the maximum number of virtual CPUs supported for a guest VM of a
* specific type. The 'type' parameter here corresponds to the 'type'
* attribute in the <domain> element of the XML.
*
* @param type
* @return the number of CPUs
* @throws LibvirtException
*/
public int getMaxVcpus(String type) throws LibvirtException {
int returnValue = libvirt.virConnectGetMaxVcpus(VCP, type);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Gets the name of the Hypervisor software used.
*
* @return the name
* @throws LibvirtException
*/
public String getType() throws LibvirtException {
String returnValue = libvirt.virConnectGetType(VCP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Returns the URI (name) of the hypervisor connection. Normally this is the
* same as or similar to the string passed to the
* virConnectOpen/virConnectOpenReadOnly call, but the driver may make the
* URI canonical.
*
* @return the URI
* @throws LibvirtException
*/
public String getURI() throws LibvirtException {
String returnValue = libvirt.virConnectGetURI(VCP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Gets the version level of the Hypervisor running. This may work only with
* hypervisor call, i.e. with priviledged access to the hypervisor, not with
* a Read-Only connection. If the version can't be extracted by lack of
* capacities returns 0.
*
* @return major * 1,000,000 + minor * 1,000 + release
* @throws LibvirtException
*/
public long getVersion() throws LibvirtException {
LongByReference hvVer = new LongByReference();
int result = libvirt.virConnectGetVersion(VCP, hvVer);
ErrorHandler.processError(libvirt, result);
return hvVer.getValue();
}
/**
* Define an interface (or modify existing interface configuration)
*
* @param xmlDesc
* the interface to create
* @return the Interface object
* @throws LibvirtException
*/
public Interface interfaceDefineXML(String xmlDesc) throws LibvirtException {
Interface returnValue = null;
InterfacePointer ptr = libvirt.virInterfaceDefineXML(VCP, xmlDesc, 0);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Interface(this, ptr);
}
return returnValue;
}
/**
* Try to lookup an interface on the given hypervisor based on its MAC.
*
* @throws LibvirtException
*/
public Interface interfaceLookupByMACString(String mac) throws LibvirtException {
Interface returnValue = null;
InterfacePointer ptr = libvirt.virInterfaceLookupByMACString(VCP, mac);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Interface(this, ptr);
}
return returnValue;
}
/**
* Try to lookup an interface on the given hypervisor based on its name.
*
* @throws LibvirtException
*/
public Interface interfaceLookupByName(String name) throws LibvirtException {
Interface returnValue = null;
InterfacePointer ptr = libvirt.virInterfaceLookupByName(VCP, name);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Interface(this, ptr);
}
return returnValue;
}
/**
* Determine if the connection is encrypted
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectIsEncrypted">Libvirt
* Documentation</a>
* @return 1 if encrypted, 0 if not encrypted, -1 on error
* @throws LibvirtException
*/
public int isEncrypted() throws LibvirtException {
int returnValue = libvirt.virConnectIsEncrypted(VCP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Determine if the connection is secure
*
* @see <a
* href="http://www.libvirt.org/html/libvirt-libvirt.html#virConnectIsSecure">Libvirt
* Documentation</a>
* @return 1 if secure, 0 if not secure, -1 on error
* @throws LibvirtException
*/
public int isSecure() throws LibvirtException {
int returnValue = libvirt.virConnectIsSecure(VCP);
ErrorHandler.processError(libvirt, returnValue);
return returnValue;
}
/**
* Lists the names of the defined but inactive domains
*
* @return an Array of Strings that contains the names of the defined
* domains currently inactive
* @throws LibvirtException
*/
public String[] listDefinedDomains() throws LibvirtException {
int maxnames = numOfDefinedDomains();
String[] names = new String[maxnames];
if (maxnames > 0) {
int result = libvirt.virConnectListDefinedDomains(VCP, names, maxnames);
ErrorHandler.processError(libvirt, result);
}
return names;
}
/**
* Provides the list of names of defined interfaces on this host
*
* @return an Array of Strings that contains the names of the interfaces on
* this host
* @throws LibvirtException
*/
public String[] listDefinedInterfaces() throws LibvirtException {
int num = numOfDefinedInterfaces();
String[] returnValue = new String[num];
if (num > 0) {
int result = libvirt.virConnectListDefinedInterfaces(VCP, returnValue, num);
ErrorHandler.processError(libvirt, result);
}
return returnValue;
}
/**
* Lists the inactive networks
*
* @return an Array of Strings that contains the names of the inactive
* networks
* @throws LibvirtException
*/
public String[] listDefinedNetworks() throws LibvirtException {
int maxnames = numOfDefinedNetworks();
String[] names = new String[maxnames];
if (maxnames > 0) {
int result = libvirt.virConnectListDefinedNetworks(VCP, names, maxnames);
ErrorHandler.processError(libvirt, result);
}
return names;
}
/**
* Provides the list of names of inactive storage pools.
*
* @return an Array of Strings that contains the names of the defined
* storage pools
* @throws LibvirtException
*/
public String[] listDefinedStoragePools() throws LibvirtException {
int num = numOfDefinedStoragePools();
String[] returnValue = new String[num];
int result = libvirt.virConnectListDefinedStoragePools(VCP, returnValue, num);
ErrorHandler.processError(libvirt, result);
return returnValue;
}
/**
* List the names of the devices on this node
*
* @param capabilityName
* optional capability name
*/
public String[] listDevices(String capabilityName) throws LibvirtException {
int maxDevices = numOfDevices(capabilityName);
String[] names = new String[maxDevices];
if (maxDevices > 0) {
int result = libvirt.virNodeListDevices(VCP, capabilityName, names, maxDevices, 0);
ErrorHandler.processError(libvirt, result);
}
return names;
}
/**
* Lists the active domains.
*
* @return and array of the IDs of the active domains
* @throws LibvirtException
*/
public int[] listDomains() throws LibvirtException {
int maxids = numOfDomains();
int[] ids = new int[maxids];
if (maxids > 0) {
int result = libvirt.virConnectListDomains(VCP, ids, maxids);
ErrorHandler.processError(libvirt, result);
}
return ids;
}
/**
* Provides the list of names of interfaces on this host
*
* @return an Array of Strings that contains the names of the interfaces on
* this host
* @throws LibvirtException
*/
public String[] listInterfaces() throws LibvirtException {
int num = numOfInterfaces();
String[] returnValue = new String[num];
if (num > 0) {
int result = libvirt.virConnectListInterfaces(VCP, returnValue, num);
ErrorHandler.processError(libvirt, result);
}
return returnValue;
}
/**
* Lists the names of the network filters
*
* @return an Array of Strings that contains the names network filters
* @throws LibvirtException
*/
public String[] listNetworkFilters() throws LibvirtException {
int maxnames = numOfNetworkFilters();
String[] names = new String[maxnames];
if (maxnames > 0) {
int result = libvirt.virConnectListNWFilters(VCP, names, maxnames);
ErrorHandler.processError(libvirt, result);
}
return names;
}
/**
* Lists the active networks.
*
* @return an Array of Strings that contains the names of the active
* networks
* @throws LibvirtException
*/
public String[] listNetworks() throws LibvirtException {
int maxnames = numOfNetworks();
String[] names = new String[maxnames];
if (maxnames > 0) {
int result = libvirt.virConnectListNetworks(VCP, names, maxnames);
ErrorHandler.processError(libvirt, result);
}
return names;
}
/**
* Retrieve the List UUIDs of defined secrets
*
* @return an Array of Strings that contains the uuids of the defined
* secrets
*/
public String[] listSecrets() throws LibvirtException {
int num = numOfSecrets();
String[] returnValue = new String[num];
int result = libvirt.virConnectListSecrets(VCP, returnValue, num);
ErrorHandler.processError(libvirt, result);
return returnValue;
}
/**
* Provides the list of names of active storage pools.
*
* @return an Array of Strings that contains the names of the defined
* storage pools
* @throws LibvirtException
*/
public String[] listStoragePools() throws LibvirtException {
int num = numOfStoragePools();
String[] returnValue = new String[num];
int result = libvirt.virConnectListStoragePools(VCP, returnValue, num);
ErrorHandler.processError(libvirt, result);
return returnValue;
}
/**
* Creates and starts a new virtual network. The properties of the network
* are based on an XML description similar to the one returned by
* virNetworkGetXMLDesc()
*
* @param xmlDesc
* the Network Description
* @return the Network object representing the created network
* @throws LibvirtException
* @see <a href="http://libvirt.org/format.html#Net1" >The XML format
* description</a>
*/
public Network networkCreateXML(String xmlDesc) throws LibvirtException {
Network returnValue = null;
NetworkPointer ptr = libvirt.virNetworkCreateXML(VCP, xmlDesc);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Network(this, ptr);
}
return returnValue;
}
/**
* Defines a network, but does not create it. The properties of the network
* are based on an XML description similar to the one returned by
* virNetworkGetXMLDesc()
*
* @param xmlDesc
* @return the resulting Network object
* @throws LibvirtException
* @see <a href="http://libvirt.org/format.html#Net1" >The XML format
* description</a>
*/
public Network networkDefineXML(String xmlDesc) throws LibvirtException {
Network returnValue = null;
NetworkPointer ptr = libvirt.virNetworkDefineXML(VCP, xmlDesc);
ErrorHandler.processError(libvirt, ptr);
if (ptr != null) {
returnValue = new Network(this, ptr);
}
return returnValue;
}