]> xenbits.xensource.com Git - libvirt.git/commitdiff
Query block allocation extent from QEMU monitor
authorDaniel P. Berrange <berrange@redhat.com>
Fri, 14 May 2010 13:10:01 +0000 (09:10 -0400)
committerEric Blake <eblake@redhat.com>
Mon, 24 May 2010 21:55:51 +0000 (15:55 -0600)
The virDomainGetBlockInfo API allows query physical block
extent and allocated block extent. These are normally the
same value unless storing a special format like qcow2
inside a block device. In this scenario we can query QEMU
to get the actual allocated extent.

Since last time:

 - Return fatal error in text monitor
 - Only invoke monitor command for block devices
 - Fix error handling JSON code

* src/qemu/qemu_driver.c: Fill in block aloction extent when VM
  is running
* src/qemu/qemu_monitor.c, src/qemu/qemu_monitor.h,
  src/qemu/qemu_monitor_json.c, src/qemu/qemu_monitor_json.h,
  src/qemu/qemu_monitor_text.c, src/qemu/qemu_monitor_text.h: Add
  API to query the highest block extent via info blockstats

src/qemu/qemu_driver.c
src/qemu/qemu_monitor.c
src/qemu/qemu_monitor.h
src/qemu/qemu_monitor_json.c
src/qemu/qemu_monitor_json.h
src/qemu/qemu_monitor_text.c
src/qemu/qemu_monitor_text.h

index 28860c9dd362e62a0aa8f4a86330953c30aefdfe..6da538964de1d9e5220e37d268a3098ffc833f37 100644 (file)
@@ -9453,6 +9453,7 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
     int fd = -1;
     off_t end;
     virStorageFileMetadata meta;
+    virDomainDiskDefPtr disk = NULL;
     struct stat sb;
     int i;
 
@@ -9479,19 +9480,17 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
     for (i = 0 ; i < vm->def->ndisks ; i++) {
         if (vm->def->disks[i]->src != NULL &&
             STREQ (vm->def->disks[i]->src, path)) {
-            ret = 0;
+            disk = vm->def->disks[i];
             break;
         }
     }
 
-    if (ret != 0) {
+    if (!disk) {
         qemuReportError(VIR_ERR_INVALID_ARG,
                         _("invalid path %s not assigned to domain"), path);
         goto cleanup;
     }
 
-    ret = -1;
-
     /* The path is correct, now try to open it and get its size. */
     fd = open (path, O_RDONLY);
     if (fd == -1) {
@@ -9541,11 +9540,31 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
     if (meta.capacity)
         info->capacity = meta.capacity;
 
-    /* XXX allocation will need to be pulled from QEMU for
-     * the qcow inside LVM case */
+    /* Set default value .. */
     info->allocation = info->physical;
 
-    ret = 0;
+    /* ..but if guest is running & not using raw
+       disk format and on a block device, then query
+       highest allocated extent from QEMU */
+    if (virDomainObjIsActive(vm) &&
+        disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK &&
+        meta.format != VIR_STORAGE_FILE_RAW &&
+        S_ISBLK(sb.st_mode)) {
+        qemuDomainObjPrivatePtr priv = vm->privateData;
+        if (qemuDomainObjBeginJob(vm) < 0)
+            goto cleanup;
+
+        qemuDomainObjEnterMonitor(vm);
+        ret = qemuMonitorGetBlockExtent(priv->mon,
+                                        disk->info.alias,
+                                        &info->allocation);
+        qemuDomainObjExitMonitor(vm);
+
+        if (qemuDomainObjEndJob(vm) == 0)
+            vm = NULL;
+    } else {
+        ret = 0;
+    }
 
 cleanup:
     if (fd != -1)
index b1254a119ae16e0c67d26ab83c87541d1b1a1cc6..3e18ac855b4624c95e9fe3f6a02a191d88e31811 100644 (file)
@@ -1060,6 +1060,22 @@ int qemuMonitorGetBlockStatsInfo(qemuMonitorPtr mon,
     return ret;
 }
 
+int qemuMonitorGetBlockExtent(qemuMonitorPtr mon,
+                              const char *devname,
+                              unsigned long long *extent)
+{
+    int ret;
+    DEBUG("mon=%p, fd=%d, devname=%p",
+          mon, mon->fd, devname);
+
+    if (mon->json)
+        ret = qemuMonitorJSONGetBlockExtent(mon, devname, extent);
+    else
+        ret = qemuMonitorTextGetBlockExtent(mon, devname, extent);
+
+    return ret;
+}
+
 
 int qemuMonitorSetVNCPassword(qemuMonitorPtr mon,
                               const char *password)
index ecbf655fd17ee1e6312cfd3e6ef0e77de91d6940..1870b22440f2cf936e60b153b25daf5716e4ed87 100644 (file)
@@ -185,6 +185,10 @@ int qemuMonitorGetBlockStatsInfo(qemuMonitorPtr mon,
                                  long long *wr_bytes,
                                  long long *errs);
 
+int qemuMonitorGetBlockExtent(qemuMonitorPtr mon,
+                              const char *devname,
+                              unsigned long long *extent);
+
 
 int qemuMonitorSetVNCPassword(qemuMonitorPtr mon,
                               const char *password);
index 3c0a82365f31d396e2f13aa56741f9b0e62441be..f56bcdcab0d2f9bfa7ecfe42253d8706b57abb8e 100644 (file)
@@ -1143,6 +1143,100 @@ cleanup:
 }
 
 
+int qemuMonitorJSONGetBlockExtent(qemuMonitorPtr mon,
+                                  const char *devname,
+                                  unsigned long long *extent)
+{
+    int ret = -1;
+    int i;
+    int found = 0;
+    virJSONValuePtr cmd = qemuMonitorJSONMakeCommand("query-blockstats",
+                                                     NULL);
+    virJSONValuePtr reply = NULL;
+    virJSONValuePtr devices;
+
+    *extent = 0;
+
+    if (!cmd)
+        return -1;
+
+    if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
+        goto cleanup;
+
+    if (qemuMonitorJSONCheckError(cmd, reply) < 0)
+        goto cleanup;
+
+    devices = virJSONValueObjectGet(reply, "return");
+    if (!devices || devices->type != VIR_JSON_TYPE_ARRAY) {
+        qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                        _("blockstats reply was missing device list"));
+        goto cleanup;
+    }
+
+    for (i = 0 ; i < virJSONValueArraySize(devices) ; i++) {
+        virJSONValuePtr dev = virJSONValueArrayGet(devices, i);
+        virJSONValuePtr stats;
+        virJSONValuePtr parent;
+        const char *thisdev;
+        if (!dev || dev->type != VIR_JSON_TYPE_OBJECT) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                            _("blockstats device entry was not in expected format"));
+            goto cleanup;
+        }
+
+        if ((thisdev = virJSONValueObjectGetString(dev, "device")) == NULL) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                            _("blockstats device entry was not in expected format"));
+            goto cleanup;
+        }
+
+        /* New QEMU has separate names for host & guest side of the disk
+         * and libvirt gives the host side a 'drive-' prefix. The passed
+         * in devname is the guest side though
+         */
+        if (STRPREFIX(thisdev, QEMU_DRIVE_HOST_PREFIX))
+            thisdev += strlen(QEMU_DRIVE_HOST_PREFIX);
+
+        if (STRNEQ(thisdev, devname))
+            continue;
+
+        found = 1;
+        if ((parent = virJSONValueObjectGet(dev, "parent")) == NULL ||
+            parent->type != VIR_JSON_TYPE_OBJECT) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                            _("blockstats parent entry was not in expected format"));
+            goto cleanup;
+        }
+
+        if ((stats = virJSONValueObjectGet(parent, "stats")) == NULL ||
+            stats->type != VIR_JSON_TYPE_OBJECT) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                            _("blockstats stats entry was not in expected format"));
+            goto cleanup;
+        }
+
+        if (virJSONValueObjectGetNumberUlong(stats, "wr_highest_offset", extent) < 0) {
+            qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                            _("cannot read %s statistic"),
+                            "wr_highest_offset");
+            goto cleanup;
+        }
+    }
+
+    if (!found) {
+        qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                        _("cannot find statistics for device '%s'"), devname);
+        goto cleanup;
+    }
+    ret = 0;
+
+cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
+
+
 int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon,
                                   const char *password)
 {
index 9948fd8612cb277b8830e6cf52a704cc68e80936..c57821132e2b7cbac593a89e6a6f59021a829dae 100644 (file)
@@ -56,6 +56,9 @@ int qemuMonitorJSONGetBlockStatsInfo(qemuMonitorPtr mon,
                                      long long *wr_req,
                                      long long *wr_bytes,
                                      long long *errs);
+int qemuMonitorJSONGetBlockExtent(qemuMonitorPtr mon,
+                                  const char *devname,
+                                  unsigned long long *extent);
 
 
 int qemuMonitorJSONSetVNCPassword(qemuMonitorPtr mon,
index 3df078ad5774d1937bad9fb041eec1a8b815cd93..55115505c354ffb1474d2d44dd102511420fba2b 100644 (file)
@@ -712,6 +712,16 @@ int qemuMonitorTextGetBlockStatsInfo(qemuMonitorPtr mon,
 }
 
 
+int qemuMonitorTextGetBlockExtent(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
+                                  const char *devname ATTRIBUTE_UNUSED,
+                                  unsigned long long *extent ATTRIBUTE_UNUSED)
+{
+    qemuReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                    _("unable to query block extent with this QEMU"));
+    return -1;
+}
+
+
 static int
 qemuMonitorSendVNCPassphrase(qemuMonitorPtr mon ATTRIBUTE_UNUSED,
                              qemuMonitorMessagePtr msg,
index 4fe40e22ca78d5109253c11776b7453852b0d1be..91eed84b0651d8ffff8caa2bfc99dba2996b82f8 100644 (file)
@@ -55,7 +55,9 @@ int qemuMonitorTextGetBlockStatsInfo(qemuMonitorPtr mon,
                                      long long *wr_req,
                                      long long *wr_bytes,
                                      long long *errs);
-
+int qemuMonitorTextGetBlockExtent(qemuMonitorPtr mon,
+                                  const char *devname,
+                                  unsigned long long *extent);
 
 int qemuMonitorTextSetVNCPassword(qemuMonitorPtr mon,
                                   const char *password);