Commit 9230eaf6797ffce465aef2a6c5b26d605c162a24

Authored by aliguori
1 parent f965509c

qemu-img: adding a "-F base_fmt" option to "qemu-img create -b" (Uri Lublin)

If the user specifies the backing file format,
then when opening the backing file, there is no need
to probe the (backing file) image to figure out its format.

This follows my previous patches implementing bdrv_create2
which keeps (for qcow2 only) the backing file format
as a qcow2-extension

Suggested by Daniel P. Berrange.

Signed-off-by: Uri Lublin <uril@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6910 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 2 changed files with 23 additions and 5 deletions
qemu-img.c
... ... @@ -25,6 +25,7 @@
25 25 #include "osdep.h"
26 26 #include "block_int.h"
27 27 #include <assert.h>
  28 +#include <stdio.h>
28 29  
29 30 #ifdef _WIN32
30 31 #include <windows.h>
... ... @@ -57,7 +58,7 @@ static void help(void)
57 58 "QEMU disk image utility\n"
58 59 "\n"
59 60 "Command syntax:\n"
60   - " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
  61 + " create [-e] [-6] [-F fmt] [-b base_image] [-f fmt] filename [size]\n"
61 62 " commit [-f fmt] filename\n"
62 63 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
63 64 " info [-f fmt] filename\n"
... ... @@ -217,6 +218,7 @@ static int img_create(int argc, char **argv)
217 218 {
218 219 int c, ret, flags;
219 220 const char *fmt = "raw";
  221 + const char *base_fmt = NULL;
220 222 const char *filename;
221 223 const char *base_filename = NULL;
222 224 uint64_t size;
... ... @@ -226,13 +228,16 @@ static int img_create(int argc, char **argv)
226 228  
227 229 flags = 0;
228 230 for(;;) {
229   - c = getopt(argc, argv, "b:f:he6");
  231 + c = getopt(argc, argv, "F:b:f:he6");
230 232 if (c == -1)
231 233 break;
232 234 switch(c) {
233 235 case 'h':
234 236 help();
235 237 break;
  238 + case 'F':
  239 + base_fmt = optarg;
  240 + break;
236 241 case 'b':
237 242 base_filename = optarg;
238 243 break;
... ... @@ -253,7 +258,15 @@ static int img_create(int argc, char **argv)
253 258 size = 0;
254 259 if (base_filename) {
255 260 BlockDriverState *bs;
256   - bs = bdrv_new_open(base_filename, NULL);
  261 + BlockDriver *base_drv = NULL;
  262 +
  263 + if (base_fmt) {
  264 + base_drv = bdrv_find_format(base_fmt);
  265 + if (base_drv == NULL)
  266 + error("Unknown basefile format '%s'", base_fmt);
  267 + }
  268 +
  269 + bs = bdrv_new_open(base_filename, base_fmt);
257 270 bdrv_get_geometry(bs, &size);
258 271 size *= 512;
259 272 bdrv_delete(bs);
... ... @@ -284,9 +297,12 @@ static int img_create(int argc, char **argv)
284 297 if (base_filename) {
285 298 printf(", backing_file=%s",
286 299 base_filename);
  300 + if (base_fmt)
  301 + printf(", backing_fmt=%s",
  302 + base_fmt);
287 303 }
288 304 printf(", size=%" PRIu64 " kB\n", size / 1024);
289   - ret = bdrv_create(drv, filename, size / 512, base_filename, flags);
  305 + ret = bdrv_create2(drv, filename, size / 512, base_filename, base_fmt, flags);
290 306 if (ret < 0) {
291 307 if (ret == -ENOTSUP) {
292 308 error("Formatting or formatting option not supported for file format '%s'", fmt);
... ...
qemu-img.texi
... ... @@ -8,7 +8,7 @@ usage: qemu-img command [command options]
8 8  
9 9 The following commands are supported:
10 10 @table @option
11   -@item create [-e] [-6] [-b @var{base_image}] [-f @var{fmt}] @var{filename} [@var{size}]
  11 +@item create [-e] [-6] [-F @var{base_fmt}] [-b @var{base_image}] [-f @var{fmt}] @var{filename} [@var{size}]
12 12 @item commit [-f @var{fmt}] @var{filename}
13 13 @item convert [-c] [-e] [-6] [-f @var{fmt}] [-O @var{output_fmt}] [-B @var{output_base_image}] @var{filename} [@var{filename2} [...]] @var{output_filename}
14 14 @item info [-f @var{fmt}] @var{filename}
... ... @@ -27,6 +27,8 @@ forces the output image to be created as a copy on write
27 27 image of the specified base image; @code{output_base_image} should have the same
28 28 content as the input's base image, however the path, image format, etc may
29 29 differ
  30 +@item base_fmt
  31 +is the disk image format of @var{base_image}. for more information look at @var{fmt}
30 32 @item fmt
31 33 is the disk image format. It is guessed automatically in most cases. The following formats are supported:
32 34  
... ...