This allows us to send Qemu Guest Agent commands to running domains.
Signed-off-by: Wido den Hollander <wido(a)widodh.nl>
---
src/main/java/org/libvirt/Domain.java | 36 ++++++++++++++++++++++++++
src/main/java/org/libvirt/Library.java | 3 +++
src/main/java/org/libvirt/jna/LibvirtQemu.java | 16 ++++++++++++
3 files changed, 55 insertions(+)
create mode 100644 src/main/java/org/libvirt/jna/LibvirtQemu.java
diff --git a/src/main/java/org/libvirt/Domain.java
b/src/main/java/org/libvirt/Domain.java
index 83a500c..c24df48 100644
--- a/src/main/java/org/libvirt/Domain.java
+++ b/src/main/java/org/libvirt/Domain.java
@@ -8,6 +8,7 @@ import org.libvirt.jna.CString;
import org.libvirt.jna.DomainPointer;
import org.libvirt.jna.DomainSnapshotPointer;
import org.libvirt.jna.Libvirt;
+import org.libvirt.jna.LibvirtQemu;
import org.libvirt.jna.SizeT;
import org.libvirt.jna.virDomainBlockInfo;
import org.libvirt.jna.virDomainBlockStats;
@@ -22,6 +23,7 @@ import org.libvirt.event.LifecycleListener;
import org.libvirt.event.PMWakeupListener;
import org.libvirt.event.PMSuspendListener;
import static org.libvirt.Library.libvirt;
+import static org.libvirt.Library.libvirtqemu;
import static org.libvirt.ErrorHandler.processError;
import static org.libvirt.ErrorHandler.processErrorIfZero;
@@ -141,6 +143,23 @@ public class Domain {
public static final int NO_METADATA = (1 << 4);
}
+ static final class QemuAgentFlags {
+ /**
+ * Do not wait for a result
+ */
+ public static final int VIR_DOMAIN_QEMU_AGENT_COMMAND_NOWAIT = 0;
+
+ /**
+ * Use default timeout value
+ */
+ public static final int VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT = -1;
+
+ /**
+ * Block forever waiting for a result
+ */
+ public static final int VIR_DOMAIN_QEMU_AGENT_COMMAND_BLOCK = -2;
+ }
+
/**
* the native virDomainPtr.
*/
@@ -1556,4 +1575,21 @@ public class Domain {
return processError(libvirt.virDomainUpdateDeviceFlags(VDP, xml, flags));
}
+ /**
+ * Send a Qemu Guest Agent command to a domain
+ *
+ * @param cmd
+ * The command which has to be send
+ * @param timeout
+ * The timeout for waiting on an answer. See QemuAgentFlags for more
information.
+ * @param flags
+ * Flags to be send
+ * @return String
+ * @throws LibvirtException
+ */
+ public String qemuAgentCommand(String cmd, int timeout, int flags) throws
LibvirtException {
+ CString result = libvirtqemu.virDomainQemuAgentCommand(this.VDP, cmd, timeout,
flags);
+ processError(result);
+ return result.toString();
+ }
}
diff --git a/src/main/java/org/libvirt/Library.java
b/src/main/java/org/libvirt/Library.java
index 8e054c6..30f15be 100644
--- a/src/main/java/org/libvirt/Library.java
+++ b/src/main/java/org/libvirt/Library.java
@@ -2,6 +2,7 @@ package org.libvirt;
import org.libvirt.jna.Libvirt;
import org.libvirt.jna.Libvirt.VirEventTimeoutCallback;
+import org.libvirt.jna.LibvirtQemu;
import org.libvirt.jna.CString;
import static org.libvirt.ErrorHandler.processError;
@@ -34,6 +35,7 @@ public final class Library {
};
final static Libvirt libvirt;
+ final static LibvirtQemu libvirtqemu;
// an empty string array constant
// prefer this over creating empty arrays dynamically.
@@ -47,6 +49,7 @@ public final class Library {
} catch (Exception e) {
e.printStackTrace();
}
+ libvirtqemu = LibvirtQemu.INSTANCE;
}
private Library() {}
diff --git a/src/main/java/org/libvirt/jna/LibvirtQemu.java
b/src/main/java/org/libvirt/jna/LibvirtQemu.java
new file mode 100644
index 0000000..82213e9
--- /dev/null
+++ b/src/main/java/org/libvirt/jna/LibvirtQemu.java
@@ -0,0 +1,16 @@
+package org.libvirt.jna;
+
+import com.sun.jna.Library;
+import com.sun.jna.Native;
+import com.sun.jna.Platform;
+
+/**
+ * The libvirt Qemu interface which is exposed via JNA
+ */
+
+public interface LibvirtQemu extends Library {
+
+ LibvirtQemu INSTANCE = (LibvirtQemu) Native.loadLibrary(Platform.isWindows() ?
"virt-qemu-0" : "virt-qemu", LibvirtQemu.class);
+
+ CString virDomainQemuAgentCommand(DomainPointer virDomainPtr, String cmd, int
timeout, int flags);
+}
--
1.9.1