qemu-img.c (53f76e5857b66dd7fa90efa9ea8326eff5aa693f) qemu-img.c (f88e1a4201e31cac0438df395dfcdd45ac35c17d)
1/*
2 * QEMU disk image utility
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 274 unchanged lines hidden (view full) ---

283static int img_create(int argc, char **argv)
284{
285 int c, ret = 0;
286 uint64_t img_size = -1;
287 const char *fmt = "raw";
288 const char *base_fmt = NULL;
289 const char *filename;
290 const char *base_filename = NULL;
1/*
2 * QEMU disk image utility
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights

--- 274 unchanged lines hidden (view full) ---

283static int img_create(int argc, char **argv)
284{
285 int c, ret = 0;
286 uint64_t img_size = -1;
287 const char *fmt = "raw";
288 const char *base_fmt = NULL;
289 const char *filename;
290 const char *base_filename = NULL;
291 BlockDriver *drv, *proto_drv;
292 QEMUOptionParameter *param = NULL, *create_options = NULL;
293 QEMUOptionParameter *backing_fmt = NULL;
294 char *options = NULL;
295
296 for(;;) {
297 c = getopt(argc, argv, "F:b:f:he6o:");
298 if (c == -1) {
299 break;
300 }
301 switch(c) {

--- 44 unchanged lines hidden (view full) ---

346 img_size = (uint64_t)sval;
347 }
348
349 if (options && !strcmp(options, "?")) {
350 ret = print_block_option_help(filename, fmt);
351 goto out;
352 }
353
291 char *options = NULL;
292
293 for(;;) {
294 c = getopt(argc, argv, "F:b:f:he6o:");
295 if (c == -1) {
296 break;
297 }
298 switch(c) {

--- 44 unchanged lines hidden (view full) ---

343 img_size = (uint64_t)sval;
344 }
345
346 if (options && !strcmp(options, "?")) {
347 ret = print_block_option_help(filename, fmt);
348 goto out;
349 }
350
354 /* Find driver and parse its options */
355 drv = bdrv_find_format(fmt);
356 if (!drv) {
357 error("Unknown file format '%s'", fmt);
358 ret = -1;
359 goto out;
360 }
361
362 proto_drv = bdrv_find_protocol(filename);
363 if (!proto_drv) {
364 error("Unknown protocol '%s'", filename);
365 ret = -1;
366 goto out;
367 }
368
369 create_options = append_option_parameters(create_options,
370 drv->create_options);
371 create_options = append_option_parameters(create_options,
372 proto_drv->create_options);
373
374 /* Create parameter list with default values */
375 param = parse_option_parameters("", create_options, param);
376
377 set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
378
379 /* Parse -o options */
380 if (options) {
381 param = parse_option_parameters(options, create_options, param);
382 if (param == NULL) {
383 error("Invalid options for file format '%s'.", fmt);
384 ret = -1;
385 goto out;
386 }
387 }
388
389 /* Add old-style options to parameters */
390 ret = add_old_style_options(fmt, param, base_filename, base_fmt);
391 if (ret < 0) {
392 goto out;
393 }
394
395 backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
396 if (backing_fmt && backing_fmt->value.s) {
397 if (!bdrv_find_format(backing_fmt->value.s)) {
398 error("Unknown backing file format '%s'",
399 backing_fmt->value.s);
400 ret = -1;
401 goto out;
402 }
403 }
404
405 // The size for the image must always be specified, with one exception:
406 // If we are using a backing file, we can obtain the size from there
407 if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
408
409 QEMUOptionParameter *backing_file =
410 get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
411
412 if (backing_file && backing_file->value.s) {
413 BlockDriverState *bs;
414 uint64_t size;
415 const char *fmt = NULL;
416 char buf[32];
417
418 if (backing_fmt && backing_fmt->value.s) {
419 fmt = backing_fmt->value.s;
420 }
421
422 bs = bdrv_new_open(backing_file->value.s, fmt, BDRV_O_FLAGS);
423 if (!bs) {
424 ret = -1;
425 goto out;
426 }
427 bdrv_get_geometry(bs, &size);
428 size *= 512;
429 bdrv_delete(bs);
430
431 snprintf(buf, sizeof(buf), "%" PRId64, size);
432 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
433 } else {
434 error("Image creation needs a size parameter");
435 ret = -1;
436 goto out;
437 }
438 }
439
440 printf("Formatting '%s', fmt=%s ", filename, fmt);
441 print_option_parameters(param);
442 puts("");
443
444 ret = bdrv_create(drv, filename, param);
445
446 if (ret < 0) {
447 if (ret == -ENOTSUP) {
448 error("Formatting or formatting option not supported for file format '%s'", fmt);
449 } else if (ret == -EFBIG) {
450 error("The image size is too large for file format '%s'", fmt);
451 } else {
452 error("%s: error while creating %s: %s", filename, fmt, strerror(-ret));
453 }
454 }
351 ret = bdrv_img_create(filename, fmt, base_filename, base_fmt,
352 options, img_size, BDRV_O_FLAGS);
455out:
353out:
456 free_option_parameters(create_options);
457 free_option_parameters(param);
458 if (ret) {
459 return 1;
460 }
461 return 0;
462}
463
464/*
465 * Checks an image for consistency. Exit codes:

--- 1169 unchanged lines hidden ---
354 if (ret) {
355 return 1;
356 }
357 return 0;
358}
359
360/*
361 * Checks an image for consistency. Exit codes:

--- 1169 unchanged lines hidden ---