aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Command line utility to exercise the QEMU I/O path. |
| 3 | * |
| 4 | * Copyright (C) 2009 Red Hat, Inc. |
| 5 | * Copyright (c) 2003-2005 Silicon Graphics, Inc. |
| 6 | * |
| 7 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 8 | * See the COPYING file in the top-level directory. |
| 9 | */ |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 10 | #include "qemu/osdep.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 11 | #include <getopt.h> |
Stefan Weil | c32d766 | 2009-08-31 22:16:16 +0200 | [diff] [blame] | 12 | #include <libgen.h> |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 13 | |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 14 | #include "qapi/error.h" |
Kevin Wolf | 3d21994 | 2013-06-05 14:19:39 +0200 | [diff] [blame] | 15 | #include "qemu-io.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 16 | #include "qemu/error-report.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 17 | #include "qemu/main-loop.h" |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 18 | #include "qemu/option.h" |
| 19 | #include "qemu/config-file.h" |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 20 | #include "qemu/readline.h" |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 21 | #include "qemu/log.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 22 | #include "qapi/qmp/qstring.h" |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 23 | #include "qapi/qmp/qbool.h" |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 24 | #include "qom/object_interfaces.h" |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 25 | #include "sysemu/block-backend.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 26 | #include "block/block_int.h" |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 27 | #include "trace/control.h" |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 28 | #include "crypto/init.h" |
Eric Blake | 4face32 | 2017-08-03 11:33:51 -0500 | [diff] [blame] | 29 | #include "qemu-version.h" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 30 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 31 | #define CMD_NOFILE_OK 0x01 |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 32 | |
Stefan Weil | f988388 | 2014-03-05 22:23:00 +0100 | [diff] [blame] | 33 | static char *progname; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 34 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 35 | static BlockBackend *qemuio_blk; |
Kevin Wolf | 191c289 | 2010-09-16 13:18:08 +0200 | [diff] [blame] | 36 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 37 | /* qemu-io commands passed using -c */ |
| 38 | static int ncmdline; |
| 39 | static char **cmdline; |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 40 | static bool imageOpts; |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 41 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 42 | static ReadLineState *readline_state; |
| 43 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 44 | static int close_f(BlockBackend *blk, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 45 | { |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 46 | blk_unref(qemuio_blk); |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 47 | qemuio_blk = NULL; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 48 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static const cmdinfo_t close_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 52 | .name = "close", |
| 53 | .altname = "c", |
| 54 | .cfunc = close_f, |
| 55 | .oneline = "close the current open file", |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 58 | static int openfile(char *name, int flags, bool writethrough, bool force_share, |
| 59 | QDict *opts) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 60 | { |
Max Reitz | 34b5d2c | 2013-09-05 14:45:29 +0200 | [diff] [blame] | 61 | Error *local_err = NULL; |
| 62 | |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 63 | if (qemuio_blk) { |
Markus Armbruster | b988468 | 2015-12-18 16:35:18 +0100 | [diff] [blame] | 64 | error_report("file open already, try 'help close'"); |
Markus Armbruster | 29f2601 | 2014-05-28 11:16:59 +0200 | [diff] [blame] | 65 | QDECREF(opts); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 66 | return 1; |
| 67 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 68 | |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 69 | if (force_share) { |
| 70 | if (!opts) { |
| 71 | opts = qdict_new(); |
| 72 | } |
| 73 | if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE) |
| 74 | && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) { |
| 75 | error_report("-U conflicts with image options"); |
| 76 | QDECREF(opts); |
| 77 | return 1; |
| 78 | } |
Eric Blake | 579cf1d | 2017-05-15 14:54:39 -0500 | [diff] [blame] | 79 | qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true); |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 80 | } |
Max Reitz | efaa7c4 | 2016-03-16 19:54:38 +0100 | [diff] [blame] | 81 | qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err); |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 82 | if (!qemuio_blk) { |
Markus Armbruster | b988468 | 2015-12-18 16:35:18 +0100 | [diff] [blame] | 83 | error_reportf_err(local_err, "can't open%s%s: ", |
| 84 | name ? " device " : "", name ?: ""); |
Markus Armbruster | dbb651c | 2014-09-08 18:50:58 +0200 | [diff] [blame] | 85 | return 1; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 86 | } |
Christoph Hellwig | 1db6947 | 2009-07-15 23:11:21 +0200 | [diff] [blame] | 87 | |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 88 | blk_set_enable_write_cache(qemuio_blk, !writethrough); |
Daniel P. Berrange | 8caf021 | 2015-05-12 17:09:21 +0100 | [diff] [blame] | 89 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 90 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 93 | static void open_help(void) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 94 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 95 | printf( |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 96 | "\n" |
| 97 | " opens a new file in the requested mode\n" |
| 98 | "\n" |
| 99 | " Example:\n" |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 100 | " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 101 | "\n" |
| 102 | " Opens a file for subsequent use by all of the other qemu-io commands.\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 103 | " -r, -- open file read-only\n" |
| 104 | " -s, -- use snapshot file\n" |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 105 | " -n, -- disable host cache, short for -t none\n" |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 106 | " -U, -- force shared permissions\n" |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 107 | " -k, -- use kernel AIO implementation (on Linux only)\n" |
| 108 | " -t, -- use the given cache mode for the image\n" |
| 109 | " -d, -- use the given discard mode for the image\n" |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 110 | " -o, -- options to be given to the block driver" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 111 | "\n"); |
| 112 | } |
| 113 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 114 | static int open_f(BlockBackend *blk, int argc, char **argv); |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 115 | |
| 116 | static const cmdinfo_t open_cmd = { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 117 | .name = "open", |
| 118 | .altname = "o", |
| 119 | .cfunc = open_f, |
| 120 | .argmin = 1, |
| 121 | .argmax = -1, |
| 122 | .flags = CMD_NOFILE_OK, |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 123 | .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]", |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 124 | .oneline = "open the file specified by path", |
| 125 | .help = open_help, |
Blue Swirl | 22a2bdc | 2009-11-21 09:06:46 +0000 | [diff] [blame] | 126 | }; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 127 | |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 128 | static QemuOptsList empty_opts = { |
| 129 | .name = "drive", |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 130 | .merge_lists = true, |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 131 | .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head), |
| 132 | .desc = { |
| 133 | /* no elements => accept any params */ |
| 134 | { /* end of list */ } |
| 135 | }, |
| 136 | }; |
| 137 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 138 | static int open_f(BlockBackend *blk, int argc, char **argv) |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 139 | { |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 140 | int flags = BDRV_O_UNMAP; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 141 | int readonly = 0; |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 142 | bool writethrough = true; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 143 | int c; |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 144 | QemuOpts *qopts; |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 145 | QDict *opts; |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 146 | bool force_share = false; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 147 | |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 148 | while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 149 | switch (c) { |
| 150 | case 's': |
| 151 | flags |= BDRV_O_SNAPSHOT; |
| 152 | break; |
| 153 | case 'n': |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 154 | flags |= BDRV_O_NOCACHE; |
| 155 | writethrough = false; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 156 | break; |
| 157 | case 'r': |
| 158 | readonly = 1; |
| 159 | break; |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 160 | case 'k': |
| 161 | flags |= BDRV_O_NATIVE_AIO; |
| 162 | break; |
| 163 | case 't': |
| 164 | if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { |
| 165 | error_report("Invalid cache option: %s", optarg); |
| 166 | qemu_opts_reset(&empty_opts); |
| 167 | return 0; |
| 168 | } |
| 169 | break; |
| 170 | case 'd': |
| 171 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
| 172 | error_report("Invalid discard option: %s", optarg); |
| 173 | qemu_opts_reset(&empty_opts); |
| 174 | return 0; |
| 175 | } |
| 176 | break; |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 177 | case 'o': |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 178 | if (imageOpts) { |
| 179 | printf("--image-opts and 'open -o' are mutually exclusive\n"); |
Eric Blake | b8d970f | 2016-05-07 21:16:41 -0600 | [diff] [blame] | 180 | qemu_opts_reset(&empty_opts); |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 181 | return 0; |
| 182 | } |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 183 | if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) { |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 184 | qemu_opts_reset(&empty_opts); |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 185 | return 0; |
| 186 | } |
Max Reitz | b543c5c | 2013-10-11 14:02:10 +0200 | [diff] [blame] | 187 | break; |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 188 | case 'U': |
| 189 | force_share = true; |
| 190 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 191 | default: |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 192 | qemu_opts_reset(&empty_opts); |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 193 | return qemuio_command_usage(&open_cmd); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 194 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 195 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 196 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 197 | if (!readonly) { |
| 198 | flags |= BDRV_O_RDWR; |
| 199 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 200 | |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 201 | if (imageOpts && (optind == argc - 1)) { |
| 202 | if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) { |
| 203 | qemu_opts_reset(&empty_opts); |
| 204 | return 0; |
| 205 | } |
| 206 | optind++; |
| 207 | } |
| 208 | |
Markus Armbruster | 443422f | 2014-05-28 11:16:58 +0200 | [diff] [blame] | 209 | qopts = qemu_opts_find(&empty_opts, NULL); |
| 210 | opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; |
| 211 | qemu_opts_reset(&empty_opts); |
| 212 | |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 213 | if (optind == argc - 1) { |
Eric Blake | 64ebf55 | 2017-06-05 15:38:42 -0500 | [diff] [blame] | 214 | openfile(argv[optind], flags, writethrough, force_share, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 215 | } else if (optind == argc) { |
Eric Blake | 64ebf55 | 2017-06-05 15:38:42 -0500 | [diff] [blame] | 216 | openfile(NULL, flags, writethrough, force_share, opts); |
Max Reitz | fd0fee3 | 2013-12-20 19:28:20 +0100 | [diff] [blame] | 217 | } else { |
Markus Armbruster | 29f2601 | 2014-05-28 11:16:59 +0200 | [diff] [blame] | 218 | QDECREF(opts); |
Eric Blake | 64ebf55 | 2017-06-05 15:38:42 -0500 | [diff] [blame] | 219 | qemuio_command_usage(&open_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 220 | } |
Eric Blake | 64ebf55 | 2017-06-05 15:38:42 -0500 | [diff] [blame] | 221 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 224 | static int quit_f(BlockBackend *blk, int argc, char **argv) |
Kevin Wolf | e681be7 | 2013-06-05 14:19:34 +0200 | [diff] [blame] | 225 | { |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | static const cmdinfo_t quit_cmd = { |
| 230 | .name = "quit", |
| 231 | .altname = "q", |
| 232 | .cfunc = quit_f, |
| 233 | .argmin = -1, |
| 234 | .argmax = -1, |
| 235 | .flags = CMD_FLAG_GLOBAL, |
| 236 | .oneline = "exit the program", |
| 237 | }; |
| 238 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 239 | static void usage(const char *name) |
| 240 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 241 | printf( |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 242 | "Usage: %s [OPTIONS]... [-c STRING]... [file]\n" |
Stefan Weil | 84844a2 | 2009-06-22 15:08:47 +0200 | [diff] [blame] | 243 | "QEMU Disk exerciser\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 244 | "\n" |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 245 | " --object OBJECTDEF define an object such as 'secret' for\n" |
| 246 | " passwords and/or encryption keys\n" |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 247 | " --image-opts treat file as option string\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 248 | " -c, --cmd STRING execute command with its arguments\n" |
| 249 | " from the given string\n" |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 250 | " -f, --format FMT specifies the block driver to use\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 251 | " -r, --read-only export read-only\n" |
| 252 | " -s, --snapshot use snapshot file\n" |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 253 | " -n, --nocache disable host cache, short for -t none\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 254 | " -m, --misalign misalign allocations for O_DIRECT\n" |
Christoph Hellwig | 5c6c3a6 | 2009-08-20 16:58:35 +0200 | [diff] [blame] | 255 | " -k, --native-aio use kernel AIO implementation (on Linux only)\n" |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 256 | " -t, --cache=MODE use the given cache mode for the image\n" |
Eric Blake | e4e12bb | 2016-05-07 21:16:40 -0600 | [diff] [blame] | 257 | " -d, --discard=MODE use the given discard mode for the image\n" |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 258 | " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 259 | " specify tracing options\n" |
| 260 | " see qemu-img(1) man page for full description\n" |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 261 | " -U, --force-share force shared permissions\n" |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 262 | " -h, --help display this help and exit\n" |
| 263 | " -V, --version output version information and exit\n" |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 264 | "\n" |
Eric Blake | f5048cb | 2017-08-03 11:33:53 -0500 | [diff] [blame] | 265 | "See '%s -c help' for information on available commands.\n" |
| 266 | "\n" |
| 267 | QEMU_HELP_BOTTOM "\n", |
Maria Kustova | d208cc3 | 2014-03-18 09:59:19 +0400 | [diff] [blame] | 268 | name, name); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 271 | static char *get_prompt(void) |
| 272 | { |
| 273 | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; |
| 274 | |
| 275 | if (!prompt[0]) { |
| 276 | snprintf(prompt, sizeof(prompt), "%s> ", progname); |
| 277 | } |
| 278 | |
| 279 | return prompt; |
| 280 | } |
| 281 | |
Stefan Weil | d5d1507 | 2014-01-25 18:18:23 +0100 | [diff] [blame] | 282 | static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, |
| 283 | const char *fmt, ...) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 284 | { |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 285 | va_list ap; |
| 286 | va_start(ap, fmt); |
| 287 | vprintf(fmt, ap); |
| 288 | va_end(ap); |
| 289 | } |
| 290 | |
| 291 | static void readline_flush_func(void *opaque) |
| 292 | { |
| 293 | fflush(stdout); |
| 294 | } |
| 295 | |
| 296 | static void readline_func(void *opaque, const char *str, void *readline_opaque) |
| 297 | { |
| 298 | char **line = readline_opaque; |
| 299 | *line = g_strdup(str); |
| 300 | } |
| 301 | |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 302 | static void completion_match(const char *cmd, void *opaque) |
| 303 | { |
| 304 | readline_add_completion(readline_state, cmd); |
| 305 | } |
| 306 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 307 | static void readline_completion_func(void *opaque, const char *str) |
| 308 | { |
Stefan Hajnoczi | 4694020 | 2013-11-14 11:54:18 +0100 | [diff] [blame] | 309 | readline_set_completion_index(readline_state, strlen(str)); |
| 310 | qemuio_complete_command(str, completion_match, NULL); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static char *fetchline_readline(void) |
| 314 | { |
| 315 | char *line = NULL; |
| 316 | |
| 317 | readline_start(readline_state, get_prompt(), 0, readline_func, &line); |
| 318 | while (!line) { |
| 319 | int ch = getchar(); |
| 320 | if (ch == EOF) { |
| 321 | break; |
| 322 | } |
| 323 | readline_handle_byte(readline_state, ch); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 324 | } |
| 325 | return line; |
| 326 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 327 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 328 | #define MAXREADLINESZ 1024 |
| 329 | static char *fetchline_fgets(void) |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 330 | { |
| 331 | char *p, *line = g_malloc(MAXREADLINESZ); |
| 332 | |
| 333 | if (!fgets(line, MAXREADLINESZ, stdin)) { |
| 334 | g_free(line); |
| 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | p = line + strlen(line); |
| 339 | if (p != line && p[-1] == '\n') { |
| 340 | p[-1] = '\0'; |
| 341 | } |
| 342 | |
| 343 | return line; |
| 344 | } |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 345 | |
| 346 | static char *fetchline(void) |
| 347 | { |
| 348 | if (readline_state) { |
| 349 | return fetchline_readline(); |
| 350 | } else { |
| 351 | return fetchline_fgets(); |
| 352 | } |
| 353 | } |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 354 | |
| 355 | static void prep_fetchline(void *opaque) |
| 356 | { |
| 357 | int *fetchable = opaque; |
| 358 | |
| 359 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 360 | *fetchable= 1; |
| 361 | } |
| 362 | |
| 363 | static void command_loop(void) |
| 364 | { |
| 365 | int i, done = 0, fetchable = 0, prompted = 0; |
| 366 | char *input; |
| 367 | |
| 368 | for (i = 0; !done && i < ncmdline; i++) { |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 369 | done = qemuio_command(qemuio_blk, cmdline[i]); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 370 | } |
| 371 | if (cmdline) { |
| 372 | g_free(cmdline); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | while (!done) { |
| 377 | if (!prompted) { |
| 378 | printf("%s", get_prompt()); |
| 379 | fflush(stdout); |
| 380 | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); |
| 381 | prompted = 1; |
| 382 | } |
| 383 | |
| 384 | main_loop_wait(false); |
| 385 | |
| 386 | if (!fetchable) { |
| 387 | continue; |
| 388 | } |
| 389 | |
| 390 | input = fetchline(); |
| 391 | if (input == NULL) { |
| 392 | break; |
| 393 | } |
Max Reitz | 4c7b7e9 | 2015-02-05 13:58:22 -0500 | [diff] [blame] | 394 | done = qemuio_command(qemuio_blk, input); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 395 | g_free(input); |
| 396 | |
| 397 | prompted = 0; |
| 398 | fetchable = 0; |
| 399 | } |
| 400 | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
| 401 | } |
| 402 | |
| 403 | static void add_user_command(char *optarg) |
| 404 | { |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 405 | cmdline = g_renew(char *, cmdline, ++ncmdline); |
Kevin Wolf | d1174f1 | 2013-06-05 14:19:37 +0200 | [diff] [blame] | 406 | cmdline[ncmdline-1] = optarg; |
| 407 | } |
| 408 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 409 | static void reenable_tty_echo(void) |
| 410 | { |
| 411 | qemu_set_tty_echo(STDIN_FILENO, true); |
| 412 | } |
| 413 | |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 414 | enum { |
| 415 | OPTION_OBJECT = 256, |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 416 | OPTION_IMAGE_OPTS = 257, |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 417 | }; |
| 418 | |
| 419 | static QemuOptsList qemu_object_opts = { |
| 420 | .name = "object", |
| 421 | .implied_opt_name = "qom-type", |
| 422 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), |
| 423 | .desc = { |
| 424 | { } |
| 425 | }, |
| 426 | }; |
| 427 | |
| 428 | |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 429 | static QemuOptsList file_opts = { |
| 430 | .name = "file", |
| 431 | .implied_opt_name = "file", |
| 432 | .head = QTAILQ_HEAD_INITIALIZER(file_opts.head), |
| 433 | .desc = { |
| 434 | /* no elements => accept any params */ |
| 435 | { /* end of list */ } |
| 436 | }, |
| 437 | }; |
| 438 | |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 439 | int main(int argc, char **argv) |
| 440 | { |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 441 | int readonly = 0; |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 442 | const char *sopt = "hVc:d:f:rsnmkt:T:U"; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 443 | const struct option lopt[] = { |
Daniel P. Berrange | a513416 | 2016-02-17 10:10:23 +0000 | [diff] [blame] | 444 | { "help", no_argument, NULL, 'h' }, |
| 445 | { "version", no_argument, NULL, 'V' }, |
Daniel P. Berrange | a513416 | 2016-02-17 10:10:23 +0000 | [diff] [blame] | 446 | { "cmd", required_argument, NULL, 'c' }, |
| 447 | { "format", required_argument, NULL, 'f' }, |
| 448 | { "read-only", no_argument, NULL, 'r' }, |
| 449 | { "snapshot", no_argument, NULL, 's' }, |
| 450 | { "nocache", no_argument, NULL, 'n' }, |
| 451 | { "misalign", no_argument, NULL, 'm' }, |
| 452 | { "native-aio", no_argument, NULL, 'k' }, |
| 453 | { "discard", required_argument, NULL, 'd' }, |
| 454 | { "cache", required_argument, NULL, 't' }, |
| 455 | { "trace", required_argument, NULL, 'T' }, |
| 456 | { "object", required_argument, NULL, OPTION_OBJECT }, |
| 457 | { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS }, |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 458 | { "force-share", no_argument, 0, 'U'}, |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 459 | { NULL, 0, NULL, 0 } |
| 460 | }; |
| 461 | int c; |
| 462 | int opt_index = 0; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 463 | int flags = BDRV_O_UNMAP; |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 464 | bool writethrough = true; |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 465 | Error *local_error = NULL; |
Max Reitz | 1b58b43 | 2015-02-05 13:58:20 -0500 | [diff] [blame] | 466 | QDict *opts = NULL; |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 467 | const char *format = NULL; |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 468 | char *trace_file = NULL; |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 469 | bool force_share = false; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 470 | |
MORITA Kazutaka | 526eda1 | 2013-07-23 17:30:11 +0900 | [diff] [blame] | 471 | #ifdef CONFIG_POSIX |
| 472 | signal(SIGPIPE, SIG_IGN); |
| 473 | #endif |
| 474 | |
Daniel P. Berrange | fe4db84 | 2016-10-04 14:35:52 +0100 | [diff] [blame] | 475 | module_call_init(MODULE_INIT_TRACE); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 476 | progname = basename(argv[0]); |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 477 | qemu_init_exec_dir(argv[0]); |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 478 | |
Eduardo Habkost | e8f2d27 | 2016-05-12 11:10:04 -0300 | [diff] [blame] | 479 | qcrypto_init(&error_fatal); |
Daniel P. Berrange | c229708 | 2016-04-06 12:12:06 +0100 | [diff] [blame] | 480 | |
Daniel P. Berrange | 064097d | 2016-02-10 18:41:01 +0000 | [diff] [blame] | 481 | module_call_init(MODULE_INIT_QOM); |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 482 | qemu_add_opts(&qemu_object_opts); |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 483 | qemu_add_opts(&qemu_trace_opts); |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 484 | bdrv_init(); |
| 485 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 486 | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
| 487 | switch (c) { |
| 488 | case 's': |
| 489 | flags |= BDRV_O_SNAPSHOT; |
| 490 | break; |
| 491 | case 'n': |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 492 | flags |= BDRV_O_NOCACHE; |
| 493 | writethrough = false; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 494 | break; |
Paolo Bonzini | 9e8f183 | 2013-02-08 14:06:11 +0100 | [diff] [blame] | 495 | case 'd': |
| 496 | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
| 497 | error_report("Invalid discard option: %s", optarg); |
| 498 | exit(1); |
| 499 | } |
| 500 | break; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 501 | case 'f': |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 502 | format = optarg; |
Kevin Wolf | be6273d | 2014-11-20 16:27:06 +0100 | [diff] [blame] | 503 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 504 | case 'c': |
| 505 | add_user_command(optarg); |
| 506 | break; |
| 507 | case 'r': |
| 508 | readonly = 1; |
| 509 | break; |
| 510 | case 'm': |
Stefan Weil | f988388 | 2014-03-05 22:23:00 +0100 | [diff] [blame] | 511 | qemuio_misalign = true; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 512 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 513 | case 'k': |
| 514 | flags |= BDRV_O_NATIVE_AIO; |
| 515 | break; |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 516 | case 't': |
Kevin Wolf | e151fc1 | 2016-03-15 12:52:30 +0100 | [diff] [blame] | 517 | if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { |
Kevin Wolf | 592fa07 | 2012-04-18 12:07:39 +0200 | [diff] [blame] | 518 | error_report("Invalid cache option: %s", optarg); |
| 519 | exit(1); |
| 520 | } |
| 521 | break; |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 522 | case 'T': |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 523 | g_free(trace_file); |
| 524 | trace_file = trace_opt_parse(optarg); |
Stefan Hajnoczi | d7bb72c | 2012-03-12 16:36:07 +0000 | [diff] [blame] | 525 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 526 | case 'V': |
Eric Blake | 4face32 | 2017-08-03 11:33:51 -0500 | [diff] [blame] | 527 | printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n" |
| 528 | QEMU_COPYRIGHT "\n", progname); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 529 | exit(0); |
| 530 | case 'h': |
| 531 | usage(progname); |
| 532 | exit(0); |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 533 | case 'U': |
| 534 | force_share = true; |
| 535 | break; |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 536 | case OPTION_OBJECT: { |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 537 | QemuOpts *qopts; |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 538 | qopts = qemu_opts_parse_noisily(&qemu_object_opts, |
| 539 | optarg, true); |
| 540 | if (!qopts) { |
| 541 | exit(1); |
| 542 | } |
| 543 | } break; |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 544 | case OPTION_IMAGE_OPTS: |
| 545 | imageOpts = true; |
| 546 | break; |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 547 | default: |
| 548 | usage(progname); |
| 549 | exit(1); |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 550 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 551 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 552 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 553 | if ((argc - optind) > 1) { |
| 554 | usage(progname); |
| 555 | exit(1); |
| 556 | } |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 557 | |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 558 | if (format && imageOpts) { |
| 559 | error_report("--image-opts and -f are mutually exclusive"); |
| 560 | exit(1); |
| 561 | } |
| 562 | |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 563 | if (qemu_init_main_loop(&local_error)) { |
Markus Armbruster | 565f65d | 2015-02-12 13:55:05 +0100 | [diff] [blame] | 564 | error_report_err(local_error); |
Chrysostomos Nanakos | 2f78e49 | 2014-09-18 14:30:49 +0300 | [diff] [blame] | 565 | exit(1); |
| 566 | } |
Zhi Yong Wu | a57d114 | 2012-02-19 22:24:59 +0800 | [diff] [blame] | 567 | |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 568 | if (qemu_opts_foreach(&qemu_object_opts, |
| 569 | user_creatable_add_opts_foreach, |
Markus Armbruster | 51b9b47 | 2016-04-27 16:29:09 +0200 | [diff] [blame] | 570 | NULL, NULL)) { |
Daniel P. Berrange | 9ba371b | 2016-02-17 10:10:16 +0000 | [diff] [blame] | 571 | exit(1); |
| 572 | } |
| 573 | |
Denis V. Lunev | e9a8085 | 2016-06-17 17:44:11 +0300 | [diff] [blame] | 574 | if (!trace_init_backends()) { |
| 575 | exit(1); |
| 576 | } |
| 577 | trace_init_file(trace_file); |
| 578 | qemu_set_log(LOG_TRACE); |
| 579 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 580 | /* initialize commands */ |
Kevin Wolf | c2cdf5c | 2013-06-05 14:19:36 +0200 | [diff] [blame] | 581 | qemuio_add_command(&quit_cmd); |
| 582 | qemuio_add_command(&open_cmd); |
| 583 | qemuio_add_command(&close_cmd); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 584 | |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 585 | if (isatty(STDIN_FILENO)) { |
| 586 | readline_state = readline_init(readline_printf_func, |
| 587 | readline_flush_func, |
| 588 | NULL, |
| 589 | readline_completion_func); |
| 590 | qemu_set_tty_echo(STDIN_FILENO, false); |
| 591 | atexit(reenable_tty_echo); |
| 592 | } |
| 593 | |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 594 | /* open the device */ |
| 595 | if (!readonly) { |
| 596 | flags |= BDRV_O_RDWR; |
| 597 | } |
| 598 | |
| 599 | if ((argc - optind) == 1) { |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 600 | if (imageOpts) { |
| 601 | QemuOpts *qopts = NULL; |
| 602 | qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false); |
| 603 | if (!qopts) { |
| 604 | exit(1); |
| 605 | } |
| 606 | opts = qemu_opts_to_qdict(qopts, NULL); |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 607 | if (openfile(NULL, flags, writethrough, force_share, opts)) { |
Nir Soffer | b7aa131 | 2017-02-01 02:31:18 +0200 | [diff] [blame] | 608 | exit(1); |
| 609 | } |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 610 | } else { |
| 611 | if (format) { |
| 612 | opts = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 613 | qdict_put_str(opts, "driver", format); |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 614 | } |
Fam Zheng | 459571f | 2017-05-03 00:35:41 +0800 | [diff] [blame] | 615 | if (openfile(argv[optind], flags, writethrough, |
| 616 | force_share, opts)) { |
Nir Soffer | b7aa131 | 2017-02-01 02:31:18 +0200 | [diff] [blame] | 617 | exit(1); |
| 618 | } |
Daniel P. Berrange | 499afa2 | 2016-02-17 10:10:18 +0000 | [diff] [blame] | 619 | } |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 620 | } |
| 621 | command_loop(); |
| 622 | |
| 623 | /* |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 624 | * Make sure all outstanding requests complete before the program exits. |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 625 | */ |
Stefan Hajnoczi | 922453b | 2011-11-30 12:23:43 +0000 | [diff] [blame] | 626 | bdrv_drain_all(); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 627 | |
Markus Armbruster | 26f54e9 | 2014-10-07 13:59:04 +0200 | [diff] [blame] | 628 | blk_unref(qemuio_blk); |
Stefan Hajnoczi | 0cf17e1 | 2013-11-14 11:54:17 +0100 | [diff] [blame] | 629 | g_free(readline_state); |
Devin Nakamura | 43642b3 | 2011-07-11 11:22:16 -0400 | [diff] [blame] | 630 | return 0; |
aliguori | e3aff4f | 2009-04-05 19:14:04 +0000 | [diff] [blame] | 631 | } |