[libvirt-java] [PATCH 51/65] Implement Domain.blockPeek

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This wraps the virDomainBlockPeek libvirt function, available since
version 0.4.5.

Signed-off-by: Claudio Bley <cbley@xxxxxxxxxx>
---
 src/main/java/org/libvirt/Domain.java      |   53 ++++++++++++++++++++++++++++
 src/main/java/org/libvirt/jna/Libvirt.java |    4 +++
 2 files changed, 57 insertions(+)

diff --git a/src/main/java/org/libvirt/Domain.java b/src/main/java/org/libvirt/Domain.java
index f3fffdd..f57460f 100644
--- a/src/main/java/org/libvirt/Domain.java
+++ b/src/main/java/org/libvirt/Domain.java
@@ -1,5 +1,7 @@
 package org.libvirt;
 
+import java.nio.ByteBuffer;
+
 import org.libvirt.event.IOErrorListener;
 import org.libvirt.jna.DomainPointer;
 import org.libvirt.jna.DomainSnapshotPointer;
@@ -280,6 +282,57 @@ public class Domain {
     }
 
     /**
+     * Read the contents of a domain's disk device.
+     * <p>
+     * Typical uses for this are to determine if the domain has
+     * written a Master Boot Record (indicating that the domain has
+     * completed installation), or to try to work out the state of the
+     * domain's filesystems.
+     * <p>
+     * (Note that in the local case you might try to open the block
+     * device or file directly, but that won't work in the remote
+     * case, nor if you don't have sufficient permission. Hence the
+     * need for this call).
+     * <p>
+     * The disk parameter can either be an unambiguous source name of
+     * the block device (the {@code <source file='...'/>} sub-element,
+     * such as "/path/to/image"), or <em>(since 0.9.5)</em> the device
+     * target shorthand (the {@code <target dev='...'/>} sub-element,
+     * such as "xvda").
+     * <p>
+     * Valid names can be found by calling {@link #getXMLDesc} and
+     * inspecting elements within {@code //domain/devices/disk}.
+     * <p>
+     * This method always reads the number of bytes remaining in the
+     * buffer, that is, {@code buffer.remaining()} at the moment this
+     * method is invoked. Upon return the buffer's position will be
+     * equal to the limit, the limit itself will not have changed.
+     *
+     * @param  disk    the path to the block device, or device shorthand
+     * @param  offset  the offset within block device
+     * @param  buffer  the buffer receiving the data
+     */
+    public void blockPeek(String disk, long offset, ByteBuffer buffer) throws LibvirtException {
+        SizeT size = new SizeT();
+
+        // older libvirt has a limitation on the size of data
+        // transferred per request in the remote driver. So, split
+        // larger requests into 64K blocks.
+
+        do {
+            final int req = Math.min(65536, buffer.remaining());
+
+            size.setValue(req);
+
+            processError(libvirt.virDomainBlockPeek(this.VDP, disk, offset, size, buffer, 0));
+
+            buffer.position(buffer.position() + req);
+        } while (buffer.hasRemaining());
+
+        assert buffer.position() == buffer.limit();
+    }
+
+    /**
      * 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
diff --git a/src/main/java/org/libvirt/jna/Libvirt.java b/src/main/java/org/libvirt/jna/Libvirt.java
index 3bd63e1..0452246 100644
--- a/src/main/java/org/libvirt/jna/Libvirt.java
+++ b/src/main/java/org/libvirt/jna/Libvirt.java
@@ -1,5 +1,7 @@
 package org.libvirt.jna;
 
+import java.nio.ByteBuffer;
+
 import com.sun.jna.Callback;
 import com.sun.jna.Library;
 import com.sun.jna.Native;
@@ -243,6 +245,8 @@ public interface Libvirt extends Library {
     int virDomainAbortJob(DomainPointer virDomainPtr);
     int virDomainAttachDevice(DomainPointer virDomainPtr, String deviceXML);
     int virDomainAttachDeviceFlags(DomainPointer virDomainPtr, String deviceXML, int flags);
+    int virDomainBlockPeek(DomainPointer virDomainPtr, String disk, long offset, SizeT size,
+                           ByteBuffer buffer, int flags);
     int virDomainBlockStats(DomainPointer virDomainPtr, String path, virDomainBlockStats stats, SizeT size);
     int virDomainBlockResize(DomainPointer virDomainPtr, String disk, long size, int flags);
     int virDomainCoreDump(DomainPointer virDomainPtr, String to, int flags);
-- 
1.7.9.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]