Change the backing file of images
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(-)
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
@@ -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
@@ -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
@@ -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
Anthony Liguori
AppliedRe: [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_BACKINGflag 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.Signed-off-by Kevin Wolf<[email protected]>
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@@ -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@@ -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@@ -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));
PATCH v4 2/3 block: Add bdrv_change_backing_file
In reply to Kevin WolfIntroduce 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
@@ -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
@@ -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
@@ -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
PATCH v4 3/3 qemu-img rebase
In reply to Kevin WolfThis 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
@@ -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