render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Daniel P. Berrange e3a592
From 726a63a437efd96510ce316bf30d16f213d4db27 Mon Sep 17 00:00:00 2001
Daniel P. Berrange e3a592
From: Daniel P. Berrange <berrange@redhat.com>
Daniel P. Berrange e3a592
Date: Tue, 15 Jun 2010 16:15:51 +0100
Daniel P. Berrange e3a592
Subject: [PATCH 04/11] Require format to be passed into virStorageFileGetMetadata
Daniel P. Berrange e3a592
Daniel P. Berrange e3a592
Require the disk image to be passed into virStorageFileGetMetadata.
Daniel P. Berrange e3a592
If this is set to VIR_STORAGE_FILE_AUTO, then the format will be
Daniel P. Berrange e3a592
resolved using probing. This makes it easier to control when
Daniel P. Berrange e3a592
probing will be used
Daniel P. Berrange e3a592
Daniel P. Berrange e3a592
* src/qemu/qemu_driver.c, src/qemu/qemu_security_dac.c,
Daniel P. Berrange e3a592
  src/security/security_selinux.c, src/security/virt-aa-helper.c:
Daniel P. Berrange e3a592
  Set VIR_STORAGE_FILE_AUTO when calling virStorageFileGetMetadata.
Daniel P. Berrange e3a592
* src/storage/storage_backend_fs.c: Probe for disk format before
Daniel P. Berrange e3a592
  calling virStorageFileGetMetadata.
Daniel P. Berrange e3a592
* src/util/storage_file.h, src/util/storage_file.c: Remove format
Daniel P. Berrange e3a592
  from virStorageFileMeta struct & require it to be passed into
Daniel P. Berrange e3a592
  method.
Daniel P. Berrange e3a592
---
Daniel P. Berrange e3a592
 src/qemu/qemu_driver.c           |   27 +++++++++++++++++---
Daniel P. Berrange e3a592
 src/qemu/qemu_security_dac.c     |    4 ++-
Daniel P. Berrange e3a592
 src/security/security_selinux.c  |    4 ++-
Daniel P. Berrange e3a592
 src/security/virt-aa-helper.c    |    4 ++-
Daniel P. Berrange e3a592
 src/storage/storage_backend_fs.c |   11 ++++++--
Daniel P. Berrange e3a592
 src/util/storage_file.c          |   50 +++++++++++++++++++++++++------------
Daniel P. Berrange e3a592
 src/util/storage_file.h          |    3 +-
Daniel P. Berrange e3a592
 7 files changed, 76 insertions(+), 27 deletions(-)
Daniel P. Berrange e3a592
Daniel P. Berrange e3a592
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
Daniel P. Berrange e3a592
index 487bfa3..97f2990 100644
Daniel P. Berrange e3a592
--- a/src/qemu/qemu_driver.c
Daniel P. Berrange e3a592
+++ b/src/qemu/qemu_driver.c
Daniel P. Berrange e3a592
@@ -3069,7 +3069,9 @@ static int qemuSetupDiskCgroup(virCgroupPtr cgroup,
Daniel P. Berrange e3a592
             }
Daniel P. Berrange e3a592
         }
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-        rc = virStorageFileGetMetadata(path, &meta);
Daniel P. Berrange e3a592
+        rc = virStorageFileGetMetadata(path,
Daniel P. Berrange e3a592
+                                       VIR_STORAGE_FILE_AUTO,
Daniel P. Berrange e3a592
+                                       &meta);
Daniel P. Berrange e3a592
         if (rc < 0)
Daniel P. Berrange e3a592
             VIR_WARN("Unable to lookup parent image for %s", path);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
@@ -3119,7 +3121,9 @@ static int qemuTeardownDiskCgroup(virCgroupPtr cgroup,
Daniel P. Berrange e3a592
             }
Daniel P. Berrange e3a592
         }
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-        rc = virStorageFileGetMetadata(path, &meta);
Daniel P. Berrange e3a592
+        rc = virStorageFileGetMetadata(path,
Daniel P. Berrange e3a592
+                                       VIR_STORAGE_FILE_AUTO,
Daniel P. Berrange e3a592
+                                       &meta);
Daniel P. Berrange e3a592
         if (rc < 0)
Daniel P. Berrange e3a592
             VIR_WARN("Unable to lookup parent image for %s", path);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
@@ -9614,6 +9618,7 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
Daniel P. Berrange e3a592
     virDomainDiskDefPtr disk = NULL;
Daniel P. Berrange e3a592
     struct stat sb;
Daniel P. Berrange e3a592
     int i;
Daniel P. Berrange e3a592
+    int format;
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
     virCheckFlags(0, -1);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
@@ -9658,7 +9663,21 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
Daniel P. Berrange e3a592
     }
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
     /* Probe for magic formats */
Daniel P. Berrange e3a592
-    if (virStorageFileGetMetadataFromFD(path, fd, &meta) < 0)
Daniel P. Berrange e3a592
+    if (disk->driverType) {
Daniel P. Berrange e3a592
+        if ((format = virStorageFileFormatTypeFromString(disk->driverType)) < 0) {
Daniel P. Berrange e3a592
+            qemuReportError(VIR_ERR_INTERNAL_ERROR,
Daniel P. Berrange e3a592
+                            _("unknown disk format %s for %s"),
Daniel P. Berrange e3a592
+                            disk->driverType, disk->src);
Daniel P. Berrange e3a592
+            goto cleanup;
Daniel P. Berrange e3a592
+        }
Daniel P. Berrange e3a592
+    } else {
Daniel P. Berrange e3a592
+        if ((format = virStorageFileProbeFormat(disk->src)) < 0)
Daniel P. Berrange e3a592
+            goto cleanup;
Daniel P. Berrange e3a592
+    }
Daniel P. Berrange e3a592
+
Daniel P. Berrange e3a592
+    if (virStorageFileGetMetadataFromFD(path, fd,
Daniel P. Berrange e3a592
+                                        format,
Daniel P. Berrange e3a592
+                                        &meta) < 0)
Daniel P. Berrange e3a592
         goto cleanup;
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
     /* Get info for normal formats */
Daniel P. Berrange e3a592
@@ -9706,7 +9725,7 @@ static int qemuDomainGetBlockInfo(virDomainPtr dom,
Daniel P. Berrange e3a592
        highest allocated extent from QEMU */
Daniel P. Berrange e3a592
     if (virDomainObjIsActive(vm) &&
Daniel P. Berrange e3a592
         disk->type == VIR_DOMAIN_DISK_TYPE_BLOCK &&
Daniel P. Berrange e3a592
-        meta.format != VIR_STORAGE_FILE_RAW &&
Daniel P. Berrange e3a592
+        format != VIR_STORAGE_FILE_RAW &&
Daniel P. Berrange e3a592
         S_ISBLK(sb.st_mode)) {
Daniel P. Berrange e3a592
         qemuDomainObjPrivatePtr priv = vm->privateData;
Daniel P. Berrange e3a592
         if (qemuDomainObjBeginJob(vm) < 0)
Daniel P. Berrange e3a592
diff --git a/src/qemu/qemu_security_dac.c b/src/qemu/qemu_security_dac.c
Daniel P. Berrange e3a592
index 95015b0..acfe48e 100644
Daniel P. Berrange e3a592
--- a/src/qemu/qemu_security_dac.c
Daniel P. Berrange e3a592
+++ b/src/qemu/qemu_security_dac.c
Daniel P. Berrange e3a592
@@ -115,7 +115,9 @@ qemuSecurityDACSetSecurityImageLabel(virDomainObjPtr vm ATTRIBUTE_UNUSED,
Daniel P. Berrange e3a592
         virStorageFileMetadata meta;
Daniel P. Berrange e3a592
         int ret;
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-        ret = virStorageFileGetMetadata(path, &meta);
Daniel P. Berrange e3a592
+        ret = virStorageFileGetMetadata(path,
Daniel P. Berrange e3a592
+                                        VIR_STORAGE_FILE_AUTO,
Daniel P. Berrange e3a592
+                                        &meta);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
         if (path != disk->src)
Daniel P. Berrange e3a592
             VIR_FREE(path);
Daniel P. Berrange e3a592
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
Daniel P. Berrange e3a592
index e5eef19..5c0f002 100644
Daniel P. Berrange e3a592
--- a/src/security/security_selinux.c
Daniel P. Berrange e3a592
+++ b/src/security/security_selinux.c
Daniel P. Berrange e3a592
@@ -457,7 +457,9 @@ SELinuxSetSecurityImageLabel(virDomainObjPtr vm,
Daniel P. Berrange e3a592
         virStorageFileMetadata meta;
Daniel P. Berrange e3a592
         int ret;
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-        ret = virStorageFileGetMetadata(path, &meta);
Daniel P. Berrange e3a592
+        ret = virStorageFileGetMetadata(path,
Daniel P. Berrange e3a592
+                                        VIR_STORAGE_FILE_AUTO,
Daniel P. Berrange e3a592
+                                        &meta);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
         if (path != disk->src)
Daniel P. Berrange e3a592
             VIR_FREE(path);
Daniel P. Berrange e3a592
diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c
Daniel P. Berrange e3a592
index c66f107..2c045e6 100644
Daniel P. Berrange e3a592
--- a/src/security/virt-aa-helper.c
Daniel P. Berrange e3a592
+++ b/src/security/virt-aa-helper.c
Daniel P. Berrange e3a592
@@ -830,7 +830,9 @@ get_files(vahControl * ctl)
Daniel P. Berrange e3a592
             do {
Daniel P. Berrange e3a592
                 virStorageFileMetadata meta;
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-                ret = virStorageFileGetMetadata(path, &meta);
Daniel P. Berrange e3a592
+                ret = virStorageFileGetMetadata(path,
Daniel P. Berrange e3a592
+                                                VIR_STORAGE_FILE_AUTO,
Daniel P. Berrange e3a592
+                                                &meta);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
                 if (path != ctl->def->disks[i]->src)
Daniel P. Berrange e3a592
                     VIR_FREE(path);
Daniel P. Berrange e3a592
diff --git a/src/storage/storage_backend_fs.c b/src/storage/storage_backend_fs.c
Daniel P. Berrange e3a592
index f0cd770..d3ac0fe 100644
Daniel P. Berrange e3a592
--- a/src/storage/storage_backend_fs.c
Daniel P. Berrange e3a592
+++ b/src/storage/storage_backend_fs.c
Daniel P. Berrange e3a592
@@ -75,14 +75,19 @@ virStorageBackendProbeTarget(virStorageVolTargetPtr target,
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
     memset(&meta, 0, sizeof(meta));
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-    if (virStorageFileGetMetadataFromFD(target->path, fd, &meta) < 0) {
Daniel P. Berrange e3a592
+    if ((target->format = virStorageFileProbeFormatFromFD(target->path, fd)) < 0) {
Daniel P. Berrange e3a592
         close(fd);
Daniel P. Berrange e3a592
         return -1;
Daniel P. Berrange e3a592
     }
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-    close(fd);
Daniel P. Berrange e3a592
+    if (virStorageFileGetMetadataFromFD(target->path, fd,
Daniel P. Berrange e3a592
+                                        target->format,
Daniel P. Berrange e3a592
+                                        &meta) < 0) {
Daniel P. Berrange e3a592
+        close(fd);
Daniel P. Berrange e3a592
+        return -1;
Daniel P. Berrange e3a592
+    }
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-    target->format = meta.format;
Daniel P. Berrange e3a592
+    close(fd);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
     if (backingStore) {
Daniel P. Berrange e3a592
         *backingStore = meta.backingStore;
Daniel P. Berrange e3a592
diff --git a/src/util/storage_file.c b/src/util/storage_file.c
Daniel P. Berrange e3a592
index 221268b..9712d92 100644
Daniel P. Berrange e3a592
--- a/src/util/storage_file.c
Daniel P. Berrange e3a592
+++ b/src/util/storage_file.c
Daniel P. Berrange e3a592
@@ -696,18 +696,23 @@ virStorageFileProbeFormat(const char *path)
Daniel P. Berrange e3a592
 /**
Daniel P. Berrange e3a592
  * virStorageFileGetMetadataFromFD:
Daniel P. Berrange e3a592
  *
Daniel P. Berrange e3a592
- * Probe for the format of 'fd' (which is an open file descriptor
Daniel P. Berrange e3a592
- * for the file 'path'), filling 'meta' with the detected
Daniel P. Berrange e3a592
- * format and other associated metadata.
Daniel P. Berrange e3a592
+ * Extract metadata about the storage volume with the specified
Daniel P. Berrange e3a592
+ * image format. If image format is VIR_STORAGE_FILE_AUTO, it
Daniel P. Berrange e3a592
+ * will probe to automatically identify the format. 
Daniel P. Berrange e3a592
  *
Daniel P. Berrange e3a592
- * Callers are advised never to trust the returned 'meta->format'
Daniel P. Berrange e3a592
- * unless it is listed as VIR_STORAGE_FILE_RAW, since a
Daniel P. Berrange e3a592
- * malicious guest can turn a raw file into any other non-raw
Daniel P. Berrange e3a592
- * format at will.
Daniel P. Berrange e3a592
+ * Callers are advised never to use VIR_STORAGE_FILE_AUTO as a
Daniel P. Berrange e3a592
+ * format, since a malicious guest can turn a raw file into any
Daniel P. Berrange e3a592
+ * other non-raw format at will.
Daniel P. Berrange e3a592
+ *
Daniel P. Berrange e3a592
+ * If the returned meta.backingStoreFormat is VIR_STORAGE_FILE_AUTO
Daniel P. Berrange e3a592
+ * it indicates the image didn't specify an explicit format for its
Daniel P. Berrange e3a592
+ * backing store. Callers are advised against probing for the
Daniel P. Berrange e3a592
+ * backing store format in this case.
Daniel P. Berrange e3a592
  */
Daniel P. Berrange e3a592
 int
Daniel P. Berrange e3a592
 virStorageFileGetMetadataFromFD(const char *path,
Daniel P. Berrange e3a592
                                 int fd,
Daniel P. Berrange e3a592
+                                int format,
Daniel P. Berrange e3a592
                                 virStorageFileMetadata *meta)
Daniel P. Berrange e3a592
 {
Daniel P. Berrange e3a592
     unsigned char *head;
Daniel P. Berrange e3a592
@@ -731,9 +736,16 @@ virStorageFileGetMetadataFromFD(const char *path,
Daniel P. Berrange e3a592
         goto cleanup;
Daniel P. Berrange e3a592
     }
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-    meta->format = virStorageFileProbeFormatFromBuf(path, head, len);
Daniel P. Berrange e3a592
+    if (format == VIR_STORAGE_FILE_AUTO)
Daniel P. Berrange e3a592
+        format = virStorageFileProbeFormatFromBuf(path, head, len);
Daniel P. Berrange e3a592
+
Daniel P. Berrange e3a592
+    if (format < 0 ||
Daniel P. Berrange e3a592
+        format >= VIR_STORAGE_FILE_LAST) {
Daniel P. Berrange e3a592
+        virReportSystemError(EINVAL, _("unknown storage file format %d"), format);
Daniel P. Berrange e3a592
+        return -1;
Daniel P. Berrange e3a592
+    }
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-    ret = virStorageFileGetMetadataFromBuf(meta->format, path, head, len, meta);
Daniel P. Berrange e3a592
+    ret = virStorageFileGetMetadataFromBuf(format, path, head, len, meta);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
 cleanup:
Daniel P. Berrange e3a592
     VIR_FREE(head);
Daniel P. Berrange e3a592
@@ -743,16 +755,22 @@ cleanup:
Daniel P. Berrange e3a592
 /**
Daniel P. Berrange e3a592
  * virStorageFileGetMetadata:
Daniel P. Berrange e3a592
  *
Daniel P. Berrange e3a592
- * Probe for the format of 'path', filling 'meta' with the detected
Daniel P. Berrange e3a592
- * format and other associated metadata.
Daniel P. Berrange e3a592
+ * Extract metadata about the storage volume with the specified
Daniel P. Berrange e3a592
+ * image format. If image format is VIR_STORAGE_FILE_AUTO, it
Daniel P. Berrange e3a592
+ * will probe to automatically identify the format. 
Daniel P. Berrange e3a592
  *
Daniel P. Berrange e3a592
- * Callers are advised never to trust the returned 'meta->format'
Daniel P. Berrange e3a592
- * unless it is listed as VIR_STORAGE_FILE_RAW, since a
Daniel P. Berrange e3a592
- * malicious guest can turn a raw file into any other non-raw
Daniel P. Berrange e3a592
- * format at will.
Daniel P. Berrange e3a592
+ * Callers are advised never to use VIR_STORAGE_FILE_AUTO as a
Daniel P. Berrange e3a592
+ * format, since a malicious guest can turn a raw file into any
Daniel P. Berrange e3a592
+ * other non-raw format at will.
Daniel P. Berrange e3a592
+ *
Daniel P. Berrange e3a592
+ * If the returned meta.backingStoreFormat is VIR_STORAGE_FILE_AUTO
Daniel P. Berrange e3a592
+ * it indicates the image didn't specify an explicit format for its
Daniel P. Berrange e3a592
+ * backing store. Callers are advised against probing for the
Daniel P. Berrange e3a592
+ * backing store format in this case.
Daniel P. Berrange e3a592
  */
Daniel P. Berrange e3a592
 int
Daniel P. Berrange e3a592
 virStorageFileGetMetadata(const char *path,
Daniel P. Berrange e3a592
+                          int format,
Daniel P. Berrange e3a592
                           virStorageFileMetadata *meta)
Daniel P. Berrange e3a592
 {
Daniel P. Berrange e3a592
     int fd, ret;
Daniel P. Berrange e3a592
@@ -762,7 +780,7 @@ virStorageFileGetMetadata(const char *path,
Daniel P. Berrange e3a592
         return -1;
Daniel P. Berrange e3a592
     }
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
-    ret = virStorageFileGetMetadataFromFD(path, fd, meta);
Daniel P. Berrange e3a592
+    ret = virStorageFileGetMetadataFromFD(path, fd, format, meta);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
     close(fd);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
diff --git a/src/util/storage_file.h b/src/util/storage_file.h
Daniel P. Berrange e3a592
index 3420d44..6853182 100644
Daniel P. Berrange e3a592
--- a/src/util/storage_file.h
Daniel P. Berrange e3a592
+++ b/src/util/storage_file.h
Daniel P. Berrange e3a592
@@ -46,7 +46,6 @@ enum virStorageFileFormat {
Daniel P. Berrange e3a592
 VIR_ENUM_DECL(virStorageFileFormat);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
 typedef struct _virStorageFileMetadata {
Daniel P. Berrange e3a592
-    int format;
Daniel P. Berrange e3a592
     char *backingStore;
Daniel P. Berrange e3a592
     int backingStoreFormat;
Daniel P. Berrange e3a592
     unsigned long long capacity;
Daniel P. Berrange e3a592
@@ -62,9 +61,11 @@ int virStorageFileProbeFormatFromFD(const char *path,
Daniel P. Berrange e3a592
                                     int fd);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
 int virStorageFileGetMetadata(const char *path,
Daniel P. Berrange e3a592
+                              int format,
Daniel P. Berrange e3a592
                               virStorageFileMetadata *meta);
Daniel P. Berrange e3a592
 int virStorageFileGetMetadataFromFD(const char *path,
Daniel P. Berrange e3a592
                                     int fd,
Daniel P. Berrange e3a592
+                                    int format,
Daniel P. Berrange e3a592
                                     virStorageFileMetadata *meta);
Daniel P. Berrange e3a592
 
Daniel P. Berrange e3a592
 int virStorageFileIsSharedFS(const char *path);
Daniel P. Berrange e3a592
-- 
Daniel P. Berrange e3a592
1.7.1.1
Daniel P. Berrange e3a592