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,6 +25,7 @@
25 #include "osdep.h" 25 #include "osdep.h"
26 #include "block_int.h" 26 #include "block_int.h"
27 #include <assert.h> 27 #include <assert.h>
  28 +#include <stdio.h>
28 29
29 #ifdef _WIN32 30 #ifdef _WIN32
30 #include <windows.h> 31 #include <windows.h>
@@ -57,7 +58,7 @@ static void help(void) @@ -57,7 +58,7 @@ static void help(void)
57 "QEMU disk image utility\n" 58 "QEMU disk image utility\n"
58 "\n" 59 "\n"
59 "Command syntax:\n" 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 " commit [-f fmt] filename\n" 62 " commit [-f fmt] filename\n"
62 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n" 63 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
63 " info [-f fmt] filename\n" 64 " info [-f fmt] filename\n"
@@ -217,6 +218,7 @@ static int img_create(int argc, char **argv) @@ -217,6 +218,7 @@ static int img_create(int argc, char **argv)
217 { 218 {
218 int c, ret, flags; 219 int c, ret, flags;
219 const char *fmt = "raw"; 220 const char *fmt = "raw";
  221 + const char *base_fmt = NULL;
220 const char *filename; 222 const char *filename;
221 const char *base_filename = NULL; 223 const char *base_filename = NULL;
222 uint64_t size; 224 uint64_t size;
@@ -226,13 +228,16 @@ static int img_create(int argc, char **argv) @@ -226,13 +228,16 @@ static int img_create(int argc, char **argv)
226 228
227 flags = 0; 229 flags = 0;
228 for(;;) { 230 for(;;) {
229 - c = getopt(argc, argv, "b:f:he6"); 231 + c = getopt(argc, argv, "F:b:f:he6");
230 if (c == -1) 232 if (c == -1)
231 break; 233 break;
232 switch(c) { 234 switch(c) {
233 case 'h': 235 case 'h':
234 help(); 236 help();
235 break; 237 break;
  238 + case 'F':
  239 + base_fmt = optarg;
  240 + break;
236 case 'b': 241 case 'b':
237 base_filename = optarg; 242 base_filename = optarg;
238 break; 243 break;
@@ -253,7 +258,15 @@ static int img_create(int argc, char **argv) @@ -253,7 +258,15 @@ static int img_create(int argc, char **argv)
253 size = 0; 258 size = 0;
254 if (base_filename) { 259 if (base_filename) {
255 BlockDriverState *bs; 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 bdrv_get_geometry(bs, &size); 270 bdrv_get_geometry(bs, &size);
258 size *= 512; 271 size *= 512;
259 bdrv_delete(bs); 272 bdrv_delete(bs);
@@ -284,9 +297,12 @@ static int img_create(int argc, char **argv) @@ -284,9 +297,12 @@ static int img_create(int argc, char **argv)
284 if (base_filename) { 297 if (base_filename) {
285 printf(", backing_file=%s", 298 printf(", backing_file=%s",
286 base_filename); 299 base_filename);
  300 + if (base_fmt)
  301 + printf(", backing_fmt=%s",
  302 + base_fmt);
287 } 303 }
288 printf(", size=%" PRIu64 " kB\n", size / 1024); 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 if (ret < 0) { 306 if (ret < 0) {
291 if (ret == -ENOTSUP) { 307 if (ret == -ENOTSUP) {
292 error("Formatting or formatting option not supported for file format '%s'", fmt); 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,7 +8,7 @@ usage: qemu-img command [command options]
8 8
9 The following commands are supported: 9 The following commands are supported:
10 @table @option 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 @item commit [-f @var{fmt}] @var{filename} 12 @item commit [-f @var{fmt}] @var{filename}
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} 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 @item info [-f @var{fmt}] @var{filename} 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,6 +27,8 @@ forces the output image to be created as a copy on write
27 image of the specified base image; @code{output_base_image} should have the same 27 image of the specified base image; @code{output_base_image} should have the same
28 content as the input's base image, however the path, image format, etc may 28 content as the input's base image, however the path, image format, etc may
29 differ 29 differ
  30 +@item base_fmt
  31 +is the disk image format of @var{base_image}. for more information look at @var{fmt}
30 @item fmt 32 @item fmt
31 is the disk image format. It is guessed automatically in most cases. The following formats are supported: 33 is the disk image format. It is guessed automatically in most cases. The following formats are supported:
32 34