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