- // The size for the image must always be specified, with one exception:
- // If we are using a backing file, we can obtain the size from there
- if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
-
- QEMUOptionParameter *backing_file =
- get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
- QEMUOptionParameter *backing_fmt =
- get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
-
- if (backing_file && backing_file->value.s) {
- BlockDriverState *bs;
- uint64_t size;
- const char *fmt = NULL;
- char buf[32];
-
- if (backing_fmt && backing_fmt->value.s) {
- if (bdrv_find_format(backing_fmt->value.s)) {
- fmt = backing_fmt->value.s;
- } else {
- error("Unknown backing file format '%s'",
- backing_fmt->value.s);
- ret = -1;
- goto out;
- }
- }
-
- bs = bdrv_new_open(backing_file->value.s, fmt, BDRV_O_FLAGS);
- if (!bs) {
- ret = -1;
- goto out;
- }
- bdrv_get_geometry(bs, &size);
- size *= 512;
- bdrv_delete(bs);
-
- snprintf(buf, sizeof(buf), "%" PRId64, size);
- set_option_parameter(param, BLOCK_OPT_SIZE, buf);
- } else {
- error("Image creation needs a size parameter");
- ret = -1;
- goto out;
- }
- }
-
- printf("Formatting '%s', fmt=%s ", filename, fmt);
- print_option_parameters(param);
- puts("");
-
- ret = bdrv_create(drv, filename, param);
- free_option_parameters(create_options);
- free_option_parameters(param);
-
- if (ret < 0) {
- if (ret == -ENOTSUP) {
- error("Formatting or formatting option not supported for file format '%s'", fmt);
- } else if (ret == -EFBIG) {
- error("The image size is too large for file format '%s'", fmt);
- } else {
- error("%s: error while creating %s: %s", filename, fmt, strerror(-ret));
- }
- }