]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
storage: Add additional errors/checks for disk label
authorJohn Ferlan <jferlan@redhat.com>
Thu, 1 Oct 2015 12:24:14 +0000 (08:24 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Mon, 5 Oct 2015 12:14:44 +0000 (08:14 -0400)
Let's check to ensure we can find the Partition Table in the label
and that libvirt actually recognizes that type; otherwise, when we
go to read the partitions during a refresh operation we may not be
reading what we expect.

This will expand upon the types of errors or reason that a build
would fail, so we can create more direct error messages.

src/libvirt_private.syms
src/storage/storage_backend_disk.c

index c1b7b87e91dad5ed840698f53c08c029a4ee6c83..be6ee19546479e6ea545e353adc02923d361ecdd 100644 (file)
@@ -834,6 +834,7 @@ virStoragePoolDefParseFile;
 virStoragePoolDefParseNode;
 virStoragePoolDefParseSourceString;
 virStoragePoolDefParseString;
+virStoragePoolFormatDiskTypeFromString;
 virStoragePoolFormatDiskTypeToString;
 virStoragePoolFormatFileSystemNetTypeToString;
 virStoragePoolFormatFileSystemTypeToString;
index 6f9fab17eba29b0d99e743f169ca03f43fcfcde1..b66a4a1a8114fc45a27834290957e41bfdc2c416 100644 (file)
@@ -396,7 +396,9 @@ virStorageBackendDiskRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
  * Check for a valid disk label (partition table) on device
  *
  * return: 0 - valid disk label found
- *        >0 - no or unrecognized disk label
+ *         1 - no or unrecognized disk label
+ *         2 - did not find the Partition Table type
+ *         3 - Partition Table type unknown
  *        <0 - error finding the disk label
  */
 static int
@@ -408,6 +410,7 @@ virStorageBackendDiskFindLabel(const char* device)
     virCommandPtr cmd = virCommandNew(PARTED);
     char *output = NULL;
     char *error = NULL;
+    char *start, *end;
     int ret = -1;
 
     virCommandAddArgSet(cmd, args);
@@ -422,15 +425,40 @@ virStorageBackendDiskFindLabel(const char* device)
             (error && strstr(error, "unrecognised disk label"))) {
             ret = 1;
         }
+        goto cleanup;
+    }
+
+    /* Search for "Partition Table:" in the output. If not present,
+     * then we cannot validate the partition table type.
+     */
+    if (!(start = strstr(output, "Partition Table: ")) ||
+        !(end = strstr(start, "\n"))) {
+        VIR_DEBUG("Unable to find tag in output: %s", output);
+        ret = 2;
+        goto cleanup;
+    }
+    start += strlen("Partition Table: ");
+    *end = '\0';
+
+    /* on disk it's "msdos", but we document/use "dos" so deal with it here */
+    if (STREQ(start, "msdos"))
+        start += 2;
+
+    /* Make sure we know about this type */
+    if (virStoragePoolFormatDiskTypeFromString(start) < 0) {
+        ret = 3;
+        goto cleanup;
     }
 
+    ret = 0;
+
+ cleanup:
     virCommandFree(cmd);
     VIR_FREE(output);
     VIR_FREE(error);
     return ret;
 }
 
-
 /**
  * Determine whether the label on the disk is valid or in a known format
  * for the purpose of rewriting the label during build
@@ -452,12 +480,19 @@ virStorageBackendDiskValidLabel(const char *device,
     int check;
 
     check = virStorageBackendDiskFindLabel(device);
-    if (check > 0) {
+    if (check == 1) {
         if (writelabel)
             valid = true;
         else
             virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                            _("Unrecognized disk label found, requires build"));
+    } else if (check == 2) {
+        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+                       _("Unable to determine Partition Type, "
+                         "requires build --overwrite"));
+    } else if (check == 3) {
+        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+                       _("Unknown Partition Type, requires build --overwrite"));
     } else if (check < 0) {
         virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                        _("Error checking for disk label, failed to get "