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