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