Skip to contentSkip to conversation

Change the backing file of images

PATCH Applied Kevin Wolf 2 participants 5 messages Jan 12, 2010 – Jan 13, 2010 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

v4 cover Change the backing file of images

This patch series adds the functionality to change the backing file of existing images. This includes simple renaming of a backing file without converting the COW image as well as safe rebasing onto a different backing file that has some changes compared to the current backing file.

v2:
- Added missing braces in copied code

v3:
- Added some missing error checks
- Fixed storing the backing file format in qcow2
- Open backing files with right format

v4:
- Handle bdrv_pwrite failure

Kevin Wolf (3):
block: Introduce BDRV_O_NO_BACKING
block: Add bdrv_change_backing_file
qemu-img rebase

 block.c          |   24 +++++-
 block.h          |    3 +
 block/qcow2.c    |  101 ++++++++++++++++++++++++
 block_int.h      |    3 +
 qemu-img-cmds.hx |    6 ++
 qemu-img.c       |  227 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 361 insertions(+), 3 deletions(-)
author
#1 lore raw

PATCH v4 1/3 block: Introduce BDRV_O_NO_BACKING

If an image references a backing file that doesn't exist, qemu-img info fails to open this image. Exactly in this case the info would be valuable, though:
the user might want to find out which file is missing.

This patch introduces a BDRV_O_NO_BACKING flag to ignore the backing file when opening the image. qemu-img info is the first user and provides info now even if the backing file is invalid.

---
 block.c    |    4 ++--
 block.h    |    1 +
 qemu-img.c |    2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)
block.c +2 -2
index 30ae2b1..994109e 100644
@@ -477,7 +477,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
         unlink(filename);
     }
 #endif
-    if (bs->backing_file[0] != '\0') {
+    if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
         /* if there is a backing file, use it */
         BlockDriver *back_drv = NULL;
         bs->backing_hd = bdrv_new("");
@@ -1352,7 +1352,7 @@ const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
 void bdrv_get_backing_filename(BlockDriverState *bs,
                                char *filename, int filename_size)
 {
-    if (!bs->backing_hd) {
+    if (!bs->backing_file) {
         pstrcpy(filename, filename_size, "");
     } else {
         pstrcpy(filename, filename_size, bs->backing_file);
block.h +1 -0
index fa51ddf..f660d5f 100644
@@ -39,6 +39,7 @@ typedef struct QEMUSnapshotInfo {
 #define BDRV_O_NOCACHE     0x0020 /* do not use the host page cache */
 #define BDRV_O_CACHE_WB    0x0040 /* use write-back caching */
 #define BDRV_O_NATIVE_AIO  0x0080 /* use native AIO instead of the thread pool */
+#define BDRV_O_NO_BACKING  0x0100 /* don't open the backing file */
 
 #define BDRV_O_CACHE_MASK  (BDRV_O_NOCACHE | BDRV_O_CACHE_WB)
 
qemu-img.c +1 -1
index 1d97f2e..5ad88bf 100644
@@ -884,7 +884,7 @@ static int img_info(int argc, char **argv)
     } else {
         drv = NULL;
     }
-    if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
+    if (bdrv_open2(bs, filename, BRDV_O_FLAGS | BDRV_O_NO_BACKING, drv) < 0) {
         error("Could not open '%s'", filename);
     }
     bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
Signature · 1 line
1.6.2.5
#2 lore raw

Re: [Qemu-devel] [PATCH v4 1/3] block: Introduce BDRV_O_NO_BACKING

Quoted 10 lines · Kevin Wolf

If an image references a backing file that doesn't exist, qemu-img info fails to open this image. Exactly in this case the info would be valuable, though:
the user might want to find out which file is missing.

This patch introduces a BDRV_O_NO_BACKING flag to ignore the backing file when opening the image. qemu-img info is the first user and provides info now even if the backing file is invalid.

Applied all. Thanks.

Regards,

Anthony Liguori

Quoted 54 lines
---
  block.c    |    4 ++--
  block.h    |    1 +
  qemu-img.c |    2 +-
  3 files changed, 4 insertions(+), 3 deletions(-)
block.c +2 -2
index 30ae2b1..994109e 100644
@@ -477,7 +477,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
          unlink(filename);
      }
  #endif
-    if (bs->backing_file[0] != '\0') {
+    if ((flags&  BDRV_O_NO_BACKING) == 0&&  bs->backing_file[0] != '\0') {
          /* if there is a backing file, use it */
          BlockDriver *back_drv = NULL;
          bs->backing_hd = bdrv_new("");
@@ -1352,7 +1352,7 @@ const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
  void bdrv_get_backing_filename(BlockDriverState *bs,
                                 char *filename, int filename_size)
  {
-    if (!bs->backing_hd) {
+    if (!bs->backing_file) {
          pstrcpy(filename, filename_size, "");
      } else {
          pstrcpy(filename, filename_size, bs->backing_file);
block.h +1 -0
index fa51ddf..f660d5f 100644
@@ -39,6 +39,7 @@ typedef struct QEMUSnapshotInfo {
  #define BDRV_O_NOCACHE     0x0020 /* do not use the host page cache */
  #define BDRV_O_CACHE_WB    0x0040 /* use write-back caching */
  #define BDRV_O_NATIVE_AIO  0x0080 /* use native AIO instead of the thread pool */
+#define BDRV_O_NO_BACKING  0x0100 /* don't open the backing file */
 
  #define BDRV_O_CACHE_MASK  (BDRV_O_NOCACHE | BDRV_O_CACHE_WB)
 
qemu-img.c +1 -1
index 1d97f2e..5ad88bf 100644
@@ -884,7 +884,7 @@ static int img_info(int argc, char **argv)
      } else {
          drv = NULL;
      }
-    if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv)<  0) {
+    if (bdrv_open2(bs, filename, BRDV_O_FLAGS | BDRV_O_NO_BACKING, drv)<  0) {
          error("Could not open '%s'", filename);
      }
      bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
author
#3 lore raw

PATCH v4 2/3 block: Add bdrv_change_backing_file

In reply to Kevin Wolf

Introduce the functions needed to change the backing file of an image. The function is implemented for qcow2.

---
 block.c       |   20 +++++++++++
 block.h       |    2 +
 block/qcow2.c |  101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 block_int.h   |    3 ++
 4 files changed, 126 insertions(+), 0 deletions(-)
block.c +20 -0
index 994109e..c7bc837 100644
@@ -597,6 +597,26 @@ int bdrv_commit(BlockDriverState *bs)
     return 0;
 }
 
+/*
+ * Return values:
+ * 0        - success
+ * -EINVAL  - backing format specified, but no file
+ * -ENOSPC  - can't update the backing file because no space is left in the
+ *            image file header
+ * -ENOTSUP - format driver doesn't support changing the backing file
+ */
+int bdrv_change_backing_file(BlockDriverState *bs,
+    const char *backing_file, const char *backing_fmt)
+{
+    BlockDriver *drv = bs->drv;
+
+    if (drv->bdrv_change_backing_file != NULL) {
+        return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
+    } else {
+        return -ENOTSUP;
+    }
+}
+
 static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
                                    size_t size)
 {
block.h +2 -0
index f660d5f..bee9ec5 100644
@@ -83,6 +83,8 @@ int64_t bdrv_getlength(BlockDriverState *bs);
 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr);
 void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs);
 int bdrv_commit(BlockDriverState *bs);
+int bdrv_change_backing_file(BlockDriverState *bs,
+    const char *backing_file, const char *backing_fmt);
 void bdrv_register(BlockDriver *bdrv);
 
 /* async block I/O */
block/qcow2.c +101 -0

113 lines not on this page · raw message

block_int.h +3 -0
index 9a3b2e0..a0ebd90 100644
@@ -98,6 +98,9 @@ struct BlockDriver {
     int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
                              int64_t pos, int size);
 
+    int (*bdrv_change_backing_file)(BlockDriverState *bs,
+        const char *backing_file, const char *backing_fmt);
+
     /* removable device specific */
     int (*bdrv_is_inserted)(BlockDriverState *bs);
     int (*bdrv_media_changed)(BlockDriverState *bs);
Signature · 1 line
1.6.2.5
author
#4 lore raw

PATCH v4 3/3 qemu-img rebase

In reply to Kevin Wolf

This adds a rebase subcommand to qemu-img which allows to change the backing file of an image.

In default mode, both the current and the new backing file need to exist, and after the rebase, the COW image is guaranteed to have the same guest visible content as before. To achieve this, old and new backing file are compared and, if necessary, data is copied from the old backing file into the COW image.

With -u an unsafe mode is enabled that doesn't require the backing files to exist. It merely changes the backing file reference in the COW image. This is useful for renaming or moving the backing file. The user is responsible to make sure that the new backing file has no changes compared to the old one, or corruption may occur.

---
 qemu-img-cmds.hx |    6 ++
 qemu-img.c       |  225 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 231 insertions(+), 0 deletions(-)
qemu-img-cmds.hx +6 -0
index 641bd87..f28ef36 100644
@@ -43,5 +43,11 @@ DEF("snapshot", img_snapshot,
     "snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename")
 STEXI
 @item snapshot [-l | -a @var{snapshot} | -c @var{snapshot} | -d @var{snapshot}] @var{filename}
+ETEXI
+
+DEF("rebase", img_rebase,
+    "rebase [-u] -b backing_file [-F backing_fmt] filename")
+STEXI
+@item rebase [-u] -b @var{backing_file} [-F @var{backing_fmt}] @var{filename}
 @end table
 ETEXI
qemu-img.c +225 -0

243 lines not on this page · raw message

Signature · 1 line
1.6.2.5