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