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