Skip to contentSkip to conversation

Change the backing file of images

PATCH Superseded Kevin Wolf 2 participants 6 messages Dec 10, 2009 – Dec 11, 2009 Block layer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

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.

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    |   82 +++++++++++++++++++++++
 block_int.h      |    3 +
 qemu-img-cmds.hx |    6 ++
 qemu-img.c       |  192 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 307 insertions(+), 3 deletions(-)
author
#1 lore raw

PATCH 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 853f025..a794875 100644
@@ -476,7 +476,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("");
@@ -1209,7 +1209,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 4a8b628..880d450 100644
@@ -38,6 +38,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
author
#2 lore raw

PATCH 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 |   82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 block_int.h   |    3 ++
 4 files changed, 107 insertions(+), 0 deletions(-)
block.c +20 -0
index a794875..02d7e7e 100644
@@ -596,6 +596,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 880d450..564f0fe 100644
@@ -80,6 +80,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 +82 -0

94 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
#3 lore raw

PATCH 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       |  190 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 196 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 +190 -0

208 lines not on this page · raw message

Signature · 1 line
1.6.2.5
#4 lore raw

Re: [Qemu-devel] [PATCH 3/3] qemu-img rebase

Quoted 2 lines · Kevin Wolf

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

[..snip..]

qemu-img.c static int img_snapshot(int argc, char **argv) · line 1103

Quoted 2 lines
qemu-img.c +2 -0
+    if ((optind >= argc) || !out_baseimg)
+        help();

Coding style..

[..snip..]

Signature · 1 line
mailto:[email protected]
author
#5 lore raw

PATCH v2 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.

---

v2:
- Added missing braces in copied code

 qemu-img-cmds.hx |    6 ++
 qemu-img.c       |  192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 198 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 +192 -0

210 lines not on this page · raw message

Signature · 1 line
1.6.2.5