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