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 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1068 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 1069 | * a non-NUL byte. |
| 1070 | * |
| 1071 | * 'pnum' is set to the number of sectors (including and immediately following |
| 1072 | * the first one) that are known to be in the same allocated/unallocated state. |
| 1073 | */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1074 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
| 1075 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1076 | bool is_zero; |
| 1077 | int i; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1078 | |
| 1079 | if (n <= 0) { |
| 1080 | *pnum = 0; |
| 1081 | return 0; |
| 1082 | } |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1083 | is_zero = buffer_is_zero(buf, 512); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1084 | for(i = 1; i < n; i++) { |
| 1085 | buf += 512; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1086 | if (is_zero != buffer_is_zero(buf, 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1087 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1088 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1089 | } |
| 1090 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1091 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1094 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1095 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 1096 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 1097 | * breaking up write requests for only small sparse areas. |
| 1098 | */ |
| 1099 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, |
| 1100 | int min) |
| 1101 | { |
| 1102 | int ret; |
| 1103 | int num_checked, num_used; |
| 1104 | |
| 1105 | if (n < min) { |
| 1106 | min = n; |
| 1107 | } |
| 1108 | |
| 1109 | ret = is_allocated_sectors(buf, n, pnum); |
| 1110 | if (!ret) { |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
| 1114 | num_used = *pnum; |
| 1115 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1116 | n -= *pnum; |
| 1117 | num_checked = num_used; |
| 1118 | |
| 1119 | while (n > 0) { |
| 1120 | ret = is_allocated_sectors(buf, n, pnum); |
| 1121 | |
| 1122 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1123 | n -= *pnum; |
| 1124 | num_checked += *pnum; |
| 1125 | if (ret) { |
| 1126 | num_used = num_checked; |
| 1127 | } else if (*pnum >= min) { |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | *pnum = num_used; |
| 1133 | return 1; |
| 1134 | } |
| 1135 | |
| 1136 | /* |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1137 | * Compares two buffers sector by sector. Returns 0 if the first sector of both |
| 1138 | * buffers matches, non-zero otherwise. |
| 1139 | * |
| 1140 | * pnum is set to the number of sectors (including and immediately following |
| 1141 | * the first one) that are known to have the same comparison result |
| 1142 | */ |
| 1143 | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, |
| 1144 | int *pnum) |
| 1145 | { |
Radim KrÄmĆ”Å | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1146 | bool res; |
| 1147 | int i; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1148 | |
| 1149 | if (n <= 0) { |
| 1150 | *pnum = 0; |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | res = !!memcmp(buf1, buf2, 512); |
| 1155 | for(i = 1; i < n; i++) { |
| 1156 | buf1 += 512; |
| 1157 | buf2 += 512; |
| 1158 | |
| 1159 | if (!!memcmp(buf1, buf2, 512) != res) { |
| 1160 | break; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | *pnum = i; |
| 1165 | return res; |
| 1166 | } |
| 1167 | |
Kevin Wolf | 80ee15a | 2009-09-15 12:30:43 +0200 | [diff] [blame] | 1168 | #define IO_BUF_SIZE (2 * 1024 * 1024) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1169 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1170 | static int64_t sectors_to_bytes(int64_t sectors) |
| 1171 | { |
| 1172 | return sectors << BDRV_SECTOR_BITS; |
| 1173 | } |
| 1174 | |
| 1175 | static int64_t sectors_to_process(int64_t total, int64_t from) |
| 1176 | { |
| 1177 | return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS); |
| 1178 | } |
| 1179 | |
| 1180 | /* |
| 1181 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1182 | * |
| 1183 | * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero |
| 1184 | * data and negative value on error. |
| 1185 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1186 | * @param blk: BlockBackend for the image |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1187 | * @param sect_num: Number of first sector to check |
| 1188 | * @param sect_count: Number of sectors to check |
| 1189 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1190 | * @param buffer: Allocated buffer for storing read data |
| 1191 | * @param quiet: Flag for quiet mode |
| 1192 | */ |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1193 | static int check_empty_sectors(BlockBackend *blk, int64_t sect_num, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1194 | int sect_count, const char *filename, |
| 1195 | uint8_t *buffer, bool quiet) |
| 1196 | { |
| 1197 | int pnum, ret = 0; |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 1198 | ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer, |
| 1199 | sect_count << BDRV_SECTOR_BITS); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1200 | if (ret < 0) { |
| 1201 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
| 1202 | sectors_to_bytes(sect_num), filename, strerror(-ret)); |
| 1203 | return ret; |
| 1204 | } |
| 1205 | ret = is_allocated_sectors(buffer, sect_count, &pnum); |
| 1206 | if (ret || pnum != sect_count) { |
| 1207 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
| 1208 | sectors_to_bytes(ret ? sect_num : sect_num + pnum)); |
| 1209 | return 1; |
| 1210 | } |
| 1211 | |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * Compares two images. Exit codes: |
| 1217 | * |
| 1218 | * 0 - Images are identical |
| 1219 | * 1 - Images differ |
| 1220 | * >1 - Error occurred |
| 1221 | */ |
| 1222 | static int img_compare(int argc, char **argv) |
| 1223 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1224 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1225 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1226 | BlockDriverState *bs1, *bs2; |
| 1227 | int64_t total_sectors1, total_sectors2; |
| 1228 | uint8_t *buf1 = NULL, *buf2 = NULL; |
| 1229 | int pnum1, pnum2; |
| 1230 | int allocated1, allocated2; |
| 1231 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1232 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1233 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1234 | bool writethrough; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1235 | int64_t total_sectors; |
| 1236 | int64_t sector_num = 0; |
| 1237 | int64_t nb_sectors; |
| 1238 | int c, pnum; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1239 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1240 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1241 | bool force_share = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1242 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1243 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1244 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1245 | static const struct option long_options[] = { |
| 1246 | {"help", no_argument, 0, 'h'}, |
| 1247 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1248 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1249 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1250 | {0, 0, 0, 0} |
| 1251 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1252 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1253 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1254 | if (c == -1) { |
| 1255 | break; |
| 1256 | } |
| 1257 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1258 | case ':': |
| 1259 | missing_argument(argv[optind - 1]); |
| 1260 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1261 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1262 | unrecognized_option(argv[optind - 1]); |
| 1263 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1264 | case 'h': |
| 1265 | help(); |
| 1266 | break; |
| 1267 | case 'f': |
| 1268 | fmt1 = optarg; |
| 1269 | break; |
| 1270 | case 'F': |
| 1271 | fmt2 = optarg; |
| 1272 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1273 | case 'T': |
| 1274 | cache = optarg; |
| 1275 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1276 | case 'p': |
| 1277 | progress = true; |
| 1278 | break; |
| 1279 | case 'q': |
| 1280 | quiet = true; |
| 1281 | break; |
| 1282 | case 's': |
| 1283 | strict = true; |
| 1284 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1285 | case 'U': |
| 1286 | force_share = true; |
| 1287 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1288 | case OPTION_OBJECT: { |
| 1289 | QemuOpts *opts; |
| 1290 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1291 | optarg, true); |
| 1292 | if (!opts) { |
| 1293 | ret = 2; |
| 1294 | goto out4; |
| 1295 | } |
| 1296 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1297 | case OPTION_IMAGE_OPTS: |
| 1298 | image_opts = true; |
| 1299 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | /* Progress is not shown in Quiet mode */ |
| 1304 | if (quiet) { |
| 1305 | progress = false; |
| 1306 | } |
| 1307 | |
| 1308 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1309 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1310 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1311 | } |
| 1312 | filename1 = argv[optind++]; |
| 1313 | filename2 = argv[optind++]; |
| 1314 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1315 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1316 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 1317 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1318 | ret = 2; |
| 1319 | goto out4; |
| 1320 | } |
| 1321 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1322 | /* Initialize before goto out */ |
| 1323 | qemu_progress_init(progress, 2.0); |
| 1324 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1325 | flags = 0; |
| 1326 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1327 | if (ret < 0) { |
| 1328 | error_report("Invalid source cache option: %s", cache); |
| 1329 | ret = 2; |
| 1330 | goto out3; |
| 1331 | } |
| 1332 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1333 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
| 1334 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1335 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1336 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1337 | goto out3; |
| 1338 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1339 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1340 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
| 1341 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1342 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1343 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1344 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1345 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1346 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1347 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1348 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1349 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1350 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
| 1351 | total_sectors1 = blk_nb_sectors(blk1); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1352 | if (total_sectors1 < 0) { |
| 1353 | error_report("Can't get size of %s: %s", |
| 1354 | filename1, strerror(-total_sectors1)); |
| 1355 | ret = 4; |
| 1356 | goto out; |
| 1357 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1358 | total_sectors2 = blk_nb_sectors(blk2); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1359 | if (total_sectors2 < 0) { |
| 1360 | error_report("Can't get size of %s: %s", |
| 1361 | filename2, strerror(-total_sectors2)); |
| 1362 | ret = 4; |
| 1363 | goto out; |
| 1364 | } |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1365 | total_sectors = MIN(total_sectors1, total_sectors2); |
| 1366 | progress_base = MAX(total_sectors1, total_sectors2); |
| 1367 | |
| 1368 | qemu_progress_print(0, 100); |
| 1369 | |
| 1370 | if (strict && total_sectors1 != total_sectors2) { |
| 1371 | ret = 1; |
| 1372 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1373 | goto out; |
| 1374 | } |
| 1375 | |
| 1376 | for (;;) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1377 | int64_t status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1378 | BlockDriverState *file; |
| 1379 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1380 | nb_sectors = sectors_to_process(total_sectors, sector_num); |
| 1381 | if (nb_sectors <= 0) { |
| 1382 | break; |
| 1383 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1384 | status1 = bdrv_get_block_status_above(bs1, NULL, sector_num, |
| 1385 | total_sectors1 - sector_num, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1386 | &pnum1, &file); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1387 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1388 | ret = 3; |
| 1389 | error_report("Sector allocation test failed for %s", filename1); |
| 1390 | goto out; |
| 1391 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1392 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1393 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1394 | status2 = bdrv_get_block_status_above(bs2, NULL, sector_num, |
| 1395 | total_sectors2 - sector_num, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1396 | &pnum2, &file); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1397 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1398 | ret = 3; |
| 1399 | error_report("Sector allocation test failed for %s", filename2); |
| 1400 | goto out; |
| 1401 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1402 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
| 1403 | if (pnum1) { |
| 1404 | nb_sectors = MIN(nb_sectors, pnum1); |
| 1405 | } |
| 1406 | if (pnum2) { |
| 1407 | nb_sectors = MIN(nb_sectors, pnum2); |
| 1408 | } |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1409 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1410 | if (strict) { |
| 1411 | if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) != |
| 1412 | (status2 & ~BDRV_BLOCK_OFFSET_MASK)) { |
| 1413 | ret = 1; |
| 1414 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
| 1415 | " block status mismatch!\n", |
| 1416 | sectors_to_bytes(sector_num)); |
| 1417 | goto out; |
| 1418 | } |
| 1419 | } |
| 1420 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
| 1421 | nb_sectors = MIN(pnum1, pnum2); |
| 1422 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1423 | if (allocated1) { |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 1424 | ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1, |
| 1425 | nb_sectors << BDRV_SECTOR_BITS); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1426 | if (ret < 0) { |
| 1427 | error_report("Error while reading offset %" PRId64 " of %s:" |
| 1428 | " %s", sectors_to_bytes(sector_num), filename1, |
| 1429 | strerror(-ret)); |
| 1430 | ret = 4; |
| 1431 | goto out; |
| 1432 | } |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 1433 | ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2, |
| 1434 | nb_sectors << BDRV_SECTOR_BITS); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1435 | if (ret < 0) { |
| 1436 | error_report("Error while reading offset %" PRId64 |
| 1437 | " of %s: %s", sectors_to_bytes(sector_num), |
| 1438 | filename2, strerror(-ret)); |
| 1439 | ret = 4; |
| 1440 | goto out; |
| 1441 | } |
| 1442 | ret = compare_sectors(buf1, buf2, nb_sectors, &pnum); |
| 1443 | if (ret || pnum != nb_sectors) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1444 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
| 1445 | sectors_to_bytes( |
| 1446 | ret ? sector_num : sector_num + pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1447 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1448 | goto out; |
| 1449 | } |
| 1450 | } |
| 1451 | } else { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1452 | |
| 1453 | if (allocated1) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1454 | ret = check_empty_sectors(blk1, sector_num, nb_sectors, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1455 | filename1, buf1, quiet); |
| 1456 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1457 | ret = check_empty_sectors(blk2, sector_num, nb_sectors, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1458 | filename2, buf1, quiet); |
| 1459 | } |
| 1460 | if (ret) { |
| 1461 | if (ret < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1462 | error_report("Error while reading offset %" PRId64 ": %s", |
| 1463 | sectors_to_bytes(sector_num), strerror(-ret)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1464 | ret = 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1465 | } |
| 1466 | goto out; |
| 1467 | } |
| 1468 | } |
| 1469 | sector_num += nb_sectors; |
| 1470 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); |
| 1471 | } |
| 1472 | |
| 1473 | if (total_sectors1 != total_sectors2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1474 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1475 | int64_t total_sectors_over; |
| 1476 | const char *filename_over; |
| 1477 | |
| 1478 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
| 1479 | if (total_sectors1 > total_sectors2) { |
| 1480 | total_sectors_over = total_sectors1; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1481 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1482 | filename_over = filename1; |
| 1483 | } else { |
| 1484 | total_sectors_over = total_sectors2; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1485 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1486 | filename_over = filename2; |
| 1487 | } |
| 1488 | |
| 1489 | for (;;) { |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 1490 | int64_t count; |
| 1491 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1492 | nb_sectors = sectors_to_process(total_sectors_over, sector_num); |
| 1493 | if (nb_sectors <= 0) { |
| 1494 | break; |
| 1495 | } |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 1496 | ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, |
| 1497 | sector_num * BDRV_SECTOR_SIZE, |
| 1498 | nb_sectors * BDRV_SECTOR_SIZE, |
| 1499 | &count); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1500 | if (ret < 0) { |
| 1501 | ret = 3; |
| 1502 | error_report("Sector allocation test failed for %s", |
| 1503 | filename_over); |
| 1504 | goto out; |
| 1505 | |
| 1506 | } |
Eric Blake | 51b0a48 | 2017-07-07 07:44:59 -0500 | [diff] [blame] | 1507 | /* TODO relax this once bdrv_is_allocated_above does not enforce |
| 1508 | * sector alignment */ |
| 1509 | assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)); |
| 1510 | nb_sectors = count >> BDRV_SECTOR_BITS; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1511 | if (ret) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1512 | ret = check_empty_sectors(blk_over, sector_num, nb_sectors, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1513 | filename_over, buf1, quiet); |
| 1514 | if (ret) { |
| 1515 | if (ret < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1516 | error_report("Error while reading offset %" PRId64 |
| 1517 | " of %s: %s", sectors_to_bytes(sector_num), |
| 1518 | filename_over, strerror(-ret)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1519 | ret = 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1520 | } |
| 1521 | goto out; |
| 1522 | } |
| 1523 | } |
| 1524 | sector_num += nb_sectors; |
| 1525 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | qprintf(quiet, "Images are identical.\n"); |
| 1530 | ret = 0; |
| 1531 | |
| 1532 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1533 | qemu_vfree(buf1); |
| 1534 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1535 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1536 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1537 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1538 | out3: |
| 1539 | qemu_progress_end(); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1540 | out4: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1541 | return ret; |
| 1542 | } |
| 1543 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1544 | enum ImgConvertBlockStatus { |
| 1545 | BLK_DATA, |
| 1546 | BLK_ZERO, |
| 1547 | BLK_BACKING_FILE, |
| 1548 | }; |
| 1549 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1550 | #define MAX_COROUTINES 16 |
| 1551 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1552 | typedef struct ImgConvertState { |
| 1553 | BlockBackend **src; |
| 1554 | int64_t *src_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1555 | int src_num; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1556 | int64_t total_sectors; |
| 1557 | int64_t allocated_sectors; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1558 | int64_t allocated_done; |
| 1559 | int64_t sector_num; |
| 1560 | int64_t wr_offs; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1561 | enum ImgConvertBlockStatus status; |
| 1562 | int64_t sector_next_status; |
| 1563 | BlockBackend *target; |
| 1564 | bool has_zero_init; |
| 1565 | bool compressed; |
| 1566 | bool target_has_backing; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1567 | bool wr_in_order; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1568 | int min_sparse; |
| 1569 | size_t cluster_sectors; |
| 1570 | size_t buf_sectors; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1571 | long num_coroutines; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1572 | int running_coroutines; |
| 1573 | Coroutine *co[MAX_COROUTINES]; |
| 1574 | int64_t wait_sector_num[MAX_COROUTINES]; |
| 1575 | CoMutex lock; |
| 1576 | int ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1577 | } ImgConvertState; |
| 1578 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1579 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
| 1580 | int *src_cur, int64_t *src_cur_offset) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1581 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1582 | *src_cur = 0; |
| 1583 | *src_cur_offset = 0; |
| 1584 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { |
| 1585 | *src_cur_offset += s->src_sectors[*src_cur]; |
| 1586 | (*src_cur)++; |
| 1587 | assert(*src_cur < s->src_num); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1592 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1593 | int64_t ret, src_cur_offset; |
| 1594 | int n, src_cur; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1595 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1596 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1597 | |
| 1598 | assert(s->total_sectors > sector_num); |
| 1599 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1600 | |
| 1601 | if (s->sector_next_status <= sector_num) { |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1602 | BlockDriverState *file; |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1603 | if (s->target_has_backing) { |
| 1604 | ret = bdrv_get_block_status(blk_bs(s->src[src_cur]), |
| 1605 | sector_num - src_cur_offset, |
| 1606 | n, &n, &file); |
| 1607 | } else { |
| 1608 | ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL, |
| 1609 | sector_num - src_cur_offset, |
| 1610 | n, &n, &file); |
| 1611 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1612 | if (ret < 0) { |
| 1613 | return ret; |
| 1614 | } |
| 1615 | |
| 1616 | if (ret & BDRV_BLOCK_ZERO) { |
| 1617 | s->status = BLK_ZERO; |
| 1618 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1619 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1620 | } else { |
Vladimir Sementsov-Ogievskiy | 9f1b92a | 2017-04-07 14:34:04 +0300 | [diff] [blame] | 1621 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | s->sector_next_status = sector_num + n; |
| 1625 | } |
| 1626 | |
| 1627 | n = MIN(n, s->sector_next_status - sector_num); |
| 1628 | if (s->status == BLK_DATA) { |
| 1629 | n = MIN(n, s->buf_sectors); |
| 1630 | } |
| 1631 | |
| 1632 | /* We need to write complete clusters for compressed images, so if an |
| 1633 | * unallocated area is shorter than that, we must consider the whole |
| 1634 | * cluster allocated. */ |
| 1635 | if (s->compressed) { |
| 1636 | if (n < s->cluster_sectors) { |
| 1637 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1638 | s->status = BLK_DATA; |
| 1639 | } else { |
| 1640 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | return n; |
| 1645 | } |
| 1646 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1647 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
| 1648 | int nb_sectors, uint8_t *buf) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1649 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1650 | int n, ret; |
| 1651 | QEMUIOVector qiov; |
| 1652 | struct iovec iov; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1653 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1654 | assert(nb_sectors <= s->buf_sectors); |
| 1655 | while (nb_sectors > 0) { |
| 1656 | BlockBackend *blk; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1657 | int src_cur; |
| 1658 | int64_t bs_sectors, src_cur_offset; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1659 | |
| 1660 | /* In the case of compression with multiple source files, we can get a |
| 1661 | * nb_sectors that spreads into the next part. So we must be able to |
| 1662 | * read across multiple BDSes for one convert_read() call. */ |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1663 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
| 1664 | blk = s->src[src_cur]; |
| 1665 | bs_sectors = s->src_sectors[src_cur]; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1666 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1667 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); |
| 1668 | iov.iov_base = buf; |
| 1669 | iov.iov_len = n << BDRV_SECTOR_BITS; |
| 1670 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1671 | |
| 1672 | ret = blk_co_preadv( |
| 1673 | blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS, |
| 1674 | n << BDRV_SECTOR_BITS, &qiov, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1675 | if (ret < 0) { |
| 1676 | return ret; |
| 1677 | } |
| 1678 | |
| 1679 | sector_num += n; |
| 1680 | nb_sectors -= n; |
| 1681 | buf += n * BDRV_SECTOR_SIZE; |
| 1682 | } |
| 1683 | |
| 1684 | return 0; |
| 1685 | } |
| 1686 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1687 | |
| 1688 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, |
| 1689 | int nb_sectors, uint8_t *buf, |
| 1690 | enum ImgConvertBlockStatus status) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1691 | { |
| 1692 | int ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1693 | QEMUIOVector qiov; |
| 1694 | struct iovec iov; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1695 | |
| 1696 | while (nb_sectors > 0) { |
| 1697 | int n = nb_sectors; |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1698 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
| 1699 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1700 | switch (status) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1701 | case BLK_BACKING_FILE: |
| 1702 | /* If we have a backing file, leave clusters unallocated that are |
| 1703 | * unallocated in the source image, so that the backing file is |
| 1704 | * visible at the respective offset. */ |
| 1705 | assert(s->target_has_backing); |
| 1706 | break; |
| 1707 | |
| 1708 | case BLK_DATA: |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1709 | /* If we're told to keep the target fully allocated (-S 0) or there |
| 1710 | * is real non-zero data, we must write it. Otherwise we can treat |
| 1711 | * it as zero sectors. |
| 1712 | * Compressed clusters need to be written as a whole, so in that |
| 1713 | * case we can only save the write if the buffer is completely |
| 1714 | * zeroed. */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1715 | if (!s->min_sparse || |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1716 | (!s->compressed && |
| 1717 | is_allocated_sectors_min(buf, n, &n, s->min_sparse)) || |
| 1718 | (s->compressed && |
| 1719 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1720 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1721 | iov.iov_base = buf; |
| 1722 | iov.iov_len = n << BDRV_SECTOR_BITS; |
| 1723 | qemu_iovec_init_external(&qiov, &iov, 1); |
| 1724 | |
| 1725 | ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS, |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1726 | n << BDRV_SECTOR_BITS, &qiov, flags); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1727 | if (ret < 0) { |
| 1728 | return ret; |
| 1729 | } |
| 1730 | break; |
| 1731 | } |
| 1732 | /* fall-through */ |
| 1733 | |
| 1734 | case BLK_ZERO: |
| 1735 | if (s->has_zero_init) { |
Lidong Chen | db933fb | 2017-04-27 10:58:27 +0800 | [diff] [blame] | 1736 | assert(!s->target_has_backing); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1737 | break; |
| 1738 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1739 | ret = blk_co_pwrite_zeroes(s->target, |
| 1740 | sector_num << BDRV_SECTOR_BITS, |
| 1741 | n << BDRV_SECTOR_BITS, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1742 | if (ret < 0) { |
| 1743 | return ret; |
| 1744 | } |
| 1745 | break; |
| 1746 | } |
| 1747 | |
| 1748 | sector_num += n; |
| 1749 | nb_sectors -= n; |
| 1750 | buf += n * BDRV_SECTOR_SIZE; |
| 1751 | } |
| 1752 | |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1756 | static void coroutine_fn convert_co_do_copy(void *opaque) |
| 1757 | { |
| 1758 | ImgConvertState *s = opaque; |
| 1759 | uint8_t *buf = NULL; |
| 1760 | int ret, i; |
| 1761 | int index = -1; |
| 1762 | |
| 1763 | for (i = 0; i < s->num_coroutines; i++) { |
| 1764 | if (s->co[i] == qemu_coroutine_self()) { |
| 1765 | index = i; |
| 1766 | break; |
| 1767 | } |
| 1768 | } |
| 1769 | assert(index >= 0); |
| 1770 | |
| 1771 | s->running_coroutines++; |
| 1772 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1773 | |
| 1774 | while (1) { |
| 1775 | int n; |
| 1776 | int64_t sector_num; |
| 1777 | enum ImgConvertBlockStatus status; |
| 1778 | |
| 1779 | qemu_co_mutex_lock(&s->lock); |
| 1780 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { |
| 1781 | qemu_co_mutex_unlock(&s->lock); |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1782 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1783 | } |
| 1784 | n = convert_iteration_sectors(s, s->sector_num); |
| 1785 | if (n < 0) { |
| 1786 | qemu_co_mutex_unlock(&s->lock); |
| 1787 | s->ret = n; |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1788 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1789 | } |
| 1790 | /* save current sector and allocation status to local variables */ |
| 1791 | sector_num = s->sector_num; |
| 1792 | status = s->status; |
| 1793 | if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1794 | n = MIN(n, s->buf_sectors); |
| 1795 | } |
| 1796 | /* increment global sector counter so that other coroutines can |
| 1797 | * already continue reading beyond this request */ |
| 1798 | s->sector_num += n; |
| 1799 | qemu_co_mutex_unlock(&s->lock); |
| 1800 | |
| 1801 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { |
| 1802 | s->allocated_done += n; |
| 1803 | qemu_progress_print(100.0 * s->allocated_done / |
| 1804 | s->allocated_sectors, 0); |
| 1805 | } |
| 1806 | |
| 1807 | if (status == BLK_DATA) { |
| 1808 | ret = convert_co_read(s, sector_num, n, buf); |
| 1809 | if (ret < 0) { |
| 1810 | error_report("error while reading sector %" PRId64 |
| 1811 | ": %s", sector_num, strerror(-ret)); |
| 1812 | s->ret = ret; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1813 | } |
| 1814 | } else if (!s->min_sparse && status == BLK_ZERO) { |
| 1815 | status = BLK_DATA; |
| 1816 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); |
| 1817 | } |
| 1818 | |
| 1819 | if (s->wr_in_order) { |
| 1820 | /* keep writes in order */ |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1821 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1822 | s->wait_sector_num[index] = sector_num; |
| 1823 | qemu_coroutine_yield(); |
| 1824 | } |
| 1825 | s->wait_sector_num[index] = -1; |
| 1826 | } |
| 1827 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1828 | if (s->ret == -EINPROGRESS) { |
| 1829 | ret = convert_co_write(s, sector_num, n, buf, status); |
| 1830 | if (ret < 0) { |
| 1831 | error_report("error while writing sector %" PRId64 |
| 1832 | ": %s", sector_num, strerror(-ret)); |
| 1833 | s->ret = ret; |
| 1834 | } |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | if (s->wr_in_order) { |
| 1838 | /* reenter the coroutine that might have waited |
| 1839 | * for this write to complete */ |
| 1840 | s->wr_offs = sector_num + n; |
| 1841 | for (i = 0; i < s->num_coroutines; i++) { |
| 1842 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { |
| 1843 | /* |
| 1844 | * A -> B -> A cannot occur because A has |
| 1845 | * s->wait_sector_num[i] == -1 during A -> B. Therefore |
| 1846 | * B will never enter A during this time window. |
| 1847 | */ |
| 1848 | qemu_coroutine_enter(s->co[i]); |
| 1849 | break; |
| 1850 | } |
| 1851 | } |
| 1852 | } |
| 1853 | } |
| 1854 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1855 | qemu_vfree(buf); |
| 1856 | s->co[index] = NULL; |
| 1857 | s->running_coroutines--; |
| 1858 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { |
| 1859 | /* the convert job finished successfully */ |
| 1860 | s->ret = 0; |
| 1861 | } |
| 1862 | } |
| 1863 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1864 | static int convert_do_copy(ImgConvertState *s) |
| 1865 | { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1866 | int ret, i, n; |
| 1867 | int64_t sector_num = 0; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* Check whether we have zero initialisation or can get it efficiently */ |
| 1870 | s->has_zero_init = s->min_sparse && !s->target_has_backing |
| 1871 | ? bdrv_has_zero_init(blk_bs(s->target)) |
| 1872 | : false; |
| 1873 | |
| 1874 | if (!s->has_zero_init && !s->target_has_backing && |
| 1875 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) |
| 1876 | { |
Kevin Wolf | 720ff28 | 2016-06-16 15:13:15 +0200 | [diff] [blame] | 1877 | ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1878 | if (ret == 0) { |
| 1879 | s->has_zero_init = true; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 1884 | * can be copied at a time. */ |
| 1885 | if (s->compressed) { |
| 1886 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 1887 | error_report("invalid cluster size"); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1888 | return -EINVAL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1889 | } |
| 1890 | s->buf_sectors = s->cluster_sectors; |
| 1891 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1892 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1893 | while (sector_num < s->total_sectors) { |
| 1894 | n = convert_iteration_sectors(s, sector_num); |
| 1895 | if (n < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1896 | return n; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1897 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 1898 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 1899 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1900 | s->allocated_sectors += n; |
| 1901 | } |
| 1902 | sector_num += n; |
| 1903 | } |
| 1904 | |
| 1905 | /* Do the copy */ |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1906 | s->sector_next_status = 0; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1907 | s->ret = -EINPROGRESS; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1908 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1909 | qemu_co_mutex_init(&s->lock); |
| 1910 | for (i = 0; i < s->num_coroutines; i++) { |
| 1911 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); |
| 1912 | s->wait_sector_num[i] = -1; |
| 1913 | qemu_coroutine_enter(s->co[i]); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1914 | } |
| 1915 | |
Anton Nefedov | b91127e | 2017-04-26 11:33:15 +0300 | [diff] [blame] | 1916 | while (s->running_coroutines) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1917 | main_loop_wait(false); |
| 1918 | } |
| 1919 | |
| 1920 | if (s->compressed && !s->ret) { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1921 | /* signal EOF to align */ |
Pavel Butsykin | fe5c135 | 2016-07-22 11:17:40 +0300 | [diff] [blame] | 1922 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1923 | if (ret < 0) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1924 | return ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 1928 | return s->ret; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1929 | } |
| 1930 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1931 | static int img_convert(int argc, char **argv) |
| 1932 | { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1933 | int c, bs_i, flags, src_flags = 0; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 1934 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1935 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
| 1936 | *out_filename, *out_baseimg_param, *snapshot_name = NULL; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 1937 | BlockDriver *drv = NULL, *proto_drv = NULL; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1938 | BlockDriverInfo bdi; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1939 | BlockDriverState *out_bs; |
| 1940 | QemuOpts *opts = NULL, *sn_opts = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 1941 | QemuOptsList *create_opts = NULL; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1942 | char *options = NULL; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 1943 | Error *local_err = NULL; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1944 | bool writethrough, src_writethrough, quiet = false, image_opts = false, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 1945 | skip_create = false, progress = false, tgt_image_opts = false; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1946 | int64_t ret = -EINVAL; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1947 | bool force_share = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1948 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1949 | ImgConvertState s = (ImgConvertState) { |
| 1950 | /* Need at least 4k of zeros for sparse detection */ |
| 1951 | .min_sparse = 8, |
| 1952 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
| 1953 | .wr_in_order = true, |
| 1954 | .num_coroutines = 8, |
| 1955 | }; |
| 1956 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1957 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1958 | static const struct option long_options[] = { |
| 1959 | {"help", no_argument, 0, 'h'}, |
| 1960 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1961 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 1962 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 1963 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1964 | {0, 0, 0, 0} |
| 1965 | }; |
Daniel P. Berrange | 6b4df54 | 2017-07-04 13:30:09 +0100 | [diff] [blame] | 1966 | 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] | 1967 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1968 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1969 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1970 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1971 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1972 | case ':': |
| 1973 | missing_argument(argv[optind - 1]); |
| 1974 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1975 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 1976 | unrecognized_option(argv[optind - 1]); |
| 1977 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1978 | case 'h': |
| 1979 | help(); |
| 1980 | break; |
| 1981 | case 'f': |
| 1982 | fmt = optarg; |
| 1983 | break; |
| 1984 | case 'O': |
| 1985 | out_fmt = optarg; |
| 1986 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1987 | case 'B': |
| 1988 | out_baseimg = optarg; |
| 1989 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1990 | case 'c': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 1991 | s.compressed = true; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1992 | break; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1993 | case 'o': |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1994 | if (!is_valid_option_list(optarg)) { |
| 1995 | error_report("Invalid option list: %s", optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1996 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1997 | } |
| 1998 | if (!options) { |
| 1999 | options = g_strdup(optarg); |
| 2000 | } else { |
| 2001 | char *old_options = options; |
| 2002 | options = g_strdup_printf("%s,%s", options, optarg); |
| 2003 | g_free(old_options); |
| 2004 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2005 | break; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2006 | case 's': |
| 2007 | snapshot_name = optarg; |
| 2008 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2009 | case 'l': |
| 2010 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 2011 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 2012 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2013 | if (!sn_opts) { |
| 2014 | error_report("Failed in parsing snapshot param '%s'", |
| 2015 | optarg); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2016 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2017 | } |
| 2018 | } else { |
| 2019 | snapshot_name = optarg; |
| 2020 | } |
| 2021 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2022 | case 'S': |
| 2023 | { |
| 2024 | int64_t sval; |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 2025 | |
| 2026 | sval = cvtnum(optarg); |
| 2027 | if (sval < 0) { |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2028 | error_report("Invalid minimum zero buffer size for sparse output specified"); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2029 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2030 | } |
| 2031 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2032 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 2033 | break; |
| 2034 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2035 | case 'p': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2036 | progress = true; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2037 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2038 | case 't': |
| 2039 | cache = optarg; |
| 2040 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2041 | case 'T': |
| 2042 | src_cache = optarg; |
| 2043 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2044 | case 'q': |
| 2045 | quiet = true; |
| 2046 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2047 | case 'n': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2048 | skip_create = true; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2049 | break; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2050 | case 'm': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2051 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
| 2052 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2053 | error_report("Invalid number of coroutines. Allowed number of" |
| 2054 | " coroutines is between 1 and %d", MAX_COROUTINES); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2055 | goto fail_getopt; |
| 2056 | } |
| 2057 | break; |
| 2058 | case 'W': |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2059 | s.wr_in_order = false; |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2060 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2061 | case 'U': |
| 2062 | force_share = true; |
| 2063 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2064 | case OPTION_OBJECT: { |
| 2065 | QemuOpts *object_opts; |
| 2066 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2067 | optarg, true); |
| 2068 | if (!object_opts) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2069 | goto fail_getopt; |
| 2070 | } |
| 2071 | break; |
Max Reitz | 3258b91 | 2017-04-26 15:46:47 +0200 | [diff] [blame] | 2072 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2073 | case OPTION_IMAGE_OPTS: |
| 2074 | image_opts = true; |
| 2075 | break; |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2076 | case OPTION_TARGET_IMAGE_OPTS: |
| 2077 | tgt_image_opts = true; |
| 2078 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2079 | } |
| 2080 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2081 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2082 | if (!out_fmt && !tgt_image_opts) { |
| 2083 | out_fmt = "raw"; |
| 2084 | } |
| 2085 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2086 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2087 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 2088 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2089 | goto fail_getopt; |
| 2090 | } |
| 2091 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2092 | if (!s.wr_in_order && s.compressed) { |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2093 | error_report("Out of order write and compress are mutually exclusive"); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2094 | goto fail_getopt; |
| 2095 | } |
| 2096 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2097 | if (tgt_image_opts && !skip_create) { |
| 2098 | error_report("--target-image-opts requires use of -n flag"); |
| 2099 | goto fail_getopt; |
| 2100 | } |
| 2101 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2102 | s.src_num = argc - optind - 1; |
| 2103 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; |
| 2104 | |
| 2105 | if (options && has_help_option(options)) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2106 | if (out_fmt) { |
| 2107 | ret = print_block_option_help(out_filename, out_fmt); |
| 2108 | goto fail_getopt; |
| 2109 | } else { |
| 2110 | error_report("Option help requires a format be specified"); |
| 2111 | goto fail_getopt; |
| 2112 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | if (s.src_num < 1) { |
| 2116 | error_report("Must specify image file name"); |
| 2117 | goto fail_getopt; |
| 2118 | } |
| 2119 | |
| 2120 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2121 | /* ret is still -EINVAL until here */ |
| 2122 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
| 2123 | if (ret < 0) { |
| 2124 | error_report("Invalid source cache option: %s", src_cache); |
Peter Lieven | 2d9187b | 2017-02-28 13:40:07 +0100 | [diff] [blame] | 2125 | goto fail_getopt; |
| 2126 | } |
| 2127 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2128 | /* Initialize before goto out */ |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2129 | if (quiet) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2130 | progress = false; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2131 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2132 | qemu_progress_init(progress, 1.0); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2133 | qemu_progress_print(0, 100); |
| 2134 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2135 | s.src = g_new0(BlockBackend *, s.src_num); |
| 2136 | s.src_sectors = g_new(int64_t, s.src_num); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2137 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2138 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2139 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2140 | fmt, src_flags, src_writethrough, quiet, |
| 2141 | force_share); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2142 | if (!s.src[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2143 | ret = -1; |
| 2144 | goto out; |
| 2145 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2146 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
| 2147 | if (s.src_sectors[bs_i] < 0) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2148 | error_report("Could not get size of %s: %s", |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2149 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2150 | ret = -1; |
| 2151 | goto out; |
| 2152 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2153 | s.total_sectors += s.src_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 2154 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2155 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2156 | if (sn_opts) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2157 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
Peter Maydell | 10d6eda | 2017-02-10 16:28:24 +0000 | [diff] [blame] | 2158 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 2159 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 2160 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2161 | } else if (snapshot_name != NULL) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2162 | if (s.src_num > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 2163 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2164 | ret = -1; |
| 2165 | goto out; |
| 2166 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 2167 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2168 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
| 2169 | &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2170 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2171 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2172 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 2173 | ret = -1; |
| 2174 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2177 | if (!skip_create) { |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2178 | /* Find driver and parse its options */ |
| 2179 | drv = bdrv_find_format(out_fmt); |
| 2180 | if (!drv) { |
| 2181 | error_report("Unknown file format '%s'", out_fmt); |
| 2182 | ret = -1; |
| 2183 | goto out; |
| 2184 | } |
| 2185 | |
| 2186 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
| 2187 | if (!proto_drv) { |
| 2188 | error_report_err(local_err); |
| 2189 | ret = -1; |
| 2190 | goto out; |
| 2191 | } |
| 2192 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2193 | if (!drv->create_opts) { |
| 2194 | error_report("Format driver '%s' does not support image creation", |
| 2195 | drv->format_name); |
| 2196 | ret = -1; |
| 2197 | goto out; |
| 2198 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2199 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2200 | if (!proto_drv->create_opts) { |
| 2201 | error_report("Protocol driver '%s' does not support image creation", |
| 2202 | proto_drv->format_name); |
| 2203 | ret = -1; |
| 2204 | goto out; |
| 2205 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 2206 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2207 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 2208 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 2209 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2210 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2211 | if (options) { |
| 2212 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 2213 | if (local_err) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2214 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2215 | ret = -1; |
| 2216 | goto out; |
| 2217 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2218 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2219 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2220 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512, |
Markus Armbruster | 39101f2 | 2015-02-12 16:46:36 +0100 | [diff] [blame] | 2221 | &error_abort); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2222 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
| 2223 | if (ret < 0) { |
| 2224 | goto out; |
| 2225 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2226 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2227 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2228 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2229 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2230 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2231 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2232 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2233 | s.target_has_backing = (bool) out_baseimg; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2234 | |
Max Reitz | 48758a8 | 2017-04-26 15:46:48 +0200 | [diff] [blame] | 2235 | if (s.src_num > 1 && out_baseimg) { |
| 2236 | error_report("Having a backing file for the target makes no sense when " |
| 2237 | "concatenating multiple input images"); |
| 2238 | ret = -1; |
| 2239 | goto out; |
| 2240 | } |
| 2241 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2242 | /* Check if compression is supported */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2243 | if (s.compressed) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2244 | bool encryption = |
| 2245 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2246 | const char *encryptfmt = |
| 2247 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2248 | const char *preallocation = |
| 2249 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2250 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2251 | if (drv && !drv->bdrv_co_pwritev_compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2252 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2253 | ret = -1; |
| 2254 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
Daniel P. Berrange | 0cb8d47 | 2017-06-23 17:24:06 +0100 | [diff] [blame] | 2257 | if (encryption || encryptfmt) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2258 | error_report("Compression and encryption not supported at " |
| 2259 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2260 | ret = -1; |
| 2261 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2262 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2263 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2264 | if (preallocation |
| 2265 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2266 | { |
| 2267 | error_report("Compression and preallocation not supported at " |
| 2268 | "the same time"); |
| 2269 | ret = -1; |
| 2270 | goto out; |
| 2271 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2272 | } |
| 2273 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2274 | if (!skip_create) { |
| 2275 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2276 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2277 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2278 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2279 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2280 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2281 | } |
| 2282 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2283 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2284 | 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] | 2285 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2286 | if (ret < 0) { |
| 2287 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2288 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2289 | } |
| 2290 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2291 | if (skip_create) { |
| 2292 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, |
| 2293 | flags, writethrough, quiet, false); |
| 2294 | } else { |
| 2295 | /* TODO ultimately we should allow --target-image-opts |
| 2296 | * to be used even when -n is not given. |
| 2297 | * That has to wait for bdrv_create to be improved |
| 2298 | * to allow filenames in option syntax |
| 2299 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 2300 | s.target = img_open_new_file(out_filename, opts, out_fmt, |
| 2301 | flags, writethrough, quiet, false); |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2302 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2303 | if (!s.target) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2304 | ret = -1; |
| 2305 | goto out; |
| 2306 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2307 | out_bs = blk_bs(s.target); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2308 | |
Daniel P. Berrange | 305b4c6 | 2017-05-15 17:47:11 +0100 | [diff] [blame] | 2309 | if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) { |
| 2310 | error_report("Compression not supported for this file format"); |
| 2311 | ret = -1; |
| 2312 | goto out; |
| 2313 | } |
| 2314 | |
Eric Blake | 5def6b8 | 2016-06-23 16:37:19 -0600 | [diff] [blame] | 2315 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2316 | * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB) |
| 2317 | * as maximum. */ |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2318 | s.buf_sectors = MIN(32768, |
| 2319 | MAX(s.buf_sectors, |
| 2320 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, |
| 2321 | out_bs->bl.pdiscard_alignment >> |
| 2322 | BDRV_SECTOR_BITS))); |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2323 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2324 | if (skip_create) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2325 | int64_t output_sectors = blk_nb_sectors(s.target); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2326 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2327 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2328 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2329 | ret = -1; |
| 2330 | goto out; |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2331 | } else if (output_sectors < s.total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2332 | error_report("output file is smaller than input file"); |
| 2333 | ret = -1; |
| 2334 | goto out; |
| 2335 | } |
| 2336 | } |
| 2337 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2338 | ret = bdrv_get_info(out_bs, &bdi); |
| 2339 | if (ret < 0) { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2340 | if (s.compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2341 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2342 | goto out; |
| 2343 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2344 | } else { |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2345 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
| 2346 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2347 | } |
| 2348 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2349 | ret = convert_do_copy(&s); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2350 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2351 | if (!ret) { |
| 2352 | qemu_progress_print(100, 0); |
| 2353 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2354 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2355 | qemu_opts_del(opts); |
| 2356 | qemu_opts_free(create_opts); |
Markus Armbruster | fbf28a4 | 2014-09-29 16:07:55 +0200 | [diff] [blame] | 2357 | qemu_opts_del(sn_opts); |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2358 | blk_unref(s.target); |
| 2359 | if (s.src) { |
| 2360 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
| 2361 | blk_unref(s.src[bs_i]); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2362 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2363 | g_free(s.src); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2364 | } |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2365 | g_free(s.src_sectors); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2366 | fail_getopt: |
| 2367 | g_free(options); |
| 2368 | |
Peter Lieven | 9fd77f9 | 2017-04-21 11:11:55 +0200 | [diff] [blame] | 2369 | return !!ret; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2372 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2373 | static void dump_snapshots(BlockDriverState *bs) |
| 2374 | { |
| 2375 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2376 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2377 | |
| 2378 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2379 | if (nb_sns <= 0) |
| 2380 | return; |
| 2381 | printf("Snapshot list:\n"); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2382 | bdrv_snapshot_dump(fprintf, stdout, NULL); |
| 2383 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2384 | for(i = 0; i < nb_sns; i++) { |
| 2385 | sn = &sn_tab[i]; |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2386 | bdrv_snapshot_dump(fprintf, stdout, sn); |
| 2387 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2388 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2389 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2392 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2393 | { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2394 | QString *str; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2395 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2396 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2397 | |
| 2398 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); |
| 2399 | visit_complete(v, &obj); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2400 | str = qobject_to_json_pretty(obj); |
| 2401 | assert(str != NULL); |
| 2402 | printf("%s\n", qstring_get_str(str)); |
| 2403 | qobject_decref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2404 | visit_free(v); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2405 | QDECREF(str); |
| 2406 | } |
| 2407 | |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2408 | static void dump_json_image_info(ImageInfo *info) |
| 2409 | { |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2410 | QString *str; |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2411 | QObject *obj; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 2412 | Visitor *v = qobject_output_visitor_new(&obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2413 | |
| 2414 | visit_type_ImageInfo(v, NULL, &info, &error_abort); |
| 2415 | visit_complete(v, &obj); |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2416 | str = qobject_to_json_pretty(obj); |
| 2417 | assert(str != NULL); |
| 2418 | printf("%s\n", qstring_get_str(str)); |
| 2419 | qobject_decref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 2420 | visit_free(v); |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2421 | QDECREF(str); |
| 2422 | } |
| 2423 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2424 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2425 | { |
| 2426 | ImageInfoList *elem; |
| 2427 | bool delim = false; |
| 2428 | |
| 2429 | for (elem = list; elem; elem = elem->next) { |
| 2430 | if (delim) { |
| 2431 | printf("\n"); |
| 2432 | } |
| 2433 | delim = true; |
| 2434 | |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2435 | bdrv_image_info_dump(fprintf, stdout, elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2440 | { |
| 2441 | return strcmp(a, b) == 0; |
| 2442 | } |
| 2443 | |
| 2444 | /** |
| 2445 | * Open an image file chain and return an ImageInfoList |
| 2446 | * |
| 2447 | * @filename: topmost image filename |
| 2448 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2449 | * @chain: true - enumerate entire backing file chain |
| 2450 | * false - only topmost image file |
| 2451 | * |
| 2452 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2453 | * image file. If there was an error a message will have been printed to |
| 2454 | * stderr. |
| 2455 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2456 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2457 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2458 | const char *fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2459 | bool chain, bool force_share) |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2460 | { |
| 2461 | ImageInfoList *head = NULL; |
| 2462 | ImageInfoList **last = &head; |
| 2463 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2464 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2465 | |
| 2466 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2467 | |
| 2468 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2469 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2470 | BlockDriverState *bs; |
| 2471 | ImageInfo *info; |
| 2472 | ImageInfoList *elem; |
| 2473 | |
| 2474 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2475 | error_report("Backing file '%s' creates an infinite loop.", |
| 2476 | filename); |
| 2477 | goto err; |
| 2478 | } |
| 2479 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2480 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2481 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2482 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
| 2483 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2484 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2485 | goto err; |
| 2486 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2487 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2488 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2489 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2490 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2491 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2492 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2493 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2494 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2495 | |
| 2496 | elem = g_new0(ImageInfoList, 1); |
| 2497 | elem->value = info; |
| 2498 | *last = elem; |
| 2499 | last = &elem->next; |
| 2500 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2501 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2502 | |
| 2503 | filename = fmt = NULL; |
| 2504 | if (chain) { |
| 2505 | if (info->has_full_backing_filename) { |
| 2506 | filename = info->full_backing_filename; |
| 2507 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2508 | error_report("Could not determine absolute backing filename," |
| 2509 | " but backing filename '%s' present", |
| 2510 | info->backing_filename); |
| 2511 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2512 | } |
| 2513 | if (info->has_backing_filename_format) { |
| 2514 | fmt = info->backing_filename_format; |
| 2515 | } |
| 2516 | } |
| 2517 | } |
| 2518 | g_hash_table_destroy(filenames); |
| 2519 | return head; |
| 2520 | |
| 2521 | err: |
| 2522 | qapi_free_ImageInfoList(head); |
| 2523 | g_hash_table_destroy(filenames); |
| 2524 | return NULL; |
| 2525 | } |
| 2526 | |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2527 | static int img_info(int argc, char **argv) |
| 2528 | { |
| 2529 | int c; |
| 2530 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2531 | bool chain = false; |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2532 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2533 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2534 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2535 | bool force_share = false; |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2536 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2537 | fmt = NULL; |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2538 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2539 | for(;;) { |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2540 | int option_index = 0; |
| 2541 | static const struct option long_options[] = { |
| 2542 | {"help", no_argument, 0, 'h'}, |
| 2543 | {"format", required_argument, 0, 'f'}, |
| 2544 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2545 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2546 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2547 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2548 | {"force-share", no_argument, 0, 'U'}, |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2549 | {0, 0, 0, 0} |
| 2550 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2551 | c = getopt_long(argc, argv, ":f:hU", |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2552 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2553 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2554 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2555 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2556 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2557 | case ':': |
| 2558 | missing_argument(argv[optind - 1]); |
| 2559 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2560 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2561 | unrecognized_option(argv[optind - 1]); |
| 2562 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2563 | case 'h': |
| 2564 | help(); |
| 2565 | break; |
| 2566 | case 'f': |
| 2567 | fmt = optarg; |
| 2568 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2569 | case 'U': |
| 2570 | force_share = true; |
| 2571 | break; |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2572 | case OPTION_OUTPUT: |
| 2573 | output = optarg; |
| 2574 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2575 | case OPTION_BACKING_CHAIN: |
| 2576 | chain = true; |
| 2577 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2578 | case OPTION_OBJECT: { |
| 2579 | QemuOpts *opts; |
| 2580 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2581 | optarg, true); |
| 2582 | if (!opts) { |
| 2583 | return 1; |
| 2584 | } |
| 2585 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2586 | case OPTION_IMAGE_OPTS: |
| 2587 | image_opts = true; |
| 2588 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2589 | } |
| 2590 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2591 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2592 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2593 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2594 | filename = argv[optind++]; |
| 2595 | |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2596 | if (output && !strcmp(output, "json")) { |
| 2597 | output_format = OFORMAT_JSON; |
| 2598 | } else if (output && !strcmp(output, "human")) { |
| 2599 | output_format = OFORMAT_HUMAN; |
| 2600 | } else if (output) { |
| 2601 | error_report("--output must be used with human or json as argument."); |
| 2602 | return 1; |
| 2603 | } |
| 2604 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2605 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2606 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 2607 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2608 | return 1; |
| 2609 | } |
| 2610 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2611 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
| 2612 | force_share); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2613 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2614 | return 1; |
| 2615 | } |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2616 | |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2617 | switch (output_format) { |
| 2618 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2619 | dump_human_image_info_list(list); |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2620 | break; |
| 2621 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2622 | if (chain) { |
| 2623 | dump_json_image_info_list(list); |
| 2624 | } else { |
| 2625 | dump_json_image_info(list->value); |
| 2626 | } |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2627 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2628 | } |
BenoƮt Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2629 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2630 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2631 | return 0; |
| 2632 | } |
| 2633 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2634 | static void dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 2635 | MapEntry *next) |
| 2636 | { |
| 2637 | switch (output_format) { |
| 2638 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2639 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2640 | error_report("File contains external, encrypted or compressed clusters."); |
| 2641 | exit(1); |
| 2642 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2643 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2644 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2645 | e->start, e->length, |
| 2646 | e->has_offset ? e->offset : 0, |
| 2647 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2648 | } |
| 2649 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 2650 | * Modify the flags here to allow more coalescing. |
| 2651 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2652 | if (next && (!next->data || next->zero)) { |
| 2653 | next->data = false; |
| 2654 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2655 | } |
| 2656 | break; |
| 2657 | case OFORMAT_JSON: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2658 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," |
| 2659 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2660 | (e->start == 0 ? "[" : ",\n"), |
| 2661 | e->start, e->length, e->depth, |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2662 | e->zero ? "true" : "false", |
| 2663 | e->data ? "true" : "false"); |
| 2664 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 2665 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2666 | } |
| 2667 | putchar('}'); |
| 2668 | |
| 2669 | if (!next) { |
| 2670 | printf("]\n"); |
| 2671 | } |
| 2672 | break; |
| 2673 | } |
| 2674 | } |
| 2675 | |
| 2676 | static int get_block_status(BlockDriverState *bs, int64_t sector_num, |
| 2677 | int nb_sectors, MapEntry *e) |
| 2678 | { |
| 2679 | int64_t ret; |
| 2680 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 2681 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2682 | bool has_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2683 | |
| 2684 | /* As an optimization, we could cache the current range of unallocated |
| 2685 | * clusters in each file of the chain, and avoid querying the same |
| 2686 | * range repeatedly. |
| 2687 | */ |
| 2688 | |
| 2689 | depth = 0; |
| 2690 | for (;;) { |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 2691 | ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors, |
| 2692 | &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2693 | if (ret < 0) { |
| 2694 | return ret; |
| 2695 | } |
| 2696 | assert(nb_sectors); |
| 2697 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 2698 | break; |
| 2699 | } |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 2700 | bs = backing_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2701 | if (bs == NULL) { |
| 2702 | ret = 0; |
| 2703 | break; |
| 2704 | } |
| 2705 | |
| 2706 | depth++; |
| 2707 | } |
| 2708 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2709 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 2710 | |
| 2711 | *e = (MapEntry) { |
| 2712 | .start = sector_num * BDRV_SECTOR_SIZE, |
| 2713 | .length = nb_sectors * BDRV_SECTOR_SIZE, |
| 2714 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 2715 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
| 2716 | .offset = ret & BDRV_BLOCK_OFFSET_MASK, |
| 2717 | .has_offset = has_offset, |
| 2718 | .depth = depth, |
| 2719 | .has_filename = file && has_offset, |
| 2720 | .filename = file && has_offset ? file->filename : NULL, |
| 2721 | }; |
| 2722 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2723 | return 0; |
| 2724 | } |
| 2725 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2726 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 2727 | { |
| 2728 | if (curr->length == 0) { |
| 2729 | return false; |
| 2730 | } |
| 2731 | if (curr->zero != next->zero || |
| 2732 | curr->data != next->data || |
| 2733 | curr->depth != next->depth || |
| 2734 | curr->has_filename != next->has_filename || |
| 2735 | curr->has_offset != next->has_offset) { |
| 2736 | return false; |
| 2737 | } |
| 2738 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 2739 | return false; |
| 2740 | } |
| 2741 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 2742 | return false; |
| 2743 | } |
| 2744 | return true; |
| 2745 | } |
| 2746 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2747 | static int img_map(int argc, char **argv) |
| 2748 | { |
| 2749 | int c; |
| 2750 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2751 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2752 | BlockDriverState *bs; |
| 2753 | const char *filename, *fmt, *output; |
| 2754 | int64_t length; |
| 2755 | MapEntry curr = { .length = 0 }, next; |
| 2756 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2757 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2758 | bool force_share = false; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2759 | |
| 2760 | fmt = NULL; |
| 2761 | output = NULL; |
| 2762 | for (;;) { |
| 2763 | int option_index = 0; |
| 2764 | static const struct option long_options[] = { |
| 2765 | {"help", no_argument, 0, 'h'}, |
| 2766 | {"format", required_argument, 0, 'f'}, |
| 2767 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2768 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2769 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2770 | {"force-share", no_argument, 0, 'U'}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2771 | {0, 0, 0, 0} |
| 2772 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2773 | c = getopt_long(argc, argv, ":f:hU", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2774 | long_options, &option_index); |
| 2775 | if (c == -1) { |
| 2776 | break; |
| 2777 | } |
| 2778 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2779 | case ':': |
| 2780 | missing_argument(argv[optind - 1]); |
| 2781 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2782 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2783 | unrecognized_option(argv[optind - 1]); |
| 2784 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2785 | case 'h': |
| 2786 | help(); |
| 2787 | break; |
| 2788 | case 'f': |
| 2789 | fmt = optarg; |
| 2790 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2791 | case 'U': |
| 2792 | force_share = true; |
| 2793 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2794 | case OPTION_OUTPUT: |
| 2795 | output = optarg; |
| 2796 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2797 | case OPTION_OBJECT: { |
| 2798 | QemuOpts *opts; |
| 2799 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2800 | optarg, true); |
| 2801 | if (!opts) { |
| 2802 | return 1; |
| 2803 | } |
| 2804 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2805 | case OPTION_IMAGE_OPTS: |
| 2806 | image_opts = true; |
| 2807 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2808 | } |
| 2809 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2810 | if (optind != argc - 1) { |
| 2811 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2812 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2813 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2814 | |
| 2815 | if (output && !strcmp(output, "json")) { |
| 2816 | output_format = OFORMAT_JSON; |
| 2817 | } else if (output && !strcmp(output, "human")) { |
| 2818 | output_format = OFORMAT_HUMAN; |
| 2819 | } else if (output) { |
| 2820 | error_report("--output must be used with human or json as argument."); |
| 2821 | return 1; |
| 2822 | } |
| 2823 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2824 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2825 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 2826 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2827 | return 1; |
| 2828 | } |
| 2829 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2830 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2831 | if (!blk) { |
| 2832 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2833 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2834 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2835 | |
| 2836 | if (output_format == OFORMAT_HUMAN) { |
| 2837 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
| 2838 | } |
| 2839 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2840 | length = blk_getlength(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2841 | while (curr.start + curr.length < length) { |
| 2842 | int64_t nsectors_left; |
| 2843 | int64_t sector_num; |
| 2844 | int n; |
| 2845 | |
| 2846 | sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS; |
| 2847 | |
| 2848 | /* Probe up to 1 GiB at a time. */ |
| 2849 | nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num; |
| 2850 | n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left); |
| 2851 | ret = get_block_status(bs, sector_num, n, &next); |
| 2852 | |
| 2853 | if (ret < 0) { |
| 2854 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 2855 | goto out; |
| 2856 | } |
| 2857 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2858 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2859 | curr.length += next.length; |
| 2860 | continue; |
| 2861 | } |
| 2862 | |
| 2863 | if (curr.length > 0) { |
| 2864 | dump_map_entry(output_format, &curr, &next); |
| 2865 | } |
| 2866 | curr = next; |
| 2867 | } |
| 2868 | |
| 2869 | dump_map_entry(output_format, &curr, NULL); |
| 2870 | |
| 2871 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2872 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2873 | return ret < 0; |
| 2874 | } |
| 2875 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2876 | #define SNAPSHOT_LIST 1 |
| 2877 | #define SNAPSHOT_CREATE 2 |
| 2878 | #define SNAPSHOT_APPLY 3 |
| 2879 | #define SNAPSHOT_DELETE 4 |
| 2880 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2881 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2882 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2883 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2884 | BlockDriverState *bs; |
| 2885 | QEMUSnapshotInfo sn; |
| 2886 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2887 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2888 | int action = 0; |
| 2889 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2890 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 2891 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2892 | bool image_opts = false; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2893 | bool force_share = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2894 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2895 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2896 | /* Parse commandline parameters */ |
| 2897 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2898 | static const struct option long_options[] = { |
| 2899 | {"help", no_argument, 0, 'h'}, |
| 2900 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2901 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2902 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2903 | {0, 0, 0, 0} |
| 2904 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2905 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2906 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2907 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2908 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2909 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2910 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2911 | case ':': |
| 2912 | missing_argument(argv[optind - 1]); |
| 2913 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2914 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 2915 | unrecognized_option(argv[optind - 1]); |
| 2916 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2917 | case 'h': |
| 2918 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2919 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2920 | case 'l': |
| 2921 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2922 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2923 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2924 | } |
| 2925 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 2926 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2927 | break; |
| 2928 | case 'a': |
| 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_APPLY; |
| 2934 | snapshot_name = optarg; |
| 2935 | break; |
| 2936 | case 'c': |
| 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_CREATE; |
| 2942 | snapshot_name = optarg; |
| 2943 | break; |
| 2944 | case 'd': |
| 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_DELETE; |
| 2950 | snapshot_name = optarg; |
| 2951 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2952 | case 'q': |
| 2953 | quiet = true; |
| 2954 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2955 | case 'U': |
| 2956 | force_share = true; |
| 2957 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2958 | case OPTION_OBJECT: { |
| 2959 | QemuOpts *opts; |
| 2960 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2961 | optarg, true); |
| 2962 | if (!opts) { |
| 2963 | return 1; |
| 2964 | } |
| 2965 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2966 | case OPTION_IMAGE_OPTS: |
| 2967 | image_opts = true; |
| 2968 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2969 | } |
| 2970 | } |
| 2971 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2972 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2973 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2974 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2975 | filename = argv[optind++]; |
| 2976 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2977 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2978 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 2979 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2980 | return 1; |
| 2981 | } |
| 2982 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2983 | /* Open the image */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 2984 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
| 2985 | force_share); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2986 | if (!blk) { |
| 2987 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2988 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2989 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2990 | |
| 2991 | /* Perform the requested action */ |
| 2992 | switch(action) { |
| 2993 | case SNAPSHOT_LIST: |
| 2994 | dump_snapshots(bs); |
| 2995 | break; |
| 2996 | |
| 2997 | case SNAPSHOT_CREATE: |
| 2998 | memset(&sn, 0, sizeof(sn)); |
| 2999 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 3000 | |
| 3001 | qemu_gettimeofday(&tv); |
| 3002 | sn.date_sec = tv.tv_sec; |
| 3003 | sn.date_nsec = tv.tv_usec * 1000; |
| 3004 | |
| 3005 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3006 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3007 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3008 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3009 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3010 | break; |
| 3011 | |
| 3012 | case SNAPSHOT_APPLY: |
| 3013 | ret = bdrv_snapshot_goto(bs, snapshot_name); |
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 apply 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_DELETE: |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3021 | bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 3022 | if (err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 3023 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 3024 | snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 3025 | ret = 1; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3026 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3027 | break; |
| 3028 | } |
| 3029 | |
| 3030 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3031 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3032 | if (ret) { |
| 3033 | return 1; |
| 3034 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3035 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3038 | static int img_rebase(int argc, char **argv) |
| 3039 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3040 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3041 | uint8_t *buf_old = NULL; |
| 3042 | uint8_t *buf_new = NULL; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3043 | BlockDriverState *bs = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3044 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3045 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 3046 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3047 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3048 | int unsafe = 0; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3049 | bool force_share = false; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3050 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3051 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 3052 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3053 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3054 | |
| 3055 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3056 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3057 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3058 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3059 | out_baseimg = NULL; |
| 3060 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3061 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3062 | static const struct option long_options[] = { |
| 3063 | {"help", no_argument, 0, 'h'}, |
| 3064 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3065 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3066 | {"force-share", no_argument, 0, 'U'}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3067 | {0, 0, 0, 0} |
| 3068 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3069 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3070 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3071 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3072 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3073 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3074 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3075 | case ':': |
| 3076 | missing_argument(argv[optind - 1]); |
| 3077 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3078 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3079 | unrecognized_option(argv[optind - 1]); |
| 3080 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3081 | case 'h': |
| 3082 | help(); |
| 3083 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 3084 | case 'f': |
| 3085 | fmt = optarg; |
| 3086 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3087 | case 'F': |
| 3088 | out_basefmt = optarg; |
| 3089 | break; |
| 3090 | case 'b': |
| 3091 | out_baseimg = optarg; |
| 3092 | break; |
| 3093 | case 'u': |
| 3094 | unsafe = 1; |
| 3095 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3096 | case 'p': |
| 3097 | progress = 1; |
| 3098 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3099 | case 't': |
| 3100 | cache = optarg; |
| 3101 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3102 | case 'T': |
| 3103 | src_cache = optarg; |
| 3104 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3105 | case 'q': |
| 3106 | quiet = true; |
| 3107 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3108 | case OPTION_OBJECT: { |
| 3109 | QemuOpts *opts; |
| 3110 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3111 | optarg, true); |
| 3112 | if (!opts) { |
| 3113 | return 1; |
| 3114 | } |
| 3115 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3116 | case OPTION_IMAGE_OPTS: |
| 3117 | image_opts = true; |
| 3118 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3119 | case 'U': |
| 3120 | force_share = true; |
| 3121 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3122 | } |
| 3123 | } |
| 3124 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3125 | if (quiet) { |
| 3126 | progress = 0; |
| 3127 | } |
| 3128 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3129 | if (optind != argc - 1) { |
| 3130 | error_exit("Expecting one image file name"); |
| 3131 | } |
| 3132 | if (!unsafe && !out_baseimg) { |
| 3133 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 3134 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3135 | filename = argv[optind++]; |
| 3136 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3137 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3138 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 3139 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3140 | return 1; |
| 3141 | } |
| 3142 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3143 | qemu_progress_init(progress, 2.0); |
| 3144 | qemu_progress_print(0, 100); |
| 3145 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3146 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3147 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3148 | if (ret < 0) { |
| 3149 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3150 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 3151 | } |
| 3152 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3153 | src_flags = 0; |
| 3154 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3155 | if (ret < 0) { |
| 3156 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3157 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 3158 | } |
| 3159 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3160 | /* The source files are opened read-only, don't care about WCE */ |
| 3161 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 3162 | (void) src_writethrough; |
| 3163 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3164 | /* |
| 3165 | * Open the images. |
| 3166 | * |
| 3167 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 3168 | * the reference to a renamed or moved backing file. |
| 3169 | */ |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3170 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3171 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3172 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3173 | ret = -1; |
| 3174 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3175 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3176 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3177 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3178 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3179 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3180 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3181 | ret = -1; |
| 3182 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 3187 | if (!unsafe) { |
Jeff Cody | 9a29e18 | 2015-01-22 08:03:30 -0500 | [diff] [blame] | 3188 | char backing_name[PATH_MAX]; |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3189 | QDict *options = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3190 | |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3191 | if (bs->backing_format[0] != '\0') { |
| 3192 | options = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3193 | qdict_put_str(options, "driver", bs->backing_format); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3194 | } |
| 3195 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3196 | if (force_share) { |
| 3197 | if (!options) { |
| 3198 | options = qdict_new(); |
| 3199 | } |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 3200 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3201 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3202 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3203 | blk_old_backing = blk_new_open(backing_name, NULL, |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3204 | options, src_flags, &local_err); |
| 3205 | if (!blk_old_backing) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 3206 | error_reportf_err(local_err, |
| 3207 | "Could not open old backing file '%s': ", |
| 3208 | backing_name); |
Xu Tian | e84a0dd | 2016-10-09 17:17:27 +0800 | [diff] [blame] | 3209 | ret = -1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3210 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3211 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3212 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3213 | if (out_baseimg[0]) { |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3214 | options = qdict_new(); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3215 | if (out_basefmt) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 3216 | qdict_put_str(options, "driver", out_basefmt); |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3217 | } |
| 3218 | if (force_share) { |
| 3219 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3220 | } |
| 3221 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3222 | blk_new_backing = blk_new_open(out_baseimg, NULL, |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 3223 | options, src_flags, &local_err); |
| 3224 | if (!blk_new_backing) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 3225 | error_reportf_err(local_err, |
| 3226 | "Could not open new backing file '%s': ", |
| 3227 | out_baseimg); |
Xu Tian | e84a0dd | 2016-10-09 17:17:27 +0800 | [diff] [blame] | 3228 | ret = -1; |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3229 | goto out; |
| 3230 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | /* |
| 3235 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 3236 | * accesses go to the backing file. We must therefore compare this cluster |
| 3237 | * in the old and new backing file, and if they differ we need to copy it |
| 3238 | * from the old backing file into the COW file. |
| 3239 | * |
| 3240 | * If qemu-img crashes during this step, no harm is done. The content of |
| 3241 | * the image is the same as the original one at any time. |
| 3242 | */ |
| 3243 | if (!unsafe) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3244 | int64_t num_sectors; |
| 3245 | int64_t old_backing_num_sectors; |
| 3246 | int64_t new_backing_num_sectors = 0; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3247 | uint64_t sector; |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3248 | int n; |
Eric Blake | d6a644b | 2017-07-07 07:44:57 -0500 | [diff] [blame] | 3249 | int64_t count; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3250 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 3251 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3252 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 3253 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3254 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3255 | num_sectors = blk_nb_sectors(blk); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3256 | if (num_sectors < 0) { |
| 3257 | error_report("Could not get size of '%s': %s", |
| 3258 | filename, strerror(-num_sectors)); |
| 3259 | ret = -1; |
| 3260 | goto out; |
| 3261 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3262 | old_backing_num_sectors = blk_nb_sectors(blk_old_backing); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3263 | if (old_backing_num_sectors < 0) { |
Jeff Cody | 9a29e18 | 2015-01-22 08:03:30 -0500 | [diff] [blame] | 3264 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3265 | |
| 3266 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
| 3267 | error_report("Could not get size of '%s': %s", |
| 3268 | backing_name, strerror(-old_backing_num_sectors)); |
| 3269 | ret = -1; |
| 3270 | goto out; |
| 3271 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3272 | if (blk_new_backing) { |
| 3273 | new_backing_num_sectors = blk_nb_sectors(blk_new_backing); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3274 | if (new_backing_num_sectors < 0) { |
| 3275 | error_report("Could not get size of '%s': %s", |
| 3276 | out_baseimg, strerror(-new_backing_num_sectors)); |
| 3277 | ret = -1; |
| 3278 | goto out; |
| 3279 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3280 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3281 | |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3282 | if (num_sectors != 0) { |
| 3283 | local_progress = (float)100 / |
| 3284 | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); |
| 3285 | } |
| 3286 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3287 | for (sector = 0; sector < num_sectors; sector += n) { |
| 3288 | |
| 3289 | /* How many sectors can we handle with the next read? */ |
| 3290 | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { |
| 3291 | n = (IO_BUF_SIZE / 512); |
| 3292 | } else { |
| 3293 | n = num_sectors - sector; |
| 3294 | } |
| 3295 | |
| 3296 | /* If the cluster is allocated, we don't need to take action */ |
Eric Blake | d6a644b | 2017-07-07 07:44:57 -0500 | [diff] [blame] | 3297 | ret = bdrv_is_allocated(bs, sector << BDRV_SECTOR_BITS, |
| 3298 | n << BDRV_SECTOR_BITS, &count); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3299 | if (ret < 0) { |
| 3300 | error_report("error while reading image metadata: %s", |
| 3301 | strerror(-ret)); |
| 3302 | goto out; |
| 3303 | } |
Eric Blake | d6a644b | 2017-07-07 07:44:57 -0500 | [diff] [blame] | 3304 | /* TODO relax this once bdrv_is_allocated does not enforce |
| 3305 | * sector alignment */ |
| 3306 | assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)); |
| 3307 | n = count >> BDRV_SECTOR_BITS; |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3308 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3309 | continue; |
| 3310 | } |
| 3311 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3312 | /* |
| 3313 | * Read old and new backing file and take into consideration that |
| 3314 | * backing files may be smaller than the COW image. |
| 3315 | */ |
| 3316 | if (sector >= old_backing_num_sectors) { |
| 3317 | memset(buf_old, 0, n * BDRV_SECTOR_SIZE); |
| 3318 | } else { |
| 3319 | if (sector + n > old_backing_num_sectors) { |
| 3320 | n = old_backing_num_sectors - sector; |
| 3321 | } |
| 3322 | |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 3323 | ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS, |
| 3324 | buf_old, n << BDRV_SECTOR_BITS); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3325 | if (ret < 0) { |
| 3326 | error_report("error while reading from old backing file"); |
| 3327 | goto out; |
| 3328 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3329 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3330 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3331 | if (sector >= new_backing_num_sectors || !blk_new_backing) { |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3332 | memset(buf_new, 0, n * BDRV_SECTOR_SIZE); |
| 3333 | } else { |
| 3334 | if (sector + n > new_backing_num_sectors) { |
| 3335 | n = new_backing_num_sectors - sector; |
| 3336 | } |
| 3337 | |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 3338 | ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS, |
| 3339 | buf_new, n << BDRV_SECTOR_BITS); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3340 | if (ret < 0) { |
| 3341 | error_report("error while reading from new backing file"); |
| 3342 | goto out; |
| 3343 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | /* If they differ, we need to write to the COW file */ |
| 3347 | uint64_t written = 0; |
| 3348 | |
| 3349 | while (written < n) { |
| 3350 | int pnum; |
| 3351 | |
| 3352 | if (compare_sectors(buf_old + written * 512, |
Kevin Wolf | 60b1bd4 | 2010-02-17 12:32:59 +0100 | [diff] [blame] | 3353 | buf_new + written * 512, n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3354 | { |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 3355 | ret = blk_pwrite(blk, |
| 3356 | (sector + written) << BDRV_SECTOR_BITS, |
| 3357 | buf_old + written * 512, |
| 3358 | pnum << BDRV_SECTOR_BITS, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3359 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3360 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3361 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3362 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | written += pnum; |
| 3367 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3368 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3369 | } |
| 3370 | } |
| 3371 | |
| 3372 | /* |
| 3373 | * Change the backing file. All clusters that are different from the old |
| 3374 | * backing file are overwritten in the COW file now, so the visible content |
| 3375 | * doesn't change when we switch the backing file. |
| 3376 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3377 | if (out_baseimg && *out_baseimg) { |
| 3378 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
| 3379 | } else { |
| 3380 | ret = bdrv_change_backing_file(bs, NULL, NULL); |
| 3381 | } |
| 3382 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3383 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3384 | error_report("Could not change the backing file to '%s': No " |
| 3385 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3386 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3387 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3388 | out_baseimg, strerror(-ret)); |
| 3389 | } |
| 3390 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3391 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3392 | /* |
| 3393 | * TODO At this point it is possible to check if any clusters that are |
| 3394 | * allocated in the COW file are the same in the backing file. If so, they |
| 3395 | * could be dropped from the COW file. Don't do this before switching the |
| 3396 | * backing file, in case of a crash this would lead to corruption. |
| 3397 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3398 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3399 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3400 | /* Cleanup */ |
| 3401 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3402 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3403 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3404 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3405 | qemu_vfree(buf_old); |
| 3406 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3407 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3408 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3409 | if (ret) { |
| 3410 | return 1; |
| 3411 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3412 | return 0; |
| 3413 | } |
| 3414 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3415 | static int img_resize(int argc, char **argv) |
| 3416 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3417 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3418 | int c, ret, relative; |
| 3419 | const char *filename, *fmt, *size; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3420 | int64_t n, total_size, current_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3421 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3422 | BlockBackend *blk = NULL; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3423 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3424 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3425 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3426 | static QemuOptsList resize_options = { |
| 3427 | .name = "resize_options", |
| 3428 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3429 | .desc = { |
| 3430 | { |
| 3431 | .name = BLOCK_OPT_SIZE, |
| 3432 | .type = QEMU_OPT_SIZE, |
| 3433 | .help = "Virtual disk size" |
| 3434 | }, { |
| 3435 | /* end of list */ |
| 3436 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3437 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3438 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3439 | bool image_opts = false; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3440 | bool shrink = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3441 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3442 | /* Remove size from argv manually so that negative numbers are not treated |
| 3443 | * as options by getopt. */ |
| 3444 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3445 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3446 | return 1; |
| 3447 | } |
| 3448 | |
| 3449 | size = argv[--argc]; |
| 3450 | |
| 3451 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3452 | fmt = NULL; |
| 3453 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3454 | static const struct option long_options[] = { |
| 3455 | {"help", no_argument, 0, 'h'}, |
| 3456 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3457 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3458 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3459 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3460 | {0, 0, 0, 0} |
| 3461 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3462 | c = getopt_long(argc, argv, ":f:hq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3463 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3464 | if (c == -1) { |
| 3465 | break; |
| 3466 | } |
| 3467 | switch(c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3468 | case ':': |
| 3469 | missing_argument(argv[optind - 1]); |
| 3470 | break; |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3471 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3472 | unrecognized_option(argv[optind - 1]); |
| 3473 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3474 | case 'h': |
| 3475 | help(); |
| 3476 | break; |
| 3477 | case 'f': |
| 3478 | fmt = optarg; |
| 3479 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3480 | case 'q': |
| 3481 | quiet = true; |
| 3482 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3483 | case OPTION_OBJECT: { |
| 3484 | QemuOpts *opts; |
| 3485 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3486 | optarg, true); |
| 3487 | if (!opts) { |
| 3488 | return 1; |
| 3489 | } |
| 3490 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3491 | case OPTION_IMAGE_OPTS: |
| 3492 | image_opts = true; |
| 3493 | break; |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3494 | case OPTION_PREALLOCATION: |
Marc-AndrƩ Lureau | f7abe0e | 2017-08-24 10:46:10 +0200 | [diff] [blame] | 3495 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
Markus Armbruster | 06c60b6 | 2017-08-24 10:45:57 +0200 | [diff] [blame] | 3496 | PREALLOC_MODE__MAX, NULL); |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3497 | if (prealloc == PREALLOC_MODE__MAX) { |
| 3498 | error_report("Invalid preallocation mode '%s'", optarg); |
| 3499 | return 1; |
| 3500 | } |
| 3501 | break; |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3502 | case OPTION_SHRINK: |
| 3503 | shrink = true; |
| 3504 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3505 | } |
| 3506 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3507 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3508 | error_exit("Expecting one image file name"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3509 | } |
| 3510 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3511 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3512 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3513 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 3514 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3515 | return 1; |
| 3516 | } |
| 3517 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3518 | /* Choose grow, shrink, or absolute resize mode */ |
| 3519 | switch (size[0]) { |
| 3520 | case '+': |
| 3521 | relative = 1; |
| 3522 | size++; |
| 3523 | break; |
| 3524 | case '-': |
| 3525 | relative = -1; |
| 3526 | size++; |
| 3527 | break; |
| 3528 | default: |
| 3529 | relative = 0; |
| 3530 | break; |
| 3531 | } |
| 3532 | |
| 3533 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3534 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3535 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3536 | if (err) { |
| 3537 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3538 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3539 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3540 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3541 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3542 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3543 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3544 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3545 | blk = img_open(image_opts, filename, fmt, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3546 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
| 3547 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3548 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3549 | ret = -1; |
| 3550 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3551 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3552 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3553 | current_size = blk_getlength(blk); |
| 3554 | if (current_size < 0) { |
| 3555 | error_report("Failed to inquire current image length: %s", |
| 3556 | strerror(-current_size)); |
| 3557 | ret = -1; |
| 3558 | goto out; |
| 3559 | } |
| 3560 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3561 | if (relative) { |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3562 | total_size = current_size + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3563 | } else { |
| 3564 | total_size = n; |
| 3565 | } |
| 3566 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3567 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3568 | ret = -1; |
| 3569 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3570 | } |
| 3571 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3572 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
| 3573 | error_report("Preallocation can only be used for growing images"); |
| 3574 | ret = -1; |
| 3575 | goto out; |
| 3576 | } |
| 3577 | |
Pavel Butsykin | 4ffca89 | 2017-09-18 15:42:27 +0300 | [diff] [blame] | 3578 | if (total_size < current_size && !shrink) { |
| 3579 | warn_report("Shrinking an image will delete all data beyond the " |
| 3580 | "shrunken image's end. Before performing such an " |
| 3581 | "operation, make sure there is no important data there."); |
| 3582 | |
| 3583 | if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) { |
| 3584 | error_report( |
| 3585 | "Use the --shrink option to perform a shrink operation."); |
| 3586 | ret = -1; |
| 3587 | goto out; |
| 3588 | } else { |
| 3589 | warn_report("Using the --shrink option will suppress this message. " |
| 3590 | "Note that future versions of qemu-img may refuse to " |
| 3591 | "shrink images without this option."); |
| 3592 | } |
| 3593 | } |
| 3594 | |
Max Reitz | dc5f690 | 2017-06-13 22:20:55 +0200 | [diff] [blame] | 3595 | ret = blk_truncate(blk, total_size, prealloc, &err); |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 3596 | if (!ret) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3597 | qprintf(quiet, "Image resized.\n"); |
Max Reitz | ed3d2ec | 2017-03-28 22:51:27 +0200 | [diff] [blame] | 3598 | } else { |
| 3599 | error_report_err(err); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3600 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3601 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3602 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3603 | if (ret) { |
| 3604 | return 1; |
| 3605 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3606 | return 0; |
| 3607 | } |
| 3608 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3609 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 3610 | int64_t offset, int64_t total_work_size, |
| 3611 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3612 | { |
| 3613 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 3614 | } |
| 3615 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3616 | static int img_amend(int argc, char **argv) |
| 3617 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3618 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3619 | int c, ret = 0; |
| 3620 | char *options = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3621 | QemuOptsList *create_opts = NULL; |
| 3622 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3623 | const char *fmt = NULL, *filename, *cache; |
| 3624 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3625 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3626 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3627 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3628 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3629 | bool image_opts = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3630 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3631 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3632 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3633 | static const struct option long_options[] = { |
| 3634 | {"help", no_argument, 0, 'h'}, |
| 3635 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3636 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3637 | {0, 0, 0, 0} |
| 3638 | }; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3639 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3640 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3641 | if (c == -1) { |
| 3642 | break; |
| 3643 | } |
| 3644 | |
| 3645 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3646 | case ':': |
| 3647 | missing_argument(argv[optind - 1]); |
| 3648 | break; |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 3649 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3650 | unrecognized_option(argv[optind - 1]); |
| 3651 | break; |
| 3652 | case 'h': |
Stefan Hajnoczi | f707762 | 2017-03-17 18:45:40 +0800 | [diff] [blame] | 3653 | help(); |
| 3654 | break; |
| 3655 | case 'o': |
| 3656 | if (!is_valid_option_list(optarg)) { |
| 3657 | error_report("Invalid option list: %s", optarg); |
| 3658 | ret = -1; |
| 3659 | goto out_no_progress; |
| 3660 | } |
| 3661 | if (!options) { |
| 3662 | options = g_strdup(optarg); |
| 3663 | } else { |
| 3664 | char *old_options = options; |
| 3665 | options = g_strdup_printf("%s,%s", options, optarg); |
| 3666 | g_free(old_options); |
| 3667 | } |
| 3668 | break; |
| 3669 | case 'f': |
| 3670 | fmt = optarg; |
| 3671 | break; |
| 3672 | case 't': |
| 3673 | cache = optarg; |
| 3674 | break; |
| 3675 | case 'p': |
| 3676 | progress = true; |
| 3677 | break; |
| 3678 | case 'q': |
| 3679 | quiet = true; |
| 3680 | break; |
| 3681 | case OPTION_OBJECT: |
| 3682 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3683 | optarg, true); |
| 3684 | if (!opts) { |
| 3685 | ret = -1; |
| 3686 | goto out_no_progress; |
| 3687 | } |
| 3688 | break; |
| 3689 | case OPTION_IMAGE_OPTS: |
| 3690 | image_opts = true; |
| 3691 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3692 | } |
| 3693 | } |
| 3694 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3695 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3696 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3697 | } |
| 3698 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3699 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3700 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 3701 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3702 | ret = -1; |
| 3703 | goto out_no_progress; |
| 3704 | } |
| 3705 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3706 | if (quiet) { |
| 3707 | progress = false; |
| 3708 | } |
| 3709 | qemu_progress_init(progress, 1.0); |
| 3710 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3711 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 3712 | if (fmt && has_help_option(options)) { |
| 3713 | /* If a format is explicitly specified (and possibly no filename is |
| 3714 | * given), print option help here */ |
| 3715 | ret = print_block_option_help(filename, fmt); |
| 3716 | goto out; |
| 3717 | } |
| 3718 | |
| 3719 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 3720 | error_report("Expecting one image file name"); |
| 3721 | ret = -1; |
| 3722 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3723 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3724 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3725 | flags = BDRV_O_RDWR; |
| 3726 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3727 | if (ret < 0) { |
| 3728 | error_report("Invalid cache option: %s", cache); |
| 3729 | goto out; |
| 3730 | } |
| 3731 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3732 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 3733 | false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3734 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3735 | ret = -1; |
| 3736 | goto out; |
| 3737 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3738 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3739 | |
| 3740 | fmt = bs->drv->format_name; |
| 3741 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3742 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3743 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3744 | ret = print_block_option_help(filename, fmt); |
| 3745 | goto out; |
| 3746 | } |
| 3747 | |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 3748 | if (!bs->drv->create_opts) { |
| 3749 | error_report("Format driver '%s' does not support any options to amend", |
| 3750 | fmt); |
| 3751 | ret = -1; |
| 3752 | goto out; |
| 3753 | } |
| 3754 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 3755 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3756 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Paolo Bonzini | ece9086 | 2017-01-04 15:56:24 +0100 | [diff] [blame] | 3757 | qemu_opts_do_parse(opts, options, NULL, &err); |
| 3758 | if (err) { |
| 3759 | error_report_err(err); |
| 3760 | ret = -1; |
| 3761 | goto out; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3762 | } |
| 3763 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3764 | /* In case the driver does not call amend_status_cb() */ |
| 3765 | qemu_progress_print(0.f, 0); |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 3766 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3767 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3768 | if (ret < 0) { |
| 3769 | error_report("Error while amending options: %s", strerror(-ret)); |
| 3770 | goto out; |
| 3771 | } |
| 3772 | |
| 3773 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3774 | qemu_progress_end(); |
| 3775 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 3776 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3777 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3778 | qemu_opts_del(opts); |
| 3779 | qemu_opts_free(create_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3780 | g_free(options); |
| 3781 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3782 | if (ret) { |
| 3783 | return 1; |
| 3784 | } |
| 3785 | return 0; |
| 3786 | } |
| 3787 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3788 | typedef struct BenchData { |
| 3789 | BlockBackend *blk; |
| 3790 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3791 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3792 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3793 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3794 | int nrreq; |
| 3795 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3796 | int flush_interval; |
| 3797 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3798 | uint8_t *buf; |
| 3799 | QEMUIOVector *qiov; |
| 3800 | |
| 3801 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3802 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3803 | uint64_t offset; |
| 3804 | } BenchData; |
| 3805 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3806 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 3807 | { |
| 3808 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 3809 | error_report("Failed flush request: %s", strerror(-ret)); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3810 | exit(EXIT_FAILURE); |
| 3811 | } |
| 3812 | } |
| 3813 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3814 | static void bench_cb(void *opaque, int ret) |
| 3815 | { |
| 3816 | BenchData *b = opaque; |
| 3817 | BlockAIOCB *acb; |
| 3818 | |
| 3819 | if (ret < 0) { |
Markus Armbruster | df3c286 | 2016-08-03 13:37:51 +0200 | [diff] [blame] | 3820 | error_report("Failed request: %s", strerror(-ret)); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3821 | exit(EXIT_FAILURE); |
| 3822 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3823 | |
| 3824 | if (b->in_flush) { |
| 3825 | /* Just finished a flush with drained queue: Start next requests */ |
| 3826 | assert(b->in_flight == 0); |
| 3827 | b->in_flush = false; |
| 3828 | } else if (b->in_flight > 0) { |
| 3829 | int remaining = b->n - b->in_flight; |
| 3830 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3831 | b->n--; |
| 3832 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3833 | |
| 3834 | /* Time for flush? Drain queue if requested, then flush */ |
| 3835 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 3836 | if (!b->in_flight || !b->drain_on_flush) { |
| 3837 | BlockCompletionFunc *cb; |
| 3838 | |
| 3839 | if (b->drain_on_flush) { |
| 3840 | b->in_flush = true; |
| 3841 | cb = bench_cb; |
| 3842 | } else { |
| 3843 | cb = bench_undrained_flush_cb; |
| 3844 | } |
| 3845 | |
| 3846 | acb = blk_aio_flush(b->blk, cb, b); |
| 3847 | if (!acb) { |
| 3848 | error_report("Failed to issue flush request"); |
| 3849 | exit(EXIT_FAILURE); |
| 3850 | } |
| 3851 | } |
| 3852 | if (b->drain_on_flush) { |
| 3853 | return; |
| 3854 | } |
| 3855 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 3859 | int64_t offset = b->offset; |
| 3860 | /* blk_aio_* might look for completed I/Os and kick bench_cb |
| 3861 | * again, so make sure this operation is counted by in_flight |
| 3862 | * and b->offset is ready for the next submission. |
| 3863 | */ |
| 3864 | b->in_flight++; |
| 3865 | b->offset += b->step; |
| 3866 | b->offset %= b->image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3867 | if (b->write) { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 3868 | 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] | 3869 | } else { |
Paolo Bonzini | 4baaa8c | 2016-12-07 16:08:27 +0100 | [diff] [blame] | 3870 | 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] | 3871 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3872 | if (!acb) { |
| 3873 | error_report("Failed to issue request"); |
| 3874 | exit(EXIT_FAILURE); |
| 3875 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3876 | } |
| 3877 | } |
| 3878 | |
| 3879 | static int img_bench(int argc, char **argv) |
| 3880 | { |
| 3881 | int c, ret = 0; |
| 3882 | const char *fmt = NULL, *filename; |
| 3883 | bool quiet = false; |
| 3884 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3885 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3886 | int count = 75000; |
| 3887 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3888 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3889 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3890 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3891 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3892 | int flush_interval = 0; |
| 3893 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3894 | int64_t image_size; |
| 3895 | BlockBackend *blk = NULL; |
| 3896 | BenchData data = {}; |
| 3897 | int flags = 0; |
Kevin Wolf | 604e861 | 2016-06-14 11:29:32 +0200 | [diff] [blame] | 3898 | bool writethrough = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3899 | struct timeval t1, t2; |
| 3900 | int i; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3901 | bool force_share = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3902 | |
| 3903 | for (;;) { |
| 3904 | static const struct option long_options[] = { |
| 3905 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3906 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3907 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3908 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3909 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3910 | {"force-share", no_argument, 0, 'U'}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3911 | {0, 0, 0, 0} |
| 3912 | }; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 3913 | 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] | 3914 | if (c == -1) { |
| 3915 | break; |
| 3916 | } |
| 3917 | |
| 3918 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3919 | case ':': |
| 3920 | missing_argument(argv[optind - 1]); |
| 3921 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3922 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 3923 | unrecognized_option(argv[optind - 1]); |
| 3924 | break; |
| 3925 | case 'h': |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3926 | help(); |
| 3927 | break; |
| 3928 | case 'c': |
| 3929 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 3930 | unsigned long res; |
| 3931 | |
| 3932 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3933 | error_report("Invalid request count specified"); |
| 3934 | return 1; |
| 3935 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 3936 | count = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3937 | break; |
| 3938 | } |
| 3939 | case 'd': |
| 3940 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 3941 | unsigned long res; |
| 3942 | |
| 3943 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3944 | error_report("Invalid queue depth specified"); |
| 3945 | return 1; |
| 3946 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 3947 | depth = res; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3948 | break; |
| 3949 | } |
| 3950 | case 'f': |
| 3951 | fmt = optarg; |
| 3952 | break; |
| 3953 | case 'n': |
| 3954 | flags |= BDRV_O_NATIVE_AIO; |
| 3955 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3956 | case 'o': |
| 3957 | { |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 3958 | offset = cvtnum(optarg); |
| 3959 | if (offset < 0) { |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3960 | error_report("Invalid offset specified"); |
| 3961 | return 1; |
| 3962 | } |
| 3963 | break; |
| 3964 | } |
| 3965 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3966 | case 'q': |
| 3967 | quiet = true; |
| 3968 | break; |
| 3969 | case 's': |
| 3970 | { |
| 3971 | int64_t sval; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3972 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 3973 | sval = cvtnum(optarg); |
| 3974 | if (sval < 0 || sval > INT_MAX) { |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3975 | error_report("Invalid buffer size specified"); |
| 3976 | return 1; |
| 3977 | } |
| 3978 | |
| 3979 | bufsize = sval; |
| 3980 | break; |
| 3981 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3982 | case 'S': |
| 3983 | { |
| 3984 | int64_t sval; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3985 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 3986 | sval = cvtnum(optarg); |
| 3987 | if (sval < 0 || sval > INT_MAX) { |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3988 | error_report("Invalid step size specified"); |
| 3989 | return 1; |
| 3990 | } |
| 3991 | |
| 3992 | step = sval; |
| 3993 | break; |
| 3994 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3995 | case 't': |
| 3996 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 3997 | if (ret < 0) { |
| 3998 | error_report("Invalid cache mode"); |
| 3999 | ret = -1; |
| 4000 | goto out; |
| 4001 | } |
| 4002 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4003 | case 'w': |
| 4004 | flags |= BDRV_O_RDWR; |
| 4005 | is_write = true; |
| 4006 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4007 | case 'U': |
| 4008 | force_share = true; |
| 4009 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4010 | case OPTION_PATTERN: |
| 4011 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4012 | unsigned long res; |
| 4013 | |
| 4014 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4015 | error_report("Invalid pattern byte specified"); |
| 4016 | return 1; |
| 4017 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4018 | pattern = res; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4019 | break; |
| 4020 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4021 | case OPTION_FLUSH_INTERVAL: |
| 4022 | { |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4023 | unsigned long res; |
| 4024 | |
| 4025 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4026 | error_report("Invalid flush interval specified"); |
| 4027 | return 1; |
| 4028 | } |
Peter Maydell | 8b3c679 | 2017-02-10 16:28:23 +0000 | [diff] [blame] | 4029 | flush_interval = res; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4030 | break; |
| 4031 | } |
| 4032 | case OPTION_NO_DRAIN: |
| 4033 | drain_on_flush = false; |
| 4034 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4035 | case OPTION_IMAGE_OPTS: |
| 4036 | image_opts = true; |
| 4037 | break; |
| 4038 | } |
| 4039 | } |
| 4040 | |
| 4041 | if (optind != argc - 1) { |
| 4042 | error_exit("Expecting one image file name"); |
| 4043 | } |
| 4044 | filename = argv[argc - 1]; |
| 4045 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4046 | if (!is_write && flush_interval) { |
| 4047 | error_report("--flush-interval is only available in write tests"); |
| 4048 | ret = -1; |
| 4049 | goto out; |
| 4050 | } |
| 4051 | if (flush_interval && flush_interval < depth) { |
| 4052 | error_report("Flush interval can't be smaller than depth"); |
| 4053 | ret = -1; |
| 4054 | goto out; |
| 4055 | } |
| 4056 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4057 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
| 4058 | force_share); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4059 | if (!blk) { |
| 4060 | ret = -1; |
| 4061 | goto out; |
| 4062 | } |
| 4063 | |
| 4064 | image_size = blk_getlength(blk); |
| 4065 | if (image_size < 0) { |
| 4066 | ret = image_size; |
| 4067 | goto out; |
| 4068 | } |
| 4069 | |
| 4070 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4071 | .blk = blk, |
| 4072 | .image_size = image_size, |
| 4073 | .bufsize = bufsize, |
| 4074 | .step = step ?: bufsize, |
| 4075 | .nrreq = depth, |
| 4076 | .n = count, |
| 4077 | .offset = offset, |
| 4078 | .write = is_write, |
| 4079 | .flush_interval = flush_interval, |
| 4080 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4081 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4082 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4083 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4084 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 4085 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 4086 | if (flush_interval) { |
| 4087 | printf("Sending flush every %d requests\n", flush_interval); |
| 4088 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4089 | |
| 4090 | data.buf = blk_blockalign(blk, data.nrreq * data.bufsize); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 4091 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 4092 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4093 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 4094 | for (i = 0; i < data.nrreq; i++) { |
| 4095 | qemu_iovec_init(&data.qiov[i], 1); |
| 4096 | qemu_iovec_add(&data.qiov[i], |
| 4097 | data.buf + i * data.bufsize, data.bufsize); |
| 4098 | } |
| 4099 | |
| 4100 | gettimeofday(&t1, NULL); |
| 4101 | bench_cb(&data, 0); |
| 4102 | |
| 4103 | while (data.n > 0) { |
| 4104 | main_loop_wait(false); |
| 4105 | } |
| 4106 | gettimeofday(&t2, NULL); |
| 4107 | |
| 4108 | printf("Run completed in %3.3f seconds.\n", |
| 4109 | (t2.tv_sec - t1.tv_sec) |
| 4110 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 4111 | |
| 4112 | out: |
| 4113 | qemu_vfree(data.buf); |
| 4114 | blk_unref(blk); |
| 4115 | |
| 4116 | if (ret) { |
| 4117 | return 1; |
| 4118 | } |
| 4119 | return 0; |
| 4120 | } |
| 4121 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4122 | #define C_BS 01 |
| 4123 | #define C_COUNT 02 |
| 4124 | #define C_IF 04 |
| 4125 | #define C_OF 010 |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4126 | #define C_SKIP 020 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4127 | |
| 4128 | struct DdInfo { |
| 4129 | unsigned int flags; |
| 4130 | int64_t count; |
| 4131 | }; |
| 4132 | |
| 4133 | struct DdIo { |
| 4134 | int bsz; /* Block size */ |
| 4135 | char *filename; |
| 4136 | uint8_t *buf; |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4137 | int64_t offset; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4138 | }; |
| 4139 | |
| 4140 | struct DdOpts { |
| 4141 | const char *name; |
| 4142 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); |
| 4143 | unsigned int flag; |
| 4144 | }; |
| 4145 | |
| 4146 | static int img_dd_bs(const char *arg, |
| 4147 | struct DdIo *in, struct DdIo *out, |
| 4148 | struct DdInfo *dd) |
| 4149 | { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4150 | int64_t res; |
| 4151 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4152 | res = cvtnum(arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4153 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4154 | if (res <= 0 || res > INT_MAX) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4155 | error_report("invalid number: '%s'", arg); |
| 4156 | return 1; |
| 4157 | } |
| 4158 | in->bsz = out->bsz = res; |
| 4159 | |
| 4160 | return 0; |
| 4161 | } |
| 4162 | |
| 4163 | static int img_dd_count(const char *arg, |
| 4164 | struct DdIo *in, struct DdIo *out, |
| 4165 | struct DdInfo *dd) |
| 4166 | { |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4167 | dd->count = cvtnum(arg); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4168 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4169 | if (dd->count < 0) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4170 | error_report("invalid number: '%s'", arg); |
| 4171 | return 1; |
| 4172 | } |
| 4173 | |
| 4174 | return 0; |
| 4175 | } |
| 4176 | |
| 4177 | static int img_dd_if(const char *arg, |
| 4178 | struct DdIo *in, struct DdIo *out, |
| 4179 | struct DdInfo *dd) |
| 4180 | { |
| 4181 | in->filename = g_strdup(arg); |
| 4182 | |
| 4183 | return 0; |
| 4184 | } |
| 4185 | |
| 4186 | static int img_dd_of(const char *arg, |
| 4187 | struct DdIo *in, struct DdIo *out, |
| 4188 | struct DdInfo *dd) |
| 4189 | { |
| 4190 | out->filename = g_strdup(arg); |
| 4191 | |
| 4192 | return 0; |
| 4193 | } |
| 4194 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4195 | static int img_dd_skip(const char *arg, |
| 4196 | struct DdIo *in, struct DdIo *out, |
| 4197 | struct DdInfo *dd) |
| 4198 | { |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4199 | in->offset = cvtnum(arg); |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4200 | |
Markus Armbruster | 606caa0 | 2017-02-21 21:14:04 +0100 | [diff] [blame] | 4201 | if (in->offset < 0) { |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4202 | error_report("invalid number: '%s'", arg); |
| 4203 | return 1; |
| 4204 | } |
| 4205 | |
| 4206 | return 0; |
| 4207 | } |
| 4208 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4209 | static int img_dd(int argc, char **argv) |
| 4210 | { |
| 4211 | int ret = 0; |
| 4212 | char *arg = NULL; |
| 4213 | char *tmp; |
| 4214 | BlockDriver *drv = NULL, *proto_drv = NULL; |
| 4215 | BlockBackend *blk1 = NULL, *blk2 = NULL; |
| 4216 | QemuOpts *opts = NULL; |
| 4217 | QemuOptsList *create_opts = NULL; |
| 4218 | Error *local_err = NULL; |
| 4219 | bool image_opts = false; |
| 4220 | int c, i; |
| 4221 | const char *out_fmt = "raw"; |
| 4222 | const char *fmt = NULL; |
| 4223 | int64_t size = 0; |
| 4224 | int64_t block_count = 0, out_pos, in_pos; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4225 | bool force_share = false; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4226 | struct DdInfo dd = { |
| 4227 | .flags = 0, |
| 4228 | .count = 0, |
| 4229 | }; |
| 4230 | struct DdIo in = { |
| 4231 | .bsz = 512, /* Block size is by default 512 bytes */ |
| 4232 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4233 | .buf = NULL, |
| 4234 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4235 | }; |
| 4236 | struct DdIo out = { |
| 4237 | .bsz = 512, |
| 4238 | .filename = NULL, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4239 | .buf = NULL, |
| 4240 | .offset = 0 |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4241 | }; |
| 4242 | |
| 4243 | const struct DdOpts options[] = { |
| 4244 | { "bs", img_dd_bs, C_BS }, |
| 4245 | { "count", img_dd_count, C_COUNT }, |
| 4246 | { "if", img_dd_if, C_IF }, |
| 4247 | { "of", img_dd_of, C_OF }, |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4248 | { "skip", img_dd_skip, C_SKIP }, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4249 | { NULL, NULL, 0 } |
| 4250 | }; |
| 4251 | const struct option long_options[] = { |
| 4252 | { "help", no_argument, 0, 'h'}, |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4253 | { "object", required_argument, 0, OPTION_OBJECT}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4254 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4255 | { "force-share", no_argument, 0, 'U'}, |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4256 | { 0, 0, 0, 0 } |
| 4257 | }; |
| 4258 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4259 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4260 | if (c == EOF) { |
| 4261 | break; |
| 4262 | } |
| 4263 | switch (c) { |
| 4264 | case 'O': |
| 4265 | out_fmt = optarg; |
| 4266 | break; |
| 4267 | case 'f': |
| 4268 | fmt = optarg; |
| 4269 | break; |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4270 | case ':': |
| 4271 | missing_argument(argv[optind - 1]); |
| 4272 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4273 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4274 | unrecognized_option(argv[optind - 1]); |
| 4275 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4276 | case 'h': |
| 4277 | help(); |
| 4278 | break; |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4279 | case 'U': |
| 4280 | force_share = true; |
| 4281 | break; |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4282 | case OPTION_OBJECT: |
| 4283 | if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) { |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4284 | ret = -1; |
| 4285 | goto out; |
| 4286 | } |
Stefan Hajnoczi | 2a24570 | 2017-06-19 16:00:02 +0100 | [diff] [blame] | 4287 | break; |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4288 | case OPTION_IMAGE_OPTS: |
| 4289 | image_opts = true; |
| 4290 | break; |
| 4291 | } |
| 4292 | } |
| 4293 | |
| 4294 | for (i = optind; i < argc; i++) { |
| 4295 | int j; |
| 4296 | arg = g_strdup(argv[i]); |
| 4297 | |
| 4298 | tmp = strchr(arg, '='); |
| 4299 | if (tmp == NULL) { |
| 4300 | error_report("unrecognized operand %s", arg); |
| 4301 | ret = -1; |
| 4302 | goto out; |
| 4303 | } |
| 4304 | |
| 4305 | *tmp++ = '\0'; |
| 4306 | |
| 4307 | for (j = 0; options[j].name != NULL; j++) { |
| 4308 | if (!strcmp(arg, options[j].name)) { |
| 4309 | break; |
| 4310 | } |
| 4311 | } |
| 4312 | if (options[j].name == NULL) { |
| 4313 | error_report("unrecognized operand %s", arg); |
| 4314 | ret = -1; |
| 4315 | goto out; |
| 4316 | } |
| 4317 | |
| 4318 | if (options[j].f(tmp, &in, &out, &dd) != 0) { |
| 4319 | ret = -1; |
| 4320 | goto out; |
| 4321 | } |
| 4322 | dd.flags |= options[j].flag; |
| 4323 | g_free(arg); |
| 4324 | arg = NULL; |
| 4325 | } |
| 4326 | |
| 4327 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { |
| 4328 | error_report("Must specify both input and output files"); |
| 4329 | ret = -1; |
| 4330 | goto out; |
| 4331 | } |
Daniel P. Berrange | 83d4bf9 | 2017-05-15 17:47:09 +0100 | [diff] [blame] | 4332 | |
| 4333 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4334 | user_creatable_add_opts_foreach, |
| 4335 | NULL, NULL)) { |
| 4336 | ret = -1; |
| 4337 | goto out; |
| 4338 | } |
| 4339 | |
Fam Zheng | 335e993 | 2017-05-03 00:35:39 +0800 | [diff] [blame] | 4340 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
| 4341 | force_share); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4342 | |
| 4343 | if (!blk1) { |
| 4344 | ret = -1; |
| 4345 | goto out; |
| 4346 | } |
| 4347 | |
| 4348 | drv = bdrv_find_format(out_fmt); |
| 4349 | if (!drv) { |
| 4350 | error_report("Unknown file format"); |
| 4351 | ret = -1; |
| 4352 | goto out; |
| 4353 | } |
| 4354 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); |
| 4355 | |
| 4356 | if (!proto_drv) { |
| 4357 | error_report_err(local_err); |
| 4358 | ret = -1; |
| 4359 | goto out; |
| 4360 | } |
| 4361 | if (!drv->create_opts) { |
| 4362 | error_report("Format driver '%s' does not support image creation", |
| 4363 | drv->format_name); |
| 4364 | ret = -1; |
| 4365 | goto out; |
| 4366 | } |
| 4367 | if (!proto_drv->create_opts) { |
| 4368 | error_report("Protocol driver '%s' does not support image creation", |
| 4369 | proto_drv->format_name); |
| 4370 | ret = -1; |
| 4371 | goto out; |
| 4372 | } |
| 4373 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 4374 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
| 4375 | |
| 4376 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 4377 | |
| 4378 | size = blk_getlength(blk1); |
| 4379 | if (size < 0) { |
| 4380 | error_report("Failed to get size for '%s'", in.filename); |
| 4381 | ret = -1; |
| 4382 | goto out; |
| 4383 | } |
| 4384 | |
| 4385 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && |
| 4386 | dd.count * in.bsz < size) { |
| 4387 | size = dd.count * in.bsz; |
| 4388 | } |
| 4389 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4390 | /* Overflow means the specified offset is beyond input image's size */ |
| 4391 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 4392 | size < in.bsz * in.offset)) { |
| 4393 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); |
| 4394 | } else { |
| 4395 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, |
| 4396 | size - in.bsz * in.offset, &error_abort); |
| 4397 | } |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4398 | |
| 4399 | ret = bdrv_create(drv, out.filename, opts, &local_err); |
| 4400 | if (ret < 0) { |
| 4401 | error_reportf_err(local_err, |
| 4402 | "%s: error while creating output image: ", |
| 4403 | out.filename); |
| 4404 | ret = -1; |
| 4405 | goto out; |
| 4406 | } |
| 4407 | |
Daniel P. Berrange | ea204dd | 2017-05-15 17:47:10 +0100 | [diff] [blame] | 4408 | /* TODO, we can't honour --image-opts for the target, |
| 4409 | * since it needs to be given in a format compatible |
| 4410 | * with the bdrv_create() call above which does not |
| 4411 | * support image-opts style. |
| 4412 | */ |
Daniel P. Berrange | 29cf933 | 2017-05-15 17:47:12 +0100 | [diff] [blame] | 4413 | 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] | 4414 | false, false, false); |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4415 | |
| 4416 | if (!blk2) { |
| 4417 | ret = -1; |
| 4418 | goto out; |
| 4419 | } |
| 4420 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4421 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
| 4422 | size < in.offset * in.bsz)) { |
| 4423 | /* We give a warning if the skip option is bigger than the input |
| 4424 | * size and create an empty output disk image (i.e. like dd(1)). |
| 4425 | */ |
| 4426 | error_report("%s: cannot skip to specified offset", in.filename); |
| 4427 | in_pos = size; |
| 4428 | } else { |
| 4429 | in_pos = in.offset * in.bsz; |
| 4430 | } |
| 4431 | |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4432 | in.buf = g_new(uint8_t, in.bsz); |
| 4433 | |
Reda Sallahi | f7c1553 | 2016-08-10 16:16:09 +0200 | [diff] [blame] | 4434 | for (out_pos = 0; in_pos < size; block_count++) { |
Reda Sallahi | 86ce1f6 | 2016-08-10 04:43:12 +0200 | [diff] [blame] | 4435 | int in_ret, out_ret; |
| 4436 | |
| 4437 | if (in_pos + in.bsz > size) { |
| 4438 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); |
| 4439 | } else { |
| 4440 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); |
| 4441 | } |
| 4442 | if (in_ret < 0) { |
| 4443 | error_report("error while reading from input image file: %s", |
| 4444 | strerror(-in_ret)); |
| 4445 | ret = -1; |
| 4446 | goto out; |
| 4447 | } |
| 4448 | in_pos += in_ret; |
| 4449 | |
| 4450 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); |
| 4451 | |
| 4452 | if (out_ret < 0) { |
| 4453 | error_report("error while writing to output image file: %s", |
| 4454 | strerror(-out_ret)); |
| 4455 | ret = -1; |
| 4456 | goto out; |
| 4457 | } |
| 4458 | out_pos += out_ret; |
| 4459 | } |
| 4460 | |
| 4461 | out: |
| 4462 | g_free(arg); |
| 4463 | qemu_opts_del(opts); |
| 4464 | qemu_opts_free(create_opts); |
| 4465 | blk_unref(blk1); |
| 4466 | blk_unref(blk2); |
| 4467 | g_free(in.filename); |
| 4468 | g_free(out.filename); |
| 4469 | g_free(in.buf); |
| 4470 | g_free(out.buf); |
| 4471 | |
| 4472 | if (ret) { |
| 4473 | return 1; |
| 4474 | } |
| 4475 | return 0; |
| 4476 | } |
| 4477 | |
Stefan Hajnoczi | fd03c2b | 2017-07-05 13:57:36 +0100 | [diff] [blame] | 4478 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
| 4479 | { |
| 4480 | QString *str; |
| 4481 | QObject *obj; |
| 4482 | Visitor *v = qobject_output_visitor_new(&obj); |
| 4483 | |
| 4484 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); |
| 4485 | visit_complete(v, &obj); |
| 4486 | str = qobject_to_json_pretty(obj); |
| 4487 | assert(str != NULL); |
| 4488 | printf("%s\n", qstring_get_str(str)); |
| 4489 | qobject_decref(obj); |
| 4490 | visit_free(v); |
| 4491 | QDECREF(str); |
| 4492 | } |
| 4493 | |
| 4494 | static int img_measure(int argc, char **argv) |
| 4495 | { |
| 4496 | static const struct option long_options[] = { |
| 4497 | {"help", no_argument, 0, 'h'}, |
| 4498 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
| 4499 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 4500 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 4501 | {"size", required_argument, 0, OPTION_SIZE}, |
| 4502 | {"force-share", no_argument, 0, 'U'}, |
| 4503 | {0, 0, 0, 0} |
| 4504 | }; |
| 4505 | OutputFormat output_format = OFORMAT_HUMAN; |
| 4506 | BlockBackend *in_blk = NULL; |
| 4507 | BlockDriver *drv; |
| 4508 | const char *filename = NULL; |
| 4509 | const char *fmt = NULL; |
| 4510 | const char *out_fmt = "raw"; |
| 4511 | char *options = NULL; |
| 4512 | char *snapshot_name = NULL; |
| 4513 | bool force_share = false; |
| 4514 | QemuOpts *opts = NULL; |
| 4515 | QemuOpts *object_opts = NULL; |
| 4516 | QemuOpts *sn_opts = NULL; |
| 4517 | QemuOptsList *create_opts = NULL; |
| 4518 | bool image_opts = false; |
| 4519 | uint64_t img_size = UINT64_MAX; |
| 4520 | BlockMeasureInfo *info = NULL; |
| 4521 | Error *local_err = NULL; |
| 4522 | int ret = 1; |
| 4523 | int c; |
| 4524 | |
| 4525 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", |
| 4526 | long_options, NULL)) != -1) { |
| 4527 | switch (c) { |
| 4528 | case '?': |
| 4529 | case 'h': |
| 4530 | help(); |
| 4531 | break; |
| 4532 | case 'f': |
| 4533 | fmt = optarg; |
| 4534 | break; |
| 4535 | case 'O': |
| 4536 | out_fmt = optarg; |
| 4537 | break; |
| 4538 | case 'o': |
| 4539 | if (!is_valid_option_list(optarg)) { |
| 4540 | error_report("Invalid option list: %s", optarg); |
| 4541 | goto out; |
| 4542 | } |
| 4543 | if (!options) { |
| 4544 | options = g_strdup(optarg); |
| 4545 | } else { |
| 4546 | char *old_options = options; |
| 4547 | options = g_strdup_printf("%s,%s", options, optarg); |
| 4548 | g_free(old_options); |
| 4549 | } |
| 4550 | break; |
| 4551 | case 'l': |
| 4552 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
| 4553 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 4554 | optarg, false); |
| 4555 | if (!sn_opts) { |
| 4556 | error_report("Failed in parsing snapshot param '%s'", |
| 4557 | optarg); |
| 4558 | goto out; |
| 4559 | } |
| 4560 | } else { |
| 4561 | snapshot_name = optarg; |
| 4562 | } |
| 4563 | break; |
| 4564 | case 'U': |
| 4565 | force_share = true; |
| 4566 | break; |
| 4567 | case OPTION_OBJECT: |
| 4568 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 4569 | optarg, true); |
| 4570 | if (!object_opts) { |
| 4571 | goto out; |
| 4572 | } |
| 4573 | break; |
| 4574 | case OPTION_IMAGE_OPTS: |
| 4575 | image_opts = true; |
| 4576 | break; |
| 4577 | case OPTION_OUTPUT: |
| 4578 | if (!strcmp(optarg, "json")) { |
| 4579 | output_format = OFORMAT_JSON; |
| 4580 | } else if (!strcmp(optarg, "human")) { |
| 4581 | output_format = OFORMAT_HUMAN; |
| 4582 | } else { |
| 4583 | error_report("--output must be used with human or json " |
| 4584 | "as argument."); |
| 4585 | goto out; |
| 4586 | } |
| 4587 | break; |
| 4588 | case OPTION_SIZE: |
| 4589 | { |
| 4590 | int64_t sval; |
| 4591 | |
| 4592 | sval = cvtnum(optarg); |
| 4593 | if (sval < 0) { |
| 4594 | if (sval == -ERANGE) { |
| 4595 | error_report("Image size must be less than 8 EiB!"); |
| 4596 | } else { |
| 4597 | error_report("Invalid image size specified! You may use " |
| 4598 | "k, M, G, T, P or E suffixes for "); |
| 4599 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 4600 | "petabytes and exabytes."); |
| 4601 | } |
| 4602 | goto out; |
| 4603 | } |
| 4604 | img_size = (uint64_t)sval; |
| 4605 | } |
| 4606 | break; |
| 4607 | } |
| 4608 | } |
| 4609 | |
| 4610 | if (qemu_opts_foreach(&qemu_object_opts, |
| 4611 | user_creatable_add_opts_foreach, |
| 4612 | NULL, NULL)) { |
| 4613 | goto out; |
| 4614 | } |
| 4615 | |
| 4616 | if (argc - optind > 1) { |
| 4617 | error_report("At most one filename argument is allowed."); |
| 4618 | goto out; |
| 4619 | } else if (argc - optind == 1) { |
| 4620 | filename = argv[optind]; |
| 4621 | } |
| 4622 | |
| 4623 | if (!filename && |
| 4624 | (object_opts || image_opts || fmt || snapshot_name || sn_opts)) { |
| 4625 | error_report("--object, --image-opts, -f, and -l " |
| 4626 | "require a filename argument."); |
| 4627 | goto out; |
| 4628 | } |
| 4629 | if (filename && img_size != UINT64_MAX) { |
| 4630 | error_report("--size N cannot be used together with a filename."); |
| 4631 | goto out; |
| 4632 | } |
| 4633 | if (!filename && img_size == UINT64_MAX) { |
| 4634 | error_report("Either --size N or one filename must be specified."); |
| 4635 | goto out; |
| 4636 | } |
| 4637 | |
| 4638 | if (filename) { |
| 4639 | in_blk = img_open(image_opts, filename, fmt, 0, |
| 4640 | false, false, force_share); |
| 4641 | if (!in_blk) { |
| 4642 | goto out; |
| 4643 | } |
| 4644 | |
| 4645 | if (sn_opts) { |
| 4646 | bdrv_snapshot_load_tmp(blk_bs(in_blk), |
| 4647 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 4648 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 4649 | &local_err); |
| 4650 | } else if (snapshot_name != NULL) { |
| 4651 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), |
| 4652 | snapshot_name, &local_err); |
| 4653 | } |
| 4654 | if (local_err) { |
| 4655 | error_reportf_err(local_err, "Failed to load snapshot: "); |
| 4656 | goto out; |
| 4657 | } |
| 4658 | } |
| 4659 | |
| 4660 | drv = bdrv_find_format(out_fmt); |
| 4661 | if (!drv) { |
| 4662 | error_report("Unknown file format '%s'", out_fmt); |
| 4663 | goto out; |
| 4664 | } |
| 4665 | if (!drv->create_opts) { |
| 4666 | error_report("Format driver '%s' does not support image creation", |
| 4667 | drv->format_name); |
| 4668 | goto out; |
| 4669 | } |
| 4670 | |
| 4671 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 4672 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); |
| 4673 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
| 4674 | if (options) { |
| 4675 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 4676 | if (local_err) { |
| 4677 | error_report_err(local_err); |
| 4678 | error_report("Invalid options for file format '%s'", out_fmt); |
| 4679 | goto out; |
| 4680 | } |
| 4681 | } |
| 4682 | if (img_size != UINT64_MAX) { |
| 4683 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); |
| 4684 | } |
| 4685 | |
| 4686 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); |
| 4687 | if (local_err) { |
| 4688 | error_report_err(local_err); |
| 4689 | goto out; |
| 4690 | } |
| 4691 | |
| 4692 | if (output_format == OFORMAT_HUMAN) { |
| 4693 | printf("required size: %" PRIu64 "\n", info->required); |
| 4694 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); |
| 4695 | } else { |
| 4696 | dump_json_block_measure_info(info); |
| 4697 | } |
| 4698 | |
| 4699 | ret = 0; |
| 4700 | |
| 4701 | out: |
| 4702 | qapi_free_BlockMeasureInfo(info); |
| 4703 | qemu_opts_del(object_opts); |
| 4704 | qemu_opts_del(opts); |
| 4705 | qemu_opts_del(sn_opts); |
| 4706 | qemu_opts_free(create_opts); |
| 4707 | g_free(options); |
| 4708 | blk_unref(in_blk); |
| 4709 | return ret; |
| 4710 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 4711 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4712 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4713 | #define DEF(option, callback, arg_string) \ |
| 4714 | { option, callback }, |
| 4715 | #include "qemu-img-cmds.h" |
| 4716 | #undef DEF |
| 4717 | #undef GEN_DOCS |
| 4718 | { NULL, NULL, }, |
| 4719 | }; |
| 4720 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4721 | int main(int argc, char **argv) |
| 4722 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 4723 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4724 | const char *cmdname; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 4725 | Error *local_error = NULL; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 4726 | char *trace_file = NULL; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 4727 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 4728 | static const struct option long_options[] = { |
| 4729 | {"help", no_argument, 0, 'h'}, |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4730 | {"version", no_argument, 0, 'V'}, |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 4731 | {"trace", required_argument, NULL, 'T'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 4732 | {0, 0, 0, 0} |
| 4733 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4734 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 4735 | #ifdef CONFIG_POSIX |
| 4736 | signal(SIGPIPE, SIG_IGN); |
| 4737 | #endif |
| 4738 | |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 4739 | module_call_init(MODULE_INIT_TRACE); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 4740 | error_set_progname(argv[0]); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 4741 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 4742 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 4743 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 4744 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 4745 | exit(EXIT_FAILURE); |
| 4746 | } |
| 4747 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 4748 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 4749 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 4750 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4751 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4752 | if (argc < 2) { |
| 4753 | error_exit("Not enough arguments"); |
| 4754 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4755 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4756 | qemu_add_opts(&qemu_object_opts); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 4757 | qemu_add_opts(&qemu_source_opts); |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 4758 | qemu_add_opts(&qemu_trace_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 4759 | |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4760 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4761 | switch (c) { |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4762 | case ':': |
| 4763 | missing_argument(argv[optind - 1]); |
| 4764 | return 0; |
Stefan Hajnoczi | 4581c16 | 2017-03-17 18:45:39 +0800 | [diff] [blame] | 4765 | case '?': |
Stefan Hajnoczi | c919297 | 2017-03-17 18:45:41 +0800 | [diff] [blame] | 4766 | unrecognized_option(argv[optind - 1]); |
| 4767 | return 0; |
| 4768 | case 'h': |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4769 | help(); |
| 4770 | return 0; |
| 4771 | case 'V': |
| 4772 | printf(QEMU_IMG_VERSION); |
| 4773 | return 0; |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 4774 | case 'T': |
| 4775 | g_free(trace_file); |
| 4776 | trace_file = trace_opt_parse(optarg); |
| 4777 | break; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4778 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4779 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4780 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4781 | cmdname = argv[optind]; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 4782 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4783 | /* reset getopt_long scanning */ |
| 4784 | argc -= optind; |
| 4785 | if (argc < 1) { |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 4786 | return 0; |
| 4787 | } |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4788 | argv += optind; |
Denis V. Lunev | cfef6a4 | 2016-07-04 16:16:48 +0300 | [diff] [blame] | 4789 | optind = 0; |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4790 | |
Denis V. Lunev | 06a1e0c | 2016-06-17 17:44:14 +0300 | [diff] [blame] | 4791 | if (!trace_init_backends()) { |
| 4792 | exit(1); |
| 4793 | } |
| 4794 | trace_init_file(trace_file); |
| 4795 | qemu_set_log(LOG_TRACE); |
| 4796 | |
Denis V. Lunev | 1098513 | 2016-06-17 17:44:13 +0300 | [diff] [blame] | 4797 | /* find the command */ |
| 4798 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 4799 | if (!strcmp(cmdname, cmd->name)) { |
| 4800 | return cmd->handler(argc, argv); |
| 4801 | } |
| 4802 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 4803 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 4804 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 4805 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 4806 | } |