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