Sophie

Sophie

distrib > Scientific%20Linux > 5x > x86_64 > media > main-src > by-pkgid > aadbe78a25743146bb784eee19f007c5 > files > 59

kvm-83-164.el5_5.9.src.rpm

From 7b1670f2c6791f9a706dedf6aaacb960e2c2c7b5 Mon Sep 17 00:00:00 2001
From: Eduardo Habkost <ehabkost@redhat.com>
Date: Fri, 25 Sep 2009 13:40:39 -0300
Subject: [PATCH 1/2] Add host_device support to qemu-img. (Nolan Leake)

RH-Author: Kevin Wolf <kwolf@redhat.com>
Message-id: <1253716784-12002-1-git-send-email-kwolf@redhat.com>
Patchwork-id: 3512
O-Subject: [PATCH v2] Add host_device support to qemu-img. (Nolan Leake)
Bugzilla: 511072
RH-Acked-by: Juan Quintela <quintela@redhat.com>
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>

From: aliguori <aliguori@c046a42c-6fe2-441c-8c8c-71466251a162>

Bugzilla: 511072
Upstream commit: 93c65b47a6fb9ba0e2b89269a751ba3433a33427

    This patch allows the use a host_device as the destination for "qemu-img
    convert".

    I added a ->bdrv_create function host_device.  It merely verifies that
    the device exists and is large enough.

    A check is needed in the qemu-img convert loop to ensure that we write
    out all 0 sectors to the host_device.  Otherwise they end up with stale
    garbage where all zero sectors were expected.

    I also made the check against bdrv_is_allocated enabled for everything
    _except_ host devices, since there is no point in making the block
    backend write a bunch of zeros just so that we can memcmp them
    immediately afterwards.  Host devices can't benefit from this because
    there is no way to differentiate between a sector being unallocated
    because it was never written, or because it was written with all zeros
    and then made a trip through qemu-img convert.

    Finally, there is an unrelated fix for a typo in the error message
    printed if the destination device does not support ->bdrv_create.

    Signed-off-by: Nolan Leake <nolan <at> sigbus.net>
    Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

    git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6978 c046a42c-6fe2-441c-8c8c-71466251a162

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 qemu/block-raw-posix.c |   37 ++++++++++++++++++++++++++++++++++++-
 qemu/qemu-img.c        |   34 +++++++++++++++++++---------------
 2 files changed, 55 insertions(+), 16 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 qemu/block-raw-posix.c |   37 ++++++++++++++++++++++++++++++++++++-
 qemu/qemu-img.c        |   34 +++++++++++++++++++---------------
 2 files changed, 55 insertions(+), 16 deletions(-)

diff --git a/qemu/block-raw-posix.c b/qemu/block-raw-posix.c
index c9c54b7..4ec0618 100644
--- a/qemu/block-raw-posix.c
+++ b/qemu/block-raw-posix.c
@@ -1168,6 +1168,41 @@ static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
 }
 #endif /* !linux */
 
+#if defined(__linux__) || defined(__FreeBSD__)
+static int hdev_create(const char *filename, int64_t total_size,
+                       const char *backing_file, int flags)
+{
+    int fd;
+    int ret = 0;
+    struct stat stat_buf;
+
+    if (flags || backing_file)
+        return -ENOTSUP;
+
+    fd = open(filename, O_WRONLY | O_BINARY);
+    if (fd < 0)
+        return -EIO;
+
+    if (fstat(fd, &stat_buf) < 0)
+        ret = -EIO;
+    else if (!S_ISBLK(stat_buf.st_mode))
+        ret = -EIO;
+    else if (lseek(fd, 0, SEEK_END) < total_size * 512)
+        ret = -ENOSPC;
+
+    close(fd);
+    return ret;
+}
+
+#else  /* !(linux || freebsd) */
+
+static int hdev_create(const char *filename, int64_t total_size,
+                       const char *backing_file, int flags)
+{
+    return -ENOTSUP;
+}
+#endif
+
 BlockDriver bdrv_host_device = {
     "host_device",
     sizeof(BDRVRawState),
@@ -1176,7 +1211,7 @@ BlockDriver bdrv_host_device = {
     NULL,
     NULL,
     raw_close,
-    NULL,
+    hdev_create,
     raw_flush,
 
 #ifdef CONFIG_AIO
diff --git a/qemu/qemu-img.c b/qemu/qemu-img.c
index 21f3a62..e86cb88 100644
--- a/qemu/qemu-img.c
+++ b/qemu/qemu-img.c
@@ -550,7 +550,7 @@ static int img_convert(int argc, char **argv)
     ret = bdrv_create(drv, out_filename, total_sectors, out_baseimg, flags);
     if (ret < 0) {
         if (ret == -ENOTSUP) {
-            error("Formatting not supported for file format '%s'", fmt);
+            error("Formatting not supported for file format '%s'", out_fmt);
         } else {
             error("Error while formatting '%s'", out_filename);
         }
@@ -649,18 +649,17 @@ static int img_convert(int argc, char **argv)
             if (n > bs_offset + bs_sectors - sector_num)
                 n = bs_offset + bs_sectors - sector_num;
 
-            /* If the output image is being created as a copy on write image,
-               assume that sectors which are unallocated in the input image
-               are present in both the output's and input's base images (no
-               need to copy them). */
-            if (out_baseimg) {
-               if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset, n, &n1)) {
-                  sector_num += n1;
-                  continue;
-               }
-               /* The next 'n1' sectors are allocated in the input image. Copy
-                  only those as they may be followed by unallocated sectors. */
-               n = n1;
+            if (drv != &bdrv_host_device) {
+                if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
+                                       n, &n1)) {
+                    sector_num += n1;
+                    continue;
+                }
+                /* The next 'n1' sectors are allocated in the input image. Copy
+                   only those as they may be followed by unallocated sectors. */
+                n = n1;
+            } else {
+                n1 = n;
             }
 
             if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0) 
@@ -672,8 +671,13 @@ static int img_convert(int argc, char **argv)
             while (n > 0) {
                 /* If the output image is being created as a copy on write image,
                    copy all sectors even the ones containing only NUL bytes,
-                   because they may differ from the sectors in the base image. */
-                if (out_baseimg || is_allocated_sectors(buf1, n, &n1)) {
+                   because they may differ from the sectors in the base image.
+
+                   If the output is to a host device, we also write out
+                   sectors that are entirely 0, since whatever data was
+                   already there is garbage, not 0s. */
+                if (drv == &bdrv_host_device || out_baseimg ||
+                    is_allocated_sectors(buf1, n, &n1)) {
                     if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
                         error("error while writing");
                 }
-- 
1.6.3.rc4.29.g8146