Commit efa84d43cc9f4e21d564e7415778166735890bc1
Committed by
Anthony Liguori
1 parent
9ea2ea71
Convert qemu-img convert to new bdrv_create
This is part two of the qemu-img conversion. This really works the same as the previous conversion of qemu-img create: It introduces a new -o option for the generic approach and adds the old-style options to this option set. Signed-off-by: Kevin Wolf <kwolf@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing
1 changed file
with
65 additions
and
32 deletions
qemu-img.c
@@ -215,6 +215,32 @@ static BlockDriverState *bdrv_new_open(const char *filename, | @@ -215,6 +215,32 @@ static BlockDriverState *bdrv_new_open(const char *filename, | ||
215 | return bs; | 215 | return bs; |
216 | } | 216 | } |
217 | 217 | ||
218 | +static void add_old_style_options(const char *fmt, QEMUOptionParameter *list, | ||
219 | + int flags, const char *base_filename, const char *base_fmt) | ||
220 | +{ | ||
221 | + if (flags & BLOCK_FLAG_ENCRYPT) { | ||
222 | + if (set_option_parameter(list, BLOCK_OPT_ENCRYPT, "on")) { | ||
223 | + error("Encryption not supported for file format '%s'", fmt); | ||
224 | + } | ||
225 | + } | ||
226 | + if (flags & BLOCK_FLAG_COMPAT6) { | ||
227 | + if (set_option_parameter(list, BLOCK_OPT_COMPAT6, "on")) { | ||
228 | + error("VMDK version 6 not supported for file format '%s'", fmt); | ||
229 | + } | ||
230 | + } | ||
231 | + | ||
232 | + if (base_filename) { | ||
233 | + if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) { | ||
234 | + error("Backing file not supported for file format '%s'", fmt); | ||
235 | + } | ||
236 | + } | ||
237 | + if (base_fmt) { | ||
238 | + if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) { | ||
239 | + error("Backing file format not supported for file format '%s'", fmt); | ||
240 | + } | ||
241 | + } | ||
242 | +} | ||
243 | + | ||
218 | static int img_create(int argc, char **argv) | 244 | static int img_create(int argc, char **argv) |
219 | { | 245 | { |
220 | int c, ret, flags; | 246 | int c, ret, flags; |
@@ -279,27 +305,7 @@ static int img_create(int argc, char **argv) | @@ -279,27 +305,7 @@ static int img_create(int argc, char **argv) | ||
279 | } | 305 | } |
280 | 306 | ||
281 | /* Add old-style options to parameters */ | 307 | /* Add old-style options to parameters */ |
282 | - if (flags & BLOCK_FLAG_ENCRYPT) { | ||
283 | - if (set_option_parameter(param, BLOCK_OPT_ENCRYPT, "on")) { | ||
284 | - error("Encryption not supported for file format '%s'", fmt); | ||
285 | - } | ||
286 | - } | ||
287 | - if (flags & BLOCK_FLAG_COMPAT6) { | ||
288 | - if (set_option_parameter(param, BLOCK_OPT_COMPAT6, "on")) { | ||
289 | - error("VMDK version 6 not supported for file format '%s'", fmt); | ||
290 | - } | ||
291 | - } | ||
292 | - | ||
293 | - if (base_filename) { | ||
294 | - if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, base_filename)) { | ||
295 | - error("Backing file not supported for file format '%s'", fmt); | ||
296 | - } | ||
297 | - } | ||
298 | - if (base_fmt) { | ||
299 | - if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { | ||
300 | - error("Backing file format not supported for file format '%s'", fmt); | ||
301 | - } | ||
302 | - } | 308 | + add_old_style_options(fmt, param, flags, base_filename, base_fmt); |
303 | 309 | ||
304 | // The size for the image must always be specified, with one exception: | 310 | // The size for the image must always be specified, with one exception: |
305 | // If we are using a backing file, we can obtain the size from there | 311 | // If we are using a backing file, we can obtain the size from there |
@@ -525,13 +531,15 @@ static int img_convert(int argc, char **argv) | @@ -525,13 +531,15 @@ static int img_convert(int argc, char **argv) | ||
525 | uint8_t buf[IO_BUF_SIZE]; | 531 | uint8_t buf[IO_BUF_SIZE]; |
526 | const uint8_t *buf1; | 532 | const uint8_t *buf1; |
527 | BlockDriverInfo bdi; | 533 | BlockDriverInfo bdi; |
534 | + QEMUOptionParameter *param = NULL; | ||
535 | + char *options = NULL; | ||
528 | 536 | ||
529 | fmt = NULL; | 537 | fmt = NULL; |
530 | out_fmt = "raw"; | 538 | out_fmt = "raw"; |
531 | out_baseimg = NULL; | 539 | out_baseimg = NULL; |
532 | flags = 0; | 540 | flags = 0; |
533 | for(;;) { | 541 | for(;;) { |
534 | - c = getopt(argc, argv, "f:O:B:hce6"); | 542 | + c = getopt(argc, argv, "f:O:B:hce6o:"); |
535 | if (c == -1) | 543 | if (c == -1) |
536 | break; | 544 | break; |
537 | switch(c) { | 545 | switch(c) { |
@@ -556,6 +564,9 @@ static int img_convert(int argc, char **argv) | @@ -556,6 +564,9 @@ static int img_convert(int argc, char **argv) | ||
556 | case '6': | 564 | case '6': |
557 | flags |= BLOCK_FLAG_COMPAT6; | 565 | flags |= BLOCK_FLAG_COMPAT6; |
558 | break; | 566 | break; |
567 | + case 'o': | ||
568 | + options = optarg; | ||
569 | + break; | ||
559 | } | 570 | } |
560 | } | 571 | } |
561 | 572 | ||
@@ -580,19 +591,41 @@ static int img_convert(int argc, char **argv) | @@ -580,19 +591,41 @@ static int img_convert(int argc, char **argv) | ||
580 | total_sectors += bs_sectors; | 591 | total_sectors += bs_sectors; |
581 | } | 592 | } |
582 | 593 | ||
594 | + /* Find driver and parse its options */ | ||
583 | drv = bdrv_find_format(out_fmt); | 595 | drv = bdrv_find_format(out_fmt); |
584 | if (!drv) | 596 | if (!drv) |
585 | error("Unknown file format '%s'", out_fmt); | 597 | error("Unknown file format '%s'", out_fmt); |
586 | - if (flags & BLOCK_FLAG_COMPRESS && strcmp(drv->format_name, "qcow") && strcmp(drv->format_name, "qcow2")) | ||
587 | - error("Compression not supported for this file format"); | ||
588 | - if (flags & BLOCK_FLAG_ENCRYPT && strcmp(drv->format_name, "qcow") && strcmp(drv->format_name, "qcow2")) | ||
589 | - error("Encryption not supported for this file format"); | ||
590 | - if (flags & BLOCK_FLAG_COMPAT6 && strcmp(drv->format_name, "vmdk")) | ||
591 | - error("Alternative compatibility level not supported for this file format"); | ||
592 | - if (flags & BLOCK_FLAG_ENCRYPT && flags & BLOCK_FLAG_COMPRESS) | ||
593 | - error("Compression and encryption not supported at the same time"); | ||
594 | - | ||
595 | - ret = bdrv_create2(drv, out_filename, total_sectors, out_baseimg, NULL, flags); | 598 | + |
599 | + if (options) { | ||
600 | + param = parse_option_parameters(options, drv->create_options, param); | ||
601 | + if (param == NULL) { | ||
602 | + error("Invalid options for file format '%s'.", out_fmt); | ||
603 | + } | ||
604 | + } else { | ||
605 | + param = parse_option_parameters("", drv->create_options, param); | ||
606 | + } | ||
607 | + | ||
608 | + set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512); | ||
609 | + add_old_style_options(out_fmt, param, flags, out_baseimg, NULL); | ||
610 | + | ||
611 | + /* Check if compression is supported */ | ||
612 | + if (flags & BLOCK_FLAG_COMPRESS) { | ||
613 | + QEMUOptionParameter *encryption = | ||
614 | + get_option_parameter(param, BLOCK_OPT_ENCRYPT); | ||
615 | + | ||
616 | + if (!drv->bdrv_write_compressed) { | ||
617 | + error("Compression not supported for this file format"); | ||
618 | + } | ||
619 | + | ||
620 | + if (encryption && encryption->value.n) { | ||
621 | + error("Compression and encryption not supported at the same time"); | ||
622 | + } | ||
623 | + } | ||
624 | + | ||
625 | + /* Create the new image */ | ||
626 | + ret = bdrv_create(drv, out_filename, param); | ||
627 | + free_option_parameters(param); | ||
628 | + | ||
596 | if (ret < 0) { | 629 | if (ret < 0) { |
597 | if (ret == -ENOTSUP) { | 630 | if (ret == -ENOTSUP) { |
598 | error("Formatting not supported for file format '%s'", out_fmt); | 631 | error("Formatting not supported for file format '%s'", out_fmt); |