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