blob: 91e327659274017988078531e25f12073fa064be [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
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 Armbruster452fcdb2018-02-01 12:18:39 +010010
Peter Maydell80c71a22016-01-18 18:01:42 +000011#include "qemu/osdep.h"
aliguorie3aff4f2009-04-05 19:14:04 +000012#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020013#include <libgen.h>
Daniel P. Berrange0e448a02018-02-12 18:48:49 +000014#ifndef _WIN32
15#include <termios.h>
16#endif
aliguorie3aff4f2009-04-05 19:14:04 +000017
Markus Armbrustera8d25322019-05-23 16:35:08 +020018#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010019#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020020#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010021#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020023#include "qemu/module.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020024#include "qemu/option.h"
25#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010026#include "qemu/readline.h"
Denis V. Luneve9a80852016-06-17 17:44:11 +030027#include "qemu/log.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010028#include "qapi/qmp/qstring.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010029#include "qapi/qmp/qdict.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000030#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020031#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000033#include "trace/control.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010034#include "crypto/init.h"
Eric Blake4face322017-08-03 11:33:51 -050035#include "qemu-version.h"
aliguorie3aff4f2009-04-05 19:14:04 +000036
Devin Nakamura43642b32011-07-11 11:22:16 -040037#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000038
Markus Armbruster26f54e92014-10-07 13:59:04 +020039static BlockBackend *qemuio_blk;
Max Reitzb444d0e2018-05-09 21:42:58 +020040static bool quit_qemu_io;
Kevin Wolf191c2892010-09-16 13:18:08 +020041
Kevin Wolfd1174f12013-06-05 14:19:37 +020042/* qemu-io commands passed using -c */
43static int ncmdline;
44static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000045static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020046
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010047static ReadLineState *readline_state;
48
Daniel P. Berrange0e448a02018-02-12 18:48:49 +000049static int ttyEOF;
50
51static int get_eof_char(void)
52{
53#ifdef _WIN32
54 return 0x4; /* Ctrl-D */
55#else
56 struct termios tty;
57 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
58 if (errno == ENOTTY) {
59 return 0x0; /* just expect read() == 0 */
60 } else {
61 return 0x4; /* Ctrl-D */
62 }
63 }
64
65 return tty.c_cc[VEOF];
66#endif
67}
68
Max Reitzb32d7a32018-05-09 21:42:59 +020069static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000070{
Markus Armbruster26f54e92014-10-07 13:59:04 +020071 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020072 qemuio_blk = NULL;
Max Reitzb32d7a32018-05-09 21:42:59 +020073 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000074}
75
76static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040077 .name = "close",
78 .altname = "c",
79 .cfunc = close_f,
80 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000081};
82
Fam Zheng459571f2017-05-03 00:35:41 +080083static int openfile(char *name, int flags, bool writethrough, bool force_share,
84 QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000085{
Max Reitz34b5d2c2013-09-05 14:45:29 +020086 Error *local_err = NULL;
87
Max Reitz1b58b432015-02-05 13:58:20 -050088 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010089 error_report("file open already, try 'help close'");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020090 qobject_unref(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040091 return 1;
92 }
aliguorie3aff4f2009-04-05 19:14:04 +000093
Fam Zheng459571f2017-05-03 00:35:41 +080094 if (force_share) {
95 if (!opts) {
96 opts = qdict_new();
97 }
98 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
Max Reitz2a01c012018-05-02 22:20:49 +020099 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng459571f2017-05-03 00:35:41 +0800100 error_report("-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200101 qobject_unref(opts);
Fam Zheng459571f2017-05-03 00:35:41 +0800102 return 1;
103 }
Max Reitz2a01c012018-05-02 22:20:49 +0200104 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng459571f2017-05-03 00:35:41 +0800105 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100106 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -0500107 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +0100108 error_reportf_err(local_err, "can't open%s%s: ",
109 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +0200110 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400111 }
Christoph Hellwig1db69472009-07-15 23:11:21 +0200112
Kevin Wolfe151fc12016-03-15 12:52:30 +0100113 blk_set_enable_write_cache(qemuio_blk, !writethrough);
Daniel P. Berrange8caf0212015-05-12 17:09:21 +0100114
Devin Nakamura43642b32011-07-11 11:22:16 -0400115 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000116}
117
Devin Nakamura43642b32011-07-11 11:22:16 -0400118static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000119{
Devin Nakamura43642b32011-07-11 11:22:16 -0400120 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000121"\n"
122" opens a new file in the requested mode\n"
123"\n"
124" Example:\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600125" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000126"\n"
127" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000128" -r, -- open file read-only\n"
129" -s, -- use snapshot file\n"
Eric Blake0f404442017-10-05 14:02:43 -0500130" -C, -- use copy-on-read\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600131" -n, -- disable host cache, short for -t none\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800132" -U, -- force shared permissions\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600133" -k, -- use kernel AIO implementation (on Linux only)\n"
134" -t, -- use the given cache mode for the image\n"
135" -d, -- use the given discard mode for the image\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200136" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000137"\n");
138}
139
Max Reitzb32d7a32018-05-09 21:42:59 +0200140static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000141
142static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400143 .name = "open",
144 .altname = "o",
145 .cfunc = open_f,
146 .argmin = 1,
147 .argmax = -1,
148 .flags = CMD_NOFILE_OK,
Eric Blake0f404442017-10-05 14:02:43 -0500149 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400150 .oneline = "open the file specified by path",
151 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000152};
aliguorie3aff4f2009-04-05 19:14:04 +0000153
Max Reitzb543c5c2013-10-11 14:02:10 +0200154static QemuOptsList empty_opts = {
155 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200156 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200157 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
158 .desc = {
159 /* no elements => accept any params */
160 { /* end of list */ }
161 },
162};
163
Max Reitzb32d7a32018-05-09 21:42:59 +0200164static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000165{
Eric Blakeb8d970f2016-05-07 21:16:41 -0600166 int flags = BDRV_O_UNMAP;
Devin Nakamura43642b32011-07-11 11:22:16 -0400167 int readonly = 0;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100168 bool writethrough = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +0200170 int ret;
Max Reitzb543c5c2013-10-11 14:02:10 +0200171 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200172 QDict *opts;
Fam Zheng459571f2017-05-03 00:35:41 +0800173 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000174
Eric Blake0f404442017-10-05 14:02:43 -0500175 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400176 switch (c) {
177 case 's':
178 flags |= BDRV_O_SNAPSHOT;
179 break;
180 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100181 flags |= BDRV_O_NOCACHE;
182 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400183 break;
Eric Blake0f404442017-10-05 14:02:43 -0500184 case 'C':
185 flags |= BDRV_O_COPY_ON_READ;
186 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400187 case 'r':
188 readonly = 1;
189 break;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600190 case 'k':
191 flags |= BDRV_O_NATIVE_AIO;
192 break;
193 case 't':
194 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
195 error_report("Invalid cache option: %s", optarg);
196 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200197 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600198 }
199 break;
200 case 'd':
201 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
202 error_report("Invalid discard option: %s", optarg);
203 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200204 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600205 }
206 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200207 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000208 if (imageOpts) {
209 printf("--image-opts and 'open -o' are mutually exclusive\n");
Eric Blakeb8d970f2016-05-07 21:16:41 -0600210 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200211 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000212 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100213 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200214 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200215 return -EINVAL;
Max Reitzb543c5c2013-10-11 14:02:10 +0200216 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200217 break;
Fam Zheng459571f2017-05-03 00:35:41 +0800218 case 'U':
219 force_share = true;
220 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400221 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200222 qemu_opts_reset(&empty_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +0200223 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200224 return -EINVAL;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200225 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400226 }
aliguorie3aff4f2009-04-05 19:14:04 +0000227
Devin Nakamura43642b32011-07-11 11:22:16 -0400228 if (!readonly) {
229 flags |= BDRV_O_RDWR;
230 }
aliguorie3aff4f2009-04-05 19:14:04 +0000231
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000232 if (imageOpts && (optind == argc - 1)) {
233 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
234 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200235 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000236 }
237 optind++;
238 }
239
Markus Armbruster443422f2014-05-28 11:16:58 +0200240 qopts = qemu_opts_find(&empty_opts, NULL);
241 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
242 qemu_opts_reset(&empty_opts);
243
Max Reitzfd0fee32013-12-20 19:28:20 +0100244 if (optind == argc - 1) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200245 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100246 } else if (optind == argc) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200247 ret = openfile(NULL, flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100248 } else {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200249 qobject_unref(opts);
Eric Blake64ebf552017-06-05 15:38:42 -0500250 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200251 return -EINVAL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400252 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200253
254 if (ret) {
255 return -EINVAL;
256 }
257
258 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000259}
260
Max Reitzb32d7a32018-05-09 21:42:59 +0200261static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200262{
Max Reitzb444d0e2018-05-09 21:42:58 +0200263 quit_qemu_io = true;
Max Reitzb32d7a32018-05-09 21:42:59 +0200264 return 0;
Kevin Wolfe681be72013-06-05 14:19:34 +0200265}
266
267static const cmdinfo_t quit_cmd = {
268 .name = "quit",
269 .altname = "q",
270 .cfunc = quit_f,
271 .argmin = -1,
272 .argmax = -1,
273 .flags = CMD_FLAG_GLOBAL,
274 .oneline = "exit the program",
275};
276
aliguorie3aff4f2009-04-05 19:14:04 +0000277static void usage(const char *name)
278{
Devin Nakamura43642b32011-07-11 11:22:16 -0400279 printf(
Eric Blakee4e12bb2016-05-07 21:16:40 -0600280"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200281"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000282"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000283" --object OBJECTDEF define an object such as 'secret' for\n"
284" passwords and/or encryption keys\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600285" --image-opts treat file as option string\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400286" -c, --cmd STRING execute command with its arguments\n"
287" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100288" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000289" -r, --read-only export read-only\n"
290" -s, --snapshot use snapshot file\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600291" -n, --nocache disable host cache, short for -t none\n"
Eric Blake0f404442017-10-05 14:02:43 -0500292" -C, --copy-on-read enable copy-on-read\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000293" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200294" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200295" -t, --cache=MODE use the given cache mode for the image\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600296" -d, --discard=MODE use the given discard mode for the image\n"
Denis V. Luneve9a80852016-06-17 17:44:11 +0300297" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
298" specify tracing options\n"
299" see qemu-img(1) man page for full description\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800300" -U, --force-share force shared permissions\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000301" -h, --help display this help and exit\n"
302" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400303"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500304"See '%s -c help' for information on available commands.\n"
305"\n"
306QEMU_HELP_BOTTOM "\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400307 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000308}
309
Kevin Wolfd1174f12013-06-05 14:19:37 +0200310static char *get_prompt(void)
311{
312 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
313
314 if (!prompt[0]) {
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100315 snprintf(prompt, sizeof(prompt), "%s> ", error_get_progname());
Kevin Wolfd1174f12013-06-05 14:19:37 +0200316 }
317
318 return prompt;
319}
320
Stefan Weild5d15072014-01-25 18:18:23 +0100321static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
322 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200323{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100324 va_list ap;
325 va_start(ap, fmt);
326 vprintf(fmt, ap);
327 va_end(ap);
328}
329
330static void readline_flush_func(void *opaque)
331{
332 fflush(stdout);
333}
334
335static void readline_func(void *opaque, const char *str, void *readline_opaque)
336{
337 char **line = readline_opaque;
338 *line = g_strdup(str);
339}
340
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100341static void completion_match(const char *cmd, void *opaque)
342{
343 readline_add_completion(readline_state, cmd);
344}
345
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100346static void readline_completion_func(void *opaque, const char *str)
347{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100348 readline_set_completion_index(readline_state, strlen(str));
349 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100350}
351
352static char *fetchline_readline(void)
353{
354 char *line = NULL;
355
356 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
357 while (!line) {
358 int ch = getchar();
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000359 if (ttyEOF != 0x0 && ch == ttyEOF) {
360 printf("\n");
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100361 break;
362 }
363 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200364 }
365 return line;
366}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200367
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100368#define MAXREADLINESZ 1024
369static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200370{
371 char *p, *line = g_malloc(MAXREADLINESZ);
372
373 if (!fgets(line, MAXREADLINESZ, stdin)) {
374 g_free(line);
375 return NULL;
376 }
377
378 p = line + strlen(line);
379 if (p != line && p[-1] == '\n') {
380 p[-1] = '\0';
381 }
382
383 return line;
384}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100385
386static char *fetchline(void)
387{
388 if (readline_state) {
389 return fetchline_readline();
390 } else {
391 return fetchline_fgets();
392 }
393}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200394
395static void prep_fetchline(void *opaque)
396{
397 int *fetchable = opaque;
398
399 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
400 *fetchable= 1;
401}
402
Max Reitz6b3aa842018-05-09 21:43:00 +0200403static int command_loop(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200404{
Max Reitzb444d0e2018-05-09 21:42:58 +0200405 int i, fetchable = 0, prompted = 0;
Max Reitz6b3aa842018-05-09 21:43:00 +0200406 int ret, last_error = 0;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200407 char *input;
408
Max Reitzb444d0e2018-05-09 21:42:58 +0200409 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
Max Reitz6b3aa842018-05-09 21:43:00 +0200410 ret = qemuio_command(qemuio_blk, cmdline[i]);
411 if (ret < 0) {
412 last_error = ret;
413 }
Kevin Wolfd1174f12013-06-05 14:19:37 +0200414 }
415 if (cmdline) {
416 g_free(cmdline);
Max Reitz6b3aa842018-05-09 21:43:00 +0200417 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200418 }
419
Max Reitzb444d0e2018-05-09 21:42:58 +0200420 while (!quit_qemu_io) {
Kevin Wolfd1174f12013-06-05 14:19:37 +0200421 if (!prompted) {
422 printf("%s", get_prompt());
423 fflush(stdout);
424 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
425 prompted = 1;
426 }
427
428 main_loop_wait(false);
429
430 if (!fetchable) {
431 continue;
432 }
433
434 input = fetchline();
435 if (input == NULL) {
436 break;
437 }
Max Reitz6b3aa842018-05-09 21:43:00 +0200438 ret = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200439 g_free(input);
440
Max Reitz6b3aa842018-05-09 21:43:00 +0200441 if (ret < 0) {
442 last_error = ret;
443 }
444
Kevin Wolfd1174f12013-06-05 14:19:37 +0200445 prompted = 0;
446 fetchable = 0;
447 }
448 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
Max Reitz6b3aa842018-05-09 21:43:00 +0200449
450 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200451}
452
453static void add_user_command(char *optarg)
454{
Markus Armbruster5839e532014-08-19 10:31:08 +0200455 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200456 cmdline[ncmdline-1] = optarg;
457}
458
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100459static void reenable_tty_echo(void)
460{
461 qemu_set_tty_echo(STDIN_FILENO, true);
462}
463
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000464enum {
465 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000466 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000467};
468
469static QemuOptsList qemu_object_opts = {
470 .name = "object",
471 .implied_opt_name = "qom-type",
472 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
473 .desc = {
474 { }
475 },
476};
477
Kevin Wolf4fa1f0d2019-10-11 21:49:17 +0200478static bool qemu_io_object_print_help(const char *type, QemuOpts *opts)
479{
480 if (user_creatable_print_help(type, opts)) {
481 exit(0);
482 }
483 return true;
484}
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000485
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000486static 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
aliguorie3aff4f2009-04-05 19:14:04 +0000496int main(int argc, char **argv)
497{
Devin Nakamura43642b32011-07-11 11:22:16 -0400498 int readonly = 0;
Eric Blake0f404442017-10-05 14:02:43 -0500499 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
Devin Nakamura43642b32011-07-11 11:22:16 -0400500 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000501 { "help", no_argument, NULL, 'h' },
502 { "version", no_argument, NULL, 'V' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000503 { "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 Blake0f404442017-10-05 14:02:43 -0500508 { "copy-on-read", no_argument, NULL, 'C' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000509 { "misalign", no_argument, NULL, 'm' },
510 { "native-aio", no_argument, NULL, 'k' },
511 { "discard", required_argument, NULL, 'd' },
512 { "cache", required_argument, NULL, 't' },
513 { "trace", required_argument, NULL, 'T' },
514 { "object", required_argument, NULL, OPTION_OBJECT },
515 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Fam Zheng459571f2017-05-03 00:35:41 +0800516 { "force-share", no_argument, 0, 'U'},
Devin Nakamura43642b32011-07-11 11:22:16 -0400517 { NULL, 0, NULL, 0 }
518 };
519 int c;
520 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100521 int flags = BDRV_O_UNMAP;
Max Reitz6b3aa842018-05-09 21:43:00 +0200522 int ret;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100523 bool writethrough = true;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300524 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500525 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000526 const char *format = NULL;
Denis V. Luneve9a80852016-06-17 17:44:11 +0300527 char *trace_file = NULL;
Fam Zheng459571f2017-05-03 00:35:41 +0800528 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000529
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900530#ifdef CONFIG_POSIX
531 signal(SIGPIPE, SIG_IGN);
532#endif
533
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100534 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100535 module_call_init(MODULE_INIT_TRACE);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800536 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000537
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300538 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100539
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000540 module_call_init(MODULE_INIT_QOM);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000541 qemu_add_opts(&qemu_object_opts);
Denis V. Luneve9a80852016-06-17 17:44:11 +0300542 qemu_add_opts(&qemu_trace_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100543 bdrv_init();
544
Devin Nakamura43642b32011-07-11 11:22:16 -0400545 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
546 switch (c) {
547 case 's':
548 flags |= BDRV_O_SNAPSHOT;
549 break;
550 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100551 flags |= BDRV_O_NOCACHE;
552 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400553 break;
Eric Blake0f404442017-10-05 14:02:43 -0500554 case 'C':
555 flags |= BDRV_O_COPY_ON_READ;
556 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100557 case 'd':
558 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
559 error_report("Invalid discard option: %s", optarg);
560 exit(1);
561 }
562 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100563 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000564 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100565 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400566 case 'c':
567 add_user_command(optarg);
568 break;
569 case 'r':
570 readonly = 1;
571 break;
572 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100573 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400574 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400575 case 'k':
576 flags |= BDRV_O_NATIVE_AIO;
577 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200578 case 't':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100579 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf592fa072012-04-18 12:07:39 +0200580 error_report("Invalid cache option: %s", optarg);
581 exit(1);
582 }
583 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000584 case 'T':
Denis V. Luneve9a80852016-06-17 17:44:11 +0300585 g_free(trace_file);
586 trace_file = trace_opt_parse(optarg);
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000587 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400588 case 'V':
Thomas Huth7e563bf2018-02-15 12:06:47 +0100589 printf("%s version " QEMU_FULL_VERSION "\n"
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100590 QEMU_COPYRIGHT "\n", error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400591 exit(0);
592 case 'h':
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100593 usage(error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400594 exit(0);
Fam Zheng459571f2017-05-03 00:35:41 +0800595 case 'U':
596 force_share = true;
597 break;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000598 case OPTION_OBJECT: {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000599 QemuOpts *qopts;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000600 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
601 optarg, true);
602 if (!qopts) {
603 exit(1);
604 }
605 } break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000606 case OPTION_IMAGE_OPTS:
607 imageOpts = true;
608 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400609 default:
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100610 usage(error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400611 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200612 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400613 }
aliguorie3aff4f2009-04-05 19:14:04 +0000614
Devin Nakamura43642b32011-07-11 11:22:16 -0400615 if ((argc - optind) > 1) {
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100616 usage(error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400617 exit(1);
618 }
aliguorie3aff4f2009-04-05 19:14:04 +0000619
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000620 if (format && imageOpts) {
621 error_report("--image-opts and -f are mutually exclusive");
622 exit(1);
623 }
624
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300625 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100626 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300627 exit(1);
628 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800629
Markus Armbruster7e1e0c12018-10-17 10:26:43 +0200630 qemu_opts_foreach(&qemu_object_opts,
631 user_creatable_add_opts_foreach,
Kevin Wolf4fa1f0d2019-10-11 21:49:17 +0200632 qemu_io_object_print_help, &error_fatal);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000633
Denis V. Luneve9a80852016-06-17 17:44:11 +0300634 if (!trace_init_backends()) {
635 exit(1);
636 }
637 trace_init_file(trace_file);
638 qemu_set_log(LOG_TRACE);
639
Devin Nakamura43642b32011-07-11 11:22:16 -0400640 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200641 qemuio_add_command(&quit_cmd);
642 qemuio_add_command(&open_cmd);
643 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400644
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100645 if (isatty(STDIN_FILENO)) {
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000646 ttyEOF = get_eof_char();
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100647 readline_state = readline_init(readline_printf_func,
648 readline_flush_func,
649 NULL,
650 readline_completion_func);
651 qemu_set_tty_echo(STDIN_FILENO, false);
652 atexit(reenable_tty_echo);
653 }
654
Devin Nakamura43642b32011-07-11 11:22:16 -0400655 /* open the device */
656 if (!readonly) {
657 flags |= BDRV_O_RDWR;
658 }
659
660 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000661 if (imageOpts) {
662 QemuOpts *qopts = NULL;
663 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
664 if (!qopts) {
665 exit(1);
666 }
667 opts = qemu_opts_to_qdict(qopts, NULL);
Fam Zheng459571f2017-05-03 00:35:41 +0800668 if (openfile(NULL, flags, writethrough, force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200669 exit(1);
670 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000671 } else {
672 if (format) {
673 opts = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -0500674 qdict_put_str(opts, "driver", format);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000675 }
Fam Zheng459571f2017-05-03 00:35:41 +0800676 if (openfile(argv[optind], flags, writethrough,
677 force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200678 exit(1);
679 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000680 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400681 }
Max Reitz6b3aa842018-05-09 21:43:00 +0200682 ret = command_loop();
Devin Nakamura43642b32011-07-11 11:22:16 -0400683
684 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000685 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400686 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000687 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400688
Markus Armbruster26f54e92014-10-07 13:59:04 +0200689 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100690 g_free(readline_state);
Max Reitz6b3aa842018-05-09 21:43:00 +0200691
692 if (ret < 0) {
693 return 1;
694 } else {
695 return 0;
696 }
aliguorie3aff4f2009-04-05 19:14:04 +0000697}