bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1 | /* |
bellard | fb43f4d | 2006-08-07 21:34:46 +0000 | [diff] [blame] | 2 | * QEMU disk image utility |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 68d0f70 | 2008-01-06 17:21:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 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 |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Eric Blake | c2a3d7d | 2017-07-21 08:50:47 -0500 | [diff] [blame] | 26 | #include <getopt.h> |
| 27 | |
Markus Armbruster | a8d2532 | 2019-05-23 16:35:08 +0200 | [diff] [blame] | 28 | #include "qemu-common.h" |
Fam Zheng | 67a1de0 | 2016-06-01 17:44:21 +0800 | [diff] [blame] | 29 | #include "qemu-version.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 30 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 31 | #include "qapi/qapi-visit-block-core.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 32 | #include "qapi/qobject-output-visitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 33 | #include "qapi/qmp/qjson.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 34 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | fc81fa1 | 2018-02-01 12:18:40 +0100 | [diff] [blame] | 35 | #include "qapi/qmp/qstring.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 36 | #include "qemu/cutils.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 37 | #include "qemu/config-file.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 38 | #include "qemu/option.h" |
| 39 | #include "qemu/error-report.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 40 | #include "qemu/log.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 41 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 42 | #include "qemu/module.h" |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 43 | #include "qemu/units.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 44 | #include "qom/object_interfaces.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 45 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 46 | #include "block/block_int.h" |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 47 | #include "block/blockjob.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 48 | #include "block/qapi.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 49 | #include "crypto/init.h" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 50 | #include "trace/control.h" |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 51 | |
Thomas Huth | 7e563bf | 2018-02-15 12:06:47 +0100 | [diff] [blame] | 52 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \ |
Thomas Huth | 0781dd6 | 2016-10-05 11:54:44 +0200 | [diff] [blame] | 53 | "\n" QEMU_COPYRIGHT "\n" |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 54 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 55 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 56 | const char *name; |
| 57 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 58 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 59 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 60 | enum { |
| 61 | OPTION_OUTPUT = 256, |
| 62 | OPTION_BACKING_CHAIN = 257, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 63 | OPTION_OBJECT = 258, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 64 | OPTION_IMAGE_OPTS = 259, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 65 | OPTION_PATTERN = 260, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 66 | OPTION_FLUSH_INTERVAL = 261, |
| 67 | OPTION_NO_DRAIN = 262, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 68 | OPTION_TARGET_IMAGE_OPTS = 263, |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 69 | OPTION_SIZE = 264, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 70 | OPTION_PREALLOCATION = 265, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 71 | OPTION_SHRINK = 266, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 72 | OPTION_SALVAGE = 267, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | typedef enum OutputFormat { |
| 76 | OFORMAT_JSON, |
| 77 | OFORMAT_HUMAN, |
| 78 | } OutputFormat; |
| 79 | |
Kevin Wolf | e699614 | 2016-03-15 13:03:11 +0100 | [diff] [blame] | 80 | /* Default to cache=writeback as data integrity is not important for qemu-img */ |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 81 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 82 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 83 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 84 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 85 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 88 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
| 89 | { |
| 90 | va_list ap; |
| 91 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 92 | va_start(ap, fmt); |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 93 | error_vreport(fmt, ap); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 94 | va_end(ap); |
| 95 | |
Markus Armbruster | e9e1d92 | 2019-04-17 21:06:27 +0200 | [diff] [blame] | 96 | error_printf("Try 'qemu-img --help' for more information\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 97 | exit(EXIT_FAILURE); |
| 98 | } |
| 99 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 100 | static void QEMU_NORETURN missing_argument(const char *option) |
| 101 | { |
| 102 | error_exit("missing argument for option '%s'", option); |
| 103 | } |
| 104 | |
| 105 | static void QEMU_NORETURN unrecognized_option(const char *option) |
| 106 | { |
| 107 | error_exit("unrecognized option '%s'", option); |
| 108 | } |
| 109 | |
blueswir1 | d2c639d | 2009-01-24 18:19:25 +0000 | [diff] [blame] | 110 | /* Please keep in synch with qemu-img.texi */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 111 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 112 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 113 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 114 | QEMU_IMG_VERSION |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 115 | "usage: qemu-img [standard options] command [command options]\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 116 | "QEMU disk image utility\n" |
| 117 | "\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 118 | " '-h', '--help' display this help and exit\n" |
| 119 | " '-V', '--version' output version information and exit\n" |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 120 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 121 | " specify tracing options\n" |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 122 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 123 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 124 | #define DEF(option, callback, arg_string) \ |
| 125 | " " arg_string "\n" |
| 126 | #include "qemu-img-cmds.h" |
| 127 | #undef DEF |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 128 | "\n" |
| 129 | "Command parameters:\n" |
| 130 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 131 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 132 | " manual page for a description of the object properties. The most common\n" |
| 133 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 134 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 135 | " 'fmt' is the disk image format. It is guessed automatically in most cases\n" |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 136 | " 'cache' is the cache mode used to write the output disk image, the valid\n" |
Liu Yuan | 80ccf93 | 2012-04-20 17:10:56 +0800 | [diff] [blame] | 137 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 138 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 139 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 140 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 141 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 142 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 143 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 144 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 145 | " 'output_filename' is the destination disk image filename\n" |
| 146 | " 'output_fmt' is the destination format\n" |
| 147 | " 'options' is a comma separated list of format specific options in a\n" |
| 148 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 149 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 150 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 151 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 152 | " '[ID_OR_NAME]'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 153 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 154 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
| 155 | " new backing file match exactly. The image doesn't need a working\n" |
| 156 | " backing file before rebasing in this case (useful for renaming the\n" |
| 157 | " backing file). For image creation, allow creating without attempting\n" |
| 158 | " to open the backing file.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 159 | " '-h' with or without a command shows this help and lists the supported formats\n" |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 160 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 161 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 162 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 163 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 164 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 165 | " unallocated or zero sectors, and the destination image will always be\n" |
| 166 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 167 | " '--output' takes the format in which the output must be done (human or json)\n" |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 168 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 169 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 170 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 171 | "Parameters to check subcommand:\n" |
| 172 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 173 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 174 | " kinds of errors, with a higher risk of choosing the wrong fix or\n" |
Stefan Weil | 0546b8c | 2012-08-10 22:03:25 +0200 | [diff] [blame] | 175 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 176 | "\n" |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 177 | "Parameters to convert subcommand:\n" |
| 178 | " '-m' specifies how many coroutines work in parallel during the convert\n" |
| 179 | " process (defaults to 8)\n" |
| 180 | " '-W' allow to write to the target out of order rather than sequential\n" |
| 181 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 182 | "Parameters to snapshot subcommand:\n" |
| 183 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 184 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 185 | " '-c' creates a snapshot\n" |
| 186 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 187 | " '-l' lists all snapshots in the given image\n" |
| 188 | "\n" |
| 189 | "Parameters to compare subcommand:\n" |
| 190 | " '-f' first image format\n" |
| 191 | " '-F' second image format\n" |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 192 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
| 193 | "\n" |
| 194 | "Parameters to dd subcommand:\n" |
| 195 | " 'bs=BYTES' read and write up to BYTES bytes at a time " |
| 196 | "(default: 512)\n" |
| 197 | " 'count=N' copy only N input blocks\n" |
| 198 | " 'if=FILE' read from FILE\n" |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 199 | " 'of=FILE' write to FILE\n" |
| 200 | " 'skip=N' skip N bs-sized blocks at the start of input\n"; |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 201 | |
| 202 | printf("%s\nSupported formats:", help_msg); |
Andrey Shinkevich | 9ac404c | 2019-03-07 16:33:58 +0300 | [diff] [blame] | 203 | bdrv_iterate_format(format_print, NULL, false); |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 204 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 205 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 208 | static QemuOptsList qemu_object_opts = { |
| 209 | .name = "object", |
| 210 | .implied_opt_name = "qom-type", |
| 211 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), |
| 212 | .desc = { |
| 213 | { } |
| 214 | }, |
| 215 | }; |
| 216 | |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 217 | static bool qemu_img_object_print_help(const char *type, QemuOpts *opts) |
| 218 | { |
| 219 | if (user_creatable_print_help(type, opts)) { |
| 220 | exit(0); |
| 221 | } |
| 222 | return true; |
| 223 | } |
| 224 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 225 | static QemuOptsList qemu_source_opts = { |
| 226 | .name = "source", |
| 227 | .implied_opt_name = "file", |
| 228 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 229 | .desc = { |
| 230 | { } |
| 231 | }, |
| 232 | }; |
| 233 | |
Stefan Weil | 7c30f65 | 2013-06-16 17:01:05 +0200 | [diff] [blame] | 234 | static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 235 | { |
| 236 | int ret = 0; |
| 237 | if (!quiet) { |
| 238 | va_list args; |
| 239 | va_start(args, fmt); |
| 240 | ret = vprintf(fmt, args); |
| 241 | va_end(args); |
| 242 | } |
| 243 | return ret; |
| 244 | } |
| 245 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 246 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 247 | static int print_block_option_help(const char *filename, const char *fmt) |
| 248 | { |
| 249 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 250 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 251 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 252 | |
| 253 | /* Find driver and parse its options */ |
| 254 | drv = bdrv_find_format(fmt); |
| 255 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 256 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 257 | return 1; |
| 258 | } |
| 259 | |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 260 | if (!drv->create_opts) { |
| 261 | error_report("Format driver '%s' does not support image creation", fmt); |
| 262 | return 1; |
| 263 | } |
| 264 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 265 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 266 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 267 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 268 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 269 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 270 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 271 | return 1; |
| 272 | } |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 273 | if (!proto_drv->create_opts) { |
Max Reitz | f099887 | 2018-11-19 11:19:20 +0100 | [diff] [blame] | 274 | error_report("Protocol driver '%s' does not support image creation", |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 275 | proto_drv->format_name); |
Max Reitz | 3ecd5a4 | 2018-11-19 11:19:21 +0100 | [diff] [blame] | 276 | qemu_opts_free(create_opts); |
Max Reitz | d402b6a | 2018-05-09 23:00:21 +0200 | [diff] [blame] | 277 | return 1; |
| 278 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 279 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 282 | if (filename) { |
| 283 | printf("Supported options:\n"); |
| 284 | } else { |
| 285 | printf("Supported %s options:\n", fmt); |
| 286 | } |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 287 | qemu_opts_print_help(create_opts, false); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 288 | qemu_opts_free(create_opts); |
Max Reitz | f4619af | 2019-04-13 17:20:37 +0200 | [diff] [blame] | 289 | |
| 290 | if (!filename) { |
| 291 | printf("\n" |
| 292 | "The protocol level may support further options.\n" |
| 293 | "Specify the target filename to include those options.\n"); |
| 294 | } |
| 295 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 299 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 300 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 301 | QemuOpts *opts, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 302 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 303 | { |
| 304 | QDict *options; |
| 305 | Error *local_err = NULL; |
| 306 | BlockBackend *blk; |
| 307 | options = qemu_opts_to_qdict(opts, NULL); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 308 | if (force_share) { |
| 309 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 310 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 311 | error_report("--force-share/-U conflicts with image options"); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 312 | qobject_unref(options); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 313 | return NULL; |
| 314 | } |
Max Reitz | 4615f87 | 2018-05-02 22:20:50 +0200 | [diff] [blame] | 315 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 316 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 317 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 318 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 319 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 320 | return NULL; |
| 321 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 322 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 323 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 324 | return blk; |
| 325 | } |
| 326 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 327 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 328 | QDict *options, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 329 | const char *fmt, int flags, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 330 | bool writethrough, bool quiet, |
| 331 | bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 332 | { |
| 333 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 334 | Error *local_err = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 335 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 336 | if (!options) { |
| 337 | options = qdict_new(); |
| 338 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 339 | if (fmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 340 | qdict_put_str(options, "driver", fmt); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 341 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 342 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 343 | if (force_share) { |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 344 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 345 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 346 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 347 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 348 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 349 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 350 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 351 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 352 | |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 353 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 356 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 357 | static int img_add_key_secrets(void *opaque, |
| 358 | const char *name, const char *value, |
| 359 | Error **errp) |
| 360 | { |
| 361 | QDict *options = opaque; |
| 362 | |
| 363 | if (g_str_has_suffix(name, "key-secret")) { |
Eric Blake | 187f47e | 2017-06-24 12:10:07 -0600 | [diff] [blame] | 364 | qdict_put_str(options, name, value); |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 370 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 371 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 372 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 373 | const char *fmt, int flags, bool writethrough, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 374 | bool quiet, bool force_share) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 375 | { |
| 376 | BlockBackend *blk; |
| 377 | if (image_opts) { |
| 378 | QemuOpts *opts; |
| 379 | if (fmt) { |
| 380 | error_report("--image-opts and --format are mutually exclusive"); |
| 381 | return NULL; |
| 382 | } |
| 383 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 384 | filename, true); |
| 385 | if (!opts) { |
| 386 | return NULL; |
| 387 | } |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 388 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
| 389 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 390 | } else { |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 391 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 392 | force_share); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 393 | } |
| 394 | return blk; |
| 395 | } |
| 396 | |
| 397 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 398 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 399 | const char *base_filename, |
| 400 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 401 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 402 | Error *err = NULL; |
| 403 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 404 | if (base_filename) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 405 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 406 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 407 | error_report("Backing file not supported for file format '%s'", |
| 408 | fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 409 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 410 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | if (base_fmt) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 414 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 415 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 416 | error_report("Backing file format not supported for file " |
| 417 | "format '%s'", fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 418 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 419 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 420 | } |
| 421 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 422 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 425 | static int64_t cvtnum(const char *s) |
| 426 | { |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 427 | int err; |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 428 | uint64_t value; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 429 | |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 430 | err = qemu_strtosz(s, NULL, &value); |
| 431 | if (err < 0) { |
| 432 | return err; |
| 433 | } |
Markus Armbruster | f46bfdb | 2017-02-21 21:14:07 +0100 | [diff] [blame] | 434 | if (value > INT64_MAX) { |
| 435 | return -ERANGE; |
| 436 | } |
Markus Armbruster | f17fd4f | 2017-02-21 21:14:06 +0100 | [diff] [blame] | 437 | return value; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 438 | } |
| 439 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 440 | static int img_create(int argc, char **argv) |
| 441 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 442 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 443 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 444 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 445 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 446 | const char *filename; |
| 447 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 448 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 449 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 450 | bool quiet = false; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 451 | int flags = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 452 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 453 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 454 | static const struct option long_options[] = { |
| 455 | {"help", no_argument, 0, 'h'}, |
| 456 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 457 | {0, 0, 0, 0} |
| 458 | }; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 459 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 460 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 461 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 462 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 463 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 464 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 465 | case ':': |
| 466 | missing_argument(argv[optind - 1]); |
| 467 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 468 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 469 | unrecognized_option(argv[optind - 1]); |
| 470 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 471 | case 'h': |
| 472 | help(); |
| 473 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 474 | case 'F': |
| 475 | base_fmt = optarg; |
| 476 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 477 | case 'b': |
| 478 | base_filename = optarg; |
| 479 | break; |
| 480 | case 'f': |
| 481 | fmt = optarg; |
| 482 | break; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 483 | case 'o': |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 484 | if (!is_valid_option_list(optarg)) { |
| 485 | error_report("Invalid option list: %s", optarg); |
| 486 | goto fail; |
| 487 | } |
| 488 | if (!options) { |
| 489 | options = g_strdup(optarg); |
| 490 | } else { |
| 491 | char *old_options = options; |
| 492 | options = g_strdup_printf("%s,%s", options, optarg); |
| 493 | g_free(old_options); |
| 494 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 495 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 496 | case 'q': |
| 497 | quiet = true; |
| 498 | break; |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 499 | case 'u': |
| 500 | flags |= BDRV_O_NO_BACKING; |
| 501 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 502 | case OPTION_OBJECT: { |
| 503 | QemuOpts *opts; |
| 504 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 505 | optarg, true); |
| 506 | if (!opts) { |
| 507 | goto fail; |
| 508 | } |
| 509 | } break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 512 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 513 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 514 | filename = (optind < argc) ? argv[optind] : NULL; |
| 515 | if (options && has_help_option(options)) { |
| 516 | g_free(options); |
| 517 | return print_block_option_help(filename, fmt); |
| 518 | } |
| 519 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 520 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 521 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 522 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 523 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 524 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 525 | if (qemu_opts_foreach(&qemu_object_opts, |
| 526 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 527 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 528 | goto fail; |
| 529 | } |
| 530 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 531 | /* Get image size, if specified */ |
| 532 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 533 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 534 | |
| 535 | sval = cvtnum(argv[optind++]); |
| 536 | if (sval < 0) { |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 537 | if (sval == -ERANGE) { |
| 538 | error_report("Image size must be less than 8 EiB!"); |
| 539 | } else { |
| 540 | error_report("Invalid image size specified! You may use k, M, " |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 541 | "G, T, P or E suffixes for "); |
| 542 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 543 | "petabytes and exabytes."); |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 544 | } |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 545 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 546 | } |
| 547 | img_size = (uint64_t)sval; |
| 548 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 549 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 550 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 551 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 552 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 553 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
John Snow | 6e6e55f | 2017-07-17 20:34:22 -0400 | [diff] [blame] | 554 | options, img_size, flags, quiet, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 555 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 556 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 557 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 558 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 559 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 560 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 561 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 562 | |
| 563 | fail: |
| 564 | g_free(options); |
| 565 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 568 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 569 | { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 570 | QString *str; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 571 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 572 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 573 | |
| 574 | visit_type_ImageCheck(v, NULL, &check, &error_abort); |
| 575 | visit_complete(v, &obj); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 576 | str = qobject_to_json_pretty(obj); |
| 577 | assert(str != NULL); |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 578 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 579 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 580 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 581 | qobject_unref(str); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 582 | } |
| 583 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 584 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 585 | { |
| 586 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 587 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 588 | } else { |
| 589 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 590 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 591 | "Data may be corrupted, or further writes to the image " |
| 592 | "may corrupt it.\n", |
| 593 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 597 | qprintf(quiet, |
| 598 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 599 | "This means waste of disk space, but no harm to data.\n", |
| 600 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 604 | qprintf(quiet, |
| 605 | "\n%" PRId64 |
| 606 | " internal errors have occurred during the check.\n", |
| 607 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 612 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 613 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 614 | check->allocated_clusters, check->total_clusters, |
| 615 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 616 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 617 | check->compressed_clusters * 100.0 / |
| 618 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 622 | qprintf(quiet, |
| 623 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | static int collect_image_check(BlockDriverState *bs, |
| 628 | ImageCheck *check, |
| 629 | const char *filename, |
| 630 | const char *fmt, |
| 631 | int fix) |
| 632 | { |
| 633 | int ret; |
| 634 | BdrvCheckResult result; |
| 635 | |
| 636 | ret = bdrv_check(bs, &result, fix); |
| 637 | if (ret < 0) { |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | check->filename = g_strdup(filename); |
| 642 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 643 | check->check_errors = result.check_errors; |
| 644 | check->corruptions = result.corruptions; |
| 645 | check->has_corruptions = result.corruptions != 0; |
| 646 | check->leaks = result.leaks; |
| 647 | check->has_leaks = result.leaks != 0; |
| 648 | check->corruptions_fixed = result.corruptions_fixed; |
| 649 | check->has_corruptions_fixed = result.corruptions != 0; |
| 650 | check->leaks_fixed = result.leaks_fixed; |
| 651 | check->has_leaks_fixed = result.leaks != 0; |
| 652 | check->image_end_offset = result.image_end_offset; |
| 653 | check->has_image_end_offset = result.image_end_offset != 0; |
| 654 | check->total_clusters = result.bfi.total_clusters; |
| 655 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 656 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 657 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 658 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 659 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 660 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 661 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 666 | /* |
| 667 | * Checks an image for consistency. Exit codes: |
| 668 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 669 | * 0 - Check completed, image is good |
| 670 | * 1 - Check not completed because of internal errors |
| 671 | * 2 - Check completed, image is corrupted |
| 672 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 673 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 674 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 675 | static int img_check(int argc, char **argv) |
| 676 | { |
| 677 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 678 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 679 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 680 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 681 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 682 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 683 | int flags = BDRV_O_CHECK; |
| 684 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 685 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 686 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 687 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 688 | bool force_share = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 689 | |
| 690 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 691 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 692 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 693 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 694 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 695 | int option_index = 0; |
| 696 | static const struct option long_options[] = { |
| 697 | {"help", no_argument, 0, 'h'}, |
| 698 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 699 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 700 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 701 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 702 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 703 | {"force-share", no_argument, 0, 'U'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 704 | {0, 0, 0, 0} |
| 705 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 706 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 707 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 708 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 709 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 710 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 711 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 712 | case ':': |
| 713 | missing_argument(argv[optind - 1]); |
| 714 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 715 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 716 | unrecognized_option(argv[optind - 1]); |
| 717 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 718 | case 'h': |
| 719 | help(); |
| 720 | break; |
| 721 | case 'f': |
| 722 | fmt = optarg; |
| 723 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 724 | case 'r': |
| 725 | flags |= BDRV_O_RDWR; |
| 726 | |
| 727 | if (!strcmp(optarg, "leaks")) { |
| 728 | fix = BDRV_FIX_LEAKS; |
| 729 | } else if (!strcmp(optarg, "all")) { |
| 730 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 731 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 732 | error_exit("Unknown option value for -r " |
| 733 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 734 | } |
| 735 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 736 | case OPTION_OUTPUT: |
| 737 | output = optarg; |
| 738 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 739 | case 'T': |
| 740 | cache = optarg; |
| 741 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 742 | case 'q': |
| 743 | quiet = true; |
| 744 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 745 | case 'U': |
| 746 | force_share = true; |
| 747 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 748 | case OPTION_OBJECT: { |
| 749 | QemuOpts *opts; |
| 750 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 751 | optarg, true); |
| 752 | if (!opts) { |
| 753 | return 1; |
| 754 | } |
| 755 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 756 | case OPTION_IMAGE_OPTS: |
| 757 | image_opts = true; |
| 758 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 761 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 762 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 763 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 764 | filename = argv[optind++]; |
| 765 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 766 | if (output && !strcmp(output, "json")) { |
| 767 | output_format = OFORMAT_JSON; |
| 768 | } else if (output && !strcmp(output, "human")) { |
| 769 | output_format = OFORMAT_HUMAN; |
| 770 | } else if (output) { |
| 771 | error_report("--output must be used with human or json as argument."); |
| 772 | return 1; |
| 773 | } |
| 774 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 775 | if (qemu_opts_foreach(&qemu_object_opts, |
| 776 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 777 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 778 | return 1; |
| 779 | } |
| 780 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 781 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 782 | if (ret < 0) { |
| 783 | error_report("Invalid source cache option: %s", cache); |
| 784 | return 1; |
| 785 | } |
| 786 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 787 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 788 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 789 | if (!blk) { |
| 790 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 791 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 792 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 793 | |
| 794 | check = g_new0(ImageCheck, 1); |
| 795 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 796 | |
| 797 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 798 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 799 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 800 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 803 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 804 | int corruptions_fixed, leaks_fixed; |
| 805 | |
| 806 | leaks_fixed = check->leaks_fixed; |
| 807 | corruptions_fixed = check->corruptions_fixed; |
| 808 | |
| 809 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 810 | qprintf(quiet, |
| 811 | "The following inconsistencies were found and repaired:\n\n" |
| 812 | " %" PRId64 " leaked clusters\n" |
| 813 | " %" PRId64 " corruptions\n\n" |
| 814 | "Double checking the fixed image now...\n", |
| 815 | check->leaks_fixed, |
| 816 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 820 | |
| 821 | check->leaks_fixed = leaks_fixed; |
| 822 | check->corruptions_fixed = corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 825 | if (!ret) { |
| 826 | switch (output_format) { |
| 827 | case OFORMAT_HUMAN: |
| 828 | dump_human_image_check(check, quiet); |
| 829 | break; |
| 830 | case OFORMAT_JSON: |
| 831 | dump_json_image_check(check, quiet); |
| 832 | break; |
| 833 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 837 | if (ret) { |
| 838 | error_report("Check failed: %s", strerror(-ret)); |
| 839 | } else { |
| 840 | error_report("Check failed"); |
| 841 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 842 | ret = 1; |
| 843 | goto fail; |
| 844 | } |
| 845 | |
| 846 | if (check->corruptions) { |
| 847 | ret = 2; |
| 848 | } else if (check->leaks) { |
| 849 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 850 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 851 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 854 | fail: |
| 855 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 856 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 857 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 860 | typedef struct CommonBlockJobCBInfo { |
| 861 | BlockDriverState *bs; |
| 862 | Error **errp; |
| 863 | } CommonBlockJobCBInfo; |
| 864 | |
| 865 | static void common_block_job_cb(void *opaque, int ret) |
| 866 | { |
| 867 | CommonBlockJobCBInfo *cbi = opaque; |
| 868 | |
| 869 | if (ret < 0) { |
| 870 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 871 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static void run_block_job(BlockJob *job, Error **errp) |
| 875 | { |
Kevin Wolf | b75536c | 2016-04-18 17:30:17 +0200 | [diff] [blame] | 876 | AioContext *aio_context = blk_get_aio_context(job->blk); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 877 | int ret = 0; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 878 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 879 | aio_context_acquire(aio_context); |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 880 | job_ref(&job->job); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 881 | do { |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 882 | float progress = 0.0f; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 883 | aio_poll(aio_context, true); |
Kevin Wolf | 30a5c88 | 2018-05-04 12:17:20 +0200 | [diff] [blame] | 884 | if (job->job.progress_total) { |
| 885 | progress = (float)job->job.progress_current / |
| 886 | job->job.progress_total * 100.f; |
| 887 | } |
| 888 | qemu_progress_print(progress, 0); |
Kevin Wolf | df956ae | 2018-04-25 15:09:58 +0200 | [diff] [blame] | 889 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 890 | |
Kevin Wolf | dbe5e6c | 2018-04-19 13:04:01 +0200 | [diff] [blame] | 891 | if (!job_is_completed(&job->job)) { |
Kevin Wolf | 3d70ff5 | 2018-04-24 16:13:52 +0200 | [diff] [blame] | 892 | ret = job_complete_sync(&job->job, errp); |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 893 | } else { |
Kevin Wolf | 4ad3518 | 2018-04-19 17:30:16 +0200 | [diff] [blame] | 894 | ret = job->job.ret; |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 895 | } |
Kevin Wolf | 80fa2c7 | 2018-04-13 18:50:05 +0200 | [diff] [blame] | 896 | job_unref(&job->job); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 897 | aio_context_release(aio_context); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 898 | |
sochin.jiang | 4172a00 | 2017-06-15 14:47:33 +0800 | [diff] [blame] | 899 | /* publish completion progress only when success */ |
| 900 | if (!ret) { |
| 901 | qemu_progress_print(100.f, 0); |
| 902 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 903 | } |
| 904 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 905 | static int img_commit(int argc, char **argv) |
| 906 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 907 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 908 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 909 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 910 | BlockDriverState *bs, *base_bs; |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 911 | BlockJob *job; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 912 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 913 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 914 | Error *local_err = NULL; |
| 915 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 916 | bool image_opts = false; |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 917 | AioContext *aio_context; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 918 | |
| 919 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 920 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 921 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 922 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 923 | static const struct option long_options[] = { |
| 924 | {"help", no_argument, 0, 'h'}, |
| 925 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 926 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 927 | {0, 0, 0, 0} |
| 928 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 929 | c = getopt_long(argc, argv, ":f:ht:b:dpq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 930 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 931 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 932 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 933 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 934 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 935 | case ':': |
| 936 | missing_argument(argv[optind - 1]); |
| 937 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 938 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 939 | unrecognized_option(argv[optind - 1]); |
| 940 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 941 | case 'h': |
| 942 | help(); |
| 943 | break; |
| 944 | case 'f': |
| 945 | fmt = optarg; |
| 946 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 947 | case 't': |
| 948 | cache = optarg; |
| 949 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 950 | case 'b': |
| 951 | base = optarg; |
| 952 | /* -b implies -d */ |
| 953 | drop = true; |
| 954 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 955 | case 'd': |
| 956 | drop = true; |
| 957 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 958 | case 'p': |
| 959 | progress = true; |
| 960 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 961 | case 'q': |
| 962 | quiet = true; |
| 963 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 964 | case OPTION_OBJECT: { |
| 965 | QemuOpts *opts; |
| 966 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 967 | optarg, true); |
| 968 | if (!opts) { |
| 969 | return 1; |
| 970 | } |
| 971 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 972 | case OPTION_IMAGE_OPTS: |
| 973 | image_opts = true; |
| 974 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 975 | } |
| 976 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 977 | |
| 978 | /* Progress is not shown in Quiet mode */ |
| 979 | if (quiet) { |
| 980 | progress = false; |
| 981 | } |
| 982 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 983 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 984 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 985 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 986 | filename = argv[optind++]; |
| 987 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 988 | if (qemu_opts_foreach(&qemu_object_opts, |
| 989 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 990 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 991 | return 1; |
| 992 | } |
| 993 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 994 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 995 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 996 | if (ret < 0) { |
| 997 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 998 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 999 | } |
| 1000 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1001 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 1002 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1003 | if (!blk) { |
| 1004 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1005 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1006 | bs = blk_bs(blk); |
| 1007 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1008 | qemu_progress_init(progress, 1.f); |
| 1009 | qemu_progress_print(0.f, 100); |
| 1010 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1011 | if (base) { |
| 1012 | base_bs = bdrv_find_backing_image(bs, base); |
| 1013 | if (!base_bs) { |
Max Reitz | 6b33f3a | 2016-12-01 03:05:08 +0100 | [diff] [blame] | 1014 | error_setg(&local_err, |
| 1015 | "Did not find '%s' in the backing chain of '%s'", |
| 1016 | base, filename); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1017 | goto done; |
| 1018 | } |
| 1019 | } else { |
| 1020 | /* This is different from QMP, which by default uses the deepest file in |
| 1021 | * the backing chain (i.e., the very base); however, the traditional |
| 1022 | * behavior of qemu-img commit is using the immediate backing file. */ |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 1023 | base_bs = backing_bs(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 1024 | if (!base_bs) { |
| 1025 | error_setg(&local_err, "Image does not have a backing file"); |
| 1026 | goto done; |
| 1027 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1030 | cbi = (CommonBlockJobCBInfo){ |
| 1031 | .errp = &local_err, |
| 1032 | .bs = bs, |
| 1033 | }; |
| 1034 | |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1035 | aio_context = bdrv_get_aio_context(bs); |
| 1036 | aio_context_acquire(aio_context); |
Kevin Wolf | bb02b65 | 2018-04-19 17:54:56 +0200 | [diff] [blame] | 1037 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0, |
Kevin Wolf | 0db832f | 2017-02-20 18:10:05 +0100 | [diff] [blame] | 1038 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
Fam Zheng | 78bbd91 | 2017-04-21 20:27:04 +0800 | [diff] [blame] | 1039 | &cbi, false, &local_err); |
Paolo Bonzini | 9e944cb | 2016-10-27 12:49:04 +0200 | [diff] [blame] | 1040 | aio_context_release(aio_context); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1041 | if (local_err) { |
| 1042 | goto done; |
| 1043 | } |
| 1044 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1045 | /* When the block job completes, the BlockBackend reference will point to |
| 1046 | * the old backing file. In order to avoid that the top image is already |
| 1047 | * deleted, so we can still empty it afterwards, increment the reference |
| 1048 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1049 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1050 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1053 | job = block_job_get("commit"); |
Liam Merwick | 2e2db26 | 2018-11-05 21:38:37 +0000 | [diff] [blame] | 1054 | assert(job); |
Kevin Wolf | 4ef85a9 | 2017-01-25 19:16:34 +0100 | [diff] [blame] | 1055 | run_block_job(job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1056 | if (local_err) { |
| 1057 | goto unref_backing; |
| 1058 | } |
| 1059 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1060 | if (!drop && bs->drv->bdrv_make_empty) { |
| 1061 | ret = bs->drv->bdrv_make_empty(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1062 | if (ret) { |
| 1063 | error_setg_errno(&local_err, -ret, "Could not empty %s", |
| 1064 | filename); |
| 1065 | goto unref_backing; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | unref_backing: |
| 1070 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 1071 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 1072 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1073 | |
| 1074 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 1075 | qemu_progress_end(); |
| 1076 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1077 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1078 | |
| 1079 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 1080 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1081 | return 1; |
| 1082 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 1083 | |
| 1084 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1085 | return 0; |
| 1086 | } |
| 1087 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 1088 | /* |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1089 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index |
| 1090 | * of the first sector boundary within buf where the sector contains a |
| 1091 | * non-zero byte. This function is robust to a buffer that is not |
| 1092 | * sector-aligned. |
| 1093 | */ |
| 1094 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) |
| 1095 | { |
| 1096 | int64_t i; |
| 1097 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); |
| 1098 | |
| 1099 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { |
| 1100 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { |
| 1101 | return i; |
| 1102 | } |
| 1103 | } |
| 1104 | if (i < n && !buffer_is_zero(buf + i, n - end)) { |
| 1105 | return i; |
| 1106 | } |
| 1107 | return -1; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1111 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1112 | * a non-NUL byte. |
| 1113 | * |
| 1114 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1115 | * the first one) that are known to be in the same allocated/unallocated state. |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1116 | * The function will try to align the end offset to alignment boundaries so |
| 1117 | * that the request will at least end aligned and consequtive requests will |
| 1118 | * also start at an aligned offset. |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1119 | */ |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1120 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
| 1121 | int64_t sector_num, int alignment) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1122 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1123 | bool is_zero; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1124 | int i, tail; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1125 | |
| 1126 | if (n <= 0) { |
| 1127 | *pnum = 0; |
| 1128 | return 0; |
| 1129 | } |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1130 | is_zero = buffer_is_zero(buf, 512); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1131 | for(i = 1; i < n; i++) { |
| 1132 | buf += 512; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1133 | if (is_zero != buffer_is_zero(buf, 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1134 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1135 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1136 | } |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1137 | |
| 1138 | tail = (sector_num + i) & (alignment - 1); |
| 1139 | if (tail) { |
| 1140 | if (is_zero && i <= tail) { |
| 1141 | /* treat unallocated areas which only consist |
| 1142 | * of a small tail as allocated. */ |
| 1143 | is_zero = false; |
| 1144 | } |
| 1145 | if (!is_zero) { |
| 1146 | /* align up end offset of allocated areas. */ |
| 1147 | i += alignment - tail; |
| 1148 | i = MIN(i, n); |
| 1149 | } else { |
| 1150 | /* align down end offset of zero areas. */ |
| 1151 | i -= tail; |
| 1152 | } |
| 1153 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1154 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1155 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1158 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1159 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1160 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1161 | * breaking up write requests for only small sparse areas. |
| 1162 | */ |
| 1163 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1164 | int min, int64_t sector_num, int alignment) |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1165 | { |
| 1166 | int ret; |
| 1167 | int num_checked, num_used; |
| 1168 | |
| 1169 | if (n < min) { |
| 1170 | min = n; |
| 1171 | } |
| 1172 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1173 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1174 | if (!ret) { |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
| 1178 | num_used = *pnum; |
| 1179 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1180 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1181 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1182 | num_checked = num_used; |
| 1183 | |
| 1184 | while (n > 0) { |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1185 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1186 | |
| 1187 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1188 | n -= *pnum; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1189 | sector_num += *pnum; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1190 | num_checked += *pnum; |
| 1191 | if (ret) { |
| 1192 | num_used = num_checked; |
| 1193 | } else if (*pnum >= min) { |
| 1194 | break; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | *pnum = num_used; |
| 1199 | return 1; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1203 | * Compares two buffers sector by sector. Returns 0 if the first |
| 1204 | * sector of each buffer matches, non-zero otherwise. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1205 | * |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1206 | * pnum is set to the sector-aligned size of the buffer prefix that |
| 1207 | * has the same matching status as the first sector. |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1208 | */ |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1209 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
| 1210 | int64_t bytes, int64_t *pnum) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1211 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1212 | bool res; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1213 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1214 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1215 | assert(bytes > 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1216 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1217 | res = !!memcmp(buf1, buf2, i); |
| 1218 | while (i < bytes) { |
| 1219 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1220 | |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1221 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1222 | break; |
| 1223 | } |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1224 | i += len; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | *pnum = i; |
| 1228 | return res; |
| 1229 | } |
| 1230 | |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 1231 | #define IO_BUF_SIZE (2 * MiB) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1232 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1233 | /* |
| 1234 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1235 | * |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1236 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
| 1237 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison |
| 1238 | * failure), and 4 on error (the exit status for read errors), after emitting |
| 1239 | * an error message. |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1240 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1241 | * @param blk: BlockBackend for the image |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1242 | * @param offset: Starting offset to check |
| 1243 | * @param bytes: Number of bytes to check |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1244 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1245 | * @param buffer: Allocated buffer for storing read data |
| 1246 | * @param quiet: Flag for quiet mode |
| 1247 | */ |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1248 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
| 1249 | int64_t bytes, const char *filename, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1250 | uint8_t *buffer, bool quiet) |
| 1251 | { |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1252 | int ret = 0; |
| 1253 | int64_t idx; |
| 1254 | |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1255 | ret = blk_pread(blk, offset, buffer, bytes); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1256 | if (ret < 0) { |
| 1257 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1258 | offset, filename, strerror(-ret)); |
Eric Blake | 0608e40 | 2017-10-11 22:47:12 -0500 | [diff] [blame] | 1259 | return 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1260 | } |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1261 | idx = find_nonzero(buffer, bytes); |
Eric Blake | debb38a | 2017-10-11 22:47:11 -0500 | [diff] [blame] | 1262 | if (idx >= 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1263 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | c41508e | 2017-10-11 22:47:13 -0500 | [diff] [blame] | 1264 | offset + idx); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1265 | return 1; |
| 1266 | } |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * Compares two images. Exit codes: |
| 1273 | * |
| 1274 | * 0 - Images are identical |
| 1275 | * 1 - Images differ |
| 1276 | * >1 - Error occurred |
| 1277 | */ |
| 1278 | static int img_compare(int argc, char **argv) |
| 1279 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1280 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1281 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1282 | BlockDriverState *bs1, *bs2; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1283 | int64_t total_size1, total_size2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1284 | uint8_t *buf1 = NULL, *buf2 = NULL; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1285 | int64_t pnum1, pnum2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1286 | int allocated1, allocated2; |
| 1287 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1288 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1289 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1290 | bool writethrough; |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1291 | int64_t total_size; |
| 1292 | int64_t offset = 0; |
| 1293 | int64_t chunk; |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1294 | int c; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1295 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1296 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1297 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1298 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1299 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1300 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1301 | static const struct option long_options[] = { |
| 1302 | {"help", no_argument, 0, 'h'}, |
| 1303 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1304 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1305 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1306 | {0, 0, 0, 0} |
| 1307 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1308 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1309 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1310 | if (c == -1) { |
| 1311 | break; |
| 1312 | } |
| 1313 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1314 | case ':': |
| 1315 | missing_argument(argv[optind - 1]); |
| 1316 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1317 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1318 | unrecognized_option(argv[optind - 1]); |
| 1319 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1320 | case 'h': |
| 1321 | help(); |
| 1322 | break; |
| 1323 | case 'f': |
| 1324 | fmt1 = optarg; |
| 1325 | break; |
| 1326 | case 'F': |
| 1327 | fmt2 = optarg; |
| 1328 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1329 | case 'T': |
| 1330 | cache = optarg; |
| 1331 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1332 | case 'p': |
| 1333 | progress = true; |
| 1334 | break; |
| 1335 | case 'q': |
| 1336 | quiet = true; |
| 1337 | break; |
| 1338 | case 's': |
| 1339 | strict = true; |
| 1340 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1341 | case 'U': |
| 1342 | force_share = true; |
| 1343 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1344 | case OPTION_OBJECT: { |
| 1345 | QemuOpts *opts; |
| 1346 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1347 | optarg, true); |
| 1348 | if (!opts) { |
| 1349 | ret = 2; |
| 1350 | goto out4; |
| 1351 | } |
| 1352 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1353 | case OPTION_IMAGE_OPTS: |
| 1354 | image_opts = true; |
| 1355 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /* Progress is not shown in Quiet mode */ |
| 1360 | if (quiet) { |
| 1361 | progress = false; |
| 1362 | } |
| 1363 | |
| 1364 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1365 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1366 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1367 | } |
| 1368 | filename1 = argv[optind++]; |
| 1369 | filename2 = argv[optind++]; |
| 1370 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1371 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1372 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 1373 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1374 | ret = 2; |
| 1375 | goto out4; |
| 1376 | } |
| 1377 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1378 | /* Initialize before goto out */ |
| 1379 | qemu_progress_init(progress, 2.0); |
| 1380 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1381 | flags = 0; |
| 1382 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1383 | if (ret < 0) { |
| 1384 | error_report("Invalid source cache option: %s", cache); |
| 1385 | ret = 2; |
| 1386 | goto out3; |
| 1387 | } |
| 1388 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1389 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1390 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1391 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1392 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1393 | goto out3; |
| 1394 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1395 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1396 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1397 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1398 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1399 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1400 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1401 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1402 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1403 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1404 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1405 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1406 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1407 | total_size1 = blk_getlength(blk1); |
| 1408 | if (total_size1 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1409 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1410 | filename1, strerror(-total_size1)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1411 | ret = 4; |
| 1412 | goto out; |
| 1413 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1414 | total_size2 = blk_getlength(blk2); |
| 1415 | if (total_size2 < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1416 | error_report("Can't get size of %s: %s", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1417 | filename2, strerror(-total_size2)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1418 | ret = 4; |
| 1419 | goto out; |
| 1420 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1421 | total_size = MIN(total_size1, total_size2); |
| 1422 | progress_base = MAX(total_size1, total_size2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1423 | |
| 1424 | qemu_progress_print(0, 100); |
| 1425 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1426 | if (strict && total_size1 != total_size2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1427 | ret = 1; |
| 1428 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1429 | goto out; |
| 1430 | } |
| 1431 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1432 | while (offset < total_size) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1433 | int status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1434 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1435 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
| 1436 | total_size1 - offset, &pnum1, NULL, |
| 1437 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1438 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1439 | ret = 3; |
| 1440 | error_report("Sector allocation test failed for %s", filename1); |
| 1441 | goto out; |
| 1442 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1443 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1444 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1445 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
| 1446 | total_size2 - offset, &pnum2, NULL, |
| 1447 | NULL); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1448 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1449 | ret = 3; |
| 1450 | error_report("Sector allocation test failed for %s", filename2); |
| 1451 | goto out; |
| 1452 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1453 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1454 | |
| 1455 | assert(pnum1 && pnum2); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1456 | chunk = MIN(pnum1, pnum2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1457 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1458 | if (strict) { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1459 | if (status1 != status2) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1460 | ret = 1; |
| 1461 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1462 | " block status mismatch!\n", offset); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1463 | goto out; |
| 1464 | } |
| 1465 | } |
| 1466 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
Eric Blake | 7daddc6 | 2017-10-11 22:47:09 -0500 | [diff] [blame] | 1467 | /* nothing to do */ |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1468 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1469 | if (allocated1) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 1470 | int64_t pnum; |
| 1471 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1472 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1473 | ret = blk_pread(blk1, offset, buf1, chunk); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1474 | if (ret < 0) { |
| 1475 | error_report("Error while reading offset %" PRId64 |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1476 | " of %s: %s", |
| 1477 | offset, filename1, strerror(-ret)); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1478 | ret = 4; |
| 1479 | goto out; |
| 1480 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1481 | ret = blk_pread(blk2, offset, buf2, chunk); |
| 1482 | if (ret < 0) { |
| 1483 | error_report("Error while reading offset %" PRId64 |
| 1484 | " of %s: %s", |
| 1485 | offset, filename2, strerror(-ret)); |
| 1486 | ret = 4; |
| 1487 | goto out; |
| 1488 | } |
| 1489 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
| 1490 | if (ret || pnum != chunk) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1491 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1492 | offset + (ret ? 0 : pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1493 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1494 | goto out; |
| 1495 | } |
| 1496 | } |
| 1497 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1498 | chunk = MIN(chunk, IO_BUF_SIZE); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1499 | if (allocated1) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1500 | ret = check_empty_sectors(blk1, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1501 | filename1, buf1, quiet); |
| 1502 | } else { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1503 | ret = check_empty_sectors(blk2, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1504 | filename2, buf1, quiet); |
| 1505 | } |
| 1506 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1507 | goto out; |
| 1508 | } |
| 1509 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1510 | offset += chunk; |
| 1511 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1512 | } |
| 1513 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1514 | if (total_size1 != total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1515 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1516 | const char *filename_over; |
| 1517 | |
| 1518 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1519 | if (total_size1 > total_size2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1520 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1521 | filename_over = filename1; |
| 1522 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1523 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1524 | filename_over = filename2; |
| 1525 | } |
| 1526 | |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1527 | while (offset < progress_base) { |
| 1528 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, |
| 1529 | progress_base - offset, &chunk, |
| 1530 | NULL, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1531 | if (ret < 0) { |
| 1532 | ret = 3; |
| 1533 | error_report("Sector allocation test failed for %s", |
| 1534 | filename_over); |
| 1535 | goto out; |
| 1536 | |
| 1537 | } |
Eric Blake | 391cb1a | 2017-10-11 22:47:10 -0500 | [diff] [blame] | 1538 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1539 | chunk = MIN(chunk, IO_BUF_SIZE); |
| 1540 | ret = check_empty_sectors(blk_over, offset, chunk, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1541 | filename_over, buf1, quiet); |
| 1542 | if (ret) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1543 | goto out; |
| 1544 | } |
| 1545 | } |
Eric Blake | 033d9fc | 2017-10-11 22:47:16 -0500 | [diff] [blame] | 1546 | offset += chunk; |
| 1547 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | qprintf(quiet, "Images are identical.\n"); |
| 1552 | ret = 0; |
| 1553 | |
| 1554 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1555 | qemu_vfree(buf1); |
| 1556 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1557 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1558 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1559 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1560 | out3: |
| 1561 | qemu_progress_end(); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1562 | out4: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1563 | return ret; |
| 1564 | } |
| 1565 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1566 | enum ImgConvertBlockStatus { |
| 1567 | BLK_DATA, |
| 1568 | BLK_ZERO, |
| 1569 | BLK_BACKING_FILE, |
| 1570 | }; |
| 1571 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1572 | #define MAX_COROUTINES 16 |
| 1573 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1574 | typedef struct ImgConvertState { |
| 1575 | BlockBackend **src; |
| 1576 | int64_t *src_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1577 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1578 | int64_t total_sectors; |
| 1579 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1580 | int64_t allocated_done; |
| 1581 | int64_t sector_num; |
| 1582 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1583 | enum ImgConvertBlockStatus status; |
| 1584 | int64_t sector_next_status; |
| 1585 | BlockBackend *target; |
| 1586 | bool has_zero_init; |
| 1587 | bool compressed; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1588 | bool unallocated_blocks_are_zero; |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 1589 | bool target_is_new; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1590 | bool target_has_backing; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1591 | int64_t target_backing_sectors; /* negative if unknown */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1592 | bool wr_in_order; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1593 | bool copy_range; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1594 | bool salvage; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 1595 | bool quiet; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1596 | int min_sparse; |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1597 | int alignment; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1598 | size_t cluster_sectors; |
| 1599 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1600 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1601 | int running_coroutines; |
| 1602 | Coroutine *co[MAX_COROUTINES]; |
| 1603 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1604 | CoMutex lock; |
| 1605 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1606 | } ImgConvertState; |
| 1607 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1608 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1609 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1610 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1611 | *src_cur = 0; |
| 1612 | *src_cur_offset = 0; |
| 1613 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1614 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1615 | (*src_cur)++; |
| 1616 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1621 | { |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1622 | int64_t src_cur_offset; |
| 1623 | int ret, n, src_cur; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1624 | bool post_backing_zero = false; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1625 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1626 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1627 | |
| 1628 | assert(s->total_sectors > sector_num); |
| 1629 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1630 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1631 | if (s->target_backing_sectors >= 0) { |
| 1632 | if (sector_num >= s->target_backing_sectors) { |
| 1633 | post_backing_zero = s->unallocated_blocks_are_zero; |
| 1634 | } else if (sector_num + n > s->target_backing_sectors) { |
| 1635 | /* Split requests around target_backing_sectors (because |
| 1636 | * starting from there, zeros are handled differently) */ |
| 1637 | n = s->target_backing_sectors - sector_num; |
| 1638 | } |
| 1639 | } |
| 1640 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1641 | if (s->sector_next_status <= sector_num) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1642 | uint64_t offset = (sector_num - src_cur_offset) * BDRV_SECTOR_SIZE; |
| 1643 | int64_t count; |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1644 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1645 | do { |
| 1646 | count = n * BDRV_SECTOR_SIZE; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 1647 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1648 | if (s->target_has_backing) { |
| 1649 | ret = bdrv_block_status(blk_bs(s->src[src_cur]), offset, |
| 1650 | count, &count, NULL, NULL); |
| 1651 | } else { |
| 1652 | ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL, |
| 1653 | offset, count, &count, NULL, |
| 1654 | NULL); |
| 1655 | } |
| 1656 | |
| 1657 | if (ret < 0) { |
| 1658 | if (s->salvage) { |
| 1659 | if (n == 1) { |
| 1660 | if (!s->quiet) { |
| 1661 | warn_report("error while reading block status at " |
| 1662 | "offset %" PRIu64 ": %s", offset, |
| 1663 | strerror(-ret)); |
| 1664 | } |
| 1665 | /* Just try to read the data, then */ |
| 1666 | ret = BDRV_BLOCK_DATA; |
| 1667 | count = BDRV_SECTOR_SIZE; |
| 1668 | } else { |
| 1669 | /* Retry on a shorter range */ |
| 1670 | n = DIV_ROUND_UP(n, 4); |
| 1671 | } |
| 1672 | } else { |
| 1673 | error_report("error while reading block status at offset " |
| 1674 | "%" PRIu64 ": %s", offset, strerror(-ret)); |
| 1675 | return ret; |
| 1676 | } |
| 1677 | } |
| 1678 | } while (ret < 0); |
| 1679 | |
Eric Blake | 3182664 | 2017-10-11 22:47:08 -0500 | [diff] [blame] | 1680 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1681 | |
| 1682 | if (ret & BDRV_BLOCK_ZERO) { |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 1683 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1684 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1685 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1686 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1687 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | s->sector_next_status = sector_num + n; |
| 1691 | } |
| 1692 | |
| 1693 | n = MIN(n, s->sector_next_status - sector_num); |
| 1694 | if (s->status == BLK_DATA) { |
| 1695 | n = MIN(n, s->buf_sectors); |
| 1696 | } |
| 1697 | |
| 1698 | /* We need to write complete clusters for compressed images, so if an |
| 1699 | * unallocated area is shorter than that, we must consider the whole |
| 1700 | * cluster allocated. */ |
| 1701 | if (s->compressed) { |
| 1702 | if (n < s->cluster_sectors) { |
| 1703 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1704 | s->status = BLK_DATA; |
| 1705 | } else { |
| 1706 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | return n; |
| 1711 | } |
| 1712 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1713 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1714 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1715 | { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1716 | uint64_t single_read_until = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1717 | int n, ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1718 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1719 | assert(nb_sectors <= s->buf_sectors); |
| 1720 | while (nb_sectors > 0) { |
| 1721 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1722 | int src_cur; |
| 1723 | int64_t bs_sectors, src_cur_offset; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1724 | uint64_t offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1725 | |
| 1726 | /* In the case of compression with multiple source files, we can get a |
| 1727 | * nb_sectors that spreads into the next part. So we must be able to |
| 1728 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1729 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1730 | blk = s->src[src_cur]; |
| 1731 | bs_sectors = s->src_sectors[src_cur]; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1732 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1733 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1734 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1735 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1736 | if (single_read_until > offset) { |
| 1737 | n = 1; |
| 1738 | } |
| 1739 | |
| 1740 | ret = blk_co_pread(blk, offset, n << BDRV_SECTOR_BITS, buf, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1741 | if (ret < 0) { |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 1742 | if (s->salvage) { |
| 1743 | if (n > 1) { |
| 1744 | single_read_until = offset + (n << BDRV_SECTOR_BITS); |
| 1745 | continue; |
| 1746 | } else { |
| 1747 | if (!s->quiet) { |
| 1748 | warn_report("error while reading offset %" PRIu64 |
| 1749 | ": %s", offset, strerror(-ret)); |
| 1750 | } |
| 1751 | memset(buf, 0, BDRV_SECTOR_SIZE); |
| 1752 | } |
| 1753 | } else { |
| 1754 | return ret; |
| 1755 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | sector_num += n; |
| 1759 | nb_sectors -= n; |
| 1760 | buf += n * BDRV_SECTOR_SIZE; |
| 1761 | } |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1766 | |
| 1767 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1768 | int nb_sectors, uint8_t *buf, |
| 1769 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1770 | { |
| 1771 | int ret; |
| 1772 | |
| 1773 | while (nb_sectors > 0) { |
| 1774 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1775 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1776 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1777 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1778 | case BLK_BACKING_FILE: |
| 1779 | /* If we have a backing file, leave clusters unallocated that are |
| 1780 | * unallocated in the source image, so that the backing file is |
| 1781 | * visible at the respective offset. */ |
| 1782 | assert(s->target_has_backing); |
| 1783 | break; |
| 1784 | |
| 1785 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1786 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1787 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1788 | * it as zero sectors. |
| 1789 | * Compressed clusters need to be written as a whole, so in that |
| 1790 | * case we can only save the write if the buffer is completely |
| 1791 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1792 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1793 | (!s->compressed && |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 1794 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
| 1795 | sector_num, s->alignment)) || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1796 | (s->compressed && |
| 1797 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1798 | { |
Vladimir Sementsov-Ogievskiy | 265a7e5 | 2019-04-22 17:58:38 +0300 | [diff] [blame] | 1799 | ret = blk_co_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1800 | n << BDRV_SECTOR_BITS, buf, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1801 | if (ret < 0) { |
| 1802 | return ret; |
| 1803 | } |
| 1804 | break; |
| 1805 | } |
| 1806 | /* fall-through */ |
| 1807 | |
| 1808 | case BLK_ZERO: |
| 1809 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1810 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1811 | break; |
| 1812 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1813 | ret = blk_co_pwrite_zeroes(s->target, |
| 1814 | sector_num << BDRV_SECTOR_BITS, |
Nir Soffer | a3d6ae2 | 2019-03-24 02:20:12 +0200 | [diff] [blame] | 1815 | n << BDRV_SECTOR_BITS, |
| 1816 | BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1817 | if (ret < 0) { |
| 1818 | return ret; |
| 1819 | } |
| 1820 | break; |
| 1821 | } |
| 1822 | |
| 1823 | sector_num += n; |
| 1824 | nb_sectors -= n; |
| 1825 | buf += n * BDRV_SECTOR_SIZE; |
| 1826 | } |
| 1827 | |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1831 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
| 1832 | int nb_sectors) |
| 1833 | { |
| 1834 | int n, ret; |
| 1835 | |
| 1836 | while (nb_sectors > 0) { |
| 1837 | BlockBackend *blk; |
| 1838 | int src_cur; |
| 1839 | int64_t bs_sectors, src_cur_offset; |
| 1840 | int64_t offset; |
| 1841 | |
| 1842 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1843 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; |
| 1844 | blk = s->src[src_cur]; |
| 1845 | bs_sectors = s->src_sectors[src_cur]; |
| 1846 | |
| 1847 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1848 | |
| 1849 | ret = blk_co_copy_range(blk, offset, s->target, |
| 1850 | sector_num << BDRV_SECTOR_BITS, |
Vladimir Sementsov-Ogievskiy | 67b51fb | 2018-07-09 19:37:17 +0300 | [diff] [blame] | 1851 | n << BDRV_SECTOR_BITS, 0, 0); |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1852 | if (ret < 0) { |
| 1853 | return ret; |
| 1854 | } |
| 1855 | |
| 1856 | sector_num += n; |
| 1857 | nb_sectors -= n; |
| 1858 | } |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1862 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1863 | { |
| 1864 | ImgConvertState *s = opaque; |
| 1865 | uint8_t *buf = NULL; |
| 1866 | int ret, i; |
| 1867 | int index = -1; |
| 1868 | |
| 1869 | for (i = 0; i < s->num_coroutines; i++) { |
| 1870 | if (s->co[i] == qemu_coroutine_self()) { |
| 1871 | index = i; |
| 1872 | break; |
| 1873 | } |
| 1874 | } |
| 1875 | assert(index >= 0); |
| 1876 | |
| 1877 | s->running_coroutines++; |
| 1878 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1879 | |
| 1880 | while (1) { |
| 1881 | int n; |
| 1882 | int64_t sector_num; |
| 1883 | enum ImgConvertBlockStatus status; |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1884 | bool copy_range; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1885 | |
| 1886 | qemu_co_mutex_lock(&s->lock); |
| 1887 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1888 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1889 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1890 | } |
| 1891 | n = convert_iteration_sectors(s, s->sector_num); |
| 1892 | if (n < 0) { |
| 1893 | qemu_co_mutex_unlock(&s->lock); |
| 1894 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1895 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1896 | } |
| 1897 | /* save current sector and allocation status to local variables */ |
| 1898 | sector_num = s->sector_num; |
| 1899 | status = s->status; |
| 1900 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1901 | n = MIN(n, s->buf_sectors); |
| 1902 | } |
| 1903 | /* increment global sector counter so that other coroutines can |
| 1904 | * already continue reading beyond this request */ |
| 1905 | s->sector_num += n; |
| 1906 | qemu_co_mutex_unlock(&s->lock); |
| 1907 | |
| 1908 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 1909 | s->allocated_done += n; |
| 1910 | qemu_progress_print(100.0 * s->allocated_done / |
| 1911 | s->allocated_sectors, 0); |
| 1912 | } |
| 1913 | |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1914 | retry: |
| 1915 | copy_range = s->copy_range && s->status == BLK_DATA; |
| 1916 | if (status == BLK_DATA && !copy_range) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1917 | ret = convert_co_read(s, sector_num, n, buf); |
| 1918 | if (ret < 0) { |
| 1919 | error_report("error while reading sector %" PRId64 |
| 1920 | ": %s", sector_num, strerror(-ret)); |
| 1921 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1922 | } |
| 1923 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 1924 | status = BLK_DATA; |
| 1925 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 1926 | } |
| 1927 | |
| 1928 | if (s->wr_in_order) { |
| 1929 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1930 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1931 | s->wait_sector_num[index] = sector_num; |
| 1932 | qemu_coroutine_yield(); |
| 1933 | } |
| 1934 | s->wait_sector_num[index] = -1; |
| 1935 | } |
| 1936 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1937 | if (s->ret == -EINPROGRESS) { |
Fam Zheng | ee5306d | 2018-06-01 17:26:48 +0800 | [diff] [blame] | 1938 | if (copy_range) { |
| 1939 | ret = convert_co_copy_range(s, sector_num, n); |
| 1940 | if (ret) { |
| 1941 | s->copy_range = false; |
| 1942 | goto retry; |
| 1943 | } |
| 1944 | } else { |
| 1945 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 1946 | } |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1947 | if (ret < 0) { |
| 1948 | error_report("error while writing sector %" PRId64 |
| 1949 | ": %s", sector_num, strerror(-ret)); |
| 1950 | s->ret = ret; |
| 1951 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | if (s->wr_in_order) { |
| 1955 | /* reenter the coroutine that might have waited |
| 1956 | * for this write to complete */ |
| 1957 | s->wr_offs = sector_num + n; |
| 1958 | for (i = 0; i < s->num_coroutines; i++) { |
| 1959 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 1960 | /* |
| 1961 | * A -> B -> A cannot occur because A has |
| 1962 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 1963 | * B will never enter A during this time window. |
| 1964 | */ |
| 1965 | qemu_coroutine_enter(s->co[i]); |
| 1966 | break; |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | } |
| 1971 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1972 | qemu_vfree(buf); |
| 1973 | s->co[index] = NULL; |
| 1974 | s->running_coroutines--; |
| 1975 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 1976 | /* the convert job finished successfully */ |
| 1977 | s->ret = 0; |
| 1978 | } |
| 1979 | } |
| 1980 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1981 | static int convert_do_copy(ImgConvertState *s) |
| 1982 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1983 | int ret, i, n; |
| 1984 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1985 | |
| 1986 | /* Check whether we have zero initialisation or can get it efficiently */ |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 1987 | if (s->target_is_new && s->min_sparse && !s->target_has_backing) { |
| 1988 | s->has_zero_init = bdrv_has_zero_init(blk_bs(s->target)); |
| 1989 | } else { |
| 1990 | s->has_zero_init = false; |
| 1991 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1992 | |
| 1993 | if (!s->has_zero_init && !s->target_has_backing && |
| 1994 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) |
| 1995 | { |
Kevin Wolf | c9fdcf2 | 2019-03-22 13:49:28 +0100 | [diff] [blame] | 1996 | ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1997 | if (ret == 0) { |
| 1998 | s->has_zero_init = true; |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 2003 | * can be copied at a time. */ |
| 2004 | if (s->compressed) { |
| 2005 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 2006 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2007 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2008 | } |
| 2009 | s->buf_sectors = s->cluster_sectors; |
| 2010 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2011 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2012 | while (sector_num < s->total_sectors) { |
| 2013 | n = convert_iteration_sectors(s, sector_num); |
| 2014 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2015 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2016 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 2017 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 2018 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2019 | s->allocated_sectors += n; |
| 2020 | } |
| 2021 | sector_num += n; |
| 2022 | } |
| 2023 | |
| 2024 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2025 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2026 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2027 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2028 | qemu_co_mutex_init(&s->lock); |
| 2029 | for (i = 0; i < s->num_coroutines; i++) { |
| 2030 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 2031 | s->wait_sector_num[i] = -1; |
| 2032 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2033 | } |
| 2034 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 2035 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2036 | main_loop_wait(false); |
| 2037 | } |
| 2038 | |
| 2039 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2040 | /* signal EOF to align */ |
Pavel Butsykin | fe5c135 | 2016-07-22 11:17:40 +0300 | [diff] [blame] | 2041 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2042 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2043 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2047 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2048 | } |
| 2049 | |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2050 | #define MAX_BUF_SECTORS 32768 |
| 2051 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2052 | static int img_convert(int argc, char **argv) |
| 2053 | { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2054 | int c, bs_i, flags, src_flags = 0; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2055 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2056 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
| 2057 | *out_filename, *out_baseimg_param, *snapshot_name = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2058 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2059 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2060 | BlockDriverState *out_bs; |
| 2061 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2062 | QemuOptsList *create_opts = NULL; |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2063 | QDict *open_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2064 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 2065 | Error *local_err = NULL; |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2066 | bool writethrough, src_writethrough, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2067 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2068 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2069 | bool force_share = false; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2070 | bool explict_min_sparse = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2071 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2072 | ImgConvertState s = (ImgConvertState) { |
| 2073 | /* Need at least 4k of zeros for sparse detection */ |
| 2074 | .min_sparse = 8, |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2075 | .copy_range = false, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2076 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 2077 | .wr_in_order = true, |
| 2078 | .num_coroutines = 8, |
| 2079 | }; |
| 2080 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2081 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2082 | static const struct option long_options[] = { |
| 2083 | {"help", no_argument, 0, 'h'}, |
| 2084 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2085 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2086 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2087 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2088 | {"salvage", no_argument, 0, OPTION_SALVAGE}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2089 | {0, 0, 0, 0} |
| 2090 | }; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2091 | c = getopt_long(argc, argv, ":hf:O:B:Cco:l:S:pt:T:qnm:WU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2092 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2093 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2094 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2095 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2096 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2097 | case ':': |
| 2098 | missing_argument(argv[optind - 1]); |
| 2099 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2100 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2101 | unrecognized_option(argv[optind - 1]); |
| 2102 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2103 | case 'h': |
| 2104 | help(); |
| 2105 | break; |
| 2106 | case 'f': |
| 2107 | fmt = optarg; |
| 2108 | break; |
| 2109 | case 'O': |
| 2110 | out_fmt = optarg; |
| 2111 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 2112 | case 'B': |
| 2113 | out_baseimg = optarg; |
| 2114 | break; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2115 | case 'C': |
| 2116 | s.copy_range = true; |
| 2117 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2118 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2119 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2120 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2121 | case 'o': |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2122 | if (!is_valid_option_list(optarg)) { |
| 2123 | error_report("Invalid option list: %s", optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2124 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 2125 | } |
| 2126 | if (!options) { |
| 2127 | options = g_strdup(optarg); |
| 2128 | } else { |
| 2129 | char *old_options = options; |
| 2130 | options = g_strdup_printf("%s,%s", options, optarg); |
| 2131 | g_free(old_options); |
| 2132 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2133 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2134 | case 'l': |
| 2135 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2136 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2137 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2138 | if (!sn_opts) { |
| 2139 | error_report("Failed in parsing snapshot param '%s'", |
| 2140 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2141 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2142 | } |
| 2143 | } else { |
| 2144 | snapshot_name = optarg; |
| 2145 | } |
| 2146 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2147 | case 'S': |
| 2148 | { |
| 2149 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2150 | |
| 2151 | sval = cvtnum(optarg); |
Nir Soffer | 1bbbf32 | 2019-08-27 21:59:12 +0300 | [diff] [blame] | 2152 | if (sval < 0 || !QEMU_IS_ALIGNED(sval, BDRV_SECTOR_SIZE) || |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2153 | sval / BDRV_SECTOR_SIZE > MAX_BUF_SECTORS) { |
| 2154 | error_report("Invalid buffer size for sparse output specified. " |
| 2155 | "Valid sizes are multiples of %llu up to %llu. Select " |
| 2156 | "0 to disable sparse detection (fully allocates output).", |
| 2157 | BDRV_SECTOR_SIZE, MAX_BUF_SECTORS * BDRV_SECTOR_SIZE); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2158 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2159 | } |
| 2160 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2161 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2162 | explict_min_sparse = true; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2163 | break; |
| 2164 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2165 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2166 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2167 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2168 | case 't': |
| 2169 | cache = optarg; |
| 2170 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2171 | case 'T': |
| 2172 | src_cache = optarg; |
| 2173 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2174 | case 'q': |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2175 | s.quiet = true; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2176 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2177 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2178 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2179 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2180 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2181 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2182 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2183 | error_report("Invalid number of coroutines. Allowed number of" |
| 2184 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2185 | goto fail_getopt; |
| 2186 | } |
| 2187 | break; |
| 2188 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2189 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2190 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2191 | case 'U': |
| 2192 | force_share = true; |
| 2193 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2194 | case OPTION_OBJECT: { |
| 2195 | QemuOpts *object_opts; |
| 2196 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2197 | optarg, true); |
| 2198 | if (!object_opts) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2199 | goto fail_getopt; |
| 2200 | } |
| 2201 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2202 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2203 | case OPTION_IMAGE_OPTS: |
| 2204 | image_opts = true; |
| 2205 | break; |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2206 | case OPTION_SALVAGE: |
| 2207 | s.salvage = true; |
| 2208 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2209 | case OPTION_TARGET_IMAGE_OPTS: |
| 2210 | tgt_image_opts = true; |
| 2211 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2212 | } |
| 2213 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2214 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2215 | if (!out_fmt && !tgt_image_opts) { |
| 2216 | out_fmt = "raw"; |
| 2217 | } |
| 2218 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2219 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2220 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 2221 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2222 | goto fail_getopt; |
| 2223 | } |
| 2224 | |
Fam Zheng | e11ce12 | 2018-07-27 11:34:01 +0800 | [diff] [blame] | 2225 | if (s.compressed && s.copy_range) { |
| 2226 | error_report("Cannot enable copy offloading when -c is used"); |
| 2227 | goto fail_getopt; |
| 2228 | } |
| 2229 | |
| 2230 | if (explict_min_sparse && s.copy_range) { |
| 2231 | error_report("Cannot enable copy offloading when -S is used"); |
| 2232 | goto fail_getopt; |
| 2233 | } |
| 2234 | |
Max Reitz | 8eaac02 | 2019-05-07 22:35:03 +0200 | [diff] [blame] | 2235 | if (s.copy_range && s.salvage) { |
| 2236 | error_report("Cannot use copy offloading in salvaging mode"); |
| 2237 | goto fail_getopt; |
| 2238 | } |
| 2239 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2240 | if (tgt_image_opts && !skip_create) { |
| 2241 | error_report("--target-image-opts requires use of -n flag"); |
| 2242 | goto fail_getopt; |
| 2243 | } |
| 2244 | |
Kevin Wolf | ffd8e8f | 2019-08-09 11:09:21 +0200 | [diff] [blame] | 2245 | if (skip_create && options) { |
| 2246 | warn_report("-o has no effect when skipping image creation"); |
| 2247 | warn_report("This will become an error in future QEMU versions."); |
| 2248 | } |
| 2249 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2250 | s.src_num = argc - optind - 1; |
| 2251 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2252 | |
| 2253 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2254 | if (out_fmt) { |
| 2255 | ret = print_block_option_help(out_filename, out_fmt); |
| 2256 | goto fail_getopt; |
| 2257 | } else { |
| 2258 | error_report("Option help requires a format be specified"); |
| 2259 | goto fail_getopt; |
| 2260 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | if (s.src_num < 1) { |
| 2264 | error_report("Must specify image file name"); |
| 2265 | goto fail_getopt; |
| 2266 | } |
| 2267 | |
| 2268 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2269 | /* ret is still -EINVAL until here */ |
| 2270 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2271 | if (ret < 0) { |
| 2272 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2273 | goto fail_getopt; |
| 2274 | } |
| 2275 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2276 | /* Initialize before goto out */ |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2277 | if (s.quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2278 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2279 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2280 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2281 | qemu_progress_print(0, 100); |
| 2282 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2283 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2284 | s.src_sectors = g_new(int64_t, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2285 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2286 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2287 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2288 | fmt, src_flags, src_writethrough, s.quiet, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2289 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2290 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2291 | ret = -1; |
| 2292 | goto out; |
| 2293 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2294 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2295 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2296 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2297 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2298 | ret = -1; |
| 2299 | goto out; |
| 2300 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2301 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2302 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2303 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2304 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2305 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2306 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2307 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2308 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2309 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2310 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2311 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2312 | ret = -1; |
| 2313 | goto out; |
| 2314 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2315 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2316 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2317 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2318 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2319 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2320 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2321 | ret = -1; |
| 2322 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2325 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2326 | /* Find driver and parse its options */ |
| 2327 | drv = bdrv_find_format(out_fmt); |
| 2328 | if (!drv) { |
| 2329 | error_report("Unknown file format '%s'", out_fmt); |
| 2330 | ret = -1; |
| 2331 | goto out; |
| 2332 | } |
| 2333 | |
| 2334 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2335 | if (!proto_drv) { |
| 2336 | error_report_err(local_err); |
| 2337 | ret = -1; |
| 2338 | goto out; |
| 2339 | } |
| 2340 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2341 | if (!drv->create_opts) { |
| 2342 | error_report("Format driver '%s' does not support image creation", |
| 2343 | drv->format_name); |
| 2344 | ret = -1; |
| 2345 | goto out; |
| 2346 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2347 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2348 | if (!proto_drv->create_opts) { |
| 2349 | error_report("Protocol driver '%s' does not support image creation", |
| 2350 | proto_drv->format_name); |
| 2351 | ret = -1; |
| 2352 | goto out; |
| 2353 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2354 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2355 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2356 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2357 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2358 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2359 | if (options) { |
| 2360 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 2361 | if (local_err) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2362 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2363 | ret = -1; |
| 2364 | goto out; |
| 2365 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2366 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2367 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2368 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512, |
Markus Armbruster | 39101f2 | 2015-02-12 16:46:36 +0100 | [diff] [blame] | 2369 | &error_abort); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2370 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
| 2371 | if (ret < 0) { |
| 2372 | goto out; |
| 2373 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2374 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2375 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2376 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2377 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2378 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2379 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2380 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2381 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2382 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2383 | if (s.src_num > 1 && out_baseimg) { |
| 2384 | error_report("Having a backing file for the target makes no sense when " |
| 2385 | "concatenating multiple input images"); |
| 2386 | ret = -1; |
| 2387 | goto out; |
| 2388 | } |
| 2389 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2390 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2391 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2392 | bool encryption = |
| 2393 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2394 | const char *encryptfmt = |
| 2395 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2396 | const char *preallocation = |
| 2397 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2398 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2399 | if (drv && !block_driver_can_compress(drv)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2400 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2401 | ret = -1; |
| 2402 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2403 | } |
| 2404 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2405 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2406 | error_report("Compression and encryption not supported at " |
| 2407 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2408 | ret = -1; |
| 2409 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2410 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2411 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2412 | if (preallocation |
| 2413 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2414 | { |
| 2415 | error_report("Compression and preallocation not supported at " |
| 2416 | "the same time"); |
| 2417 | ret = -1; |
| 2418 | goto out; |
| 2419 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2420 | } |
| 2421 | |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2422 | /* |
| 2423 | * The later open call will need any decryption secrets, and |
| 2424 | * bdrv_create() will purge "opts", so extract them now before |
| 2425 | * they are lost. |
| 2426 | */ |
| 2427 | if (!skip_create) { |
| 2428 | open_opts = qdict_new(); |
| 2429 | qemu_opt_foreach(opts, img_add_key_secrets, open_opts, &error_abort); |
| 2430 | } |
| 2431 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2432 | if (!skip_create) { |
| 2433 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2434 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2435 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2436 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2437 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2438 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2439 | } |
| 2440 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2441 | |
Max Reitz | 4d7c487 | 2019-07-24 19:12:29 +0200 | [diff] [blame] | 2442 | s.target_is_new = !skip_create; |
| 2443 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2444 | flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2445 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2446 | if (ret < 0) { |
| 2447 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2448 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2449 | } |
| 2450 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2451 | if (skip_create) { |
| 2452 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2453 | flags, writethrough, s.quiet, false); |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2454 | } else { |
| 2455 | /* TODO ultimately we should allow --target-image-opts |
| 2456 | * to be used even when -n is not given. |
| 2457 | * That has to wait for bdrv_create to be improved |
| 2458 | * to allow filenames in option syntax |
| 2459 | */ |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2460 | s.target = img_open_file(out_filename, open_opts, out_fmt, |
Max Reitz | 3d96cb9 | 2019-05-07 22:35:02 +0200 | [diff] [blame] | 2461 | flags, writethrough, s.quiet, false); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2462 | open_opts = NULL; /* blk_new_open will have freed it */ |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2463 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2464 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2465 | ret = -1; |
| 2466 | goto out; |
| 2467 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2468 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2469 | |
Vladimir Sementsov-Ogievskiy | ac850bf | 2019-06-04 19:15:06 +0300 | [diff] [blame] | 2470 | if (s.compressed && !block_driver_can_compress(out_bs->drv)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2471 | error_report("Compression not supported for this file format"); |
| 2472 | ret = -1; |
| 2473 | goto out; |
| 2474 | } |
| 2475 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2476 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | 6360ab2 | 2018-07-13 09:15:39 +0200 | [diff] [blame] | 2477 | * or discard_alignment of the out_bs is greater. Limit to |
| 2478 | * MAX_BUF_SECTORS as maximum which is currently 32768 (16MB). */ |
| 2479 | s.buf_sectors = MIN(MAX_BUF_SECTORS, |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2480 | MAX(s.buf_sectors, |
| 2481 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2482 | out_bs->bl.pdiscard_alignment >> |
| 2483 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2484 | |
Peter Lieven | 8dcd3c9 | 2018-07-12 15:00:10 +0200 | [diff] [blame] | 2485 | /* try to align the write requests to the destination to avoid unnecessary |
| 2486 | * RMW cycles. */ |
| 2487 | s.alignment = MAX(pow2floor(s.min_sparse), |
| 2488 | DIV_ROUND_UP(out_bs->bl.request_alignment, |
| 2489 | BDRV_SECTOR_SIZE)); |
| 2490 | assert(is_power_of_2(s.alignment)); |
| 2491 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2492 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2493 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2494 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2495 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2496 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2497 | ret = -1; |
| 2498 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2499 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2500 | error_report("output file is smaller than input file"); |
| 2501 | ret = -1; |
| 2502 | goto out; |
| 2503 | } |
| 2504 | } |
| 2505 | |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2506 | if (s.target_has_backing) { |
| 2507 | /* Errors are treated as "backing length unknown" (which means |
| 2508 | * s.target_backing_sectors has to be negative, which it will |
| 2509 | * be automatically). The backing file length is used only |
| 2510 | * for optimizations, so such a case is not fatal. */ |
| 2511 | s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs); |
| 2512 | } else { |
| 2513 | s.target_backing_sectors = -1; |
| 2514 | } |
| 2515 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2516 | ret = bdrv_get_info(out_bs, &bdi); |
| 2517 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2518 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2519 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2520 | goto out; |
| 2521 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2522 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2523 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2524 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Max Reitz | 351c8ef | 2018-05-01 18:57:49 +0200 | [diff] [blame] | 2525 | s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2526 | } |
| 2527 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2528 | ret = convert_do_copy(&s); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2529 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2530 | if (!ret) { |
| 2531 | qemu_progress_print(100, 0); |
| 2532 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2533 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2534 | qemu_opts_del(opts); |
| 2535 | qemu_opts_free(create_opts); |
Markus Armbruster | fbf28a4 | 2014-09-29 16:07:55 +0200 | [diff] [blame] | 2536 | qemu_opts_del(sn_opts); |
Daniel P. Berrangé | 8d65a3c | 2018-08-14 13:39:47 +0100 | [diff] [blame] | 2537 | qobject_unref(open_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2538 | blk_unref(s.target); |
| 2539 | if (s.src) { |
| 2540 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2541 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2542 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2543 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2544 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2545 | g_free(s.src_sectors); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2546 | fail_getopt: |
| 2547 | g_free(options); |
| 2548 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2549 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2552 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2553 | static void dump_snapshots(BlockDriverState *bs) |
| 2554 | { |
| 2555 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2556 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2557 | |
| 2558 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2559 | if (nb_sns <= 0) |
| 2560 | return; |
| 2561 | printf("Snapshot list:\n"); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2562 | bdrv_snapshot_dump(NULL); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2563 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2564 | for(i = 0; i < nb_sns; i++) { |
| 2565 | sn = &sn_tab[i]; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2566 | bdrv_snapshot_dump(sn); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2567 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2568 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2569 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2572 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2573 | { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2574 | QString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2575 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2576 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2577 | |
| 2578 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2579 | visit_complete(v, &obj); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2580 | str = qobject_to_json_pretty(obj); |
| 2581 | assert(str != NULL); |
| 2582 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2583 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2584 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2585 | qobject_unref(str); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2586 | } |
| 2587 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2588 | static void dump_json_image_info(ImageInfo *info) |
| 2589 | { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2590 | QString *str; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2591 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2592 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2593 | |
| 2594 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2595 | visit_complete(v, &obj); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2596 | str = qobject_to_json_pretty(obj); |
| 2597 | assert(str != NULL); |
| 2598 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2599 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2600 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 2601 | qobject_unref(str); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2602 | } |
| 2603 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2604 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2605 | { |
| 2606 | ImageInfoList *elem; |
| 2607 | bool delim = false; |
| 2608 | |
| 2609 | for (elem = list; elem; elem = elem->next) { |
| 2610 | if (delim) { |
| 2611 | printf("\n"); |
| 2612 | } |
| 2613 | delim = true; |
| 2614 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 2615 | bdrv_image_info_dump(elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2620 | { |
| 2621 | return strcmp(a, b) == 0; |
| 2622 | } |
| 2623 | |
| 2624 | /** |
| 2625 | * Open an image file chain and return an ImageInfoList |
| 2626 | * |
| 2627 | * @filename: topmost image filename |
| 2628 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2629 | * @chain: true - enumerate entire backing file chain |
| 2630 | * false - only topmost image file |
| 2631 | * |
| 2632 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2633 | * image file. If there was an error a message will have been printed to |
| 2634 | * stderr. |
| 2635 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2636 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2637 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2638 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2639 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2640 | { |
| 2641 | ImageInfoList *head = NULL; |
| 2642 | ImageInfoList **last = &head; |
| 2643 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2644 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2645 | |
| 2646 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2647 | |
| 2648 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2649 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2650 | BlockDriverState *bs; |
| 2651 | ImageInfo *info; |
| 2652 | ImageInfoList *elem; |
| 2653 | |
| 2654 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2655 | error_report("Backing file '%s' creates an infinite loop.", |
| 2656 | filename); |
| 2657 | goto err; |
| 2658 | } |
| 2659 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2660 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2661 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2662 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2663 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2664 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2665 | goto err; |
| 2666 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2667 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2668 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2669 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2670 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2671 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2672 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2673 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2674 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2675 | |
| 2676 | elem = g_new0(ImageInfoList, 1); |
| 2677 | elem->value = info; |
| 2678 | *last = elem; |
| 2679 | last = &elem->next; |
| 2680 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2681 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2682 | |
| 2683 | filename = fmt = NULL; |
| 2684 | if (chain) { |
| 2685 | if (info->has_full_backing_filename) { |
| 2686 | filename = info->full_backing_filename; |
| 2687 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2688 | error_report("Could not determine absolute backing filename," |
| 2689 | " but backing filename '%s' present", |
| 2690 | info->backing_filename); |
| 2691 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2692 | } |
| 2693 | if (info->has_backing_filename_format) { |
| 2694 | fmt = info->backing_filename_format; |
| 2695 | } |
| 2696 | } |
| 2697 | } |
| 2698 | g_hash_table_destroy(filenames); |
| 2699 | return head; |
| 2700 | |
| 2701 | err: |
| 2702 | qapi_free_ImageInfoList(head); |
| 2703 | g_hash_table_destroy(filenames); |
| 2704 | return NULL; |
| 2705 | } |
| 2706 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2707 | static int img_info(int argc, char **argv) |
| 2708 | { |
| 2709 | int c; |
| 2710 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2711 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2712 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2713 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2714 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2715 | bool force_share = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2716 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2717 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2718 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2719 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2720 | int option_index = 0; |
| 2721 | static const struct option long_options[] = { |
| 2722 | {"help", no_argument, 0, 'h'}, |
| 2723 | {"format", required_argument, 0, 'f'}, |
| 2724 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2725 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2726 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2727 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2728 | {"force-share", no_argument, 0, 'U'}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2729 | {0, 0, 0, 0} |
| 2730 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2731 | c = getopt_long(argc, argv, ":f:hU", |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2732 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2733 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2734 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2735 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2736 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2737 | case ':': |
| 2738 | missing_argument(argv[optind - 1]); |
| 2739 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2740 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2741 | unrecognized_option(argv[optind - 1]); |
| 2742 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2743 | case 'h': |
| 2744 | help(); |
| 2745 | break; |
| 2746 | case 'f': |
| 2747 | fmt = optarg; |
| 2748 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2749 | case 'U': |
| 2750 | force_share = true; |
| 2751 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2752 | case OPTION_OUTPUT: |
| 2753 | output = optarg; |
| 2754 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2755 | case OPTION_BACKING_CHAIN: |
| 2756 | chain = true; |
| 2757 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2758 | case OPTION_OBJECT: { |
| 2759 | QemuOpts *opts; |
| 2760 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2761 | optarg, true); |
| 2762 | if (!opts) { |
| 2763 | return 1; |
| 2764 | } |
| 2765 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2766 | case OPTION_IMAGE_OPTS: |
| 2767 | image_opts = true; |
| 2768 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2769 | } |
| 2770 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2771 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2772 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2773 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2774 | filename = argv[optind++]; |
| 2775 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2776 | if (output && !strcmp(output, "json")) { |
| 2777 | output_format = OFORMAT_JSON; |
| 2778 | } else if (output && !strcmp(output, "human")) { |
| 2779 | output_format = OFORMAT_HUMAN; |
| 2780 | } else if (output) { |
| 2781 | error_report("--output must be used with human or json as argument."); |
| 2782 | return 1; |
| 2783 | } |
| 2784 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2785 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2786 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 2787 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2788 | return 1; |
| 2789 | } |
| 2790 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2791 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 2792 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2793 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2794 | return 1; |
| 2795 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2796 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2797 | switch (output_format) { |
| 2798 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2799 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2800 | break; |
| 2801 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2802 | if (chain) { |
| 2803 | dump_json_image_info_list(list); |
| 2804 | } else { |
| 2805 | dump_json_image_info(list->value); |
| 2806 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2807 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2808 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2809 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2810 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2811 | return 0; |
| 2812 | } |
| 2813 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2814 | static int dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 2815 | MapEntry *next) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2816 | { |
| 2817 | switch (output_format) { |
| 2818 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2819 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2820 | error_report("File contains external, encrypted or compressed clusters."); |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2821 | return -1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2822 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2823 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2824 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2825 | e->start, e->length, |
| 2826 | e->has_offset ? e->offset : 0, |
| 2827 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2828 | } |
| 2829 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 2830 | * Modify the flags here to allow more coalescing. |
| 2831 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2832 | if (next && (!next->data || next->zero)) { |
| 2833 | next->data = false; |
| 2834 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2835 | } |
| 2836 | break; |
| 2837 | case OFORMAT_JSON: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2838 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," |
| 2839 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2840 | (e->start == 0 ? "[" : ",\n"), |
| 2841 | e->start, e->length, e->depth, |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2842 | e->zero ? "true" : "false", |
| 2843 | e->data ? "true" : "false"); |
| 2844 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 2845 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2846 | } |
| 2847 | putchar('}'); |
| 2848 | |
| 2849 | if (!next) { |
| 2850 | printf("]\n"); |
| 2851 | } |
| 2852 | break; |
| 2853 | } |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 2854 | return 0; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2855 | } |
| 2856 | |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 2857 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
| 2858 | int64_t bytes, MapEntry *e) |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2859 | { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2860 | int ret; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2861 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 2862 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2863 | bool has_offset; |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2864 | int64_t map; |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 2865 | char *filename = NULL; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2866 | |
| 2867 | /* As an optimization, we could cache the current range of unallocated |
| 2868 | * clusters in each file of the chain, and avoid querying the same |
| 2869 | * range repeatedly. |
| 2870 | */ |
| 2871 | |
| 2872 | depth = 0; |
| 2873 | for (;;) { |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2874 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2875 | if (ret < 0) { |
| 2876 | return ret; |
| 2877 | } |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2878 | assert(bytes); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2879 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 2880 | break; |
| 2881 | } |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 2882 | bs = backing_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2883 | if (bs == NULL) { |
| 2884 | ret = 0; |
| 2885 | break; |
| 2886 | } |
| 2887 | |
| 2888 | depth++; |
| 2889 | } |
| 2890 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2891 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 2892 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 2893 | if (file && has_offset) { |
| 2894 | bdrv_refresh_filename(file); |
| 2895 | filename = file->filename; |
| 2896 | } |
| 2897 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2898 | *e = (MapEntry) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 2899 | .start = offset, |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2900 | .length = bytes, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2901 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 2902 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
Eric Blake | 237d78f | 2017-10-11 22:47:03 -0500 | [diff] [blame] | 2903 | .offset = map, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2904 | .has_offset = has_offset, |
| 2905 | .depth = depth, |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 2906 | .has_filename = filename, |
| 2907 | .filename = filename, |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2908 | }; |
| 2909 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2910 | return 0; |
| 2911 | } |
| 2912 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2913 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 2914 | { |
| 2915 | if (curr->length == 0) { |
| 2916 | return false; |
| 2917 | } |
| 2918 | if (curr->zero != next->zero || |
| 2919 | curr->data != next->data || |
| 2920 | curr->depth != next->depth || |
| 2921 | curr->has_filename != next->has_filename || |
| 2922 | curr->has_offset != next->has_offset) { |
| 2923 | return false; |
| 2924 | } |
| 2925 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 2926 | return false; |
| 2927 | } |
| 2928 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 2929 | return false; |
| 2930 | } |
| 2931 | return true; |
| 2932 | } |
| 2933 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2934 | static int img_map(int argc, char **argv) |
| 2935 | { |
| 2936 | int c; |
| 2937 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2938 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2939 | BlockDriverState *bs; |
| 2940 | const char *filename, *fmt, *output; |
| 2941 | int64_t length; |
| 2942 | MapEntry curr = { .length = 0 }, next; |
| 2943 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2944 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2945 | bool force_share = false; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2946 | |
| 2947 | fmt = NULL; |
| 2948 | output = NULL; |
| 2949 | for (;;) { |
| 2950 | int option_index = 0; |
| 2951 | static const struct option long_options[] = { |
| 2952 | {"help", no_argument, 0, 'h'}, |
| 2953 | {"format", required_argument, 0, 'f'}, |
| 2954 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2955 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2956 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2957 | {"force-share", no_argument, 0, 'U'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2958 | {0, 0, 0, 0} |
| 2959 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2960 | c = getopt_long(argc, argv, ":f:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2961 | long_options, &option_index); |
| 2962 | if (c == -1) { |
| 2963 | break; |
| 2964 | } |
| 2965 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2966 | case ':': |
| 2967 | missing_argument(argv[optind - 1]); |
| 2968 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2969 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2970 | unrecognized_option(argv[optind - 1]); |
| 2971 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2972 | case 'h': |
| 2973 | help(); |
| 2974 | break; |
| 2975 | case 'f': |
| 2976 | fmt = optarg; |
| 2977 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2978 | case 'U': |
| 2979 | force_share = true; |
| 2980 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2981 | case OPTION_OUTPUT: |
| 2982 | output = optarg; |
| 2983 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2984 | case OPTION_OBJECT: { |
| 2985 | QemuOpts *opts; |
| 2986 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2987 | optarg, true); |
| 2988 | if (!opts) { |
| 2989 | return 1; |
| 2990 | } |
| 2991 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2992 | case OPTION_IMAGE_OPTS: |
| 2993 | image_opts = true; |
| 2994 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2995 | } |
| 2996 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2997 | if (optind != argc - 1) { |
| 2998 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2999 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3000 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3001 | |
| 3002 | if (output && !strcmp(output, "json")) { |
| 3003 | output_format = OFORMAT_JSON; |
| 3004 | } else if (output && !strcmp(output, "human")) { |
| 3005 | output_format = OFORMAT_HUMAN; |
| 3006 | } else if (output) { |
| 3007 | error_report("--output must be used with human or json as argument."); |
| 3008 | return 1; |
| 3009 | } |
| 3010 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3011 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3012 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3013 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3014 | return 1; |
| 3015 | } |
| 3016 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3017 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3018 | if (!blk) { |
| 3019 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3020 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3021 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3022 | |
| 3023 | if (output_format == OFORMAT_HUMAN) { |
| 3024 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
| 3025 | } |
| 3026 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3027 | length = blk_getlength(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3028 | while (curr.start + curr.length < length) { |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3029 | int64_t offset = curr.start + curr.length; |
| 3030 | int64_t n; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3031 | |
| 3032 | /* Probe up to 1 GiB at a time. */ |
Stefano Garzarella | 97ede57 | 2019-05-08 12:43:24 +0200 | [diff] [blame] | 3033 | n = MIN(1 * GiB, length - offset); |
Eric Blake | 5e344dd | 2017-10-11 22:47:02 -0500 | [diff] [blame] | 3034 | ret = get_block_status(bs, offset, n, &next); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3035 | |
| 3036 | if (ret < 0) { |
| 3037 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 3038 | goto out; |
| 3039 | } |
| 3040 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 3041 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3042 | curr.length += next.length; |
| 3043 | continue; |
| 3044 | } |
| 3045 | |
| 3046 | if (curr.length > 0) { |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3047 | ret = dump_map_entry(output_format, &curr, &next); |
| 3048 | if (ret < 0) { |
| 3049 | goto out; |
| 3050 | } |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3051 | } |
| 3052 | curr = next; |
| 3053 | } |
| 3054 | |
Eric Blake | 30065d1 | 2019-03-26 13:40:43 -0500 | [diff] [blame] | 3055 | ret = dump_map_entry(output_format, &curr, NULL); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3056 | |
| 3057 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3058 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 3059 | return ret < 0; |
| 3060 | } |
| 3061 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3062 | #define SNAPSHOT_LIST 1 |
| 3063 | #define SNAPSHOT_CREATE 2 |
| 3064 | #define SNAPSHOT_APPLY 3 |
| 3065 | #define SNAPSHOT_DELETE 4 |
| 3066 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3067 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3068 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3069 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3070 | BlockDriverState *bs; |
| 3071 | QEMUSnapshotInfo sn; |
| 3072 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3073 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3074 | int action = 0; |
| 3075 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3076 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3077 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3078 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3079 | bool force_share = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3080 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3081 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3082 | /* Parse commandline parameters */ |
| 3083 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3084 | static const struct option long_options[] = { |
| 3085 | {"help", no_argument, 0, 'h'}, |
| 3086 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3087 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3088 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3089 | {0, 0, 0, 0} |
| 3090 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3091 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3092 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3093 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3094 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3095 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3096 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3097 | case ':': |
| 3098 | missing_argument(argv[optind - 1]); |
| 3099 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3100 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3101 | unrecognized_option(argv[optind - 1]); |
| 3102 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3103 | case 'h': |
| 3104 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3105 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3106 | case 'l': |
| 3107 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3108 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3109 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3110 | } |
| 3111 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 3112 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3113 | break; |
| 3114 | case 'a': |
| 3115 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3116 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3117 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3118 | } |
| 3119 | action = SNAPSHOT_APPLY; |
| 3120 | snapshot_name = optarg; |
| 3121 | break; |
| 3122 | case 'c': |
| 3123 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3124 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3125 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3126 | } |
| 3127 | action = SNAPSHOT_CREATE; |
| 3128 | snapshot_name = optarg; |
| 3129 | break; |
| 3130 | case 'd': |
| 3131 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3132 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3133 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3134 | } |
| 3135 | action = SNAPSHOT_DELETE; |
| 3136 | snapshot_name = optarg; |
| 3137 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3138 | case 'q': |
| 3139 | quiet = true; |
| 3140 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3141 | case 'U': |
| 3142 | force_share = true; |
| 3143 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3144 | case OPTION_OBJECT: { |
| 3145 | QemuOpts *opts; |
| 3146 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3147 | optarg, true); |
| 3148 | if (!opts) { |
| 3149 | return 1; |
| 3150 | } |
| 3151 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3152 | case OPTION_IMAGE_OPTS: |
| 3153 | image_opts = true; |
| 3154 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3155 | } |
| 3156 | } |
| 3157 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3158 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3159 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3160 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3161 | filename = argv[optind++]; |
| 3162 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3163 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3164 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3165 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3166 | return 1; |
| 3167 | } |
| 3168 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3169 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3170 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 3171 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3172 | if (!blk) { |
| 3173 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3174 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3175 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3176 | |
| 3177 | /* Perform the requested action */ |
| 3178 | switch(action) { |
| 3179 | case SNAPSHOT_LIST: |
| 3180 | dump_snapshots(bs); |
| 3181 | break; |
| 3182 | |
| 3183 | case SNAPSHOT_CREATE: |
| 3184 | memset(&sn, 0, sizeof(sn)); |
| 3185 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3186 | |
| 3187 | qemu_gettimeofday(&tv); |
| 3188 | sn.date_sec = tv.tv_sec; |
| 3189 | sn.date_nsec = tv.tv_usec * 1000; |
| 3190 | |
| 3191 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3192 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3193 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3194 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3195 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3196 | break; |
| 3197 | |
| 3198 | case SNAPSHOT_APPLY: |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3199 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3200 | if (ret) { |
Kevin Wolf | 0b62bcb | 2017-11-20 15:28:41 +0100 | [diff] [blame] | 3201 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
| 3202 | snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3203 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3204 | break; |
| 3205 | |
| 3206 | case SNAPSHOT_DELETE: |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3207 | ret = bdrv_snapshot_find(bs, &sn, snapshot_name); |
| 3208 | if (ret < 0) { |
| 3209 | error_report("Could not delete snapshot '%s': snapshot not " |
| 3210 | "found", snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3211 | ret = 1; |
Daniel Henrique Barboza | 8c04093 | 2018-11-07 11:09:59 -0200 | [diff] [blame] | 3212 | } else { |
| 3213 | ret = bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); |
| 3214 | if (ret < 0) { |
| 3215 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3216 | snapshot_name); |
| 3217 | ret = 1; |
| 3218 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3219 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3220 | break; |
| 3221 | } |
| 3222 | |
| 3223 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3224 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3225 | if (ret) { |
| 3226 | return 1; |
| 3227 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3228 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3231 | static int img_rebase(int argc, char **argv) |
| 3232 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3233 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3234 | uint8_t *buf_old = NULL; |
| 3235 | uint8_t *buf_new = NULL; |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3236 | BlockDriverState *bs = NULL, *prefix_chain_bs = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3237 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3238 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3239 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3240 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3241 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3242 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3243 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3244 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3245 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3246 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3247 | |
| 3248 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3249 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3250 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3251 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3252 | out_baseimg = NULL; |
| 3253 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3254 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3255 | static const struct option long_options[] = { |
| 3256 | {"help", no_argument, 0, 'h'}, |
| 3257 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3258 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3259 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3260 | {0, 0, 0, 0} |
| 3261 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3262 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3263 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3264 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3265 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3266 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3267 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3268 | case ':': |
| 3269 | missing_argument(argv[optind - 1]); |
| 3270 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3271 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3272 | unrecognized_option(argv[optind - 1]); |
| 3273 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3274 | case 'h': |
| 3275 | help(); |
| 3276 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3277 | case 'f': |
| 3278 | fmt = optarg; |
| 3279 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3280 | case 'F': |
| 3281 | out_basefmt = optarg; |
| 3282 | break; |
| 3283 | case 'b': |
| 3284 | out_baseimg = optarg; |
| 3285 | break; |
| 3286 | case 'u': |
| 3287 | unsafe = 1; |
| 3288 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3289 | case 'p': |
| 3290 | progress = 1; |
| 3291 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3292 | case 't': |
| 3293 | cache = optarg; |
| 3294 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3295 | case 'T': |
| 3296 | src_cache = optarg; |
| 3297 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3298 | case 'q': |
| 3299 | quiet = true; |
| 3300 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3301 | case OPTION_OBJECT: { |
| 3302 | QemuOpts *opts; |
| 3303 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3304 | optarg, true); |
| 3305 | if (!opts) { |
| 3306 | return 1; |
| 3307 | } |
| 3308 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3309 | case OPTION_IMAGE_OPTS: |
| 3310 | image_opts = true; |
| 3311 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3312 | case 'U': |
| 3313 | force_share = true; |
| 3314 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3315 | } |
| 3316 | } |
| 3317 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3318 | if (quiet) { |
| 3319 | progress = 0; |
| 3320 | } |
| 3321 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3322 | if (optind != argc - 1) { |
| 3323 | error_exit("Expecting one image file name"); |
| 3324 | } |
| 3325 | if (!unsafe && !out_baseimg) { |
| 3326 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3327 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3328 | filename = argv[optind++]; |
| 3329 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3330 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3331 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3332 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3333 | return 1; |
| 3334 | } |
| 3335 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3336 | qemu_progress_init(progress, 2.0); |
| 3337 | qemu_progress_print(0, 100); |
| 3338 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3339 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3340 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3341 | if (ret < 0) { |
| 3342 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3343 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3344 | } |
| 3345 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3346 | src_flags = 0; |
| 3347 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3348 | if (ret < 0) { |
| 3349 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3350 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3351 | } |
| 3352 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3353 | /* The source files are opened read-only, don't care about WCE */ |
| 3354 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3355 | (void) src_writethrough; |
| 3356 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3357 | /* |
| 3358 | * Open the images. |
| 3359 | * |
| 3360 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3361 | * the reference to a renamed or moved backing file. |
| 3362 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3363 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3364 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3365 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3366 | ret = -1; |
| 3367 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3368 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3369 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3370 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3371 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3372 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3373 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3374 | ret = -1; |
| 3375 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3380 | if (!unsafe) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3381 | QDict *options = NULL; |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3382 | BlockDriverState *base_bs = backing_bs(bs); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3383 | |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3384 | if (base_bs) { |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3385 | blk_old_backing = blk_new(qemu_get_aio_context(), |
| 3386 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3387 | BLK_PERM_ALL); |
| 3388 | ret = blk_insert_bs(blk_old_backing, base_bs, |
| 3389 | &local_err); |
| 3390 | if (ret < 0) { |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3391 | error_reportf_err(local_err, |
Sam Eiderman | 4ebe061 | 2019-05-23 19:33:35 +0300 | [diff] [blame] | 3392 | "Could not reuse old backing file '%s': ", |
| 3393 | base_bs->filename); |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3394 | goto out; |
| 3395 | } |
| 3396 | } else { |
| 3397 | blk_old_backing = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3398 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3399 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3400 | if (out_baseimg[0]) { |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3401 | const char *overlay_filename; |
| 3402 | char *out_real_path; |
| 3403 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3404 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3405 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3406 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3407 | } |
| 3408 | if (force_share) { |
| 3409 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3410 | } |
| 3411 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 3412 | bdrv_refresh_filename(bs); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3413 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
| 3414 | : bs->filename; |
Max Reitz | 645ae7d | 2019-02-01 20:29:14 +0100 | [diff] [blame] | 3415 | out_real_path = |
| 3416 | bdrv_get_full_backing_filename_from_filename(overlay_filename, |
| 3417 | out_baseimg, |
| 3418 | &local_err); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3419 | if (local_err) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3420 | qobject_unref(options); |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3421 | error_reportf_err(local_err, |
| 3422 | "Could not resolve backing filename: "); |
| 3423 | ret = -1; |
Max Reitz | d16699b | 2018-05-09 20:20:01 +0200 | [diff] [blame] | 3424 | goto out; |
| 3425 | } |
| 3426 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3427 | /* |
| 3428 | * Find out whether we rebase an image on top of a previous image |
| 3429 | * in its chain. |
| 3430 | */ |
| 3431 | prefix_chain_bs = bdrv_find_backing_image(bs, out_real_path); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3432 | if (prefix_chain_bs) { |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3433 | qobject_unref(options); |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3434 | g_free(out_real_path); |
Max Reitz | f22356d | 2019-05-28 21:53:38 +0200 | [diff] [blame] | 3435 | |
Kevin Wolf | d861ab3 | 2019-04-25 14:25:10 +0200 | [diff] [blame] | 3436 | blk_new_backing = blk_new(qemu_get_aio_context(), |
| 3437 | BLK_PERM_CONSISTENT_READ, |
Sam Eiderman | 330c729 | 2019-05-23 19:33:37 +0300 | [diff] [blame] | 3438 | BLK_PERM_ALL); |
| 3439 | ret = blk_insert_bs(blk_new_backing, prefix_chain_bs, |
| 3440 | &local_err); |
| 3441 | if (ret < 0) { |
| 3442 | error_reportf_err(local_err, |
| 3443 | "Could not reuse backing file '%s': ", |
| 3444 | out_baseimg); |
| 3445 | goto out; |
| 3446 | } |
| 3447 | } else { |
| 3448 | blk_new_backing = blk_new_open(out_real_path, NULL, |
| 3449 | options, src_flags, &local_err); |
| 3450 | g_free(out_real_path); |
| 3451 | if (!blk_new_backing) { |
| 3452 | error_reportf_err(local_err, |
| 3453 | "Could not open new backing file '%s': ", |
| 3454 | out_baseimg); |
| 3455 | ret = -1; |
| 3456 | goto out; |
| 3457 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3458 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | /* |
| 3463 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3464 | * accesses go to the backing file. We must therefore compare this cluster |
| 3465 | * in the old and new backing file, and if they differ we need to copy it |
| 3466 | * from the old backing file into the COW file. |
| 3467 | * |
| 3468 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3469 | * the image is the same as the original one at any time. |
| 3470 | */ |
| 3471 | if (!unsafe) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3472 | int64_t size; |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3473 | int64_t old_backing_size = 0; |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3474 | int64_t new_backing_size = 0; |
| 3475 | uint64_t offset; |
| 3476 | int64_t n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3477 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3478 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3479 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3480 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3481 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3482 | size = blk_getlength(blk); |
| 3483 | if (size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3484 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3485 | filename, strerror(-size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3486 | ret = -1; |
| 3487 | goto out; |
| 3488 | } |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3489 | if (blk_old_backing) { |
| 3490 | old_backing_size = blk_getlength(blk_old_backing); |
| 3491 | if (old_backing_size < 0) { |
| 3492 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3493 | |
Max Reitz | 35ddd93 | 2019-05-09 19:52:35 +0200 | [diff] [blame] | 3494 | bdrv_get_backing_filename(bs, backing_name, |
| 3495 | sizeof(backing_name)); |
| 3496 | error_report("Could not get size of '%s': %s", |
| 3497 | backing_name, strerror(-old_backing_size)); |
| 3498 | ret = -1; |
| 3499 | goto out; |
| 3500 | } |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3501 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3502 | if (blk_new_backing) { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3503 | new_backing_size = blk_getlength(blk_new_backing); |
| 3504 | if (new_backing_size < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3505 | error_report("Could not get size of '%s': %s", |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3506 | out_baseimg, strerror(-new_backing_size)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3507 | ret = -1; |
| 3508 | goto out; |
| 3509 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3510 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3511 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3512 | if (size != 0) { |
| 3513 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3514 | } |
| 3515 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3516 | for (offset = 0; offset < size; offset += n) { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3517 | bool buf_old_is_zero = false; |
| 3518 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3519 | /* How many bytes can we handle with the next read? */ |
| 3520 | n = MIN(IO_BUF_SIZE, size - offset); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3521 | |
| 3522 | /* If the cluster is allocated, we don't need to take action */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3523 | ret = bdrv_is_allocated(bs, offset, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3524 | if (ret < 0) { |
| 3525 | error_report("error while reading image metadata: %s", |
| 3526 | strerror(-ret)); |
| 3527 | goto out; |
| 3528 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3529 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3530 | continue; |
| 3531 | } |
| 3532 | |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3533 | if (prefix_chain_bs) { |
| 3534 | /* |
| 3535 | * If cluster wasn't changed since prefix_chain, we don't need |
| 3536 | * to take action |
| 3537 | */ |
| 3538 | ret = bdrv_is_allocated_above(backing_bs(bs), prefix_chain_bs, |
Andrey Shinkevich | 170d3bd | 2019-05-29 20:56:14 +0300 | [diff] [blame] | 3539 | false, offset, n, &n); |
Sam Eiderman | 863cc78 | 2019-05-23 19:33:36 +0300 | [diff] [blame] | 3540 | if (ret < 0) { |
| 3541 | error_report("error while reading image metadata: %s", |
| 3542 | strerror(-ret)); |
| 3543 | goto out; |
| 3544 | } |
| 3545 | if (!ret) { |
| 3546 | continue; |
| 3547 | } |
| 3548 | } |
| 3549 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3550 | /* |
| 3551 | * Read old and new backing file and take into consideration that |
| 3552 | * backing files may be smaller than the COW image. |
| 3553 | */ |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3554 | if (offset >= old_backing_size) { |
| 3555 | memset(buf_old, 0, n); |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3556 | buf_old_is_zero = true; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3557 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3558 | if (offset + n > old_backing_size) { |
| 3559 | n = old_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3560 | } |
| 3561 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3562 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3563 | if (ret < 0) { |
| 3564 | error_report("error while reading from old backing file"); |
| 3565 | goto out; |
| 3566 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3567 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3568 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3569 | if (offset >= new_backing_size || !blk_new_backing) { |
| 3570 | memset(buf_new, 0, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3571 | } else { |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3572 | if (offset + n > new_backing_size) { |
| 3573 | n = new_backing_size - offset; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3574 | } |
| 3575 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3576 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3577 | if (ret < 0) { |
| 3578 | error_report("error while reading from new backing file"); |
| 3579 | goto out; |
| 3580 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | /* If they differ, we need to write to the COW file */ |
| 3584 | uint64_t written = 0; |
| 3585 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3586 | while (written < n) { |
Eric Blake | dc61cd3 | 2017-10-11 22:47:14 -0500 | [diff] [blame] | 3587 | int64_t pnum; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3588 | |
Eric Blake | 4153628 | 2017-10-11 22:47:15 -0500 | [diff] [blame] | 3589 | if (compare_buffers(buf_old + written, buf_new + written, |
| 3590 | n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3591 | { |
Max Reitz | 1c6e877 | 2019-05-09 19:52:36 +0200 | [diff] [blame] | 3592 | if (buf_old_is_zero) { |
| 3593 | ret = blk_pwrite_zeroes(blk, offset + written, pnum, 0); |
| 3594 | } else { |
| 3595 | ret = blk_pwrite(blk, offset + written, |
| 3596 | buf_old + written, pnum, 0); |
| 3597 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3598 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3599 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3600 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3601 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | written += pnum; |
| 3606 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3607 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3608 | } |
| 3609 | } |
| 3610 | |
| 3611 | /* |
| 3612 | * Change the backing file. All clusters that are different from the old |
| 3613 | * backing file are overwritten in the COW file now, so the visible content |
| 3614 | * doesn't change when we switch the backing file. |
| 3615 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3616 | if (out_baseimg && *out_baseimg) { |
| 3617 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
| 3618 | } else { |
| 3619 | ret = bdrv_change_backing_file(bs, NULL, NULL); |
| 3620 | } |
| 3621 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3622 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3623 | error_report("Could not change the backing file to '%s': No " |
| 3624 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3625 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3626 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3627 | out_baseimg, strerror(-ret)); |
| 3628 | } |
| 3629 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3630 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3631 | /* |
| 3632 | * TODO At this point it is possible to check if any clusters that are |
| 3633 | * allocated in the COW file are the same in the backing file. If so, they |
| 3634 | * could be dropped from the COW file. Don't do this before switching the |
| 3635 | * backing file, in case of a crash this would lead to corruption. |
| 3636 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3637 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3638 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3639 | /* Cleanup */ |
| 3640 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3641 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3642 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3643 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3644 | qemu_vfree(buf_old); |
| 3645 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3646 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3647 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3648 | if (ret) { |
| 3649 | return 1; |
| 3650 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3651 | return 0; |
| 3652 | } |
| 3653 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3654 | static int img_resize(int argc, char **argv) |
| 3655 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3656 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3657 | int c, ret, relative; |
| 3658 | const char *filename, *fmt, *size; |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3659 | int64_t n, total_size, current_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3660 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3661 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3662 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3663 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3664 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3665 | static QemuOptsList resize_options = { |
| 3666 | .name = "resize_options", |
| 3667 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3668 | .desc = { |
| 3669 | { |
| 3670 | .name = BLOCK_OPT_SIZE, |
| 3671 | .type = QEMU_OPT_SIZE, |
| 3672 | .help = "Virtual disk size" |
| 3673 | }, { |
| 3674 | /* end of list */ |
| 3675 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3676 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3677 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3678 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3679 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3680 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3681 | /* Remove size from argv manually so that negative numbers are not treated |
| 3682 | * as options by getopt. */ |
| 3683 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3684 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3685 | return 1; |
| 3686 | } |
| 3687 | |
| 3688 | size = argv[--argc]; |
| 3689 | |
| 3690 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3691 | fmt = NULL; |
| 3692 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3693 | static const struct option long_options[] = { |
| 3694 | {"help", no_argument, 0, 'h'}, |
| 3695 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3696 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3697 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3698 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3699 | {0, 0, 0, 0} |
| 3700 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3701 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3702 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3703 | if (c == -1) { |
| 3704 | break; |
| 3705 | } |
| 3706 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3707 | case ':': |
| 3708 | missing_argument(argv[optind - 1]); |
| 3709 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3710 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3711 | unrecognized_option(argv[optind - 1]); |
| 3712 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3713 | case 'h': |
| 3714 | help(); |
| 3715 | break; |
| 3716 | case 'f': |
| 3717 | fmt = optarg; |
| 3718 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3719 | case 'q': |
| 3720 | quiet = true; |
| 3721 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3722 | case OPTION_OBJECT: { |
| 3723 | QemuOpts *opts; |
| 3724 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3725 | optarg, true); |
| 3726 | if (!opts) { |
| 3727 | return 1; |
| 3728 | } |
| 3729 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3730 | case OPTION_IMAGE_OPTS: |
| 3731 | image_opts = true; |
| 3732 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3733 | case OPTION_PREALLOCATION: |
Marc-André Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3734 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3735 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3736 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3737 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3738 | return 1; |
| 3739 | } |
| 3740 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3741 | case OPTION_SHRINK: |
| 3742 | shrink = true; |
| 3743 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3744 | } |
| 3745 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3746 | if (optind != argc - 1) { |
Max Reitz | be8fbd4 | 2018-02-05 17:27:45 +0100 | [diff] [blame] | 3747 | error_exit("Expecting image file name and size"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3748 | } |
| 3749 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3750 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3751 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3752 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3753 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3754 | return 1; |
| 3755 | } |
| 3756 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3757 | /* Choose grow, shrink, or absolute resize mode */ |
| 3758 | switch (size[0]) { |
| 3759 | case '+': |
| 3760 | relative = 1; |
| 3761 | size++; |
| 3762 | break; |
| 3763 | case '-': |
| 3764 | relative = -1; |
| 3765 | size++; |
| 3766 | break; |
| 3767 | default: |
| 3768 | relative = 0; |
| 3769 | break; |
| 3770 | } |
| 3771 | |
| 3772 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3773 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3774 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3775 | if (err) { |
| 3776 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3777 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3778 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3779 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3780 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3781 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3782 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3783 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3784 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3785 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 3786 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3787 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3788 | ret = -1; |
| 3789 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3790 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3791 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3792 | current_size = blk_getlength(blk); |
| 3793 | if (current_size < 0) { |
| 3794 | error_report("Failed to inquire current image length: %s", |
| 3795 | strerror(-current_size)); |
| 3796 | ret = -1; |
| 3797 | goto out; |
| 3798 | } |
| 3799 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3800 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3801 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3802 | } else { |
| 3803 | total_size = n; |
| 3804 | } |
| 3805 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3806 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3807 | ret = -1; |
| 3808 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3809 | } |
| 3810 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3811 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 3812 | error_report("Preallocation can only be used for growing images"); |
| 3813 | ret = -1; |
| 3814 | goto out; |
| 3815 | } |
| 3816 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3817 | if (total_size < current_size && !shrink) { |
| 3818 | warn_report("Shrinking an image will delete all data beyond the " |
| 3819 | "shrunken image's end. Before performing such an " |
| 3820 | "operation, make sure there is no important data there."); |
| 3821 | |
| 3822 | if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) { |
| 3823 | error_report( |
| 3824 | "Use the --shrink option to perform a shrink operation."); |
| 3825 | ret = -1; |
| 3826 | goto out; |
| 3827 | } else { |
| 3828 | warn_report("Using the --shrink option will suppress this message. " |
| 3829 | "Note that future versions of qemu-img may refuse to " |
| 3830 | "shrink images without this option."); |
| 3831 | } |
| 3832 | } |
| 3833 | |
Max Reitz | e8d04f9 | 2019-09-18 11:51:43 +0200 | [diff] [blame] | 3834 | /* |
| 3835 | * The user expects the image to have the desired size after |
| 3836 | * resizing, so pass @exact=true. It is of no use to report |
| 3837 | * success when the image has not actually been resized. |
| 3838 | */ |
| 3839 | ret = blk_truncate(blk, total_size, true, prealloc, &err); |
Max Reitz | 09c5c6d | 2019-09-18 11:51:44 +0200 | [diff] [blame] | 3840 | if (!ret) { |
| 3841 | qprintf(quiet, "Image resized.\n"); |
| 3842 | } else { |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 3843 | error_report_err(err); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3844 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3845 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3846 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3847 | if (ret) { |
| 3848 | return 1; |
| 3849 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3850 | return 0; |
| 3851 | } |
| 3852 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3853 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 3854 | int64_t offset, int64_t total_work_size, |
| 3855 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3856 | { |
| 3857 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 3858 | } |
| 3859 | |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 3860 | static int print_amend_option_help(const char *format) |
| 3861 | { |
| 3862 | BlockDriver *drv; |
| 3863 | |
| 3864 | /* Find driver and parse its options */ |
| 3865 | drv = bdrv_find_format(format); |
| 3866 | if (!drv) { |
| 3867 | error_report("Unknown file format '%s'", format); |
| 3868 | return 1; |
| 3869 | } |
| 3870 | |
| 3871 | if (!drv->bdrv_amend_options) { |
| 3872 | error_report("Format driver '%s' does not support option amendment", |
| 3873 | format); |
| 3874 | return 1; |
| 3875 | } |
| 3876 | |
| 3877 | /* Every driver supporting amendment must have create_opts */ |
| 3878 | assert(drv->create_opts); |
| 3879 | |
| 3880 | printf("Creation options for '%s':\n", format); |
Max Reitz | 6389871 | 2018-10-19 18:49:25 +0200 | [diff] [blame] | 3881 | qemu_opts_print_help(drv->create_opts, false); |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 3882 | printf("\nNote that not all of these options may be amendable.\n"); |
| 3883 | return 0; |
| 3884 | } |
| 3885 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3886 | static int img_amend(int argc, char **argv) |
| 3887 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3888 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3889 | int c, ret = 0; |
| 3890 | char *options = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3891 | QemuOptsList *create_opts = NULL; |
| 3892 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3893 | const char *fmt = NULL, *filename, *cache; |
| 3894 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3895 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3896 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3897 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3898 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3899 | bool image_opts = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3900 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3901 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3902 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3903 | static const struct option long_options[] = { |
| 3904 | {"help", no_argument, 0, 'h'}, |
| 3905 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3906 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3907 | {0, 0, 0, 0} |
| 3908 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3909 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3910 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3911 | if (c == -1) { |
| 3912 | break; |
| 3913 | } |
| 3914 | |
| 3915 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3916 | case ':': |
| 3917 | missing_argument(argv[optind - 1]); |
| 3918 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 3919 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3920 | unrecognized_option(argv[optind - 1]); |
| 3921 | break; |
| 3922 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 3923 | help(); |
| 3924 | break; |
| 3925 | case 'o': |
| 3926 | if (!is_valid_option_list(optarg)) { |
| 3927 | error_report("Invalid option list: %s", optarg); |
| 3928 | ret = -1; |
| 3929 | goto out_no_progress; |
| 3930 | } |
| 3931 | if (!options) { |
| 3932 | options = g_strdup(optarg); |
| 3933 | } else { |
| 3934 | char *old_options = options; |
| 3935 | options = g_strdup_printf("%s,%s", options, optarg); |
| 3936 | g_free(old_options); |
| 3937 | } |
| 3938 | break; |
| 3939 | case 'f': |
| 3940 | fmt = optarg; |
| 3941 | break; |
| 3942 | case 't': |
| 3943 | cache = optarg; |
| 3944 | break; |
| 3945 | case 'p': |
| 3946 | progress = true; |
| 3947 | break; |
| 3948 | case 'q': |
| 3949 | quiet = true; |
| 3950 | break; |
| 3951 | case OPTION_OBJECT: |
| 3952 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3953 | optarg, true); |
| 3954 | if (!opts) { |
| 3955 | ret = -1; |
| 3956 | goto out_no_progress; |
| 3957 | } |
| 3958 | break; |
| 3959 | case OPTION_IMAGE_OPTS: |
| 3960 | image_opts = true; |
| 3961 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3962 | } |
| 3963 | } |
| 3964 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3965 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3966 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3967 | } |
| 3968 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3969 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3970 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 3971 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3972 | ret = -1; |
| 3973 | goto out_no_progress; |
| 3974 | } |
| 3975 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3976 | if (quiet) { |
| 3977 | progress = false; |
| 3978 | } |
| 3979 | qemu_progress_init(progress, 1.0); |
| 3980 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3981 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 3982 | if (fmt && has_help_option(options)) { |
| 3983 | /* If a format is explicitly specified (and possibly no filename is |
| 3984 | * given), print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 3985 | ret = print_amend_option_help(fmt); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3986 | goto out; |
| 3987 | } |
| 3988 | |
| 3989 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 3990 | error_report("Expecting one image file name"); |
| 3991 | ret = -1; |
| 3992 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3993 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3994 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3995 | flags = BDRV_O_RDWR; |
| 3996 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3997 | if (ret < 0) { |
| 3998 | error_report("Invalid cache option: %s", cache); |
| 3999 | goto out; |
| 4000 | } |
| 4001 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4002 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4003 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4004 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4005 | ret = -1; |
| 4006 | goto out; |
| 4007 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 4008 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4009 | |
| 4010 | fmt = bs->drv->format_name; |
| 4011 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4012 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 4013 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 5164135 | 2018-05-09 23:00:20 +0200 | [diff] [blame] | 4014 | ret = print_amend_option_help(fmt); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4015 | goto out; |
| 4016 | } |
| 4017 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4018 | if (!bs->drv->bdrv_amend_options) { |
| 4019 | error_report("Format driver '%s' does not support option amendment", |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 4020 | fmt); |
| 4021 | ret = -1; |
| 4022 | goto out; |
| 4023 | } |
| 4024 | |
Max Reitz | 1f99668 | 2018-05-09 23:00:17 +0200 | [diff] [blame] | 4025 | /* Every driver supporting amendment must have create_opts */ |
| 4026 | assert(bs->drv->create_opts); |
| 4027 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 4028 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4029 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 4030 | qemu_opts_do_parse(opts, options, NULL, &err); |
| 4031 | if (err) { |
| 4032 | error_report_err(err); |
| 4033 | ret = -1; |
| 4034 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4035 | } |
| 4036 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4037 | /* In case the driver does not call amend_status_cb() */ |
| 4038 | qemu_progress_print(0.f, 0); |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 4039 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4040 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4041 | if (ret < 0) { |
Max Reitz | d1402b5 | 2018-05-09 23:00:18 +0200 | [diff] [blame] | 4042 | error_report_err(err); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4043 | goto out; |
| 4044 | } |
| 4045 | |
| 4046 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 4047 | qemu_progress_end(); |
| 4048 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 4049 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 4050 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 4051 | qemu_opts_del(opts); |
| 4052 | qemu_opts_free(create_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 4053 | g_free(options); |
| 4054 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 4055 | if (ret) { |
| 4056 | return 1; |
| 4057 | } |
| 4058 | return 0; |
| 4059 | } |
| 4060 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4061 | typedef struct BenchData { |
| 4062 | BlockBackend *blk; |
| 4063 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4064 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4065 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4066 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4067 | int nrreq; |
| 4068 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4069 | int flush_interval; |
| 4070 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4071 | uint8_t *buf; |
| 4072 | QEMUIOVector *qiov; |
| 4073 | |
| 4074 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4075 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4076 | uint64_t offset; |
| 4077 | } BenchData; |
| 4078 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4079 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 4080 | { |
| 4081 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4082 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4083 | exit(EXIT_FAILURE); |
| 4084 | } |
| 4085 | } |
| 4086 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4087 | static void bench_cb(void *opaque, int ret) |
| 4088 | { |
| 4089 | BenchData *b = opaque; |
| 4090 | BlockAIOCB *acb; |
| 4091 | |
| 4092 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 4093 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4094 | exit(EXIT_FAILURE); |
| 4095 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4096 | |
| 4097 | if (b->in_flush) { |
| 4098 | /* Just finished a flush with drained queue: Start next requests */ |
| 4099 | assert(b->in_flight == 0); |
| 4100 | b->in_flush = false; |
| 4101 | } else if (b->in_flight > 0) { |
| 4102 | int remaining = b->n - b->in_flight; |
| 4103 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4104 | b->n--; |
| 4105 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4106 | |
| 4107 | /* Time for flush? Drain queue if requested, then flush */ |
| 4108 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 4109 | if (!b->in_flight || !b->drain_on_flush) { |
| 4110 | BlockCompletionFunc *cb; |
| 4111 | |
| 4112 | if (b->drain_on_flush) { |
| 4113 | b->in_flush = true; |
| 4114 | cb = bench_cb; |
| 4115 | } else { |
| 4116 | cb = bench_undrained_flush_cb; |
| 4117 | } |
| 4118 | |
| 4119 | acb = blk_aio_flush(b->blk, cb, b); |
| 4120 | if (!acb) { |
| 4121 | error_report("Failed to issue flush request"); |
| 4122 | exit(EXIT_FAILURE); |
| 4123 | } |
| 4124 | } |
| 4125 | if (b->drain_on_flush) { |
| 4126 | return; |
| 4127 | } |
| 4128 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4132 | int64_t offset = b->offset; |
| 4133 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 4134 | * again, so make sure this operation is counted by in_flight |
| 4135 | * and b->offset is ready for the next submission. |
| 4136 | */ |
| 4137 | b->in_flight++; |
| 4138 | b->offset += b->step; |
| 4139 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4140 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4141 | acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4142 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 4143 | acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4144 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4145 | if (!acb) { |
| 4146 | error_report("Failed to issue request"); |
| 4147 | exit(EXIT_FAILURE); |
| 4148 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | static int img_bench(int argc, char **argv) |
| 4153 | { |
| 4154 | int c, ret = 0; |
| 4155 | const char *fmt = NULL, *filename; |
| 4156 | bool quiet = false; |
| 4157 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4158 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4159 | int count = 75000; |
| 4160 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4161 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4162 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4163 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4164 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4165 | int flush_interval = 0; |
| 4166 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4167 | int64_t image_size; |
| 4168 | BlockBackend *blk = NULL; |
| 4169 | BenchData data = {}; |
| 4170 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 4171 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4172 | struct timeval t1, t2; |
| 4173 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4174 | bool force_share = false; |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4175 | size_t buf_size; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4176 | |
| 4177 | for (;;) { |
| 4178 | static const struct option long_options[] = { |
| 4179 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4180 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4181 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4182 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4183 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4184 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4185 | {0, 0, 0, 0} |
| 4186 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4187 | c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4188 | if (c == -1) { |
| 4189 | break; |
| 4190 | } |
| 4191 | |
| 4192 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4193 | case ':': |
| 4194 | missing_argument(argv[optind - 1]); |
| 4195 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4196 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4197 | unrecognized_option(argv[optind - 1]); |
| 4198 | break; |
| 4199 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4200 | help(); |
| 4201 | break; |
| 4202 | case 'c': |
| 4203 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4204 | unsigned long res; |
| 4205 | |
| 4206 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4207 | error_report("Invalid request count specified"); |
| 4208 | return 1; |
| 4209 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4210 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4211 | break; |
| 4212 | } |
| 4213 | case 'd': |
| 4214 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4215 | unsigned long res; |
| 4216 | |
| 4217 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4218 | error_report("Invalid queue depth specified"); |
| 4219 | return 1; |
| 4220 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4221 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4222 | break; |
| 4223 | } |
| 4224 | case 'f': |
| 4225 | fmt = optarg; |
| 4226 | break; |
| 4227 | case 'n': |
| 4228 | flags |= BDRV_O_NATIVE_AIO; |
| 4229 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4230 | case 'o': |
| 4231 | { |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4232 | offset = cvtnum(optarg); |
| 4233 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4234 | error_report("Invalid offset specified"); |
| 4235 | return 1; |
| 4236 | } |
| 4237 | break; |
| 4238 | } |
| 4239 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4240 | case 'q': |
| 4241 | quiet = true; |
| 4242 | break; |
| 4243 | case 's': |
| 4244 | { |
| 4245 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4246 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4247 | sval = cvtnum(optarg); |
| 4248 | if (sval < 0 || sval > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4249 | error_report("Invalid buffer size specified"); |
| 4250 | return 1; |
| 4251 | } |
| 4252 | |
| 4253 | bufsize = sval; |
| 4254 | break; |
| 4255 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4256 | case 'S': |
| 4257 | { |
| 4258 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4259 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4260 | sval = cvtnum(optarg); |
| 4261 | if (sval < 0 || sval > INT_MAX) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4262 | error_report("Invalid step size specified"); |
| 4263 | return 1; |
| 4264 | } |
| 4265 | |
| 4266 | step = sval; |
| 4267 | break; |
| 4268 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4269 | case 't': |
| 4270 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 4271 | if (ret < 0) { |
| 4272 | error_report("Invalid cache mode"); |
| 4273 | ret = -1; |
| 4274 | goto out; |
| 4275 | } |
| 4276 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4277 | case 'w': |
| 4278 | flags |= BDRV_O_RDWR; |
| 4279 | is_write = true; |
| 4280 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4281 | case 'U': |
| 4282 | force_share = true; |
| 4283 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4284 | case OPTION_PATTERN: |
| 4285 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4286 | unsigned long res; |
| 4287 | |
| 4288 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4289 | error_report("Invalid pattern byte specified"); |
| 4290 | return 1; |
| 4291 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4292 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4293 | break; |
| 4294 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4295 | case OPTION_FLUSH_INTERVAL: |
| 4296 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4297 | unsigned long res; |
| 4298 | |
| 4299 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4300 | error_report("Invalid flush interval specified"); |
| 4301 | return 1; |
| 4302 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4303 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4304 | break; |
| 4305 | } |
| 4306 | case OPTION_NO_DRAIN: |
| 4307 | drain_on_flush = false; |
| 4308 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4309 | case OPTION_IMAGE_OPTS: |
| 4310 | image_opts = true; |
| 4311 | break; |
| 4312 | } |
| 4313 | } |
| 4314 | |
| 4315 | if (optind != argc - 1) { |
| 4316 | error_exit("Expecting one image file name"); |
| 4317 | } |
| 4318 | filename = argv[argc - 1]; |
| 4319 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4320 | if (!is_write && flush_interval) { |
| 4321 | error_report("--flush-interval is only available in write tests"); |
| 4322 | ret = -1; |
| 4323 | goto out; |
| 4324 | } |
| 4325 | if (flush_interval && flush_interval < depth) { |
| 4326 | error_report("Flush interval can't be smaller than depth"); |
| 4327 | ret = -1; |
| 4328 | goto out; |
| 4329 | } |
| 4330 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4331 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4332 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4333 | if (!blk) { |
| 4334 | ret = -1; |
| 4335 | goto out; |
| 4336 | } |
| 4337 | |
| 4338 | image_size = blk_getlength(blk); |
| 4339 | if (image_size < 0) { |
| 4340 | ret = image_size; |
| 4341 | goto out; |
| 4342 | } |
| 4343 | |
| 4344 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4345 | .blk = blk, |
| 4346 | .image_size = image_size, |
| 4347 | .bufsize = bufsize, |
| 4348 | .step = step ?: bufsize, |
| 4349 | .nrreq = depth, |
| 4350 | .n = count, |
| 4351 | .offset = offset, |
| 4352 | .write = is_write, |
| 4353 | .flush_interval = flush_interval, |
| 4354 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4355 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4356 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4357 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4358 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4359 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4360 | if (flush_interval) { |
| 4361 | printf("Sending flush every %d requests\n", flush_interval); |
| 4362 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4363 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4364 | buf_size = data.nrreq * data.bufsize; |
| 4365 | data.buf = blk_blockalign(blk, buf_size); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4366 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4367 | |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4368 | blk_register_buf(blk, data.buf, buf_size); |
| 4369 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4370 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4371 | for (i = 0; i < data.nrreq; i++) { |
| 4372 | qemu_iovec_init(&data.qiov[i], 1); |
| 4373 | qemu_iovec_add(&data.qiov[i], |
| 4374 | data.buf + i * data.bufsize, data.bufsize); |
| 4375 | } |
| 4376 | |
| 4377 | gettimeofday(&t1, NULL); |
| 4378 | bench_cb(&data, 0); |
| 4379 | |
| 4380 | while (data.n > 0) { |
| 4381 | main_loop_wait(false); |
| 4382 | } |
| 4383 | gettimeofday(&t2, NULL); |
| 4384 | |
| 4385 | printf("Run completed in %3.3f seconds.\n", |
| 4386 | (t2.tv_sec - t1.tv_sec) |
| 4387 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4388 | |
| 4389 | out: |
Fam Zheng | 79d4658 | 2018-01-16 14:08:58 +0800 | [diff] [blame] | 4390 | if (data.buf) { |
| 4391 | blk_unregister_buf(blk, data.buf); |
| 4392 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4393 | qemu_vfree(data.buf); |
| 4394 | blk_unref(blk); |
| 4395 | |
| 4396 | if (ret) { |
| 4397 | return 1; |
| 4398 | } |
| 4399 | return 0; |
| 4400 | } |
| 4401 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4402 | #define C_BS 01 |
| 4403 | #define C_COUNT 02 |
| 4404 | #define C_IF 04 |
| 4405 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4406 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4407 | |
| 4408 | struct DdInfo { |
| 4409 | unsigned int flags; |
| 4410 | int64_t count; |
| 4411 | }; |
| 4412 | |
| 4413 | struct DdIo { |
| 4414 | int bsz; /* Block size */ |
| 4415 | char *filename; |
| 4416 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4417 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4418 | }; |
| 4419 | |
| 4420 | struct DdOpts { |
| 4421 | const char *name; |
| 4422 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4423 | unsigned int flag; |
| 4424 | }; |
| 4425 | |
| 4426 | static int img_dd_bs(const char *arg, |
| 4427 | struct DdIo *in, struct DdIo *out, |
| 4428 | struct DdInfo *dd) |
| 4429 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4430 | int64_t res; |
| 4431 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4432 | res = cvtnum(arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4433 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4434 | if (res <= 0 || res > INT_MAX) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4435 | error_report("invalid number: '%s'", arg); |
| 4436 | return 1; |
| 4437 | } |
| 4438 | in->bsz = out->bsz = res; |
| 4439 | |
| 4440 | return 0; |
| 4441 | } |
| 4442 | |
| 4443 | static int img_dd_count(const char *arg, |
| 4444 | struct DdIo *in, struct DdIo *out, |
| 4445 | struct DdInfo *dd) |
| 4446 | { |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4447 | dd->count = cvtnum(arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4448 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4449 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4450 | error_report("invalid number: '%s'", arg); |
| 4451 | return 1; |
| 4452 | } |
| 4453 | |
| 4454 | return 0; |
| 4455 | } |
| 4456 | |
| 4457 | static int img_dd_if(const char *arg, |
| 4458 | struct DdIo *in, struct DdIo *out, |
| 4459 | struct DdInfo *dd) |
| 4460 | { |
| 4461 | in->filename = g_strdup(arg); |
| 4462 | |
| 4463 | return 0; |
| 4464 | } |
| 4465 | |
| 4466 | static int img_dd_of(const char *arg, |
| 4467 | struct DdIo *in, struct DdIo *out, |
| 4468 | struct DdInfo *dd) |
| 4469 | { |
| 4470 | out->filename = g_strdup(arg); |
| 4471 | |
| 4472 | return 0; |
| 4473 | } |
| 4474 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4475 | static int img_dd_skip(const char *arg, |
| 4476 | struct DdIo *in, struct DdIo *out, |
| 4477 | struct DdInfo *dd) |
| 4478 | { |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4479 | in->offset = cvtnum(arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4480 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4481 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4482 | error_report("invalid number: '%s'", arg); |
| 4483 | return 1; |
| 4484 | } |
| 4485 | |
| 4486 | return 0; |
| 4487 | } |
| 4488 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4489 | static int img_dd(int argc, char **argv) |
| 4490 | { |
| 4491 | int ret = 0; |
| 4492 | char *arg = NULL; |
| 4493 | char *tmp; |
| 4494 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4495 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4496 | QemuOpts *opts = NULL; |
| 4497 | QemuOptsList *create_opts = NULL; |
| 4498 | Error *local_err = NULL; |
| 4499 | bool image_opts = false; |
| 4500 | int c, i; |
| 4501 | const char *out_fmt = "raw"; |
| 4502 | const char *fmt = NULL; |
| 4503 | int64_t size = 0; |
| 4504 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4505 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4506 | struct DdInfo dd = { |
| 4507 | .flags = 0, |
| 4508 | .count = 0, |
| 4509 | }; |
| 4510 | struct DdIo in = { |
| 4511 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4512 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4513 | .buf = NULL, |
| 4514 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4515 | }; |
| 4516 | struct DdIo out = { |
| 4517 | .bsz = 512, |
| 4518 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4519 | .buf = NULL, |
| 4520 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4521 | }; |
| 4522 | |
| 4523 | const struct DdOpts options[] = { |
| 4524 | { "bs", img_dd_bs, C_BS }, |
| 4525 | { "count", img_dd_count, C_COUNT }, |
| 4526 | { "if", img_dd_if, C_IF }, |
| 4527 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4528 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4529 | { NULL, NULL, 0 } |
| 4530 | }; |
| 4531 | const struct option long_options[] = { |
| 4532 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4533 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4534 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4535 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4536 | { 0, 0, 0, 0 } |
| 4537 | }; |
| 4538 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4539 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4540 | if (c == EOF) { |
| 4541 | break; |
| 4542 | } |
| 4543 | switch (c) { |
| 4544 | case 'O': |
| 4545 | out_fmt = optarg; |
| 4546 | break; |
| 4547 | case 'f': |
| 4548 | fmt = optarg; |
| 4549 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4550 | case ':': |
| 4551 | missing_argument(argv[optind - 1]); |
| 4552 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4553 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4554 | unrecognized_option(argv[optind - 1]); |
| 4555 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4556 | case 'h': |
| 4557 | help(); |
| 4558 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4559 | case 'U': |
| 4560 | force_share = true; |
| 4561 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4562 | case OPTION_OBJECT: |
| 4563 | if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) { |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4564 | ret = -1; |
| 4565 | goto out; |
| 4566 | } |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4567 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4568 | case OPTION_IMAGE_OPTS: |
| 4569 | image_opts = true; |
| 4570 | break; |
| 4571 | } |
| 4572 | } |
| 4573 | |
| 4574 | for (i = optind; i < argc; i++) { |
| 4575 | int j; |
| 4576 | arg = g_strdup(argv[i]); |
| 4577 | |
| 4578 | tmp = strchr(arg, '='); |
| 4579 | if (tmp == NULL) { |
| 4580 | error_report("unrecognized operand %s", arg); |
| 4581 | ret = -1; |
| 4582 | goto out; |
| 4583 | } |
| 4584 | |
| 4585 | *tmp++ = '\0'; |
| 4586 | |
| 4587 | for (j = 0; options[j].name != NULL; j++) { |
| 4588 | if (!strcmp(arg, options[j].name)) { |
| 4589 | break; |
| 4590 | } |
| 4591 | } |
| 4592 | if (options[j].name == NULL) { |
| 4593 | error_report("unrecognized operand %s", arg); |
| 4594 | ret = -1; |
| 4595 | goto out; |
| 4596 | } |
| 4597 | |
| 4598 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 4599 | ret = -1; |
| 4600 | goto out; |
| 4601 | } |
| 4602 | dd.flags |= options[j].flag; |
| 4603 | g_free(arg); |
| 4604 | arg = NULL; |
| 4605 | } |
| 4606 | |
| 4607 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 4608 | error_report("Must specify both input and output files"); |
| 4609 | ret = -1; |
| 4610 | goto out; |
| 4611 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4612 | |
| 4613 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4614 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 4615 | qemu_img_object_print_help, &error_fatal)) { |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4616 | ret = -1; |
| 4617 | goto out; |
| 4618 | } |
| 4619 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4620 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 4621 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4622 | |
| 4623 | if (!blk1) { |
| 4624 | ret = -1; |
| 4625 | goto out; |
| 4626 | } |
| 4627 | |
| 4628 | drv = bdrv_find_format(out_fmt); |
| 4629 | if (!drv) { |
| 4630 | error_report("Unknown file format"); |
| 4631 | ret = -1; |
| 4632 | goto out; |
| 4633 | } |
| 4634 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 4635 | |
| 4636 | if (!proto_drv) { |
| 4637 | error_report_err(local_err); |
| 4638 | ret = -1; |
| 4639 | goto out; |
| 4640 | } |
| 4641 | if (!drv->create_opts) { |
| 4642 | error_report("Format driver '%s' does not support image creation", |
| 4643 | drv->format_name); |
| 4644 | ret = -1; |
| 4645 | goto out; |
| 4646 | } |
| 4647 | if (!proto_drv->create_opts) { |
| 4648 | error_report("Protocol driver '%s' does not support image creation", |
| 4649 | proto_drv->format_name); |
| 4650 | ret = -1; |
| 4651 | goto out; |
| 4652 | } |
| 4653 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 4654 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 4655 | |
| 4656 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 4657 | |
| 4658 | size = blk_getlength(blk1); |
| 4659 | if (size < 0) { |
| 4660 | error_report("Failed to get size for '%s'", in.filename); |
| 4661 | ret = -1; |
| 4662 | goto out; |
| 4663 | } |
| 4664 | |
| 4665 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 4666 | dd.count * in.bsz < size) { |
| 4667 | size = dd.count * in.bsz; |
| 4668 | } |
| 4669 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4670 | /* Overflow means the specified offset is beyond input image's size */ |
| 4671 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 4672 | size < in.bsz * in.offset)) { |
| 4673 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 4674 | } else { |
| 4675 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 4676 | size - in.bsz * in.offset, &error_abort); |
| 4677 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4678 | |
| 4679 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 4680 | if (ret < 0) { |
| 4681 | error_reportf_err(local_err, |
| 4682 | "%s: error while creating output image: ", |
| 4683 | out.filename); |
| 4684 | ret = -1; |
| 4685 | goto out; |
| 4686 | } |
| 4687 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 4688 | /* TODO, we can't honour --image-opts for the target, |
| 4689 | * since it needs to be given in a format compatible |
| 4690 | * with the bdrv_create() call above which does not |
| 4691 | * support image-opts style. |
| 4692 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 4693 | blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR, |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 4694 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4695 | |
| 4696 | if (!blk2) { |
| 4697 | ret = -1; |
| 4698 | goto out; |
| 4699 | } |
| 4700 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4701 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 4702 | size < in.offset * in.bsz)) { |
| 4703 | /* We give a warning if the skip option is bigger than the input |
| 4704 | * size and create an empty output disk image (i.e. like dd(1)). |
| 4705 | */ |
| 4706 | error_report("%s: cannot skip to specified offset", in.filename); |
| 4707 | in_pos = size; |
| 4708 | } else { |
| 4709 | in_pos = in.offset * in.bsz; |
| 4710 | } |
| 4711 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4712 | in.buf = g_new(uint8_t, in.bsz); |
| 4713 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4714 | for (out_pos = 0; in_pos < size; block_count++) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4715 | int in_ret, out_ret; |
| 4716 | |
| 4717 | if (in_pos + in.bsz > size) { |
| 4718 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); |
| 4719 | } else { |
| 4720 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); |
| 4721 | } |
| 4722 | if (in_ret < 0) { |
| 4723 | error_report("error while reading from input image file: %s", |
| 4724 | strerror(-in_ret)); |
| 4725 | ret = -1; |
| 4726 | goto out; |
| 4727 | } |
| 4728 | in_pos += in_ret; |
| 4729 | |
| 4730 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); |
| 4731 | |
| 4732 | if (out_ret < 0) { |
| 4733 | error_report("error while writing to output image file: %s", |
| 4734 | strerror(-out_ret)); |
| 4735 | ret = -1; |
| 4736 | goto out; |
| 4737 | } |
| 4738 | out_pos += out_ret; |
| 4739 | } |
| 4740 | |
| 4741 | out: |
| 4742 | g_free(arg); |
| 4743 | qemu_opts_del(opts); |
| 4744 | qemu_opts_free(create_opts); |
| 4745 | blk_unref(blk1); |
| 4746 | blk_unref(blk2); |
| 4747 | g_free(in.filename); |
| 4748 | g_free(out.filename); |
| 4749 | g_free(in.buf); |
| 4750 | g_free(out.buf); |
| 4751 | |
| 4752 | if (ret) { |
| 4753 | return 1; |
| 4754 | } |
| 4755 | return 0; |
| 4756 | } |
| 4757 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 4758 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 4759 | { |
| 4760 | QString *str; |
| 4761 | QObject *obj; |
| 4762 | Visitor *v = qobject_output_visitor_new(&obj); |
| 4763 | |
| 4764 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 4765 | visit_complete(v, &obj); |
| 4766 | str = qobject_to_json_pretty(obj); |
| 4767 | assert(str != NULL); |
| 4768 | printf("%s\n", qstring_get_str(str)); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 4769 | qobject_unref(obj); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 4770 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 4771 | qobject_unref(str); |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | static int img_measure(int argc, char **argv) |
| 4775 | { |
| 4776 | static const struct option long_options[] = { |
| 4777 | {"help", no_argument, 0, 'h'}, |
| 4778 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4779 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4780 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 4781 | {"size", required_argument, 0, OPTION_SIZE}, |
| 4782 | {"force-share", no_argument, 0, 'U'}, |
| 4783 | {0, 0, 0, 0} |
| 4784 | }; |
| 4785 | OutputFormat output_format = OFORMAT_HUMAN; |
| 4786 | BlockBackend *in_blk = NULL; |
| 4787 | BlockDriver *drv; |
| 4788 | const char *filename = NULL; |
| 4789 | const char *fmt = NULL; |
| 4790 | const char *out_fmt = "raw"; |
| 4791 | char *options = NULL; |
| 4792 | char *snapshot_name = NULL; |
| 4793 | bool force_share = false; |
| 4794 | QemuOpts *opts = NULL; |
| 4795 | QemuOpts *object_opts = NULL; |
| 4796 | QemuOpts *sn_opts = NULL; |
| 4797 | QemuOptsList *create_opts = NULL; |
| 4798 | bool image_opts = false; |
| 4799 | uint64_t img_size = UINT64_MAX; |
| 4800 | BlockMeasureInfo *info = NULL; |
| 4801 | Error *local_err = NULL; |
| 4802 | int ret = 1; |
| 4803 | int c; |
| 4804 | |
| 4805 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 4806 | long_options, NULL)) != -1) { |
| 4807 | switch (c) { |
| 4808 | case '?': |
| 4809 | case 'h': |
| 4810 | help(); |
| 4811 | break; |
| 4812 | case 'f': |
| 4813 | fmt = optarg; |
| 4814 | break; |
| 4815 | case 'O': |
| 4816 | out_fmt = optarg; |
| 4817 | break; |
| 4818 | case 'o': |
| 4819 | if (!is_valid_option_list(optarg)) { |
| 4820 | error_report("Invalid option list: %s", optarg); |
| 4821 | goto out; |
| 4822 | } |
| 4823 | if (!options) { |
| 4824 | options = g_strdup(optarg); |
| 4825 | } else { |
| 4826 | char *old_options = options; |
| 4827 | options = g_strdup_printf("%s,%s", options, optarg); |
| 4828 | g_free(old_options); |
| 4829 | } |
| 4830 | break; |
| 4831 | case 'l': |
| 4832 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 4833 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 4834 | optarg, false); |
| 4835 | if (!sn_opts) { |
| 4836 | error_report("Failed in parsing snapshot param '%s'", |
| 4837 | optarg); |
| 4838 | goto out; |
| 4839 | } |
| 4840 | } else { |
| 4841 | snapshot_name = optarg; |
| 4842 | } |
| 4843 | break; |
| 4844 | case 'U': |
| 4845 | force_share = true; |
| 4846 | break; |
| 4847 | case OPTION_OBJECT: |
| 4848 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 4849 | optarg, true); |
| 4850 | if (!object_opts) { |
| 4851 | goto out; |
| 4852 | } |
| 4853 | break; |
| 4854 | case OPTION_IMAGE_OPTS: |
| 4855 | image_opts = true; |
| 4856 | break; |
| 4857 | case OPTION_OUTPUT: |
| 4858 | if (!strcmp(optarg, "json")) { |
| 4859 | output_format = OFORMAT_JSON; |
| 4860 | } else if (!strcmp(optarg, "human")) { |
| 4861 | output_format = OFORMAT_HUMAN; |
| 4862 | } else { |
| 4863 | error_report("--output must be used with human or json " |
| 4864 | "as argument."); |
| 4865 | goto out; |
| 4866 | } |
| 4867 | break; |
| 4868 | case OPTION_SIZE: |
| 4869 | { |
| 4870 | int64_t sval; |
| 4871 | |
| 4872 | sval = cvtnum(optarg); |
| 4873 | if (sval < 0) { |
| 4874 | if (sval == -ERANGE) { |
| 4875 | error_report("Image size must be less than 8 EiB!"); |
| 4876 | } else { |
| 4877 | error_report("Invalid image size specified! You may use " |
| 4878 | "k, M, G, T, P or E suffixes for "); |
| 4879 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 4880 | "petabytes and exabytes."); |
| 4881 | } |
| 4882 | goto out; |
| 4883 | } |
| 4884 | img_size = (uint64_t)sval; |
| 4885 | } |
| 4886 | break; |
| 4887 | } |
| 4888 | } |
| 4889 | |
| 4890 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4891 | user_creatable_add_opts_foreach, |
Kevin Wolf | c6e5cdf | 2019-10-11 21:49:17 +0200 | [diff] [blame] | 4892 | qemu_img_object_print_help, &error_fatal)) { |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 4893 | goto out; |
| 4894 | } |
| 4895 | |
| 4896 | if (argc - optind > 1) { |
| 4897 | error_report("At most one filename argument is allowed."); |
| 4898 | goto out; |
| 4899 | } else if (argc - optind == 1) { |
| 4900 | filename = argv[optind]; |
| 4901 | } |
| 4902 | |
| 4903 | if (!filename && |
| 4904 | (object_opts || image_opts || fmt || snapshot_name || sn_opts)) { |
| 4905 | error_report("--object, --image-opts, -f, and -l " |
| 4906 | "require a filename argument."); |
| 4907 | goto out; |
| 4908 | } |
| 4909 | if (filename && img_size != UINT64_MAX) { |
| 4910 | error_report("--size N cannot be used together with a filename."); |
| 4911 | goto out; |
| 4912 | } |
| 4913 | if (!filename && img_size == UINT64_MAX) { |
| 4914 | error_report("Either --size N or one filename must be specified."); |
| 4915 | goto out; |
| 4916 | } |
| 4917 | |
| 4918 | if (filename) { |
| 4919 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 4920 | false, false, force_share); |
| 4921 | if (!in_blk) { |
| 4922 | goto out; |
| 4923 | } |
| 4924 | |
| 4925 | if (sn_opts) { |
| 4926 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 4927 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 4928 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 4929 | &local_err); |
| 4930 | } else if (snapshot_name != NULL) { |
| 4931 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 4932 | snapshot_name, &local_err); |
| 4933 | } |
| 4934 | if (local_err) { |
| 4935 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 4936 | goto out; |
| 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | drv = bdrv_find_format(out_fmt); |
| 4941 | if (!drv) { |
| 4942 | error_report("Unknown file format '%s'", out_fmt); |
| 4943 | goto out; |
| 4944 | } |
| 4945 | if (!drv->create_opts) { |
| 4946 | error_report("Format driver '%s' does not support image creation", |
| 4947 | drv->format_name); |
| 4948 | goto out; |
| 4949 | } |
| 4950 | |
| 4951 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 4952 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 4953 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 4954 | if (options) { |
| 4955 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 4956 | if (local_err) { |
| 4957 | error_report_err(local_err); |
| 4958 | error_report("Invalid options for file format '%s'", out_fmt); |
| 4959 | goto out; |
| 4960 | } |
| 4961 | } |
| 4962 | if (img_size != UINT64_MAX) { |
| 4963 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 4964 | } |
| 4965 | |
| 4966 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 4967 | if (local_err) { |
| 4968 | error_report_err(local_err); |
| 4969 | goto out; |
| 4970 | } |
| 4971 | |
| 4972 | if (output_format == OFORMAT_HUMAN) { |
| 4973 | printf("required size: %" PRIu64 "\n", info->required); |
| 4974 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
| 4975 | } else { |
| 4976 | dump_json_block_measure_info(info); |
| 4977 | } |
| 4978 | |
| 4979 | ret = 0; |
| 4980 | |
| 4981 | out: |
| 4982 | qapi_free_BlockMeasureInfo(info); |
| 4983 | qemu_opts_del(object_opts); |
| 4984 | qemu_opts_del(opts); |
| 4985 | qemu_opts_del(sn_opts); |
| 4986 | qemu_opts_free(create_opts); |
| 4987 | g_free(options); |
| 4988 | blk_unref(in_blk); |
| 4989 | return ret; |
| 4990 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4991 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4992 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4993 | #define DEF(option, callback, arg_string) \ |
| 4994 | { option, callback }, |
| 4995 | #include "qemu-img-cmds.h" |
| 4996 | #undef DEF |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4997 | { NULL, NULL, }, |
| 4998 | }; |
| 4999 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5000 | int main(int argc, char **argv) |
| 5001 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 5002 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5003 | const char *cmdname; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5004 | Error *local_error = NULL; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5005 | char *trace_file = NULL; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5006 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5007 | static const struct option long_options[] = { |
| 5008 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5009 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5010 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5011 | {0, 0, 0, 0} |
| 5012 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5013 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 5014 | #ifdef CONFIG_POSIX |
| 5015 | signal(SIGPIPE, SIG_IGN); |
| 5016 | #endif |
| 5017 | |
Christophe Fergeau | f5852ef | 2019-01-31 17:46:14 +0100 | [diff] [blame] | 5018 | error_init(argv[0]); |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 5019 | module_call_init(MODULE_INIT_TRACE); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 5020 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 5021 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5022 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 5023 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 5024 | exit(EXIT_FAILURE); |
| 5025 | } |
| 5026 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 5027 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 5028 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 5029 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5030 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5031 | if (argc < 2) { |
| 5032 | error_exit("Not enough arguments"); |
| 5033 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5034 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5035 | qemu_add_opts(&qemu_object_opts); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 5036 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5037 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 5038 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5039 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5040 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5041 | case ':': |
| 5042 | missing_argument(argv[optind - 1]); |
| 5043 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 5044 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 5045 | unrecognized_option(argv[optind - 1]); |
| 5046 | return 0; |
| 5047 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5048 | help(); |
| 5049 | return 0; |
| 5050 | case 'V': |
| 5051 | printf(QEMU_IMG_VERSION); |
| 5052 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5053 | case 'T': |
| 5054 | g_free(trace_file); |
| 5055 | trace_file = trace_opt_parse(optarg); |
| 5056 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5057 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5058 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5059 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5060 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5061 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5062 | /* reset getopt_long scanning */ |
| 5063 | argc -= optind; |
| 5064 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 5065 | return 0; |
| 5066 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5067 | argv += optind; |
Richard W.M. Jones | d339d76 | 2019-01-18 10:11:14 +0000 | [diff] [blame] | 5068 | qemu_reset_optind(); |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5069 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 5070 | if (!trace_init_backends()) { |
| 5071 | exit(1); |
| 5072 | } |
| 5073 | trace_init_file(trace_file); |
| 5074 | qemu_set_log(LOG_TRACE); |
| 5075 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 5076 | /* find the command */ |
| 5077 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 5078 | if (!strcmp(cmdname, cmd->name)) { |
| 5079 | return cmd->handler(argc, argv); |
| 5080 | } |
| 5081 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 5082 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 5083 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 5084 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 5085 | } |