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