bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1 | /* |
bellard | fb43f4d | 2006-08-07 21:34:46 +0000 | [diff] [blame] | 2 | * QEMU disk image utility |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 68d0f70 | 2008-01-06 17:21:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Fam Zheng | 67a1de0 | 2016-06-01 17:44:21 +0800 | [diff] [blame] | 25 | #include "qemu-version.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 27 | #include "qapi-visit.h" |
| 28 | #include "qapi/qmp-output-visitor.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 29 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 30 | #include "qapi/qmp/qjson.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 31 | #include "qemu/cutils.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 32 | #include "qemu/config-file.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 33 | #include "qemu/option.h" |
| 34 | #include "qemu/error-report.h" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 35 | #include "qom/object_interfaces.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 36 | #include "sysemu/sysemu.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 37 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 38 | #include "block/block_int.h" |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 39 | #include "block/blockjob.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 40 | #include "block/qapi.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 41 | #include "crypto/init.h" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 42 | #include <getopt.h> |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 43 | |
Don Slutz | 61979a6 | 2015-01-09 10:17:35 -0500 | [diff] [blame] | 44 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION QEMU_PKGVERSION \ |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 45 | ", Copyright (c) 2004-2008 Fabrice Bellard\n" |
| 46 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 47 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 48 | const char *name; |
| 49 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 50 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 51 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 52 | enum { |
| 53 | OPTION_OUTPUT = 256, |
| 54 | OPTION_BACKING_CHAIN = 257, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 55 | OPTION_OBJECT = 258, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 56 | OPTION_IMAGE_OPTS = 259, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 57 | OPTION_PATTERN = 260, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 58 | OPTION_FLUSH_INTERVAL = 261, |
| 59 | OPTION_NO_DRAIN = 262, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | typedef enum OutputFormat { |
| 63 | OFORMAT_JSON, |
| 64 | OFORMAT_HUMAN, |
| 65 | } OutputFormat; |
| 66 | |
Kevin Wolf | e699614 | 2016-03-15 13:03:11 +0100 | [diff] [blame] | 67 | /* 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] | 68 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 69 | |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 70 | static void format_print(void *opaque, const char *name) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 71 | { |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 72 | printf(" %s", name); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 75 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
| 76 | { |
| 77 | va_list ap; |
| 78 | |
| 79 | error_printf("qemu-img: "); |
| 80 | |
| 81 | va_start(ap, fmt); |
| 82 | error_vprintf(fmt, ap); |
| 83 | va_end(ap); |
| 84 | |
| 85 | error_printf("\nTry 'qemu-img --help' for more information\n"); |
| 86 | exit(EXIT_FAILURE); |
| 87 | } |
| 88 | |
blueswir1 | d2c639d | 2009-01-24 18:19:25 +0000 | [diff] [blame] | 89 | /* Please keep in synch with qemu-img.texi */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 90 | static void QEMU_NORETURN help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 91 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 92 | const char *help_msg = |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 93 | QEMU_IMG_VERSION |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 94 | "usage: qemu-img command [command options]\n" |
| 95 | "QEMU disk image utility\n" |
| 96 | "\n" |
| 97 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 98 | #define DEF(option, callback, arg_string) \ |
| 99 | " " arg_string "\n" |
| 100 | #include "qemu-img-cmds.h" |
| 101 | #undef DEF |
| 102 | #undef GEN_DOCS |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 103 | "\n" |
| 104 | "Command parameters:\n" |
| 105 | " 'filename' is a disk image filename\n" |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 106 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
| 107 | " manual page for a description of the object properties. The most common\n" |
| 108 | " object type is a 'secret', which is used to supply passwords and/or\n" |
| 109 | " encryption keys.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 110 | " '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] | 111 | " '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] | 112 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 113 | " 'directsync' and 'unsafe' (default for convert)\n" |
Stefan Hajnoczi | bb87fdf | 2014-09-02 11:01:02 +0100 | [diff] [blame] | 114 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
| 115 | " options are the same as for the 'cache' option\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 116 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 117 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
| 118 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" |
| 119 | " supported. 'b' is ignored.\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 120 | " 'output_filename' is the destination disk image filename\n" |
| 121 | " 'output_fmt' is the destination format\n" |
| 122 | " 'options' is a comma separated list of format specific options in a\n" |
| 123 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 124 | " used format\n" |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 125 | " 'snapshot_param' is param used for internal snapshot, format\n" |
| 126 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" |
| 127 | " '[ID_OR_NAME]'\n" |
| 128 | " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n" |
| 129 | " instead\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 130 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
| 131 | " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n" |
| 132 | " match exactly. The image doesn't need a working backing file before\n" |
| 133 | " rebasing in this case (useful for renaming the backing file)\n" |
| 134 | " '-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] | 135 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 136 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Peter Lieven | 11b6699 | 2013-10-24 12:07:05 +0200 | [diff] [blame] | 137 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
| 138 | " contain only zeros for qemu-img to create a sparse image during\n" |
| 139 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" |
| 140 | " unallocated or zero sectors, and the destination image will always be\n" |
| 141 | " fully allocated\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 142 | " '--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] | 143 | " '-n' skips the target volume creation (useful if the volume is created\n" |
| 144 | " prior to running qemu-img)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 145 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 146 | "Parameters to check subcommand:\n" |
| 147 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 148 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 149 | " 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] | 150 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 151 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 152 | "Parameters to snapshot subcommand:\n" |
| 153 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 154 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 155 | " '-c' creates a snapshot\n" |
| 156 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 157 | " '-l' lists all snapshots in the given image\n" |
| 158 | "\n" |
| 159 | "Parameters to compare subcommand:\n" |
| 160 | " '-f' first image format\n" |
| 161 | " '-F' second image format\n" |
| 162 | " '-s' run in Strict mode - fail on different image size or sector allocation\n"; |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 163 | |
| 164 | printf("%s\nSupported formats:", help_msg); |
Stefan Hajnoczi | 00c6d40 | 2014-08-27 12:08:56 +0100 | [diff] [blame] | 165 | bdrv_iterate_format(format_print, NULL); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 166 | printf("\n"); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 167 | exit(EXIT_SUCCESS); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 170 | static QemuOptsList qemu_object_opts = { |
| 171 | .name = "object", |
| 172 | .implied_opt_name = "qom-type", |
| 173 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), |
| 174 | .desc = { |
| 175 | { } |
| 176 | }, |
| 177 | }; |
| 178 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 179 | static QemuOptsList qemu_source_opts = { |
| 180 | .name = "source", |
| 181 | .implied_opt_name = "file", |
| 182 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), |
| 183 | .desc = { |
| 184 | { } |
| 185 | }, |
| 186 | }; |
| 187 | |
Stefan Weil | 7c30f65 | 2013-06-16 17:01:05 +0200 | [diff] [blame] | 188 | 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] | 189 | { |
| 190 | int ret = 0; |
| 191 | if (!quiet) { |
| 192 | va_list args; |
| 193 | va_start(args, fmt); |
| 194 | ret = vprintf(fmt, args); |
| 195 | va_end(args); |
| 196 | } |
| 197 | return ret; |
| 198 | } |
| 199 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 200 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 201 | static int print_block_option_help(const char *filename, const char *fmt) |
| 202 | { |
| 203 | BlockDriver *drv, *proto_drv; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 204 | QemuOptsList *create_opts = NULL; |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 205 | Error *local_err = NULL; |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 206 | |
| 207 | /* Find driver and parse its options */ |
| 208 | drv = bdrv_find_format(fmt); |
| 209 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 210 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 211 | return 1; |
| 212 | } |
| 213 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 214 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 215 | if (filename) { |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 216 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 217 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 218 | error_report_err(local_err); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 219 | qemu_opts_free(create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 220 | return 1; |
| 221 | } |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 222 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 225 | qemu_opts_print_help(create_opts); |
| 226 | qemu_opts_free(create_opts); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 230 | |
| 231 | static int img_open_password(BlockBackend *blk, const char *filename, |
Daniel P. Berrange | abb06c5 | 2016-03-21 14:11:42 +0000 | [diff] [blame] | 232 | int flags, bool quiet) |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 233 | { |
| 234 | BlockDriverState *bs; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 235 | char password[256]; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 236 | |
| 237 | bs = blk_bs(blk); |
Daniel P. Berrange | 4ef130f | 2016-03-21 14:11:43 +0000 | [diff] [blame] | 238 | if (bdrv_is_encrypted(bs) && bdrv_key_required(bs) && |
| 239 | !(flags & BDRV_O_NO_IO)) { |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 240 | qprintf(quiet, "Disk image '%s' is encrypted.\n", filename); |
| 241 | if (qemu_read_password(password, sizeof(password)) < 0) { |
| 242 | error_report("No password given"); |
| 243 | return -1; |
| 244 | } |
| 245 | if (bdrv_set_key(bs, password) < 0) { |
| 246 | error_report("invalid password"); |
| 247 | return -1; |
| 248 | } |
| 249 | } |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 254 | static BlockBackend *img_open_opts(const char *optstr, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 255 | QemuOpts *opts, int flags, bool writethrough, |
Daniel P. Berrange | abb06c5 | 2016-03-21 14:11:42 +0000 | [diff] [blame] | 256 | bool quiet) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 257 | { |
| 258 | QDict *options; |
| 259 | Error *local_err = NULL; |
| 260 | BlockBackend *blk; |
| 261 | options = qemu_opts_to_qdict(opts, NULL); |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 262 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 263 | if (!blk) { |
Daniel P. Berrange | 143605a | 2016-04-06 10:16:18 +0100 | [diff] [blame] | 264 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 265 | return NULL; |
| 266 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 267 | blk_set_enable_write_cache(blk, !writethrough); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 268 | |
Daniel P. Berrange | abb06c5 | 2016-03-21 14:11:42 +0000 | [diff] [blame] | 269 | if (img_open_password(blk, optstr, flags, quiet) < 0) { |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 270 | blk_unref(blk); |
| 271 | return NULL; |
| 272 | } |
| 273 | return blk; |
| 274 | } |
| 275 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 276 | static BlockBackend *img_open_file(const char *filename, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 277 | const char *fmt, int flags, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 278 | bool writethrough, bool quiet) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 279 | { |
| 280 | BlockBackend *blk; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 281 | Error *local_err = NULL; |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 282 | QDict *options = NULL; |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 283 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 284 | if (fmt) { |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 285 | options = qdict_new(); |
| 286 | qdict_put(options, "driver", qstring_from_str(fmt)); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 287 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 288 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 289 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
Max Reitz | 5bd3132 | 2015-02-05 13:58:16 -0500 | [diff] [blame] | 290 | if (!blk) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 291 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 292 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 293 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 294 | blk_set_enable_write_cache(blk, !writethrough); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 295 | |
Daniel P. Berrange | abb06c5 | 2016-03-21 14:11:42 +0000 | [diff] [blame] | 296 | if (img_open_password(blk, filename, flags, quiet) < 0) { |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 297 | blk_unref(blk); |
| 298 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 299 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 300 | return blk; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 303 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 304 | static BlockBackend *img_open(bool image_opts, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 305 | const char *filename, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 306 | const char *fmt, int flags, bool writethrough, |
Daniel P. Berrange | abb06c5 | 2016-03-21 14:11:42 +0000 | [diff] [blame] | 307 | bool quiet) |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 308 | { |
| 309 | BlockBackend *blk; |
| 310 | if (image_opts) { |
| 311 | QemuOpts *opts; |
| 312 | if (fmt) { |
| 313 | error_report("--image-opts and --format are mutually exclusive"); |
| 314 | return NULL; |
| 315 | } |
| 316 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), |
| 317 | filename, true); |
| 318 | if (!opts) { |
| 319 | return NULL; |
| 320 | } |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 321 | blk = img_open_opts(filename, opts, flags, writethrough, quiet); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 322 | } else { |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 323 | blk = img_open_file(filename, fmt, flags, writethrough, quiet); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 324 | } |
| 325 | return blk; |
| 326 | } |
| 327 | |
| 328 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 329 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 330 | const char *base_filename, |
| 331 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 332 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 333 | Error *err = NULL; |
| 334 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 335 | if (base_filename) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 336 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 337 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 338 | error_report("Backing file not supported for file format '%s'", |
| 339 | fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 340 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 341 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | if (base_fmt) { |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 345 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 346 | if (err) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 347 | error_report("Backing file format not supported for file " |
| 348 | "format '%s'", fmt); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 349 | error_free(err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 350 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 351 | } |
| 352 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 353 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 354 | } |
| 355 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 356 | static int img_create(int argc, char **argv) |
| 357 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 358 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 359 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 360 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 361 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 362 | const char *filename; |
| 363 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 364 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 365 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 366 | bool quiet = false; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 367 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 368 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 369 | static const struct option long_options[] = { |
| 370 | {"help", no_argument, 0, 'h'}, |
| 371 | {"object", required_argument, 0, OPTION_OBJECT}, |
| 372 | {0, 0, 0, 0} |
| 373 | }; |
| 374 | c = getopt_long(argc, argv, "F:b:f:he6o:q", |
| 375 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 376 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 377 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 378 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 379 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 380 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 381 | case 'h': |
| 382 | help(); |
| 383 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 384 | case 'F': |
| 385 | base_fmt = optarg; |
| 386 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 387 | case 'b': |
| 388 | base_filename = optarg; |
| 389 | break; |
| 390 | case 'f': |
| 391 | fmt = optarg; |
| 392 | break; |
| 393 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 394 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 395 | "encryption\' instead!"); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 396 | goto fail; |
ths | d8871c5 | 2007-10-24 16:11:42 +0000 | [diff] [blame] | 397 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 398 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 399 | "compat6\' instead!"); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 400 | goto fail; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 401 | case 'o': |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 402 | if (!is_valid_option_list(optarg)) { |
| 403 | error_report("Invalid option list: %s", optarg); |
| 404 | goto fail; |
| 405 | } |
| 406 | if (!options) { |
| 407 | options = g_strdup(optarg); |
| 408 | } else { |
| 409 | char *old_options = options; |
| 410 | options = g_strdup_printf("%s,%s", options, optarg); |
| 411 | g_free(old_options); |
| 412 | } |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 413 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 414 | case 'q': |
| 415 | quiet = true; |
| 416 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 417 | case OPTION_OBJECT: { |
| 418 | QemuOpts *opts; |
| 419 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 420 | optarg, true); |
| 421 | if (!opts) { |
| 422 | goto fail; |
| 423 | } |
| 424 | } break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 427 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 428 | /* Get the filename */ |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 429 | filename = (optind < argc) ? argv[optind] : NULL; |
| 430 | if (options && has_help_option(options)) { |
| 431 | g_free(options); |
| 432 | return print_block_option_help(filename, fmt); |
| 433 | } |
| 434 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 435 | if (optind >= argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 436 | error_exit("Expecting image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 437 | } |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 438 | optind++; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 439 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 440 | if (qemu_opts_foreach(&qemu_object_opts, |
| 441 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 442 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 443 | goto fail; |
| 444 | } |
| 445 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 446 | /* Get image size, if specified */ |
| 447 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 448 | int64_t sval; |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 449 | char *end; |
Marc-André Lureau | 4677bb4 | 2015-09-16 18:02:56 +0200 | [diff] [blame] | 450 | sval = qemu_strtosz_suffix(argv[optind++], &end, |
| 451 | QEMU_STRTOSZ_DEFSUFFIX_B); |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 452 | if (sval < 0 || *end) { |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 453 | if (sval == -ERANGE) { |
| 454 | error_report("Image size must be less than 8 EiB!"); |
| 455 | } else { |
| 456 | error_report("Invalid image size specified! You may use k, M, " |
Kevin Wolf | 5e00984 | 2013-06-05 14:19:27 +0200 | [diff] [blame] | 457 | "G, T, P or E suffixes for "); |
| 458 | error_report("kilobytes, megabytes, gigabytes, terabytes, " |
| 459 | "petabytes and exabytes."); |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 460 | } |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 461 | goto fail; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 462 | } |
| 463 | img_size = (uint64_t)sval; |
| 464 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 465 | if (optind != argc) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 466 | error_exit("Unexpected argument: %s", argv[optind]); |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 467 | } |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 468 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 469 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
Kevin Wolf | 61de4c6 | 2016-03-18 17:46:45 +0100 | [diff] [blame] | 470 | options, img_size, 0, &local_err, quiet); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 471 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 472 | error_reportf_err(local_err, "%s: ", filename); |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 473 | goto fail; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 474 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 475 | |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 476 | g_free(options); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 477 | return 0; |
Kevin Wolf | 77386bf | 2014-02-21 16:24:04 +0100 | [diff] [blame] | 478 | |
| 479 | fail: |
| 480 | g_free(options); |
| 481 | return 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 484 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 485 | { |
Markus Armbruster | 4399c43 | 2014-04-25 16:50:32 +0200 | [diff] [blame] | 486 | Error *local_err = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 487 | QString *str; |
| 488 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 489 | QObject *obj; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 490 | visit_type_ImageCheck(qmp_output_get_visitor(ov), NULL, &check, |
| 491 | &local_err); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 492 | obj = qmp_output_get_qobject(ov); |
| 493 | str = qobject_to_json_pretty(obj); |
| 494 | assert(str != NULL); |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 495 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 496 | qobject_decref(obj); |
| 497 | qmp_output_visitor_cleanup(ov); |
| 498 | QDECREF(str); |
| 499 | } |
| 500 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 501 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 502 | { |
| 503 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 504 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 505 | } else { |
| 506 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 507 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 508 | "Data may be corrupted, or further writes to the image " |
| 509 | "may corrupt it.\n", |
| 510 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 514 | qprintf(quiet, |
| 515 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 516 | "This means waste of disk space, but no harm to data.\n", |
| 517 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 521 | qprintf(quiet, |
| 522 | "\n%" PRId64 |
| 523 | " internal errors have occurred during the check.\n", |
| 524 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 529 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 530 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 531 | check->allocated_clusters, check->total_clusters, |
| 532 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 533 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 534 | check->compressed_clusters * 100.0 / |
| 535 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 539 | qprintf(quiet, |
| 540 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | static int collect_image_check(BlockDriverState *bs, |
| 545 | ImageCheck *check, |
| 546 | const char *filename, |
| 547 | const char *fmt, |
| 548 | int fix) |
| 549 | { |
| 550 | int ret; |
| 551 | BdrvCheckResult result; |
| 552 | |
| 553 | ret = bdrv_check(bs, &result, fix); |
| 554 | if (ret < 0) { |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | check->filename = g_strdup(filename); |
| 559 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 560 | check->check_errors = result.check_errors; |
| 561 | check->corruptions = result.corruptions; |
| 562 | check->has_corruptions = result.corruptions != 0; |
| 563 | check->leaks = result.leaks; |
| 564 | check->has_leaks = result.leaks != 0; |
| 565 | check->corruptions_fixed = result.corruptions_fixed; |
| 566 | check->has_corruptions_fixed = result.corruptions != 0; |
| 567 | check->leaks_fixed = result.leaks_fixed; |
| 568 | check->has_leaks_fixed = result.leaks != 0; |
| 569 | check->image_end_offset = result.image_end_offset; |
| 570 | check->has_image_end_offset = result.image_end_offset != 0; |
| 571 | check->total_clusters = result.bfi.total_clusters; |
| 572 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 573 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 574 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 575 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 576 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 577 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 578 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 583 | /* |
| 584 | * Checks an image for consistency. Exit codes: |
| 585 | * |
Max Reitz | d6635c4 | 2014-06-02 22:15:21 +0200 | [diff] [blame] | 586 | * 0 - Check completed, image is good |
| 587 | * 1 - Check not completed because of internal errors |
| 588 | * 2 - Check completed, image is corrupted |
| 589 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 590 | * 63 - Checks are not supported by the image format |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 591 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 592 | static int img_check(int argc, char **argv) |
| 593 | { |
| 594 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 595 | OutputFormat output_format = OFORMAT_HUMAN; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 596 | const char *filename, *fmt, *output, *cache; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 597 | BlockBackend *blk; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 598 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 599 | int fix = 0; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 600 | int flags = BDRV_O_CHECK; |
| 601 | bool writethrough; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 602 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 603 | bool quiet = false; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 604 | bool image_opts = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 605 | |
| 606 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 607 | output = NULL; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 608 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 609 | |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 610 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 611 | int option_index = 0; |
| 612 | static const struct option long_options[] = { |
| 613 | {"help", no_argument, 0, 'h'}, |
| 614 | {"format", required_argument, 0, 'f'}, |
Prasad Joshi | 4fd6a98 | 2014-03-25 00:08:54 +0530 | [diff] [blame] | 615 | {"repair", required_argument, 0, 'r'}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 616 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 617 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 618 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 619 | {0, 0, 0, 0} |
| 620 | }; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 621 | c = getopt_long(argc, argv, "hf:r:T:q", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 622 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 623 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 624 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 625 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 626 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 627 | case '?': |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 628 | case 'h': |
| 629 | help(); |
| 630 | break; |
| 631 | case 'f': |
| 632 | fmt = optarg; |
| 633 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 634 | case 'r': |
| 635 | flags |= BDRV_O_RDWR; |
| 636 | |
| 637 | if (!strcmp(optarg, "leaks")) { |
| 638 | fix = BDRV_FIX_LEAKS; |
| 639 | } else if (!strcmp(optarg, "all")) { |
| 640 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 641 | } else { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 642 | error_exit("Unknown option value for -r " |
| 643 | "(expecting 'leaks' or 'all'): %s", optarg); |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 644 | } |
| 645 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 646 | case OPTION_OUTPUT: |
| 647 | output = optarg; |
| 648 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 649 | case 'T': |
| 650 | cache = optarg; |
| 651 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 652 | case 'q': |
| 653 | quiet = true; |
| 654 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 655 | case OPTION_OBJECT: { |
| 656 | QemuOpts *opts; |
| 657 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 658 | optarg, true); |
| 659 | if (!opts) { |
| 660 | return 1; |
| 661 | } |
| 662 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 663 | case OPTION_IMAGE_OPTS: |
| 664 | image_opts = true; |
| 665 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 666 | } |
| 667 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 668 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 669 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 670 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 671 | filename = argv[optind++]; |
| 672 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 673 | if (output && !strcmp(output, "json")) { |
| 674 | output_format = OFORMAT_JSON; |
| 675 | } else if (output && !strcmp(output, "human")) { |
| 676 | output_format = OFORMAT_HUMAN; |
| 677 | } else if (output) { |
| 678 | error_report("--output must be used with human or json as argument."); |
| 679 | return 1; |
| 680 | } |
| 681 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 682 | if (qemu_opts_foreach(&qemu_object_opts, |
| 683 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 684 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 685 | return 1; |
| 686 | } |
| 687 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 688 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 689 | if (ret < 0) { |
| 690 | error_report("Invalid source cache option: %s", cache); |
| 691 | return 1; |
| 692 | } |
| 693 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 694 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 695 | if (!blk) { |
| 696 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 697 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 698 | bs = blk_bs(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 699 | |
| 700 | check = g_new0(ImageCheck, 1); |
| 701 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 702 | |
| 703 | if (ret == -ENOTSUP) { |
Max Reitz | 55d492d | 2014-05-31 21:33:30 +0200 | [diff] [blame] | 704 | error_report("This image format does not support checks"); |
Peter Lieven | fefddf9 | 2013-10-24 08:53:34 +0200 | [diff] [blame] | 705 | ret = 63; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 706 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 707 | } |
| 708 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 709 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 710 | int corruptions_fixed, leaks_fixed; |
| 711 | |
| 712 | leaks_fixed = check->leaks_fixed; |
| 713 | corruptions_fixed = check->corruptions_fixed; |
| 714 | |
| 715 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 716 | qprintf(quiet, |
| 717 | "The following inconsistencies were found and repaired:\n\n" |
| 718 | " %" PRId64 " leaked clusters\n" |
| 719 | " %" PRId64 " corruptions\n\n" |
| 720 | "Double checking the fixed image now...\n", |
| 721 | check->leaks_fixed, |
| 722 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 726 | |
| 727 | check->leaks_fixed = leaks_fixed; |
| 728 | check->corruptions_fixed = corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 729 | } |
| 730 | |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 731 | if (!ret) { |
| 732 | switch (output_format) { |
| 733 | case OFORMAT_HUMAN: |
| 734 | dump_human_image_check(check, quiet); |
| 735 | break; |
| 736 | case OFORMAT_JSON: |
| 737 | dump_json_image_check(check, quiet); |
| 738 | break; |
| 739 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | if (ret || check->check_errors) { |
Max Reitz | 832390a | 2014-10-23 15:29:12 +0200 | [diff] [blame] | 743 | if (ret) { |
| 744 | error_report("Check failed: %s", strerror(-ret)); |
| 745 | } else { |
| 746 | error_report("Check failed"); |
| 747 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 748 | ret = 1; |
| 749 | goto fail; |
| 750 | } |
| 751 | |
| 752 | if (check->corruptions) { |
| 753 | ret = 2; |
| 754 | } else if (check->leaks) { |
| 755 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 756 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 757 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 760 | fail: |
| 761 | qapi_free_ImageCheck(check); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 762 | blk_unref(blk); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 763 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 766 | typedef struct CommonBlockJobCBInfo { |
| 767 | BlockDriverState *bs; |
| 768 | Error **errp; |
| 769 | } CommonBlockJobCBInfo; |
| 770 | |
| 771 | static void common_block_job_cb(void *opaque, int ret) |
| 772 | { |
| 773 | CommonBlockJobCBInfo *cbi = opaque; |
| 774 | |
| 775 | if (ret < 0) { |
| 776 | error_setg_errno(cbi->errp, -ret, "Block job failed"); |
| 777 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static void run_block_job(BlockJob *job, Error **errp) |
| 781 | { |
Kevin Wolf | b75536c | 2016-04-18 17:30:17 +0200 | [diff] [blame] | 782 | AioContext *aio_context = blk_get_aio_context(job->blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 783 | |
| 784 | do { |
| 785 | aio_poll(aio_context, true); |
John Snow | 62547b8 | 2015-11-02 18:28:20 -0500 | [diff] [blame] | 786 | qemu_progress_print(job->len ? |
| 787 | ((float)job->offset / job->len * 100.f) : 0.0f, 0); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 788 | } while (!job->ready); |
| 789 | |
| 790 | block_job_complete_sync(job, errp); |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 791 | |
| 792 | /* A block job may finish instantaneously without publishing any progress, |
| 793 | * so just signal completion here */ |
| 794 | qemu_progress_print(100.f, 0); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 795 | } |
| 796 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 797 | static int img_commit(int argc, char **argv) |
| 798 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 799 | int c, ret, flags; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 800 | const char *filename, *fmt, *cache, *base; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 801 | BlockBackend *blk; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 802 | BlockDriverState *bs, *base_bs; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 803 | bool progress = false, quiet = false, drop = false; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 804 | bool writethrough; |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 805 | Error *local_err = NULL; |
| 806 | CommonBlockJobCBInfo cbi; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 807 | bool image_opts = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 808 | |
| 809 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 810 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 811 | base = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 812 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 813 | static const struct option long_options[] = { |
| 814 | {"help", no_argument, 0, 'h'}, |
| 815 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 816 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 817 | {0, 0, 0, 0} |
| 818 | }; |
| 819 | c = getopt_long(argc, argv, "f:ht:b:dpq", |
| 820 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 821 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 822 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 823 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 824 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 825 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 826 | case 'h': |
| 827 | help(); |
| 828 | break; |
| 829 | case 'f': |
| 830 | fmt = optarg; |
| 831 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 832 | case 't': |
| 833 | cache = optarg; |
| 834 | break; |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 835 | case 'b': |
| 836 | base = optarg; |
| 837 | /* -b implies -d */ |
| 838 | drop = true; |
| 839 | break; |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 840 | case 'd': |
| 841 | drop = true; |
| 842 | break; |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 843 | case 'p': |
| 844 | progress = true; |
| 845 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 846 | case 'q': |
| 847 | quiet = true; |
| 848 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 849 | case OPTION_OBJECT: { |
| 850 | QemuOpts *opts; |
| 851 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 852 | optarg, true); |
| 853 | if (!opts) { |
| 854 | return 1; |
| 855 | } |
| 856 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 857 | case OPTION_IMAGE_OPTS: |
| 858 | image_opts = true; |
| 859 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 860 | } |
| 861 | } |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 862 | |
| 863 | /* Progress is not shown in Quiet mode */ |
| 864 | if (quiet) { |
| 865 | progress = false; |
| 866 | } |
| 867 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 868 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 869 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 870 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 871 | filename = argv[optind++]; |
| 872 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 873 | if (qemu_opts_foreach(&qemu_object_opts, |
| 874 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 875 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 876 | return 1; |
| 877 | } |
| 878 | |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 879 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 880 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 881 | if (ret < 0) { |
| 882 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | a3981eb | 2014-08-26 19:17:54 +0100 | [diff] [blame] | 883 | return 1; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 884 | } |
| 885 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 886 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 887 | if (!blk) { |
| 888 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 889 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 890 | bs = blk_bs(blk); |
| 891 | |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 892 | qemu_progress_init(progress, 1.f); |
| 893 | qemu_progress_print(0.f, 100); |
| 894 | |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 895 | if (base) { |
| 896 | base_bs = bdrv_find_backing_image(bs, base); |
| 897 | if (!base_bs) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 898 | error_setg(&local_err, QERR_BASE_NOT_FOUND, base); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 899 | goto done; |
| 900 | } |
| 901 | } else { |
| 902 | /* This is different from QMP, which by default uses the deepest file in |
| 903 | * the backing chain (i.e., the very base); however, the traditional |
| 904 | * behavior of qemu-img commit is using the immediate backing file. */ |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 905 | base_bs = backing_bs(bs); |
Max Reitz | 1b22bff | 2014-10-24 15:57:40 +0200 | [diff] [blame] | 906 | if (!base_bs) { |
| 907 | error_setg(&local_err, "Image does not have a backing file"); |
| 908 | goto done; |
| 909 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 912 | cbi = (CommonBlockJobCBInfo){ |
| 913 | .errp = &local_err, |
| 914 | .bs = bs, |
| 915 | }; |
| 916 | |
| 917 | commit_active_start(bs, base_bs, 0, BLOCKDEV_ON_ERROR_REPORT, |
| 918 | common_block_job_cb, &cbi, &local_err); |
| 919 | if (local_err) { |
| 920 | goto done; |
| 921 | } |
| 922 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 923 | /* When the block job completes, the BlockBackend reference will point to |
| 924 | * the old backing file. In order to avoid that the top image is already |
| 925 | * deleted, so we can still empty it afterwards, increment the reference |
| 926 | * counter here preemptively. */ |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 927 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 928 | bdrv_ref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 931 | run_block_job(bs->job, &local_err); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 932 | if (local_err) { |
| 933 | goto unref_backing; |
| 934 | } |
| 935 | |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 936 | if (!drop && bs->drv->bdrv_make_empty) { |
| 937 | ret = bs->drv->bdrv_make_empty(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 938 | if (ret) { |
| 939 | error_setg_errno(&local_err, -ret, "Could not empty %s", |
| 940 | filename); |
| 941 | goto unref_backing; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | unref_backing: |
| 946 | if (!drop) { |
Kevin Wolf | 3f09bfb | 2015-09-15 11:58:23 +0200 | [diff] [blame] | 947 | bdrv_unref(bs); |
Max Reitz | 9a86fe4 | 2014-10-24 15:57:38 +0200 | [diff] [blame] | 948 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 949 | |
| 950 | done: |
Max Reitz | 687fa1d | 2014-10-24 15:57:39 +0200 | [diff] [blame] | 951 | qemu_progress_end(); |
| 952 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 953 | blk_unref(blk); |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 954 | |
| 955 | if (local_err) { |
Markus Armbruster | 6936f29 | 2015-02-10 15:14:02 +0100 | [diff] [blame] | 956 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 957 | return 1; |
| 958 | } |
Max Reitz | d4a3238 | 2014-10-24 15:57:37 +0200 | [diff] [blame] | 959 | |
| 960 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 961 | return 0; |
| 962 | } |
| 963 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 964 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 965 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 966 | * a non-NUL byte. |
| 967 | * |
| 968 | * 'pnum' is set to the number of sectors (including and immediately following |
| 969 | * the first one) that are known to be in the same allocated/unallocated state. |
| 970 | */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 971 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
| 972 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 973 | bool is_zero; |
| 974 | int i; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 975 | |
| 976 | if (n <= 0) { |
| 977 | *pnum = 0; |
| 978 | return 0; |
| 979 | } |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 980 | is_zero = buffer_is_zero(buf, 512); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 981 | for(i = 1; i < n; i++) { |
| 982 | buf += 512; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 983 | if (is_zero != buffer_is_zero(buf, 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 984 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 985 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 986 | } |
| 987 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 988 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 991 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 992 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 993 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 994 | * breaking up write requests for only small sparse areas. |
| 995 | */ |
| 996 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, |
| 997 | int min) |
| 998 | { |
| 999 | int ret; |
| 1000 | int num_checked, num_used; |
| 1001 | |
| 1002 | if (n < min) { |
| 1003 | min = n; |
| 1004 | } |
| 1005 | |
| 1006 | ret = is_allocated_sectors(buf, n, pnum); |
| 1007 | if (!ret) { |
| 1008 | return ret; |
| 1009 | } |
| 1010 | |
| 1011 | num_used = *pnum; |
| 1012 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1013 | n -= *pnum; |
| 1014 | num_checked = num_used; |
| 1015 | |
| 1016 | while (n > 0) { |
| 1017 | ret = is_allocated_sectors(buf, n, pnum); |
| 1018 | |
| 1019 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 1020 | n -= *pnum; |
| 1021 | num_checked += *pnum; |
| 1022 | if (ret) { |
| 1023 | num_used = num_checked; |
| 1024 | } else if (*pnum >= min) { |
| 1025 | break; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | *pnum = num_used; |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1034 | * Compares two buffers sector by sector. Returns 0 if the first sector of both |
| 1035 | * buffers matches, non-zero otherwise. |
| 1036 | * |
| 1037 | * pnum is set to the number of sectors (including and immediately following |
| 1038 | * the first one) that are known to have the same comparison result |
| 1039 | */ |
| 1040 | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, |
| 1041 | int *pnum) |
| 1042 | { |
Radim Krčmář | 8c1ac47 | 2015-02-20 17:06:15 +0100 | [diff] [blame] | 1043 | bool res; |
| 1044 | int i; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1045 | |
| 1046 | if (n <= 0) { |
| 1047 | *pnum = 0; |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | res = !!memcmp(buf1, buf2, 512); |
| 1052 | for(i = 1; i < n; i++) { |
| 1053 | buf1 += 512; |
| 1054 | buf2 += 512; |
| 1055 | |
| 1056 | if (!!memcmp(buf1, buf2, 512) != res) { |
| 1057 | break; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | *pnum = i; |
| 1062 | return res; |
| 1063 | } |
| 1064 | |
Kevin Wolf | 80ee15a | 2009-09-15 12:30:43 +0200 | [diff] [blame] | 1065 | #define IO_BUF_SIZE (2 * 1024 * 1024) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1066 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1067 | static int64_t sectors_to_bytes(int64_t sectors) |
| 1068 | { |
| 1069 | return sectors << BDRV_SECTOR_BITS; |
| 1070 | } |
| 1071 | |
| 1072 | static int64_t sectors_to_process(int64_t total, int64_t from) |
| 1073 | { |
| 1074 | return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS); |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 1079 | * |
| 1080 | * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero |
| 1081 | * data and negative value on error. |
| 1082 | * |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1083 | * @param blk: BlockBackend for the image |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1084 | * @param sect_num: Number of first sector to check |
| 1085 | * @param sect_count: Number of sectors to check |
| 1086 | * @param filename: Name of disk file we are checking (logging purpose) |
| 1087 | * @param buffer: Allocated buffer for storing read data |
| 1088 | * @param quiet: Flag for quiet mode |
| 1089 | */ |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1090 | static int check_empty_sectors(BlockBackend *blk, int64_t sect_num, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1091 | int sect_count, const char *filename, |
| 1092 | uint8_t *buffer, bool quiet) |
| 1093 | { |
| 1094 | int pnum, ret = 0; |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 1095 | ret = blk_pread(blk, sect_num << BDRV_SECTOR_BITS, buffer, |
| 1096 | sect_count << BDRV_SECTOR_BITS); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1097 | if (ret < 0) { |
| 1098 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
| 1099 | sectors_to_bytes(sect_num), filename, strerror(-ret)); |
| 1100 | return ret; |
| 1101 | } |
| 1102 | ret = is_allocated_sectors(buffer, sect_count, &pnum); |
| 1103 | if (ret || pnum != sect_count) { |
| 1104 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
| 1105 | sectors_to_bytes(ret ? sect_num : sect_num + pnum)); |
| 1106 | return 1; |
| 1107 | } |
| 1108 | |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * Compares two images. Exit codes: |
| 1114 | * |
| 1115 | * 0 - Images are identical |
| 1116 | * 1 - Images differ |
| 1117 | * >1 - Error occurred |
| 1118 | */ |
| 1119 | static int img_compare(int argc, char **argv) |
| 1120 | { |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1121 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1122 | BlockBackend *blk1, *blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1123 | BlockDriverState *bs1, *bs2; |
| 1124 | int64_t total_sectors1, total_sectors2; |
| 1125 | uint8_t *buf1 = NULL, *buf2 = NULL; |
| 1126 | int pnum1, pnum2; |
| 1127 | int allocated1, allocated2; |
| 1128 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 1129 | bool progress = false, quiet = false, strict = false; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1130 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1131 | bool writethrough; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1132 | int64_t total_sectors; |
| 1133 | int64_t sector_num = 0; |
| 1134 | int64_t nb_sectors; |
| 1135 | int c, pnum; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1136 | uint64_t progress_base; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1137 | bool image_opts = false; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1138 | |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1139 | cache = BDRV_DEFAULT_CACHE; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1140 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1141 | static const struct option long_options[] = { |
| 1142 | {"help", no_argument, 0, 'h'}, |
| 1143 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1144 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1145 | {0, 0, 0, 0} |
| 1146 | }; |
| 1147 | c = getopt_long(argc, argv, "hf:F:T:pqs", |
| 1148 | long_options, NULL); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1149 | if (c == -1) { |
| 1150 | break; |
| 1151 | } |
| 1152 | switch (c) { |
| 1153 | case '?': |
| 1154 | case 'h': |
| 1155 | help(); |
| 1156 | break; |
| 1157 | case 'f': |
| 1158 | fmt1 = optarg; |
| 1159 | break; |
| 1160 | case 'F': |
| 1161 | fmt2 = optarg; |
| 1162 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1163 | case 'T': |
| 1164 | cache = optarg; |
| 1165 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1166 | case 'p': |
| 1167 | progress = true; |
| 1168 | break; |
| 1169 | case 'q': |
| 1170 | quiet = true; |
| 1171 | break; |
| 1172 | case 's': |
| 1173 | strict = true; |
| 1174 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1175 | case OPTION_OBJECT: { |
| 1176 | QemuOpts *opts; |
| 1177 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1178 | optarg, true); |
| 1179 | if (!opts) { |
| 1180 | ret = 2; |
| 1181 | goto out4; |
| 1182 | } |
| 1183 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1184 | case OPTION_IMAGE_OPTS: |
| 1185 | image_opts = true; |
| 1186 | break; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | /* Progress is not shown in Quiet mode */ |
| 1191 | if (quiet) { |
| 1192 | progress = false; |
| 1193 | } |
| 1194 | |
| 1195 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 1196 | if (optind != argc - 2) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1197 | error_exit("Expecting two image file names"); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1198 | } |
| 1199 | filename1 = argv[optind++]; |
| 1200 | filename2 = argv[optind++]; |
| 1201 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1202 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1203 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 1204 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1205 | ret = 2; |
| 1206 | goto out4; |
| 1207 | } |
| 1208 | |
Stefan Hajnoczi | cbda016 | 2014-08-26 19:17:55 +0100 | [diff] [blame] | 1209 | /* Initialize before goto out */ |
| 1210 | qemu_progress_init(progress, 2.0); |
| 1211 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1212 | flags = 0; |
| 1213 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1214 | if (ret < 0) { |
| 1215 | error_report("Invalid source cache option: %s", cache); |
| 1216 | ret = 2; |
| 1217 | goto out3; |
| 1218 | } |
| 1219 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1220 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1221 | if (!blk1) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1222 | ret = 2; |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1223 | goto out3; |
| 1224 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1225 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1226 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1227 | if (!blk2) { |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1228 | ret = 2; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1229 | goto out2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1230 | } |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1231 | bs1 = blk_bs(blk1); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1232 | bs2 = blk_bs(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1233 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1234 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
| 1235 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); |
| 1236 | total_sectors1 = blk_nb_sectors(blk1); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1237 | if (total_sectors1 < 0) { |
| 1238 | error_report("Can't get size of %s: %s", |
| 1239 | filename1, strerror(-total_sectors1)); |
| 1240 | ret = 4; |
| 1241 | goto out; |
| 1242 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1243 | total_sectors2 = blk_nb_sectors(blk2); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1244 | if (total_sectors2 < 0) { |
| 1245 | error_report("Can't get size of %s: %s", |
| 1246 | filename2, strerror(-total_sectors2)); |
| 1247 | ret = 4; |
| 1248 | goto out; |
| 1249 | } |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1250 | total_sectors = MIN(total_sectors1, total_sectors2); |
| 1251 | progress_base = MAX(total_sectors1, total_sectors2); |
| 1252 | |
| 1253 | qemu_progress_print(0, 100); |
| 1254 | |
| 1255 | if (strict && total_sectors1 != total_sectors2) { |
| 1256 | ret = 1; |
| 1257 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 1258 | goto out; |
| 1259 | } |
| 1260 | |
| 1261 | for (;;) { |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1262 | int64_t status1, status2; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1263 | BlockDriverState *file; |
| 1264 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1265 | nb_sectors = sectors_to_process(total_sectors, sector_num); |
| 1266 | if (nb_sectors <= 0) { |
| 1267 | break; |
| 1268 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1269 | status1 = bdrv_get_block_status_above(bs1, NULL, sector_num, |
| 1270 | total_sectors1 - sector_num, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1271 | &pnum1, &file); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1272 | if (status1 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1273 | ret = 3; |
| 1274 | error_report("Sector allocation test failed for %s", filename1); |
| 1275 | goto out; |
| 1276 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1277 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1278 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1279 | status2 = bdrv_get_block_status_above(bs2, NULL, sector_num, |
| 1280 | total_sectors2 - sector_num, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1281 | &pnum2, &file); |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1282 | if (status2 < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1283 | ret = 3; |
| 1284 | error_report("Sector allocation test failed for %s", filename2); |
| 1285 | goto out; |
| 1286 | } |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1287 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
| 1288 | if (pnum1) { |
| 1289 | nb_sectors = MIN(nb_sectors, pnum1); |
| 1290 | } |
| 1291 | if (pnum2) { |
| 1292 | nb_sectors = MIN(nb_sectors, pnum2); |
| 1293 | } |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1294 | |
Fam Zheng | 25ad8e6 | 2016-01-13 16:37:41 +0800 | [diff] [blame] | 1295 | if (strict) { |
| 1296 | if ((status1 & ~BDRV_BLOCK_OFFSET_MASK) != |
| 1297 | (status2 & ~BDRV_BLOCK_OFFSET_MASK)) { |
| 1298 | ret = 1; |
| 1299 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
| 1300 | " block status mismatch!\n", |
| 1301 | sectors_to_bytes(sector_num)); |
| 1302 | goto out; |
| 1303 | } |
| 1304 | } |
| 1305 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { |
| 1306 | nb_sectors = MIN(pnum1, pnum2); |
| 1307 | } else if (allocated1 == allocated2) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1308 | if (allocated1) { |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 1309 | ret = blk_pread(blk1, sector_num << BDRV_SECTOR_BITS, buf1, |
| 1310 | nb_sectors << BDRV_SECTOR_BITS); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1311 | if (ret < 0) { |
| 1312 | error_report("Error while reading offset %" PRId64 " of %s:" |
| 1313 | " %s", sectors_to_bytes(sector_num), filename1, |
| 1314 | strerror(-ret)); |
| 1315 | ret = 4; |
| 1316 | goto out; |
| 1317 | } |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 1318 | ret = blk_pread(blk2, sector_num << BDRV_SECTOR_BITS, buf2, |
| 1319 | nb_sectors << BDRV_SECTOR_BITS); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1320 | if (ret < 0) { |
| 1321 | error_report("Error while reading offset %" PRId64 |
| 1322 | " of %s: %s", sectors_to_bytes(sector_num), |
| 1323 | filename2, strerror(-ret)); |
| 1324 | ret = 4; |
| 1325 | goto out; |
| 1326 | } |
| 1327 | ret = compare_sectors(buf1, buf2, nb_sectors, &pnum); |
| 1328 | if (ret || pnum != nb_sectors) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1329 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
| 1330 | sectors_to_bytes( |
| 1331 | ret ? sector_num : sector_num + pnum)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1332 | ret = 1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1333 | goto out; |
| 1334 | } |
| 1335 | } |
| 1336 | } else { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1337 | |
| 1338 | if (allocated1) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1339 | ret = check_empty_sectors(blk1, sector_num, nb_sectors, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1340 | filename1, buf1, quiet); |
| 1341 | } else { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1342 | ret = check_empty_sectors(blk2, sector_num, nb_sectors, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1343 | filename2, buf1, quiet); |
| 1344 | } |
| 1345 | if (ret) { |
| 1346 | if (ret < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1347 | error_report("Error while reading offset %" PRId64 ": %s", |
| 1348 | sectors_to_bytes(sector_num), strerror(-ret)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1349 | ret = 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1350 | } |
| 1351 | goto out; |
| 1352 | } |
| 1353 | } |
| 1354 | sector_num += nb_sectors; |
| 1355 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); |
| 1356 | } |
| 1357 | |
| 1358 | if (total_sectors1 != total_sectors2) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1359 | BlockBackend *blk_over; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1360 | int64_t total_sectors_over; |
| 1361 | const char *filename_over; |
| 1362 | |
| 1363 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
| 1364 | if (total_sectors1 > total_sectors2) { |
| 1365 | total_sectors_over = total_sectors1; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1366 | blk_over = blk1; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1367 | filename_over = filename1; |
| 1368 | } else { |
| 1369 | total_sectors_over = total_sectors2; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1370 | blk_over = blk2; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1371 | filename_over = filename2; |
| 1372 | } |
| 1373 | |
| 1374 | for (;;) { |
| 1375 | nb_sectors = sectors_to_process(total_sectors_over, sector_num); |
| 1376 | if (nb_sectors <= 0) { |
| 1377 | break; |
| 1378 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1379 | ret = bdrv_is_allocated_above(blk_bs(blk_over), NULL, sector_num, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1380 | nb_sectors, &pnum); |
| 1381 | if (ret < 0) { |
| 1382 | ret = 3; |
| 1383 | error_report("Sector allocation test failed for %s", |
| 1384 | filename_over); |
| 1385 | goto out; |
| 1386 | |
| 1387 | } |
| 1388 | nb_sectors = pnum; |
| 1389 | if (ret) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1390 | ret = check_empty_sectors(blk_over, sector_num, nb_sectors, |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1391 | filename_over, buf1, quiet); |
| 1392 | if (ret) { |
| 1393 | if (ret < 0) { |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1394 | error_report("Error while reading offset %" PRId64 |
| 1395 | " of %s: %s", sectors_to_bytes(sector_num), |
| 1396 | filename_over, strerror(-ret)); |
Fam Zheng | 36452f1 | 2013-11-13 20:26:49 +0800 | [diff] [blame] | 1397 | ret = 4; |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1398 | } |
| 1399 | goto out; |
| 1400 | } |
| 1401 | } |
| 1402 | sector_num += nb_sectors; |
| 1403 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | qprintf(quiet, "Images are identical.\n"); |
| 1408 | ret = 0; |
| 1409 | |
| 1410 | out: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1411 | qemu_vfree(buf1); |
| 1412 | qemu_vfree(buf2); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1413 | blk_unref(blk2); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1414 | out2: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1415 | blk_unref(blk1); |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1416 | out3: |
| 1417 | qemu_progress_end(); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1418 | out4: |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 1419 | return ret; |
| 1420 | } |
| 1421 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1422 | enum ImgConvertBlockStatus { |
| 1423 | BLK_DATA, |
| 1424 | BLK_ZERO, |
| 1425 | BLK_BACKING_FILE, |
| 1426 | }; |
| 1427 | |
| 1428 | typedef struct ImgConvertState { |
| 1429 | BlockBackend **src; |
| 1430 | int64_t *src_sectors; |
| 1431 | int src_cur, src_num; |
| 1432 | int64_t src_cur_offset; |
| 1433 | int64_t total_sectors; |
| 1434 | int64_t allocated_sectors; |
| 1435 | enum ImgConvertBlockStatus status; |
| 1436 | int64_t sector_next_status; |
| 1437 | BlockBackend *target; |
| 1438 | bool has_zero_init; |
| 1439 | bool compressed; |
| 1440 | bool target_has_backing; |
| 1441 | int min_sparse; |
| 1442 | size_t cluster_sectors; |
| 1443 | size_t buf_sectors; |
| 1444 | } ImgConvertState; |
| 1445 | |
| 1446 | static void convert_select_part(ImgConvertState *s, int64_t sector_num) |
| 1447 | { |
| 1448 | assert(sector_num >= s->src_cur_offset); |
| 1449 | while (sector_num - s->src_cur_offset >= s->src_sectors[s->src_cur]) { |
| 1450 | s->src_cur_offset += s->src_sectors[s->src_cur]; |
| 1451 | s->src_cur++; |
| 1452 | assert(s->src_cur < s->src_num); |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) |
| 1457 | { |
| 1458 | int64_t ret; |
| 1459 | int n; |
| 1460 | |
| 1461 | convert_select_part(s, sector_num); |
| 1462 | |
| 1463 | assert(s->total_sectors > sector_num); |
| 1464 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); |
| 1465 | |
| 1466 | if (s->sector_next_status <= sector_num) { |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1467 | BlockDriverState *file; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1468 | ret = bdrv_get_block_status(blk_bs(s->src[s->src_cur]), |
| 1469 | sector_num - s->src_cur_offset, |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 1470 | n, &n, &file); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1471 | if (ret < 0) { |
| 1472 | return ret; |
| 1473 | } |
| 1474 | |
| 1475 | if (ret & BDRV_BLOCK_ZERO) { |
| 1476 | s->status = BLK_ZERO; |
| 1477 | } else if (ret & BDRV_BLOCK_DATA) { |
| 1478 | s->status = BLK_DATA; |
| 1479 | } else if (!s->target_has_backing) { |
| 1480 | /* Without a target backing file we must copy over the contents of |
| 1481 | * the backing file as well. */ |
Ren Kimura | 263a6f4 | 2016-04-28 01:04:58 +0900 | [diff] [blame] | 1482 | /* Check block status of the backing file chain to avoid |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1483 | * needlessly reading zeroes and limiting the iteration to the |
| 1484 | * buffer size */ |
Ren Kimura | 263a6f4 | 2016-04-28 01:04:58 +0900 | [diff] [blame] | 1485 | ret = bdrv_get_block_status_above(blk_bs(s->src[s->src_cur]), NULL, |
| 1486 | sector_num - s->src_cur_offset, |
| 1487 | n, &n, &file); |
| 1488 | if (ret < 0) { |
| 1489 | return ret; |
| 1490 | } |
| 1491 | |
| 1492 | if (ret & BDRV_BLOCK_ZERO) { |
| 1493 | s->status = BLK_ZERO; |
| 1494 | } else { |
| 1495 | s->status = BLK_DATA; |
| 1496 | } |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1497 | } else { |
| 1498 | s->status = BLK_BACKING_FILE; |
| 1499 | } |
| 1500 | |
| 1501 | s->sector_next_status = sector_num + n; |
| 1502 | } |
| 1503 | |
| 1504 | n = MIN(n, s->sector_next_status - sector_num); |
| 1505 | if (s->status == BLK_DATA) { |
| 1506 | n = MIN(n, s->buf_sectors); |
| 1507 | } |
| 1508 | |
| 1509 | /* We need to write complete clusters for compressed images, so if an |
| 1510 | * unallocated area is shorter than that, we must consider the whole |
| 1511 | * cluster allocated. */ |
| 1512 | if (s->compressed) { |
| 1513 | if (n < s->cluster_sectors) { |
| 1514 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); |
| 1515 | s->status = BLK_DATA; |
| 1516 | } else { |
| 1517 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | return n; |
| 1522 | } |
| 1523 | |
| 1524 | static int convert_read(ImgConvertState *s, int64_t sector_num, int nb_sectors, |
| 1525 | uint8_t *buf) |
| 1526 | { |
| 1527 | int n; |
| 1528 | int ret; |
| 1529 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1530 | assert(nb_sectors <= s->buf_sectors); |
| 1531 | while (nb_sectors > 0) { |
| 1532 | BlockBackend *blk; |
| 1533 | int64_t bs_sectors; |
| 1534 | |
| 1535 | /* In the case of compression with multiple source files, we can get a |
| 1536 | * nb_sectors that spreads into the next part. So we must be able to |
| 1537 | * read across multiple BDSes for one convert_read() call. */ |
| 1538 | convert_select_part(s, sector_num); |
| 1539 | blk = s->src[s->src_cur]; |
| 1540 | bs_sectors = s->src_sectors[s->src_cur]; |
| 1541 | |
| 1542 | n = MIN(nb_sectors, bs_sectors - (sector_num - s->src_cur_offset)); |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 1543 | ret = blk_pread(blk, |
| 1544 | (sector_num - s->src_cur_offset) << BDRV_SECTOR_BITS, |
| 1545 | buf, n << BDRV_SECTOR_BITS); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1546 | if (ret < 0) { |
| 1547 | return ret; |
| 1548 | } |
| 1549 | |
| 1550 | sector_num += n; |
| 1551 | nb_sectors -= n; |
| 1552 | buf += n * BDRV_SECTOR_SIZE; |
| 1553 | } |
| 1554 | |
| 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | static int convert_write(ImgConvertState *s, int64_t sector_num, int nb_sectors, |
| 1559 | const uint8_t *buf) |
| 1560 | { |
| 1561 | int ret; |
| 1562 | |
| 1563 | while (nb_sectors > 0) { |
| 1564 | int n = nb_sectors; |
| 1565 | |
| 1566 | switch (s->status) { |
| 1567 | case BLK_BACKING_FILE: |
| 1568 | /* If we have a backing file, leave clusters unallocated that are |
| 1569 | * unallocated in the source image, so that the backing file is |
| 1570 | * visible at the respective offset. */ |
| 1571 | assert(s->target_has_backing); |
| 1572 | break; |
| 1573 | |
| 1574 | case BLK_DATA: |
| 1575 | /* We must always write compressed clusters as a whole, so don't |
| 1576 | * try to find zeroed parts in the buffer. We can only save the |
| 1577 | * write if the buffer is completely zeroed and we're allowed to |
| 1578 | * keep the target sparse. */ |
| 1579 | if (s->compressed) { |
| 1580 | if (s->has_zero_init && s->min_sparse && |
| 1581 | buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) |
| 1582 | { |
| 1583 | assert(!s->target_has_backing); |
| 1584 | break; |
| 1585 | } |
| 1586 | |
| 1587 | ret = blk_write_compressed(s->target, sector_num, buf, n); |
| 1588 | if (ret < 0) { |
| 1589 | return ret; |
| 1590 | } |
| 1591 | break; |
| 1592 | } |
| 1593 | |
| 1594 | /* If there is real non-zero data or we're told to keep the target |
| 1595 | * fully allocated (-S 0), we must write it. Otherwise we can treat |
| 1596 | * it as zero sectors. */ |
| 1597 | if (!s->min_sparse || |
| 1598 | is_allocated_sectors_min(buf, n, &n, s->min_sparse)) |
| 1599 | { |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 1600 | ret = blk_pwrite(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1601 | buf, n << BDRV_SECTOR_BITS, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1602 | if (ret < 0) { |
| 1603 | return ret; |
| 1604 | } |
| 1605 | break; |
| 1606 | } |
| 1607 | /* fall-through */ |
| 1608 | |
| 1609 | case BLK_ZERO: |
| 1610 | if (s->has_zero_init) { |
| 1611 | break; |
| 1612 | } |
Eric Blake | d004bd5 | 2016-05-24 16:25:20 -0600 | [diff] [blame] | 1613 | ret = blk_pwrite_zeroes(s->target, sector_num << BDRV_SECTOR_BITS, |
| 1614 | n << BDRV_SECTOR_BITS, 0); |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1615 | if (ret < 0) { |
| 1616 | return ret; |
| 1617 | } |
| 1618 | break; |
| 1619 | } |
| 1620 | |
| 1621 | sector_num += n; |
| 1622 | nb_sectors -= n; |
| 1623 | buf += n * BDRV_SECTOR_SIZE; |
| 1624 | } |
| 1625 | |
| 1626 | return 0; |
| 1627 | } |
| 1628 | |
| 1629 | static int convert_do_copy(ImgConvertState *s) |
| 1630 | { |
| 1631 | uint8_t *buf = NULL; |
| 1632 | int64_t sector_num, allocated_done; |
| 1633 | int ret; |
| 1634 | int n; |
| 1635 | |
| 1636 | /* Check whether we have zero initialisation or can get it efficiently */ |
| 1637 | s->has_zero_init = s->min_sparse && !s->target_has_backing |
| 1638 | ? bdrv_has_zero_init(blk_bs(s->target)) |
| 1639 | : false; |
| 1640 | |
| 1641 | if (!s->has_zero_init && !s->target_has_backing && |
| 1642 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) |
| 1643 | { |
| 1644 | ret = bdrv_make_zero(blk_bs(s->target), BDRV_REQ_MAY_UNMAP); |
| 1645 | if (ret == 0) { |
| 1646 | s->has_zero_init = true; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | /* Allocate buffer for copied data. For compressed images, only one cluster |
| 1651 | * can be copied at a time. */ |
| 1652 | if (s->compressed) { |
| 1653 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { |
| 1654 | error_report("invalid cluster size"); |
| 1655 | ret = -EINVAL; |
| 1656 | goto fail; |
| 1657 | } |
| 1658 | s->buf_sectors = s->cluster_sectors; |
| 1659 | } |
| 1660 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); |
| 1661 | |
| 1662 | /* Calculate allocated sectors for progress */ |
| 1663 | s->allocated_sectors = 0; |
| 1664 | sector_num = 0; |
| 1665 | while (sector_num < s->total_sectors) { |
| 1666 | n = convert_iteration_sectors(s, sector_num); |
| 1667 | if (n < 0) { |
| 1668 | ret = n; |
| 1669 | goto fail; |
| 1670 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 1671 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 1672 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1673 | s->allocated_sectors += n; |
| 1674 | } |
| 1675 | sector_num += n; |
| 1676 | } |
| 1677 | |
| 1678 | /* Do the copy */ |
| 1679 | s->src_cur = 0; |
| 1680 | s->src_cur_offset = 0; |
| 1681 | s->sector_next_status = 0; |
| 1682 | |
| 1683 | sector_num = 0; |
| 1684 | allocated_done = 0; |
| 1685 | |
| 1686 | while (sector_num < s->total_sectors) { |
| 1687 | n = convert_iteration_sectors(s, sector_num); |
| 1688 | if (n < 0) { |
| 1689 | ret = n; |
| 1690 | goto fail; |
| 1691 | } |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 1692 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
| 1693 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1694 | allocated_done += n; |
| 1695 | qemu_progress_print(100.0 * allocated_done / s->allocated_sectors, |
| 1696 | 0); |
| 1697 | } |
| 1698 | |
Max Reitz | aad15de | 2016-03-24 23:33:57 +0100 | [diff] [blame] | 1699 | if (s->status == BLK_DATA) { |
| 1700 | ret = convert_read(s, sector_num, n, buf); |
| 1701 | if (ret < 0) { |
| 1702 | error_report("error while reading sector %" PRId64 |
| 1703 | ": %s", sector_num, strerror(-ret)); |
| 1704 | goto fail; |
| 1705 | } |
| 1706 | } else if (!s->min_sparse && s->status == BLK_ZERO) { |
| 1707 | n = MIN(n, s->buf_sectors); |
| 1708 | memset(buf, 0, n * BDRV_SECTOR_SIZE); |
| 1709 | s->status = BLK_DATA; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | ret = convert_write(s, sector_num, n, buf); |
| 1713 | if (ret < 0) { |
| 1714 | error_report("error while writing sector %" PRId64 |
| 1715 | ": %s", sector_num, strerror(-ret)); |
| 1716 | goto fail; |
| 1717 | } |
| 1718 | |
| 1719 | sector_num += n; |
| 1720 | } |
| 1721 | |
| 1722 | if (s->compressed) { |
| 1723 | /* signal EOF to align */ |
| 1724 | ret = blk_write_compressed(s->target, 0, NULL, 0); |
| 1725 | if (ret < 0) { |
| 1726 | goto fail; |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | ret = 0; |
| 1731 | fail: |
| 1732 | qemu_vfree(buf); |
| 1733 | return ret; |
| 1734 | } |
| 1735 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1736 | static int img_convert(int argc, char **argv) |
| 1737 | { |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1738 | int c, bs_n, bs_i, compress, cluster_sectors, skip_create; |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 1739 | int64_t ret = 0; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1740 | int progress = 0, flags, src_flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1741 | bool writethrough, src_writethrough; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1742 | const char *fmt, *out_fmt, *cache, *src_cache, *out_baseimg, *out_filename; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1743 | BlockDriver *drv, *proto_drv; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1744 | BlockBackend **blk = NULL, *out_blk = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1745 | BlockDriverState **bs = NULL, *out_bs = NULL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1746 | int64_t total_sectors; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1747 | int64_t *bs_sectors = NULL; |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 1748 | size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1749 | BlockDriverInfo bdi; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 1750 | QemuOpts *opts = NULL; |
| 1751 | QemuOptsList *create_opts = NULL; |
| 1752 | const char *out_baseimg_param; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1753 | char *options = NULL; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1754 | const char *snapshot_name = NULL; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1755 | int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1756 | bool quiet = false; |
Max Reitz | cc84d90 | 2013-09-06 17:14:26 +0200 | [diff] [blame] | 1757 | Error *local_err = NULL; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1758 | QemuOpts *sn_opts = NULL; |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 1759 | ImgConvertState state; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1760 | bool image_opts = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1761 | |
| 1762 | fmt = NULL; |
| 1763 | out_fmt = "raw"; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1764 | cache = "unsafe"; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1765 | src_cache = BDRV_DEFAULT_CACHE; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1766 | out_baseimg = NULL; |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1767 | compress = 0; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 1768 | skip_create = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1769 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1770 | static const struct option long_options[] = { |
| 1771 | {"help", no_argument, 0, 'h'}, |
| 1772 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1773 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1774 | {0, 0, 0, 0} |
| 1775 | }; |
| 1776 | c = getopt_long(argc, argv, "hf:O:B:ce6o:s:l:S:pt:T:qn", |
| 1777 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1778 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1779 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1780 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1781 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1782 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1783 | case 'h': |
| 1784 | help(); |
| 1785 | break; |
| 1786 | case 'f': |
| 1787 | fmt = optarg; |
| 1788 | break; |
| 1789 | case 'O': |
| 1790 | out_fmt = optarg; |
| 1791 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1792 | case 'B': |
| 1793 | out_baseimg = optarg; |
| 1794 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1795 | case 'c': |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1796 | compress = 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1797 | break; |
| 1798 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 1799 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1800 | "encryption\' instead!"); |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1801 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1802 | goto fail_getopt; |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 1803 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 1804 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1805 | "compat6\' instead!"); |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1806 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1807 | goto fail_getopt; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1808 | case 'o': |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1809 | if (!is_valid_option_list(optarg)) { |
| 1810 | error_report("Invalid option list: %s", optarg); |
| 1811 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1812 | goto fail_getopt; |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1813 | } |
| 1814 | if (!options) { |
| 1815 | options = g_strdup(optarg); |
| 1816 | } else { |
| 1817 | char *old_options = options; |
| 1818 | options = g_strdup_printf("%s,%s", options, optarg); |
| 1819 | g_free(old_options); |
| 1820 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1821 | break; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1822 | case 's': |
| 1823 | snapshot_name = optarg; |
| 1824 | break; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1825 | case 'l': |
| 1826 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 1827 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
| 1828 | optarg, false); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1829 | if (!sn_opts) { |
| 1830 | error_report("Failed in parsing snapshot param '%s'", |
| 1831 | optarg); |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1832 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1833 | goto fail_getopt; |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1834 | } |
| 1835 | } else { |
| 1836 | snapshot_name = optarg; |
| 1837 | } |
| 1838 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1839 | case 'S': |
| 1840 | { |
| 1841 | int64_t sval; |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 1842 | char *end; |
Marc-André Lureau | 4677bb4 | 2015-09-16 18:02:56 +0200 | [diff] [blame] | 1843 | sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B); |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 1844 | if (sval < 0 || *end) { |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1845 | error_report("Invalid minimum zero buffer size for sparse output specified"); |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1846 | ret = -1; |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1847 | goto fail_getopt; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | min_sparse = sval / BDRV_SECTOR_SIZE; |
| 1851 | break; |
| 1852 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1853 | case 'p': |
| 1854 | progress = 1; |
| 1855 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1856 | case 't': |
| 1857 | cache = optarg; |
| 1858 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1859 | case 'T': |
| 1860 | src_cache = optarg; |
| 1861 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1862 | case 'q': |
| 1863 | quiet = true; |
| 1864 | break; |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 1865 | case 'n': |
| 1866 | skip_create = 1; |
| 1867 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1868 | case OPTION_OBJECT: |
| 1869 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 1870 | optarg, true); |
| 1871 | if (!opts) { |
| 1872 | goto fail_getopt; |
| 1873 | } |
| 1874 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 1875 | case OPTION_IMAGE_OPTS: |
| 1876 | image_opts = true; |
| 1877 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1878 | } |
| 1879 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1880 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1881 | if (qemu_opts_foreach(&qemu_object_opts, |
| 1882 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 1883 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 1884 | goto fail_getopt; |
| 1885 | } |
| 1886 | |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1887 | /* Initialize before goto out */ |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1888 | if (quiet) { |
| 1889 | progress = 0; |
| 1890 | } |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 1891 | qemu_progress_init(progress, 1.0); |
| 1892 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1893 | bs_n = argc - optind - 1; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 1894 | out_filename = bs_n >= 1 ? argv[argc - 1] : NULL; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1895 | |
Kevin Wolf | 2dc8328 | 2014-02-21 16:24:05 +0100 | [diff] [blame] | 1896 | if (options && has_help_option(options)) { |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 1897 | ret = print_block_option_help(out_filename, out_fmt); |
| 1898 | goto out; |
| 1899 | } |
| 1900 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1901 | if (bs_n < 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 1902 | error_exit("Must specify image file name"); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1903 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1904 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1905 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1906 | if (bs_n > 1 && out_baseimg) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1907 | error_report("-B makes no sense when concatenating multiple input " |
| 1908 | "images"); |
Jes Sorensen | 31ca34b | 2010-12-06 15:25:36 +0100 | [diff] [blame] | 1909 | ret = -1; |
| 1910 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1911 | } |
Dong Xu Wang | f8111c2 | 2012-03-15 20:13:31 +0800 | [diff] [blame] | 1912 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1913 | src_flags = 0; |
| 1914 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 1915 | if (ret < 0) { |
| 1916 | error_report("Invalid source cache option: %s", src_cache); |
| 1917 | goto out; |
| 1918 | } |
| 1919 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1920 | qemu_progress_print(0, 100); |
| 1921 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 1922 | blk = g_new0(BlockBackend *, bs_n); |
Markus Armbruster | d739f1c | 2014-06-26 13:23:24 +0200 | [diff] [blame] | 1923 | bs = g_new0(BlockDriverState *, bs_n); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1924 | bs_sectors = g_new(int64_t, bs_n); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1925 | |
| 1926 | total_sectors = 0; |
| 1927 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 1928 | blk[bs_i] = img_open(image_opts, argv[optind + bs_i], |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 1929 | fmt, src_flags, src_writethrough, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1930 | if (!blk[bs_i]) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1931 | ret = -1; |
| 1932 | goto out; |
| 1933 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 1934 | bs[bs_i] = blk_bs(blk[bs_i]); |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 1935 | bs_sectors[bs_i] = blk_nb_sectors(blk[bs_i]); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 1936 | if (bs_sectors[bs_i] < 0) { |
| 1937 | error_report("Could not get size of %s: %s", |
| 1938 | argv[optind + bs_i], strerror(-bs_sectors[bs_i])); |
| 1939 | ret = -1; |
| 1940 | goto out; |
| 1941 | } |
Markus Armbruster | d739f1c | 2014-06-26 13:23:24 +0200 | [diff] [blame] | 1942 | total_sectors += bs_sectors[bs_i]; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1943 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1944 | |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1945 | if (sn_opts) { |
| 1946 | ret = bdrv_snapshot_load_tmp(bs[0], |
| 1947 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
| 1948 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), |
| 1949 | &local_err); |
| 1950 | } else if (snapshot_name != NULL) { |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1951 | if (bs_n > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1952 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1953 | ret = -1; |
| 1954 | goto out; |
| 1955 | } |
Wenchao Xia | 7b4c478 | 2013-12-04 17:10:54 +0800 | [diff] [blame] | 1956 | |
| 1957 | bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1958 | } |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 1959 | if (local_err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 1960 | error_reportf_err(local_err, "Failed to load snapshot: "); |
Wenchao Xia | ef80654 | 2013-12-04 17:10:57 +0800 | [diff] [blame] | 1961 | ret = -1; |
| 1962 | goto out; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1965 | /* Find driver and parse its options */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1966 | drv = bdrv_find_format(out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1967 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1968 | error_report("Unknown file format '%s'", out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1969 | ret = -1; |
| 1970 | goto out; |
| 1971 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1972 | |
Max Reitz | b65a5e1 | 2015-02-05 13:58:12 -0500 | [diff] [blame] | 1973 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1974 | if (!proto_drv) { |
Markus Armbruster | 2867ce4 | 2015-03-12 16:08:02 +0100 | [diff] [blame] | 1975 | error_report_err(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1976 | ret = -1; |
| 1977 | goto out; |
| 1978 | } |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1979 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1980 | if (!skip_create) { |
| 1981 | if (!drv->create_opts) { |
| 1982 | error_report("Format driver '%s' does not support image creation", |
| 1983 | drv->format_name); |
| 1984 | ret = -1; |
| 1985 | goto out; |
| 1986 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 1987 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1988 | if (!proto_drv->create_opts) { |
| 1989 | error_report("Protocol driver '%s' does not support image creation", |
| 1990 | proto_drv->format_name); |
| 1991 | ret = -1; |
| 1992 | goto out; |
| 1993 | } |
Max Reitz | f75613c | 2014-12-02 18:32:46 +0100 | [diff] [blame] | 1994 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1995 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
| 1996 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 1997 | |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 1998 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 1999 | if (options) { |
| 2000 | qemu_opts_do_parse(opts, options, NULL, &local_err); |
| 2001 | if (local_err) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 2002 | error_report_err(local_err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 2003 | ret = -1; |
| 2004 | goto out; |
| 2005 | } |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2006 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2007 | |
Markus Armbruster | 39101f2 | 2015-02-12 16:46:36 +0100 | [diff] [blame] | 2008 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512, |
| 2009 | &error_abort); |
Max Reitz | 2e024cd | 2015-02-11 09:58:46 -0500 | [diff] [blame] | 2010 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
| 2011 | if (ret < 0) { |
| 2012 | goto out; |
| 2013 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2014 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2015 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2016 | /* Get backing file name if -o backing_file was used */ |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2017 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2018 | if (out_baseimg_param) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2019 | out_baseimg = out_baseimg_param; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2022 | /* Check if compression is supported */ |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 2023 | if (compress) { |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2024 | bool encryption = |
| 2025 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); |
| 2026 | const char *preallocation = |
| 2027 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2028 | |
| 2029 | if (!drv->bdrv_write_compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2030 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2031 | ret = -1; |
| 2032 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2033 | } |
| 2034 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2035 | if (encryption) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2036 | error_report("Compression and encryption not supported at " |
| 2037 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2038 | ret = -1; |
| 2039 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2040 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2041 | |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2042 | if (preallocation |
| 2043 | && strcmp(preallocation, "off")) |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 2044 | { |
| 2045 | error_report("Compression and preallocation not supported at " |
| 2046 | "the same time"); |
| 2047 | ret = -1; |
| 2048 | goto out; |
| 2049 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 2050 | } |
| 2051 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2052 | if (!skip_create) { |
| 2053 | /* Create the new image */ |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 2054 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2055 | if (ret < 0) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2056 | error_reportf_err(local_err, "%s: error while converting %s: ", |
| 2057 | out_filename, out_fmt); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2058 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2059 | } |
| 2060 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 2061 | |
Peter Lieven | 5a37b60 | 2013-10-24 12:07:06 +0200 | [diff] [blame] | 2062 | flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2063 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2064 | if (ret < 0) { |
| 2065 | error_report("Invalid cache option: %s", cache); |
Markus Armbruster | bb9cd2e | 2014-05-28 11:17:07 +0200 | [diff] [blame] | 2066 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2067 | } |
| 2068 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2069 | /* XXX we should allow --image-opts to trigger use of |
| 2070 | * img_open() here, but then we have trouble with |
| 2071 | * the bdrv_create() call which takes different params. |
| 2072 | * Not critical right now, so fix can wait... |
| 2073 | */ |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2074 | out_blk = img_open_file(out_filename, out_fmt, flags, writethrough, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2075 | if (!out_blk) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2076 | ret = -1; |
| 2077 | goto out; |
| 2078 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2079 | out_bs = blk_bs(out_blk); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2080 | |
Peter Lieven | f2521c9 | 2013-11-27 11:07:06 +0100 | [diff] [blame] | 2081 | /* increase bufsectors from the default 4096 (2M) if opt_transfer_length |
| 2082 | * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB) |
| 2083 | * as maximum. */ |
| 2084 | bufsectors = MIN(32768, |
| 2085 | MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length, |
| 2086 | out_bs->bl.discard_alignment)) |
| 2087 | ); |
| 2088 | |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2089 | if (skip_create) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2090 | int64_t output_sectors = blk_nb_sectors(out_blk); |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2091 | if (output_sectors < 0) { |
Gonglei | eec5eb4 | 2015-02-25 12:22:27 +0800 | [diff] [blame] | 2092 | error_report("unable to get output image length: %s", |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2093 | strerror(-output_sectors)); |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2094 | ret = -1; |
| 2095 | goto out; |
Markus Armbruster | 43716fa | 2014-06-26 13:23:21 +0200 | [diff] [blame] | 2096 | } else if (output_sectors < total_sectors) { |
Alexandre Derumier | b2e1049 | 2013-09-02 19:07:24 +0100 | [diff] [blame] | 2097 | error_report("output file is smaller than input file"); |
| 2098 | ret = -1; |
| 2099 | goto out; |
| 2100 | } |
| 2101 | } |
| 2102 | |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2103 | cluster_sectors = 0; |
| 2104 | ret = bdrv_get_info(out_bs, &bdi); |
| 2105 | if (ret < 0) { |
| 2106 | if (compress) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2107 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2108 | goto out; |
| 2109 | } |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2110 | } else { |
Fam Zheng | 85f49ca | 2014-05-06 21:08:43 +0800 | [diff] [blame] | 2111 | compress = compress || bdi.needs_compressed_writes; |
Peter Lieven | 24f833c | 2013-11-27 11:07:07 +0100 | [diff] [blame] | 2112 | cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; |
| 2113 | } |
| 2114 | |
Kevin Wolf | 690c730 | 2015-03-19 13:33:32 +0100 | [diff] [blame] | 2115 | state = (ImgConvertState) { |
| 2116 | .src = blk, |
| 2117 | .src_sectors = bs_sectors, |
| 2118 | .src_num = bs_n, |
| 2119 | .total_sectors = total_sectors, |
| 2120 | .target = out_blk, |
| 2121 | .compressed = compress, |
| 2122 | .target_has_backing = (bool) out_baseimg, |
| 2123 | .min_sparse = min_sparse, |
| 2124 | .cluster_sectors = cluster_sectors, |
| 2125 | .buf_sectors = bufsectors, |
| 2126 | }; |
| 2127 | ret = convert_do_copy(&state); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2128 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2129 | out: |
Peter Lieven | 13c28af | 2013-11-27 11:07:01 +0100 | [diff] [blame] | 2130 | if (!ret) { |
| 2131 | qemu_progress_print(100, 0); |
| 2132 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2133 | qemu_progress_end(); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 2134 | qemu_opts_del(opts); |
| 2135 | qemu_opts_free(create_opts); |
Markus Armbruster | fbf28a4 | 2014-09-29 16:07:55 +0200 | [diff] [blame] | 2136 | qemu_opts_del(sn_opts); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2137 | blk_unref(out_blk); |
Markus Armbruster | 9ba10c9 | 2014-10-07 13:59:08 +0200 | [diff] [blame] | 2138 | g_free(bs); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2139 | if (blk) { |
| 2140 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
| 2141 | blk_unref(blk[bs_i]); |
| 2142 | } |
| 2143 | g_free(blk); |
| 2144 | } |
Markus Armbruster | d739f1c | 2014-06-26 13:23:24 +0200 | [diff] [blame] | 2145 | g_free(bs_sectors); |
Kevin Wolf | 64bb01a | 2014-03-03 14:54:07 +0100 | [diff] [blame] | 2146 | fail_getopt: |
| 2147 | g_free(options); |
| 2148 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2149 | if (ret) { |
| 2150 | return 1; |
| 2151 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2152 | return 0; |
| 2153 | } |
| 2154 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 2155 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2156 | static void dump_snapshots(BlockDriverState *bs) |
| 2157 | { |
| 2158 | QEMUSnapshotInfo *sn_tab, *sn; |
| 2159 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2160 | |
| 2161 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 2162 | if (nb_sns <= 0) |
| 2163 | return; |
| 2164 | printf("Snapshot list:\n"); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2165 | bdrv_snapshot_dump(fprintf, stdout, NULL); |
| 2166 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2167 | for(i = 0; i < nb_sns; i++) { |
| 2168 | sn = &sn_tab[i]; |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2169 | bdrv_snapshot_dump(fprintf, stdout, sn); |
| 2170 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2171 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 2172 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2175 | static void dump_json_image_info_list(ImageInfoList *list) |
| 2176 | { |
Markus Armbruster | 4399c43 | 2014-04-25 16:50:32 +0200 | [diff] [blame] | 2177 | Error *local_err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2178 | QString *str; |
| 2179 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 2180 | QObject *obj; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 2181 | visit_type_ImageInfoList(qmp_output_get_visitor(ov), NULL, &list, |
| 2182 | &local_err); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2183 | obj = qmp_output_get_qobject(ov); |
| 2184 | str = qobject_to_json_pretty(obj); |
| 2185 | assert(str != NULL); |
| 2186 | printf("%s\n", qstring_get_str(str)); |
| 2187 | qobject_decref(obj); |
| 2188 | qmp_output_visitor_cleanup(ov); |
| 2189 | QDECREF(str); |
| 2190 | } |
| 2191 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2192 | static void dump_json_image_info(ImageInfo *info) |
| 2193 | { |
Markus Armbruster | 4399c43 | 2014-04-25 16:50:32 +0200 | [diff] [blame] | 2194 | Error *local_err = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2195 | QString *str; |
| 2196 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 2197 | QObject *obj; |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 2198 | visit_type_ImageInfo(qmp_output_get_visitor(ov), NULL, &info, &local_err); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2199 | obj = qmp_output_get_qobject(ov); |
| 2200 | str = qobject_to_json_pretty(obj); |
| 2201 | assert(str != NULL); |
| 2202 | printf("%s\n", qstring_get_str(str)); |
| 2203 | qobject_decref(obj); |
| 2204 | qmp_output_visitor_cleanup(ov); |
| 2205 | QDECREF(str); |
| 2206 | } |
| 2207 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2208 | static void dump_human_image_info_list(ImageInfoList *list) |
| 2209 | { |
| 2210 | ImageInfoList *elem; |
| 2211 | bool delim = false; |
| 2212 | |
| 2213 | for (elem = list; elem; elem = elem->next) { |
| 2214 | if (delim) { |
| 2215 | printf("\n"); |
| 2216 | } |
| 2217 | delim = true; |
| 2218 | |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame] | 2219 | bdrv_image_info_dump(fprintf, stdout, elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 2224 | { |
| 2225 | return strcmp(a, b) == 0; |
| 2226 | } |
| 2227 | |
| 2228 | /** |
| 2229 | * Open an image file chain and return an ImageInfoList |
| 2230 | * |
| 2231 | * @filename: topmost image filename |
| 2232 | * @fmt: topmost image format (may be NULL to autodetect) |
| 2233 | * @chain: true - enumerate entire backing file chain |
| 2234 | * false - only topmost image file |
| 2235 | * |
| 2236 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 2237 | * image file. If there was an error a message will have been printed to |
| 2238 | * stderr. |
| 2239 | */ |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2240 | static ImageInfoList *collect_image_info_list(bool image_opts, |
| 2241 | const char *filename, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2242 | const char *fmt, |
| 2243 | bool chain) |
| 2244 | { |
| 2245 | ImageInfoList *head = NULL; |
| 2246 | ImageInfoList **last = &head; |
| 2247 | GHashTable *filenames; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2248 | Error *err = NULL; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2249 | |
| 2250 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 2251 | |
| 2252 | while (filename) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2253 | BlockBackend *blk; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2254 | BlockDriverState *bs; |
| 2255 | ImageInfo *info; |
| 2256 | ImageInfoList *elem; |
| 2257 | |
| 2258 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 2259 | error_report("Backing file '%s' creates an infinite loop.", |
| 2260 | filename); |
| 2261 | goto err; |
| 2262 | } |
| 2263 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 2264 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2265 | blk = img_open(image_opts, filename, fmt, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2266 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2267 | if (!blk) { |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2268 | goto err; |
| 2269 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2270 | bs = blk_bs(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2271 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2272 | bdrv_query_image_info(bs, &info, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2273 | if (err) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 2274 | error_report_err(err); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2275 | blk_unref(blk); |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 2276 | goto err; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 2277 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2278 | |
| 2279 | elem = g_new0(ImageInfoList, 1); |
| 2280 | elem->value = info; |
| 2281 | *last = elem; |
| 2282 | last = &elem->next; |
| 2283 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2284 | blk_unref(blk); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2285 | |
| 2286 | filename = fmt = NULL; |
| 2287 | if (chain) { |
| 2288 | if (info->has_full_backing_filename) { |
| 2289 | filename = info->full_backing_filename; |
| 2290 | } else if (info->has_backing_filename) { |
John Snow | 92d617a | 2015-12-14 14:55:15 -0500 | [diff] [blame] | 2291 | error_report("Could not determine absolute backing filename," |
| 2292 | " but backing filename '%s' present", |
| 2293 | info->backing_filename); |
| 2294 | goto err; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2295 | } |
| 2296 | if (info->has_backing_filename_format) { |
| 2297 | fmt = info->backing_filename_format; |
| 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | g_hash_table_destroy(filenames); |
| 2302 | return head; |
| 2303 | |
| 2304 | err: |
| 2305 | qapi_free_ImageInfoList(head); |
| 2306 | g_hash_table_destroy(filenames); |
| 2307 | return NULL; |
| 2308 | } |
| 2309 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2310 | static int img_info(int argc, char **argv) |
| 2311 | { |
| 2312 | int c; |
| 2313 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2314 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2315 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2316 | ImageInfoList *list; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2317 | bool image_opts = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2318 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2319 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2320 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2321 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2322 | int option_index = 0; |
| 2323 | static const struct option long_options[] = { |
| 2324 | {"help", no_argument, 0, 'h'}, |
| 2325 | {"format", required_argument, 0, 'f'}, |
| 2326 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2327 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2328 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2329 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2330 | {0, 0, 0, 0} |
| 2331 | }; |
| 2332 | c = getopt_long(argc, argv, "f:h", |
| 2333 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2334 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2335 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2336 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2337 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2338 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2339 | case 'h': |
| 2340 | help(); |
| 2341 | break; |
| 2342 | case 'f': |
| 2343 | fmt = optarg; |
| 2344 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2345 | case OPTION_OUTPUT: |
| 2346 | output = optarg; |
| 2347 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2348 | case OPTION_BACKING_CHAIN: |
| 2349 | chain = true; |
| 2350 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2351 | case OPTION_OBJECT: { |
| 2352 | QemuOpts *opts; |
| 2353 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2354 | optarg, true); |
| 2355 | if (!opts) { |
| 2356 | return 1; |
| 2357 | } |
| 2358 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2359 | case OPTION_IMAGE_OPTS: |
| 2360 | image_opts = true; |
| 2361 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2362 | } |
| 2363 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2364 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2365 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2366 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2367 | filename = argv[optind++]; |
| 2368 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2369 | if (output && !strcmp(output, "json")) { |
| 2370 | output_format = OFORMAT_JSON; |
| 2371 | } else if (output && !strcmp(output, "human")) { |
| 2372 | output_format = OFORMAT_HUMAN; |
| 2373 | } else if (output) { |
| 2374 | error_report("--output must be used with human or json as argument."); |
| 2375 | return 1; |
| 2376 | } |
| 2377 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2378 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2379 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 2380 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2381 | return 1; |
| 2382 | } |
| 2383 | |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2384 | list = collect_image_info_list(image_opts, filename, fmt, chain); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2385 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2386 | return 1; |
| 2387 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2388 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2389 | switch (output_format) { |
| 2390 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2391 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2392 | break; |
| 2393 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2394 | if (chain) { |
| 2395 | dump_json_image_info_list(list); |
| 2396 | } else { |
| 2397 | dump_json_image_info(list->value); |
| 2398 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2399 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2400 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 2401 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 2402 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2403 | return 0; |
| 2404 | } |
| 2405 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2406 | static void dump_map_entry(OutputFormat output_format, MapEntry *e, |
| 2407 | MapEntry *next) |
| 2408 | { |
| 2409 | switch (output_format) { |
| 2410 | case OFORMAT_HUMAN: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2411 | if (e->data && !e->has_offset) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2412 | error_report("File contains external, encrypted or compressed clusters."); |
| 2413 | exit(1); |
| 2414 | } |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2415 | if (e->data && !e->zero) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2416 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2417 | e->start, e->length, |
| 2418 | e->has_offset ? e->offset : 0, |
| 2419 | e->has_filename ? e->filename : ""); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2420 | } |
| 2421 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. |
| 2422 | * Modify the flags here to allow more coalescing. |
| 2423 | */ |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2424 | if (next && (!next->data || next->zero)) { |
| 2425 | next->data = false; |
| 2426 | next->zero = true; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2427 | } |
| 2428 | break; |
| 2429 | case OFORMAT_JSON: |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2430 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," |
| 2431 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2432 | (e->start == 0 ? "[" : ",\n"), |
| 2433 | e->start, e->length, e->depth, |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2434 | e->zero ? "true" : "false", |
| 2435 | e->data ? "true" : "false"); |
| 2436 | if (e->has_offset) { |
Paolo Bonzini | c745bfb | 2013-09-11 18:47:52 +0200 | [diff] [blame] | 2437 | printf(", \"offset\": %"PRId64"", e->offset); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2438 | } |
| 2439 | putchar('}'); |
| 2440 | |
| 2441 | if (!next) { |
| 2442 | printf("]\n"); |
| 2443 | } |
| 2444 | break; |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | static int get_block_status(BlockDriverState *bs, int64_t sector_num, |
| 2449 | int nb_sectors, MapEntry *e) |
| 2450 | { |
| 2451 | int64_t ret; |
| 2452 | int depth; |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 2453 | BlockDriverState *file; |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2454 | bool has_offset; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2455 | |
| 2456 | /* As an optimization, we could cache the current range of unallocated |
| 2457 | * clusters in each file of the chain, and avoid querying the same |
| 2458 | * range repeatedly. |
| 2459 | */ |
| 2460 | |
| 2461 | depth = 0; |
| 2462 | for (;;) { |
Fam Zheng | 67a0fd2 | 2016-01-26 11:58:48 +0800 | [diff] [blame] | 2463 | ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors, |
| 2464 | &file); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2465 | if (ret < 0) { |
| 2466 | return ret; |
| 2467 | } |
| 2468 | assert(nb_sectors); |
| 2469 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
| 2470 | break; |
| 2471 | } |
Kevin Wolf | 760e006 | 2015-06-17 14:55:21 +0200 | [diff] [blame] | 2472 | bs = backing_bs(bs); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2473 | if (bs == NULL) { |
| 2474 | ret = 0; |
| 2475 | break; |
| 2476 | } |
| 2477 | |
| 2478 | depth++; |
| 2479 | } |
| 2480 | |
John Snow | 2875645 | 2016-02-05 13:12:33 -0500 | [diff] [blame] | 2481 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
| 2482 | |
| 2483 | *e = (MapEntry) { |
| 2484 | .start = sector_num * BDRV_SECTOR_SIZE, |
| 2485 | .length = nb_sectors * BDRV_SECTOR_SIZE, |
| 2486 | .data = !!(ret & BDRV_BLOCK_DATA), |
| 2487 | .zero = !!(ret & BDRV_BLOCK_ZERO), |
| 2488 | .offset = ret & BDRV_BLOCK_OFFSET_MASK, |
| 2489 | .has_offset = has_offset, |
| 2490 | .depth = depth, |
| 2491 | .has_filename = file && has_offset, |
| 2492 | .filename = file && has_offset ? file->filename : NULL, |
| 2493 | }; |
| 2494 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2495 | return 0; |
| 2496 | } |
| 2497 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2498 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
| 2499 | { |
| 2500 | if (curr->length == 0) { |
| 2501 | return false; |
| 2502 | } |
| 2503 | if (curr->zero != next->zero || |
| 2504 | curr->data != next->data || |
| 2505 | curr->depth != next->depth || |
| 2506 | curr->has_filename != next->has_filename || |
| 2507 | curr->has_offset != next->has_offset) { |
| 2508 | return false; |
| 2509 | } |
| 2510 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { |
| 2511 | return false; |
| 2512 | } |
| 2513 | if (curr->has_offset && curr->offset + curr->length != next->offset) { |
| 2514 | return false; |
| 2515 | } |
| 2516 | return true; |
| 2517 | } |
| 2518 | |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2519 | static int img_map(int argc, char **argv) |
| 2520 | { |
| 2521 | int c; |
| 2522 | OutputFormat output_format = OFORMAT_HUMAN; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2523 | BlockBackend *blk; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2524 | BlockDriverState *bs; |
| 2525 | const char *filename, *fmt, *output; |
| 2526 | int64_t length; |
| 2527 | MapEntry curr = { .length = 0 }, next; |
| 2528 | int ret = 0; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2529 | bool image_opts = false; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2530 | |
| 2531 | fmt = NULL; |
| 2532 | output = NULL; |
| 2533 | for (;;) { |
| 2534 | int option_index = 0; |
| 2535 | static const struct option long_options[] = { |
| 2536 | {"help", no_argument, 0, 'h'}, |
| 2537 | {"format", required_argument, 0, 'f'}, |
| 2538 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2539 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2540 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2541 | {0, 0, 0, 0} |
| 2542 | }; |
| 2543 | c = getopt_long(argc, argv, "f:h", |
| 2544 | long_options, &option_index); |
| 2545 | if (c == -1) { |
| 2546 | break; |
| 2547 | } |
| 2548 | switch (c) { |
| 2549 | case '?': |
| 2550 | case 'h': |
| 2551 | help(); |
| 2552 | break; |
| 2553 | case 'f': |
| 2554 | fmt = optarg; |
| 2555 | break; |
| 2556 | case OPTION_OUTPUT: |
| 2557 | output = optarg; |
| 2558 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2559 | case OPTION_OBJECT: { |
| 2560 | QemuOpts *opts; |
| 2561 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2562 | optarg, true); |
| 2563 | if (!opts) { |
| 2564 | return 1; |
| 2565 | } |
| 2566 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2567 | case OPTION_IMAGE_OPTS: |
| 2568 | image_opts = true; |
| 2569 | break; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2570 | } |
| 2571 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2572 | if (optind != argc - 1) { |
| 2573 | error_exit("Expecting one image file name"); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2574 | } |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2575 | filename = argv[optind]; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2576 | |
| 2577 | if (output && !strcmp(output, "json")) { |
| 2578 | output_format = OFORMAT_JSON; |
| 2579 | } else if (output && !strcmp(output, "human")) { |
| 2580 | output_format = OFORMAT_HUMAN; |
| 2581 | } else if (output) { |
| 2582 | error_report("--output must be used with human or json as argument."); |
| 2583 | return 1; |
| 2584 | } |
| 2585 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2586 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2587 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 2588 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2589 | return 1; |
| 2590 | } |
| 2591 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2592 | blk = img_open(image_opts, filename, fmt, 0, false, false); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2593 | if (!blk) { |
| 2594 | return 1; |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2595 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2596 | bs = blk_bs(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2597 | |
| 2598 | if (output_format == OFORMAT_HUMAN) { |
| 2599 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); |
| 2600 | } |
| 2601 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2602 | length = blk_getlength(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2603 | while (curr.start + curr.length < length) { |
| 2604 | int64_t nsectors_left; |
| 2605 | int64_t sector_num; |
| 2606 | int n; |
| 2607 | |
| 2608 | sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS; |
| 2609 | |
| 2610 | /* Probe up to 1 GiB at a time. */ |
| 2611 | nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num; |
| 2612 | n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left); |
| 2613 | ret = get_block_status(bs, sector_num, n, &next); |
| 2614 | |
| 2615 | if (ret < 0) { |
| 2616 | error_report("Could not read file metadata: %s", strerror(-ret)); |
| 2617 | goto out; |
| 2618 | } |
| 2619 | |
Fam Zheng | 16b0d55 | 2016-01-26 11:59:02 +0800 | [diff] [blame] | 2620 | if (entry_mergeable(&curr, &next)) { |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2621 | curr.length += next.length; |
| 2622 | continue; |
| 2623 | } |
| 2624 | |
| 2625 | if (curr.length > 0) { |
| 2626 | dump_map_entry(output_format, &curr, &next); |
| 2627 | } |
| 2628 | curr = next; |
| 2629 | } |
| 2630 | |
| 2631 | dump_map_entry(output_format, &curr, NULL); |
| 2632 | |
| 2633 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2634 | blk_unref(blk); |
Paolo Bonzini | 4c93a13b | 2013-09-04 19:00:33 +0200 | [diff] [blame] | 2635 | return ret < 0; |
| 2636 | } |
| 2637 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2638 | #define SNAPSHOT_LIST 1 |
| 2639 | #define SNAPSHOT_CREATE 2 |
| 2640 | #define SNAPSHOT_APPLY 3 |
| 2641 | #define SNAPSHOT_DELETE 4 |
| 2642 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2643 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2644 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2645 | BlockBackend *blk; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2646 | BlockDriverState *bs; |
| 2647 | QEMUSnapshotInfo sn; |
| 2648 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2649 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2650 | int action = 0; |
| 2651 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2652 | bool quiet = false; |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 2653 | Error *err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2654 | bool image_opts = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2655 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2656 | bdrv_oflags = BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2657 | /* Parse commandline parameters */ |
| 2658 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2659 | static const struct option long_options[] = { |
| 2660 | {"help", no_argument, 0, 'h'}, |
| 2661 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2662 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2663 | {0, 0, 0, 0} |
| 2664 | }; |
| 2665 | c = getopt_long(argc, argv, "la:c:d:hq", |
| 2666 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2667 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2668 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2669 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2670 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2671 | case '?': |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2672 | case 'h': |
| 2673 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2674 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2675 | case 'l': |
| 2676 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2677 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2678 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2679 | } |
| 2680 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 2681 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2682 | break; |
| 2683 | case 'a': |
| 2684 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2685 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2686 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2687 | } |
| 2688 | action = SNAPSHOT_APPLY; |
| 2689 | snapshot_name = optarg; |
| 2690 | break; |
| 2691 | case 'c': |
| 2692 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2693 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2694 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2695 | } |
| 2696 | action = SNAPSHOT_CREATE; |
| 2697 | snapshot_name = optarg; |
| 2698 | break; |
| 2699 | case 'd': |
| 2700 | if (action) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2701 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2702 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2703 | } |
| 2704 | action = SNAPSHOT_DELETE; |
| 2705 | snapshot_name = optarg; |
| 2706 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2707 | case 'q': |
| 2708 | quiet = true; |
| 2709 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2710 | case OPTION_OBJECT: { |
| 2711 | QemuOpts *opts; |
| 2712 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2713 | optarg, true); |
| 2714 | if (!opts) { |
| 2715 | return 1; |
| 2716 | } |
| 2717 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2718 | case OPTION_IMAGE_OPTS: |
| 2719 | image_opts = true; |
| 2720 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 2724 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2725 | error_exit("Expecting one image file name"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2726 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2727 | filename = argv[optind++]; |
| 2728 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2729 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2730 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 2731 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2732 | return 1; |
| 2733 | } |
| 2734 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2735 | /* Open the image */ |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2736 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2737 | if (!blk) { |
| 2738 | return 1; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2739 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2740 | bs = blk_bs(blk); |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2741 | |
| 2742 | /* Perform the requested action */ |
| 2743 | switch(action) { |
| 2744 | case SNAPSHOT_LIST: |
| 2745 | dump_snapshots(bs); |
| 2746 | break; |
| 2747 | |
| 2748 | case SNAPSHOT_CREATE: |
| 2749 | memset(&sn, 0, sizeof(sn)); |
| 2750 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 2751 | |
| 2752 | qemu_gettimeofday(&tv); |
| 2753 | sn.date_sec = tv.tv_sec; |
| 2754 | sn.date_nsec = tv.tv_usec * 1000; |
| 2755 | |
| 2756 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2757 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2758 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2759 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2760 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2761 | break; |
| 2762 | |
| 2763 | case SNAPSHOT_APPLY: |
| 2764 | ret = bdrv_snapshot_goto(bs, snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2765 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2766 | error_report("Could not apply snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2767 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2768 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2769 | break; |
| 2770 | |
| 2771 | case SNAPSHOT_DELETE: |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 2772 | bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 2773 | if (err) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2774 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
| 2775 | snapshot_name); |
Wenchao Xia | a89d89d | 2013-09-11 14:04:33 +0800 | [diff] [blame] | 2776 | ret = 1; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2777 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2778 | break; |
| 2779 | } |
| 2780 | |
| 2781 | /* Cleanup */ |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2782 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2783 | if (ret) { |
| 2784 | return 1; |
| 2785 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2786 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2789 | static int img_rebase(int argc, char **argv) |
| 2790 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 2791 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 2792 | uint8_t *buf_old = NULL; |
| 2793 | uint8_t *buf_new = NULL; |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2794 | BlockDriverState *bs = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2795 | char *filename; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2796 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
| 2797 | int c, flags, src_flags, ret; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2798 | bool writethrough, src_writethrough; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2799 | int unsafe = 0; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2800 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2801 | bool quiet = false; |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 2802 | Error *local_err = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2803 | bool image_opts = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2804 | |
| 2805 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 2806 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2807 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2808 | src_cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2809 | out_baseimg = NULL; |
| 2810 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2811 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2812 | static const struct option long_options[] = { |
| 2813 | {"help", no_argument, 0, 'h'}, |
| 2814 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2815 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2816 | {0, 0, 0, 0} |
| 2817 | }; |
| 2818 | c = getopt_long(argc, argv, "hf:F:b:upt:T:q", |
| 2819 | long_options, NULL); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2820 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2821 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2822 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2823 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2824 | case '?': |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2825 | case 'h': |
| 2826 | help(); |
| 2827 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 2828 | case 'f': |
| 2829 | fmt = optarg; |
| 2830 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2831 | case 'F': |
| 2832 | out_basefmt = optarg; |
| 2833 | break; |
| 2834 | case 'b': |
| 2835 | out_baseimg = optarg; |
| 2836 | break; |
| 2837 | case 'u': |
| 2838 | unsafe = 1; |
| 2839 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2840 | case 'p': |
| 2841 | progress = 1; |
| 2842 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2843 | case 't': |
| 2844 | cache = optarg; |
| 2845 | break; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2846 | case 'T': |
| 2847 | src_cache = optarg; |
| 2848 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2849 | case 'q': |
| 2850 | quiet = true; |
| 2851 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2852 | case OPTION_OBJECT: { |
| 2853 | QemuOpts *opts; |
| 2854 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 2855 | optarg, true); |
| 2856 | if (!opts) { |
| 2857 | return 1; |
| 2858 | } |
| 2859 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 2860 | case OPTION_IMAGE_OPTS: |
| 2861 | image_opts = true; |
| 2862 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2866 | if (quiet) { |
| 2867 | progress = 0; |
| 2868 | } |
| 2869 | |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 2870 | if (optind != argc - 1) { |
| 2871 | error_exit("Expecting one image file name"); |
| 2872 | } |
| 2873 | if (!unsafe && !out_baseimg) { |
| 2874 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 2875 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2876 | filename = argv[optind++]; |
| 2877 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2878 | if (qemu_opts_foreach(&qemu_object_opts, |
| 2879 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 2880 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 2881 | return 1; |
| 2882 | } |
| 2883 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2884 | qemu_progress_init(progress, 2.0); |
| 2885 | qemu_progress_print(0, 100); |
| 2886 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2887 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2888 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2889 | if (ret < 0) { |
| 2890 | error_report("Invalid cache option: %s", cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 2891 | goto out; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 2892 | } |
| 2893 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2894 | src_flags = 0; |
| 2895 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2896 | if (ret < 0) { |
| 2897 | error_report("Invalid source cache option: %s", src_cache); |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 2898 | goto out; |
Max Reitz | 4005595 | 2014-07-22 22:58:42 +0200 | [diff] [blame] | 2899 | } |
| 2900 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2901 | /* The source files are opened read-only, don't care about WCE */ |
| 2902 | assert((src_flags & BDRV_O_RDWR) == 0); |
| 2903 | (void) src_writethrough; |
| 2904 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2905 | /* |
| 2906 | * Open the images. |
| 2907 | * |
| 2908 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 2909 | * the reference to a renamed or moved backing file. |
| 2910 | */ |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 2911 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2912 | if (!blk) { |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 2913 | ret = -1; |
| 2914 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2915 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 2916 | bs = blk_bs(blk); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2917 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2918 | if (out_basefmt != NULL) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2919 | if (bdrv_find_format(out_basefmt) == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2920 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2921 | ret = -1; |
| 2922 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | /* For safe rebasing we need to compare old and new backing file */ |
Stefan Hajnoczi | 40ed35a | 2014-08-26 19:17:56 +0100 | [diff] [blame] | 2927 | if (!unsafe) { |
Jeff Cody | 9a29e18 | 2015-01-22 08:03:30 -0500 | [diff] [blame] | 2928 | char backing_name[PATH_MAX]; |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2929 | QDict *options = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2930 | |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2931 | if (bs->backing_format[0] != '\0') { |
| 2932 | options = qdict_new(); |
| 2933 | qdict_put(options, "driver", qstring_from_str(bs->backing_format)); |
| 2934 | } |
| 2935 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2936 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2937 | blk_old_backing = blk_new_open(backing_name, NULL, |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2938 | options, src_flags, &local_err); |
| 2939 | if (!blk_old_backing) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2940 | error_reportf_err(local_err, |
| 2941 | "Could not open old backing file '%s': ", |
| 2942 | backing_name); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2943 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2944 | } |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2945 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2946 | if (out_baseimg[0]) { |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2947 | if (out_basefmt) { |
| 2948 | options = qdict_new(); |
| 2949 | qdict_put(options, "driver", qstring_from_str(out_basefmt)); |
| 2950 | } else { |
| 2951 | options = NULL; |
| 2952 | } |
| 2953 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 2954 | blk_new_backing = blk_new_open(out_baseimg, NULL, |
Max Reitz | 644483d | 2015-02-05 13:58:17 -0500 | [diff] [blame] | 2955 | options, src_flags, &local_err); |
| 2956 | if (!blk_new_backing) { |
Markus Armbruster | c29b77f | 2015-12-18 16:35:14 +0100 | [diff] [blame] | 2957 | error_reportf_err(local_err, |
| 2958 | "Could not open new backing file '%s': ", |
| 2959 | out_baseimg); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2960 | goto out; |
| 2961 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | /* |
| 2966 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 2967 | * accesses go to the backing file. We must therefore compare this cluster |
| 2968 | * in the old and new backing file, and if they differ we need to copy it |
| 2969 | * from the old backing file into the COW file. |
| 2970 | * |
| 2971 | * If qemu-img crashes during this step, no harm is done. The content of |
| 2972 | * the image is the same as the original one at any time. |
| 2973 | */ |
| 2974 | if (!unsafe) { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2975 | int64_t num_sectors; |
| 2976 | int64_t old_backing_num_sectors; |
| 2977 | int64_t new_backing_num_sectors = 0; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2978 | uint64_t sector; |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 2979 | int n; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 2980 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 2981 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2982 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
| 2983 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2984 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2985 | num_sectors = blk_nb_sectors(blk); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2986 | if (num_sectors < 0) { |
| 2987 | error_report("Could not get size of '%s': %s", |
| 2988 | filename, strerror(-num_sectors)); |
| 2989 | ret = -1; |
| 2990 | goto out; |
| 2991 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 2992 | old_backing_num_sectors = blk_nb_sectors(blk_old_backing); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2993 | if (old_backing_num_sectors < 0) { |
Jeff Cody | 9a29e18 | 2015-01-22 08:03:30 -0500 | [diff] [blame] | 2994 | char backing_name[PATH_MAX]; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 2995 | |
| 2996 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
| 2997 | error_report("Could not get size of '%s': %s", |
| 2998 | backing_name, strerror(-old_backing_num_sectors)); |
| 2999 | ret = -1; |
| 3000 | goto out; |
| 3001 | } |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3002 | if (blk_new_backing) { |
| 3003 | new_backing_num_sectors = blk_nb_sectors(blk_new_backing); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 3004 | if (new_backing_num_sectors < 0) { |
| 3005 | error_report("Could not get size of '%s': %s", |
| 3006 | out_baseimg, strerror(-new_backing_num_sectors)); |
| 3007 | ret = -1; |
| 3008 | goto out; |
| 3009 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3010 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3011 | |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 3012 | if (num_sectors != 0) { |
| 3013 | local_progress = (float)100 / |
| 3014 | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); |
| 3015 | } |
| 3016 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3017 | for (sector = 0; sector < num_sectors; sector += n) { |
| 3018 | |
| 3019 | /* How many sectors can we handle with the next read? */ |
| 3020 | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { |
| 3021 | n = (IO_BUF_SIZE / 512); |
| 3022 | } else { |
| 3023 | n = num_sectors - sector; |
| 3024 | } |
| 3025 | |
| 3026 | /* If the cluster is allocated, we don't need to take action */ |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3027 | ret = bdrv_is_allocated(bs, sector, n, &n); |
Paolo Bonzini | d663640 | 2013-09-04 19:00:25 +0200 | [diff] [blame] | 3028 | if (ret < 0) { |
| 3029 | error_report("error while reading image metadata: %s", |
| 3030 | strerror(-ret)); |
| 3031 | goto out; |
| 3032 | } |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 3033 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3034 | continue; |
| 3035 | } |
| 3036 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3037 | /* |
| 3038 | * Read old and new backing file and take into consideration that |
| 3039 | * backing files may be smaller than the COW image. |
| 3040 | */ |
| 3041 | if (sector >= old_backing_num_sectors) { |
| 3042 | memset(buf_old, 0, n * BDRV_SECTOR_SIZE); |
| 3043 | } else { |
| 3044 | if (sector + n > old_backing_num_sectors) { |
| 3045 | n = old_backing_num_sectors - sector; |
| 3046 | } |
| 3047 | |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 3048 | ret = blk_pread(blk_old_backing, sector << BDRV_SECTOR_BITS, |
| 3049 | buf_old, n << BDRV_SECTOR_BITS); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3050 | if (ret < 0) { |
| 3051 | error_report("error while reading from old backing file"); |
| 3052 | goto out; |
| 3053 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3054 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3055 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3056 | if (sector >= new_backing_num_sectors || !blk_new_backing) { |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3057 | memset(buf_new, 0, n * BDRV_SECTOR_SIZE); |
| 3058 | } else { |
| 3059 | if (sector + n > new_backing_num_sectors) { |
| 3060 | n = new_backing_num_sectors - sector; |
| 3061 | } |
| 3062 | |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 3063 | ret = blk_pread(blk_new_backing, sector << BDRV_SECTOR_BITS, |
| 3064 | buf_new, n << BDRV_SECTOR_BITS); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 3065 | if (ret < 0) { |
| 3066 | error_report("error while reading from new backing file"); |
| 3067 | goto out; |
| 3068 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | /* If they differ, we need to write to the COW file */ |
| 3072 | uint64_t written = 0; |
| 3073 | |
| 3074 | while (written < n) { |
| 3075 | int pnum; |
| 3076 | |
| 3077 | if (compare_sectors(buf_old + written * 512, |
Kevin Wolf | 60b1bd4 | 2010-02-17 12:32:59 +0100 | [diff] [blame] | 3078 | buf_new + written * 512, n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3079 | { |
Eric Blake | 9166920 | 2016-05-06 10:26:43 -0600 | [diff] [blame] | 3080 | ret = blk_pwrite(blk, |
| 3081 | (sector + written) << BDRV_SECTOR_BITS, |
| 3082 | buf_old + written * 512, |
| 3083 | pnum << BDRV_SECTOR_BITS, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3084 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3085 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3086 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3087 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3088 | } |
| 3089 | } |
| 3090 | |
| 3091 | written += pnum; |
| 3092 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3093 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | /* |
| 3098 | * Change the backing file. All clusters that are different from the old |
| 3099 | * backing file are overwritten in the COW file now, so the visible content |
| 3100 | * doesn't change when we switch the backing file. |
| 3101 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 3102 | if (out_baseimg && *out_baseimg) { |
| 3103 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
| 3104 | } else { |
| 3105 | ret = bdrv_change_backing_file(bs, NULL, NULL); |
| 3106 | } |
| 3107 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3108 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3109 | error_report("Could not change the backing file to '%s': No " |
| 3110 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3111 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3112 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3113 | out_baseimg, strerror(-ret)); |
| 3114 | } |
| 3115 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3116 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3117 | /* |
| 3118 | * TODO At this point it is possible to check if any clusters that are |
| 3119 | * allocated in the COW file are the same in the backing file. If so, they |
| 3120 | * could be dropped from the COW file. Don't do this before switching the |
| 3121 | * backing file, in case of a crash this would lead to corruption. |
| 3122 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3123 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 3124 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3125 | /* Cleanup */ |
| 3126 | if (!unsafe) { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3127 | blk_unref(blk_old_backing); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3128 | blk_unref(blk_new_backing); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3129 | } |
Paolo Bonzini | 396374c | 2016-02-25 23:53:54 +0100 | [diff] [blame] | 3130 | qemu_vfree(buf_old); |
| 3131 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3132 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3133 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3134 | if (ret) { |
| 3135 | return 1; |
| 3136 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 3137 | return 0; |
| 3138 | } |
| 3139 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3140 | static int img_resize(int argc, char **argv) |
| 3141 | { |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3142 | Error *err = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3143 | int c, ret, relative; |
| 3144 | const char *filename, *fmt, *size; |
| 3145 | int64_t n, total_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3146 | bool quiet = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3147 | BlockBackend *blk = NULL; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3148 | QemuOpts *param; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3149 | |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3150 | static QemuOptsList resize_options = { |
| 3151 | .name = "resize_options", |
| 3152 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 3153 | .desc = { |
| 3154 | { |
| 3155 | .name = BLOCK_OPT_SIZE, |
| 3156 | .type = QEMU_OPT_SIZE, |
| 3157 | .help = "Virtual disk size" |
| 3158 | }, { |
| 3159 | /* end of list */ |
| 3160 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3161 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3162 | }; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3163 | bool image_opts = false; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3164 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3165 | /* Remove size from argv manually so that negative numbers are not treated |
| 3166 | * as options by getopt. */ |
| 3167 | if (argc < 3) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3168 | error_exit("Not enough arguments"); |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 3169 | return 1; |
| 3170 | } |
| 3171 | |
| 3172 | size = argv[--argc]; |
| 3173 | |
| 3174 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3175 | fmt = NULL; |
| 3176 | for(;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3177 | static const struct option long_options[] = { |
| 3178 | {"help", no_argument, 0, 'h'}, |
| 3179 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3180 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3181 | {0, 0, 0, 0} |
| 3182 | }; |
| 3183 | c = getopt_long(argc, argv, "f:hq", |
| 3184 | long_options, NULL); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3185 | if (c == -1) { |
| 3186 | break; |
| 3187 | } |
| 3188 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 3189 | case '?': |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3190 | case 'h': |
| 3191 | help(); |
| 3192 | break; |
| 3193 | case 'f': |
| 3194 | fmt = optarg; |
| 3195 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3196 | case 'q': |
| 3197 | quiet = true; |
| 3198 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3199 | case OPTION_OBJECT: { |
| 3200 | QemuOpts *opts; |
| 3201 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3202 | optarg, true); |
| 3203 | if (!opts) { |
| 3204 | return 1; |
| 3205 | } |
| 3206 | } break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3207 | case OPTION_IMAGE_OPTS: |
| 3208 | image_opts = true; |
| 3209 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3210 | } |
| 3211 | } |
Kevin Wolf | fc11eb2 | 2013-08-05 10:53:04 +0200 | [diff] [blame] | 3212 | if (optind != argc - 1) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3213 | error_exit("Expecting one image file name"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3214 | } |
| 3215 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3216 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3217 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3218 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 3219 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3220 | return 1; |
| 3221 | } |
| 3222 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3223 | /* Choose grow, shrink, or absolute resize mode */ |
| 3224 | switch (size[0]) { |
| 3225 | case '+': |
| 3226 | relative = 1; |
| 3227 | size++; |
| 3228 | break; |
| 3229 | case '-': |
| 3230 | relative = -1; |
| 3231 | size++; |
| 3232 | break; |
| 3233 | default: |
| 3234 | relative = 0; |
| 3235 | break; |
| 3236 | } |
| 3237 | |
| 3238 | /* Parse size */ |
Peter Crosthwaite | 87ea75d | 2014-01-01 18:49:17 -0800 | [diff] [blame] | 3239 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
Markus Armbruster | f43e47d | 2015-02-12 17:52:20 +0100 | [diff] [blame] | 3240 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
Markus Armbruster | 6750e79 | 2015-02-12 17:43:08 +0100 | [diff] [blame] | 3241 | if (err) { |
| 3242 | error_report_err(err); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3243 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3244 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3245 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3246 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 3247 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 3248 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3249 | |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 3250 | blk = img_open(image_opts, filename, fmt, |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3251 | BDRV_O_RDWR, false, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3252 | if (!blk) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 3253 | ret = -1; |
| 3254 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3255 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3256 | |
| 3257 | if (relative) { |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3258 | total_size = blk_getlength(blk) + n * relative; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3259 | } else { |
| 3260 | total_size = n; |
| 3261 | } |
| 3262 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3263 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3264 | ret = -1; |
| 3265 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3266 | } |
| 3267 | |
Max Reitz | f1d3cd7 | 2015-02-05 13:58:18 -0500 | [diff] [blame] | 3268 | ret = blk_truncate(blk, total_size); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3269 | switch (ret) { |
| 3270 | case 0: |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 3271 | qprintf(quiet, "Image resized.\n"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3272 | break; |
| 3273 | case -ENOTSUP: |
Kevin Wolf | 259b217 | 2012-03-06 12:44:45 +0100 | [diff] [blame] | 3274 | error_report("This image does not support resize"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3275 | break; |
| 3276 | case -EACCES: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3277 | error_report("Image is read-only"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3278 | break; |
| 3279 | default: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 3280 | error_report("Error resizing image (%d)", -ret); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3281 | break; |
| 3282 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3283 | out: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3284 | blk_unref(blk); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 3285 | if (ret) { |
| 3286 | return 1; |
| 3287 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 3288 | return 0; |
| 3289 | } |
| 3290 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3291 | static void amend_status_cb(BlockDriverState *bs, |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 3292 | int64_t offset, int64_t total_work_size, |
| 3293 | void *opaque) |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3294 | { |
| 3295 | qemu_progress_print(100.f * offset / total_work_size, 0); |
| 3296 | } |
| 3297 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3298 | static int img_amend(int argc, char **argv) |
| 3299 | { |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3300 | Error *err = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3301 | int c, ret = 0; |
| 3302 | char *options = NULL; |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3303 | QemuOptsList *create_opts = NULL; |
| 3304 | QemuOpts *opts = NULL; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3305 | const char *fmt = NULL, *filename, *cache; |
| 3306 | int flags; |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3307 | bool writethrough; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3308 | bool quiet = false, progress = false; |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3309 | BlockBackend *blk = NULL; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3310 | BlockDriverState *bs = NULL; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3311 | bool image_opts = false; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3312 | |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3313 | cache = BDRV_DEFAULT_CACHE; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3314 | for (;;) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3315 | static const struct option long_options[] = { |
| 3316 | {"help", no_argument, 0, 'h'}, |
| 3317 | {"object", required_argument, 0, OPTION_OBJECT}, |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3318 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3319 | {0, 0, 0, 0} |
| 3320 | }; |
| 3321 | c = getopt_long(argc, argv, "ho:f:t:pq", |
| 3322 | long_options, NULL); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3323 | if (c == -1) { |
| 3324 | break; |
| 3325 | } |
| 3326 | |
| 3327 | switch (c) { |
| 3328 | case 'h': |
| 3329 | case '?': |
| 3330 | help(); |
| 3331 | break; |
| 3332 | case 'o': |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3333 | if (!is_valid_option_list(optarg)) { |
| 3334 | error_report("Invalid option list: %s", optarg); |
| 3335 | ret = -1; |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 3336 | goto out_no_progress; |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3337 | } |
| 3338 | if (!options) { |
| 3339 | options = g_strdup(optarg); |
| 3340 | } else { |
| 3341 | char *old_options = options; |
| 3342 | options = g_strdup_printf("%s,%s", options, optarg); |
| 3343 | g_free(old_options); |
| 3344 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3345 | break; |
| 3346 | case 'f': |
| 3347 | fmt = optarg; |
| 3348 | break; |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3349 | case 't': |
| 3350 | cache = optarg; |
| 3351 | break; |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3352 | case 'p': |
| 3353 | progress = true; |
| 3354 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3355 | case 'q': |
| 3356 | quiet = true; |
| 3357 | break; |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3358 | case OPTION_OBJECT: |
| 3359 | opts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 3360 | optarg, true); |
| 3361 | if (!opts) { |
| 3362 | ret = -1; |
| 3363 | goto out_no_progress; |
| 3364 | } |
| 3365 | break; |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3366 | case OPTION_IMAGE_OPTS: |
| 3367 | image_opts = true; |
| 3368 | break; |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3369 | } |
| 3370 | } |
| 3371 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3372 | if (!options) { |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3373 | error_exit("Must specify options (-o)"); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3376 | if (qemu_opts_foreach(&qemu_object_opts, |
| 3377 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 3378 | NULL, NULL)) { |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3379 | ret = -1; |
| 3380 | goto out_no_progress; |
| 3381 | } |
| 3382 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3383 | if (quiet) { |
| 3384 | progress = false; |
| 3385 | } |
| 3386 | qemu_progress_init(progress, 1.0); |
| 3387 | |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3388 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
| 3389 | if (fmt && has_help_option(options)) { |
| 3390 | /* If a format is explicitly specified (and possibly no filename is |
| 3391 | * given), print option help here */ |
| 3392 | ret = print_block_option_help(filename, fmt); |
| 3393 | goto out; |
| 3394 | } |
| 3395 | |
| 3396 | if (optind != argc - 1) { |
Max Reitz | b2f27e4 | 2014-10-27 11:12:52 +0100 | [diff] [blame] | 3397 | error_report("Expecting one image file name"); |
| 3398 | ret = -1; |
| 3399 | goto out; |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3400 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3401 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3402 | flags = BDRV_O_RDWR; |
| 3403 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
Max Reitz | bd39e6e | 2014-07-22 22:58:43 +0200 | [diff] [blame] | 3404 | if (ret < 0) { |
| 3405 | error_report("Invalid cache option: %s", cache); |
| 3406 | goto out; |
| 3407 | } |
| 3408 | |
Kevin Wolf | ce09954 | 2016-03-15 13:01:04 +0100 | [diff] [blame] | 3409 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3410 | if (!blk) { |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3411 | ret = -1; |
| 3412 | goto out; |
| 3413 | } |
Markus Armbruster | 7e7d56d | 2014-10-07 13:59:05 +0200 | [diff] [blame] | 3414 | bs = blk_bs(blk); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3415 | |
| 3416 | fmt = bs->drv->format_name; |
| 3417 | |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3418 | if (has_help_option(options)) { |
Kevin Wolf | a283cb6 | 2014-02-21 16:24:07 +0100 | [diff] [blame] | 3419 | /* If the format was auto-detected, print option help here */ |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3420 | ret = print_block_option_help(filename, fmt); |
| 3421 | goto out; |
| 3422 | } |
| 3423 | |
Max Reitz | b2439d2 | 2014-12-02 18:32:47 +0100 | [diff] [blame] | 3424 | if (!bs->drv->create_opts) { |
| 3425 | error_report("Format driver '%s' does not support any options to amend", |
| 3426 | fmt); |
| 3427 | ret = -1; |
| 3428 | goto out; |
| 3429 | } |
| 3430 | |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 3431 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3432 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3433 | if (options) { |
| 3434 | qemu_opts_do_parse(opts, options, NULL, &err); |
| 3435 | if (err) { |
Markus Armbruster | 97a2ca7 | 2015-03-14 10:23:15 +0100 | [diff] [blame] | 3436 | error_report_err(err); |
Markus Armbruster | dc523cd34 | 2015-02-12 18:37:11 +0100 | [diff] [blame] | 3437 | ret = -1; |
| 3438 | goto out; |
| 3439 | } |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3440 | } |
| 3441 | |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3442 | /* In case the driver does not call amend_status_cb() */ |
| 3443 | qemu_progress_print(0.f, 0); |
Max Reitz | 8b13976 | 2015-07-27 17:51:32 +0200 | [diff] [blame] | 3444 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL); |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3445 | qemu_progress_print(100.f, 0); |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3446 | if (ret < 0) { |
| 3447 | error_report("Error while amending options: %s", strerror(-ret)); |
| 3448 | goto out; |
| 3449 | } |
| 3450 | |
| 3451 | out: |
Max Reitz | 76a3a34 | 2014-10-27 11:12:51 +0100 | [diff] [blame] | 3452 | qemu_progress_end(); |
| 3453 | |
Max Reitz | e814dff | 2015-08-20 16:00:38 -0700 | [diff] [blame] | 3454 | out_no_progress: |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 3455 | blk_unref(blk); |
Chunyan Liu | 83d0521 | 2014-06-05 17:20:51 +0800 | [diff] [blame] | 3456 | qemu_opts_del(opts); |
| 3457 | qemu_opts_free(create_opts); |
Kevin Wolf | 626f84f | 2014-02-21 16:24:06 +0100 | [diff] [blame] | 3458 | g_free(options); |
| 3459 | |
Max Reitz | 6f176b4 | 2013-09-03 10:09:50 +0200 | [diff] [blame] | 3460 | if (ret) { |
| 3461 | return 1; |
| 3462 | } |
| 3463 | return 0; |
| 3464 | } |
| 3465 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3466 | typedef struct BenchData { |
| 3467 | BlockBackend *blk; |
| 3468 | uint64_t image_size; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3469 | bool write; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3470 | int bufsize; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3471 | int step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3472 | int nrreq; |
| 3473 | int n; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3474 | int flush_interval; |
| 3475 | bool drain_on_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3476 | uint8_t *buf; |
| 3477 | QEMUIOVector *qiov; |
| 3478 | |
| 3479 | int in_flight; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3480 | bool in_flush; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3481 | uint64_t offset; |
| 3482 | } BenchData; |
| 3483 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3484 | static void bench_undrained_flush_cb(void *opaque, int ret) |
| 3485 | { |
| 3486 | if (ret < 0) { |
| 3487 | error_report("Failed flush request: %s\n", strerror(-ret)); |
| 3488 | exit(EXIT_FAILURE); |
| 3489 | } |
| 3490 | } |
| 3491 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3492 | static void bench_cb(void *opaque, int ret) |
| 3493 | { |
| 3494 | BenchData *b = opaque; |
| 3495 | BlockAIOCB *acb; |
| 3496 | |
| 3497 | if (ret < 0) { |
| 3498 | error_report("Failed request: %s\n", strerror(-ret)); |
| 3499 | exit(EXIT_FAILURE); |
| 3500 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3501 | |
| 3502 | if (b->in_flush) { |
| 3503 | /* Just finished a flush with drained queue: Start next requests */ |
| 3504 | assert(b->in_flight == 0); |
| 3505 | b->in_flush = false; |
| 3506 | } else if (b->in_flight > 0) { |
| 3507 | int remaining = b->n - b->in_flight; |
| 3508 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3509 | b->n--; |
| 3510 | b->in_flight--; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3511 | |
| 3512 | /* Time for flush? Drain queue if requested, then flush */ |
| 3513 | if (b->flush_interval && remaining % b->flush_interval == 0) { |
| 3514 | if (!b->in_flight || !b->drain_on_flush) { |
| 3515 | BlockCompletionFunc *cb; |
| 3516 | |
| 3517 | if (b->drain_on_flush) { |
| 3518 | b->in_flush = true; |
| 3519 | cb = bench_cb; |
| 3520 | } else { |
| 3521 | cb = bench_undrained_flush_cb; |
| 3522 | } |
| 3523 | |
| 3524 | acb = blk_aio_flush(b->blk, cb, b); |
| 3525 | if (!acb) { |
| 3526 | error_report("Failed to issue flush request"); |
| 3527 | exit(EXIT_FAILURE); |
| 3528 | } |
| 3529 | } |
| 3530 | if (b->drain_on_flush) { |
| 3531 | return; |
| 3532 | } |
| 3533 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3537 | if (b->write) { |
| 3538 | acb = blk_aio_pwritev(b->blk, b->offset, b->qiov, 0, |
| 3539 | bench_cb, b); |
| 3540 | } else { |
| 3541 | acb = blk_aio_preadv(b->blk, b->offset, b->qiov, 0, |
| 3542 | bench_cb, b); |
| 3543 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3544 | if (!acb) { |
| 3545 | error_report("Failed to issue request"); |
| 3546 | exit(EXIT_FAILURE); |
| 3547 | } |
| 3548 | b->in_flight++; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3549 | b->offset += b->step; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3550 | b->offset %= b->image_size; |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | static int img_bench(int argc, char **argv) |
| 3555 | { |
| 3556 | int c, ret = 0; |
| 3557 | const char *fmt = NULL, *filename; |
| 3558 | bool quiet = false; |
| 3559 | bool image_opts = false; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3560 | bool is_write = false; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3561 | int count = 75000; |
| 3562 | int depth = 64; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3563 | int64_t offset = 0; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3564 | size_t bufsize = 4096; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3565 | int pattern = 0; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3566 | size_t step = 0; |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3567 | int flush_interval = 0; |
| 3568 | bool drain_on_flush = true; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3569 | int64_t image_size; |
| 3570 | BlockBackend *blk = NULL; |
| 3571 | BenchData data = {}; |
| 3572 | int flags = 0; |
| 3573 | bool writethrough; |
| 3574 | struct timeval t1, t2; |
| 3575 | int i; |
| 3576 | |
| 3577 | for (;;) { |
| 3578 | static const struct option long_options[] = { |
| 3579 | {"help", no_argument, 0, 'h'}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3580 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3581 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3582 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3583 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3584 | {0, 0, 0, 0} |
| 3585 | }; |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3586 | c = getopt_long(argc, argv, "hc:d:f:no:qs:S:t:w", long_options, NULL); |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3587 | if (c == -1) { |
| 3588 | break; |
| 3589 | } |
| 3590 | |
| 3591 | switch (c) { |
| 3592 | case 'h': |
| 3593 | case '?': |
| 3594 | help(); |
| 3595 | break; |
| 3596 | case 'c': |
| 3597 | { |
| 3598 | char *end; |
| 3599 | errno = 0; |
| 3600 | count = strtoul(optarg, &end, 0); |
| 3601 | if (errno || *end || count > INT_MAX) { |
| 3602 | error_report("Invalid request count specified"); |
| 3603 | return 1; |
| 3604 | } |
| 3605 | break; |
| 3606 | } |
| 3607 | case 'd': |
| 3608 | { |
| 3609 | char *end; |
| 3610 | errno = 0; |
| 3611 | depth = strtoul(optarg, &end, 0); |
| 3612 | if (errno || *end || depth > INT_MAX) { |
| 3613 | error_report("Invalid queue depth specified"); |
| 3614 | return 1; |
| 3615 | } |
| 3616 | break; |
| 3617 | } |
| 3618 | case 'f': |
| 3619 | fmt = optarg; |
| 3620 | break; |
| 3621 | case 'n': |
| 3622 | flags |= BDRV_O_NATIVE_AIO; |
| 3623 | break; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3624 | case 'o': |
| 3625 | { |
| 3626 | char *end; |
| 3627 | errno = 0; |
| 3628 | offset = qemu_strtosz_suffix(optarg, &end, |
| 3629 | QEMU_STRTOSZ_DEFSUFFIX_B); |
| 3630 | if (offset < 0|| *end) { |
| 3631 | error_report("Invalid offset specified"); |
| 3632 | return 1; |
| 3633 | } |
| 3634 | break; |
| 3635 | } |
| 3636 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3637 | case 'q': |
| 3638 | quiet = true; |
| 3639 | break; |
| 3640 | case 's': |
| 3641 | { |
| 3642 | int64_t sval; |
| 3643 | char *end; |
| 3644 | |
| 3645 | sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B); |
| 3646 | if (sval < 0 || sval > INT_MAX || *end) { |
| 3647 | error_report("Invalid buffer size specified"); |
| 3648 | return 1; |
| 3649 | } |
| 3650 | |
| 3651 | bufsize = sval; |
| 3652 | break; |
| 3653 | } |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3654 | case 'S': |
| 3655 | { |
| 3656 | int64_t sval; |
| 3657 | char *end; |
| 3658 | |
| 3659 | sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B); |
| 3660 | if (sval < 0 || sval > INT_MAX || *end) { |
| 3661 | error_report("Invalid step size specified"); |
| 3662 | return 1; |
| 3663 | } |
| 3664 | |
| 3665 | step = sval; |
| 3666 | break; |
| 3667 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3668 | case 't': |
| 3669 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); |
| 3670 | if (ret < 0) { |
| 3671 | error_report("Invalid cache mode"); |
| 3672 | ret = -1; |
| 3673 | goto out; |
| 3674 | } |
| 3675 | break; |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3676 | case 'w': |
| 3677 | flags |= BDRV_O_RDWR; |
| 3678 | is_write = true; |
| 3679 | break; |
| 3680 | case OPTION_PATTERN: |
| 3681 | { |
| 3682 | char *end; |
| 3683 | errno = 0; |
| 3684 | pattern = strtoul(optarg, &end, 0); |
| 3685 | if (errno || *end || pattern > 0xff) { |
| 3686 | error_report("Invalid pattern byte specified"); |
| 3687 | return 1; |
| 3688 | } |
| 3689 | break; |
| 3690 | } |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3691 | case OPTION_FLUSH_INTERVAL: |
| 3692 | { |
| 3693 | char *end; |
| 3694 | errno = 0; |
| 3695 | flush_interval = strtoul(optarg, &end, 0); |
| 3696 | if (errno || *end || flush_interval > INT_MAX) { |
| 3697 | error_report("Invalid flush interval specified"); |
| 3698 | return 1; |
| 3699 | } |
| 3700 | break; |
| 3701 | } |
| 3702 | case OPTION_NO_DRAIN: |
| 3703 | drain_on_flush = false; |
| 3704 | break; |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3705 | case OPTION_IMAGE_OPTS: |
| 3706 | image_opts = true; |
| 3707 | break; |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | if (optind != argc - 1) { |
| 3712 | error_exit("Expecting one image file name"); |
| 3713 | } |
| 3714 | filename = argv[argc - 1]; |
| 3715 | |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3716 | if (!is_write && flush_interval) { |
| 3717 | error_report("--flush-interval is only available in write tests"); |
| 3718 | ret = -1; |
| 3719 | goto out; |
| 3720 | } |
| 3721 | if (flush_interval && flush_interval < depth) { |
| 3722 | error_report("Flush interval can't be smaller than depth"); |
| 3723 | ret = -1; |
| 3724 | goto out; |
| 3725 | } |
| 3726 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3727 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet); |
| 3728 | if (!blk) { |
| 3729 | ret = -1; |
| 3730 | goto out; |
| 3731 | } |
| 3732 | |
| 3733 | image_size = blk_getlength(blk); |
| 3734 | if (image_size < 0) { |
| 3735 | ret = image_size; |
| 3736 | goto out; |
| 3737 | } |
| 3738 | |
| 3739 | data = (BenchData) { |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3740 | .blk = blk, |
| 3741 | .image_size = image_size, |
| 3742 | .bufsize = bufsize, |
| 3743 | .step = step ?: bufsize, |
| 3744 | .nrreq = depth, |
| 3745 | .n = count, |
| 3746 | .offset = offset, |
| 3747 | .write = is_write, |
| 3748 | .flush_interval = flush_interval, |
| 3749 | .drain_on_flush = drain_on_flush, |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3750 | }; |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3751 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3752 | "(starting at offset %" PRId64 ", step size %d)\n", |
Kevin Wolf | d3199a3 | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3753 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
Kevin Wolf | 83de9be | 2015-07-13 13:13:17 +0200 | [diff] [blame] | 3754 | data.offset, data.step); |
Kevin Wolf | 55d539c | 2016-06-03 13:59:41 +0200 | [diff] [blame] | 3755 | if (flush_interval) { |
| 3756 | printf("Sending flush every %d requests\n", flush_interval); |
| 3757 | } |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3758 | |
| 3759 | data.buf = blk_blockalign(blk, data.nrreq * data.bufsize); |
Kevin Wolf | b6495fa | 2015-07-10 18:09:18 +0200 | [diff] [blame] | 3760 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
| 3761 | |
Kevin Wolf | b6133b8 | 2014-08-05 14:17:13 +0200 | [diff] [blame] | 3762 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
| 3763 | for (i = 0; i < data.nrreq; i++) { |
| 3764 | qemu_iovec_init(&data.qiov[i], 1); |
| 3765 | qemu_iovec_add(&data.qiov[i], |
| 3766 | data.buf + i * data.bufsize, data.bufsize); |
| 3767 | } |
| 3768 | |
| 3769 | gettimeofday(&t1, NULL); |
| 3770 | bench_cb(&data, 0); |
| 3771 | |
| 3772 | while (data.n > 0) { |
| 3773 | main_loop_wait(false); |
| 3774 | } |
| 3775 | gettimeofday(&t2, NULL); |
| 3776 | |
| 3777 | printf("Run completed in %3.3f seconds.\n", |
| 3778 | (t2.tv_sec - t1.tv_sec) |
| 3779 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); |
| 3780 | |
| 3781 | out: |
| 3782 | qemu_vfree(data.buf); |
| 3783 | blk_unref(blk); |
| 3784 | |
| 3785 | if (ret) { |
| 3786 | return 1; |
| 3787 | } |
| 3788 | return 0; |
| 3789 | } |
| 3790 | |
| 3791 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3792 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3793 | #define DEF(option, callback, arg_string) \ |
| 3794 | { option, callback }, |
| 3795 | #include "qemu-img-cmds.h" |
| 3796 | #undef DEF |
| 3797 | #undef GEN_DOCS |
| 3798 | { NULL, NULL, }, |
| 3799 | }; |
| 3800 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3801 | int main(int argc, char **argv) |
| 3802 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 3803 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3804 | const char *cmdname; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 3805 | Error *local_error = NULL; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3806 | int c; |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3807 | static const struct option long_options[] = { |
| 3808 | {"help", no_argument, 0, 'h'}, |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 3809 | {"version", no_argument, 0, 'v'}, |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3810 | {0, 0, 0, 0} |
| 3811 | }; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3812 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 3813 | #ifdef CONFIG_POSIX |
| 3814 | signal(SIGPIPE, SIG_IGN); |
| 3815 | #endif |
| 3816 | |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 3817 | error_set_progname(argv[0]); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 3818 | qemu_init_exec_dir(argv[0]); |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 3819 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 3820 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 3821 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 3822 | exit(EXIT_FAILURE); |
| 3823 | } |
| 3824 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 3825 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 3826 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 3827 | module_call_init(MODULE_INIT_QOM); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3828 | bdrv_init(); |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3829 | if (argc < 2) { |
| 3830 | error_exit("Not enough arguments"); |
| 3831 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3832 | cmdname = argv[1]; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3833 | |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3834 | qemu_add_opts(&qemu_object_opts); |
Daniel P. Berrange | eb769f7 | 2016-02-17 10:10:20 +0000 | [diff] [blame] | 3835 | qemu_add_opts(&qemu_source_opts); |
Daniel P. Berrange | 3babeb1 | 2016-02-17 10:10:17 +0000 | [diff] [blame] | 3836 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3837 | /* find the command */ |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 3838 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3839 | if (!strcmp(cmdname, cmd->name)) { |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3840 | return cmd->handler(argc - 1, argv + 1); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3841 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3842 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3843 | |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 3844 | c = getopt_long(argc, argv, "h", long_options, NULL); |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3845 | |
| 3846 | if (c == 'h') { |
| 3847 | help(); |
| 3848 | } |
Jeff Cody | 5f6979c | 2014-04-28 14:37:18 -0400 | [diff] [blame] | 3849 | if (c == 'v') { |
| 3850 | printf(QEMU_IMG_VERSION); |
| 3851 | return 0; |
| 3852 | } |
Jeff Cody | 7db1689 | 2014-04-25 17:02:32 -0400 | [diff] [blame] | 3853 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 3854 | /* not found */ |
Fam Zheng | ac1307a | 2014-04-22 13:36:11 +0800 | [diff] [blame] | 3855 | error_exit("Command not found: %s", cmdname); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 3856 | } |