forked from lstoll/libvirt-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.java
More file actions
175 lines (155 loc) · 4.53 KB
/
Copy pathDevice.java
File metadata and controls
175 lines (155 loc) · 4.53 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
package org.libvirt;
import org.libvirt.jna.DevicePointer;
import org.libvirt.jna.Libvirt;
/**
* A device which is attached to a node
*/
public class Device {
/**
* the native virDomainPtr.
*/
DevicePointer 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 Device object from a DevicePointer, and a Connect object.
*
* @param virConnect
* the Domain's hypervisor
* @param VDP
* the native virDomainPtr
*/
Device(Connect virConnect, DevicePointer VDP) {
this.virConnect = virConnect;
this.VDP = VDP;
libvirt = virConnect.libvirt;
}
/**
* Destroy the device object. The virtual device is removed from the host
* operating system. This function may require privileged access.
*
* @throws LibvirtException
* @returns 0 for success, -1 for failure.
*/
public int destroy() throws LibvirtException {
int success = 0;
if (VDP != null) {
success = libvirt.virNodeDeviceDestroy(VDP);
ErrorHandler.processError(libvirt, success);
VDP = null;
}
return success;
}
/**
* Dettach the node device from the node itself so that it may be assigned
* to a guest domain.
*
* @throws LibvirtException
*/
public int detach() throws LibvirtException {
int num = libvirt.virNodeDeviceDettach(VDP);
ErrorHandler.processError(libvirt, num);
return num;
}
@Override
public void finalize() throws LibvirtException {
free();
}
/**
* Frees this device 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.virNodeDeviceFree(VDP);
ErrorHandler.processError(libvirt, success);
VDP = null;
}
return success;
}
/**
* Returns the name of the device
*
* @throws LibvirtException
*/
public String getName() throws LibvirtException {
String name = libvirt.virNodeDeviceGetName(VDP);
ErrorHandler.processError(libvirt, name);
return name;
}
/**
* Returns the number of capabilities which the instance has.
*
* @throws LibvirtException
*/
public int getNumberOfCapabilities() throws LibvirtException {
int num = libvirt.virNodeDeviceNumOfCaps(VDP);
ErrorHandler.processError(libvirt, num);
return num;
}
/**
* Returns the parent of the device
*
* @throws LibvirtException
*/
public String getParent() throws LibvirtException {
String parent = libvirt.virNodeDeviceGetParent(VDP);
ErrorHandler.processError(libvirt, parent);
return parent;
}
/**
* Returns the XML description of the device
*
* @throws LibvirtException
*/
public String getXMLDescription() throws LibvirtException {
String desc = libvirt.virNodeDeviceGetXMLDesc(VDP);
ErrorHandler.processError(libvirt, desc);
return desc;
}
/**
* List the capabilities of the device
*
* @throws LibvirtException
*/
public String[] listCapabilities() throws LibvirtException {
int maxCaps = getNumberOfCapabilities();
String[] names = new String[maxCaps];
if (maxCaps > 0) {
int result = libvirt.virNodeDeviceListCaps(VDP, names, maxCaps);
ErrorHandler.processError(libvirt, result);
}
return names;
}
/**
* ReAttach a device to the node.
*
* @throws LibvirtException
*/
public int reAttach() throws LibvirtException {
int num = libvirt.virNodeDeviceReAttach(VDP);
ErrorHandler.processError(libvirt, num);
return num;
}
/**
* Reset a previously dettached node device to the node before or after
* assigning it to a guest.
*
* @throws LibvirtException
*/
public int reset() throws LibvirtException {
int num = libvirt.virNodeDeviceReset(VDP);
ErrorHandler.processError(libvirt, num);
return num;
}
}