blob: 8d5d5911cb55359d842f3d9dbb57e9c535859152 [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 Armbrusterda34e652016-03-14 09:01:28 +010018#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020019#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010020#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010021#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020022#include "qemu/option.h"
23#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010024#include "qemu/readline.h"
Denis V. Luneve9a80852016-06-17 17:44:11 +030025#include "qemu/log.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010026#include "qapi/qmp/qstring.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010027#include "qapi/qmp/qdict.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000028#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020029#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010030#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000031#include "trace/control.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010032#include "crypto/init.h"
Eric Blake4face322017-08-03 11:33:51 -050033#include "qemu-version.h"
aliguorie3aff4f2009-04-05 19:14:04 +000034
Devin Nakamura43642b32011-07-11 11:22:16 -040035#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000036
Markus Armbruster26f54e92014-10-07 13:59:04 +020037static BlockBackend *qemuio_blk;
Max Reitzb444d0e2018-05-09 21:42:58 +020038static bool quit_qemu_io;
Kevin Wolf191c2892010-09-16 13:18:08 +020039
Kevin Wolfd1174f12013-06-05 14:19:37 +020040/* qemu-io commands passed using -c */
41static int ncmdline;
42static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000043static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020044
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010045static ReadLineState *readline_state;
46
Daniel P. Berrange0e448a02018-02-12 18:48:49 +000047static int ttyEOF;
48
49static int get_eof_char(void)
50{
51#ifdef _WIN32
52 return 0x4; /* Ctrl-D */
53#else
54 struct termios tty;
55 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
56 if (errno == ENOTTY) {
57 return 0x0; /* just expect read() == 0 */
58 } else {
59 return 0x4; /* Ctrl-D */
60 }
61 }
62
63 return tty.c_cc[VEOF];
64#endif
65}
66
Max Reitzb32d7a32018-05-09 21:42:59 +020067static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000068{
Markus Armbruster26f54e92014-10-07 13:59:04 +020069 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020070 qemuio_blk = NULL;
Max Reitzb32d7a32018-05-09 21:42:59 +020071 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000072}
73
74static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040075 .name = "close",
76 .altname = "c",
77 .cfunc = close_f,
78 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000079};
80
Fam Zheng459571f2017-05-03 00:35:41 +080081static int openfile(char *name, int flags, bool writethrough, bool force_share,
82 QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000083{
Max Reitz34b5d2c2013-09-05 14:45:29 +020084 Error *local_err = NULL;
85
Max Reitz1b58b432015-02-05 13:58:20 -050086 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010087 error_report("file open already, try 'help close'");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020088 qobject_unref(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040089 return 1;
90 }
aliguorie3aff4f2009-04-05 19:14:04 +000091
Fam Zheng459571f2017-05-03 00:35:41 +080092 if (force_share) {
93 if (!opts) {
94 opts = qdict_new();
95 }
96 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
Max Reitz2a01c012018-05-02 22:20:49 +020097 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng459571f2017-05-03 00:35:41 +080098 error_report("-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020099 qobject_unref(opts);
Fam Zheng459571f2017-05-03 00:35:41 +0800100 return 1;
101 }
Max Reitz2a01c012018-05-02 22:20:49 +0200102 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng459571f2017-05-03 00:35:41 +0800103 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100104 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -0500105 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +0100106 error_reportf_err(local_err, "can't open%s%s: ",
107 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +0200108 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400109 }
Christoph Hellwig1db69472009-07-15 23:11:21 +0200110
Kevin Wolfe151fc12016-03-15 12:52:30 +0100111 blk_set_enable_write_cache(qemuio_blk, !writethrough);
Daniel P. Berrange8caf0212015-05-12 17:09:21 +0100112
Devin Nakamura43642b32011-07-11 11:22:16 -0400113 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000114}
115
Devin Nakamura43642b32011-07-11 11:22:16 -0400116static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000117{
Devin Nakamura43642b32011-07-11 11:22:16 -0400118 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000119"\n"
120" opens a new file in the requested mode\n"
121"\n"
122" Example:\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600123" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000124"\n"
125" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000126" -r, -- open file read-only\n"
127" -s, -- use snapshot file\n"
Eric Blake0f404442017-10-05 14:02:43 -0500128" -C, -- use copy-on-read\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600129" -n, -- disable host cache, short for -t none\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800130" -U, -- force shared permissions\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600131" -k, -- use kernel AIO implementation (on Linux only)\n"
132" -t, -- use the given cache mode for the image\n"
133" -d, -- use the given discard mode for the image\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200134" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000135"\n");
136}
137
Max Reitzb32d7a32018-05-09 21:42:59 +0200138static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000139
140static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400141 .name = "open",
142 .altname = "o",
143 .cfunc = open_f,
144 .argmin = 1,
145 .argmax = -1,
146 .flags = CMD_NOFILE_OK,
Eric Blake0f404442017-10-05 14:02:43 -0500147 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400148 .oneline = "open the file specified by path",
149 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000150};
aliguorie3aff4f2009-04-05 19:14:04 +0000151
Max Reitzb543c5c2013-10-11 14:02:10 +0200152static QemuOptsList empty_opts = {
153 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200154 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200155 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
156 .desc = {
157 /* no elements => accept any params */
158 { /* end of list */ }
159 },
160};
161
Max Reitzb32d7a32018-05-09 21:42:59 +0200162static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000163{
Eric Blakeb8d970f2016-05-07 21:16:41 -0600164 int flags = BDRV_O_UNMAP;
Devin Nakamura43642b32011-07-11 11:22:16 -0400165 int readonly = 0;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100166 bool writethrough = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400167 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +0200168 int ret;
Max Reitzb543c5c2013-10-11 14:02:10 +0200169 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200170 QDict *opts;
Fam Zheng459571f2017-05-03 00:35:41 +0800171 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000172
Eric Blake0f404442017-10-05 14:02:43 -0500173 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400174 switch (c) {
175 case 's':
176 flags |= BDRV_O_SNAPSHOT;
177 break;
178 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100179 flags |= BDRV_O_NOCACHE;
180 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400181 break;
Eric Blake0f404442017-10-05 14:02:43 -0500182 case 'C':
183 flags |= BDRV_O_COPY_ON_READ;
184 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400185 case 'r':
186 readonly = 1;
187 break;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600188 case 'k':
189 flags |= BDRV_O_NATIVE_AIO;
190 break;
191 case 't':
192 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
193 error_report("Invalid cache option: %s", optarg);
194 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200195 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600196 }
197 break;
198 case 'd':
199 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
200 error_report("Invalid discard option: %s", optarg);
201 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200202 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600203 }
204 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200205 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000206 if (imageOpts) {
207 printf("--image-opts and 'open -o' are mutually exclusive\n");
Eric Blakeb8d970f2016-05-07 21:16:41 -0600208 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200209 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000210 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100211 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200212 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200213 return -EINVAL;
Max Reitzb543c5c2013-10-11 14:02:10 +0200214 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200215 break;
Fam Zheng459571f2017-05-03 00:35:41 +0800216 case 'U':
217 force_share = true;
218 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400219 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200220 qemu_opts_reset(&empty_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +0200221 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200222 return -EINVAL;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200223 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400224 }
aliguorie3aff4f2009-04-05 19:14:04 +0000225
Devin Nakamura43642b32011-07-11 11:22:16 -0400226 if (!readonly) {
227 flags |= BDRV_O_RDWR;
228 }
aliguorie3aff4f2009-04-05 19:14:04 +0000229
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000230 if (imageOpts && (optind == argc - 1)) {
231 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
232 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200233 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000234 }
235 optind++;
236 }
237
Markus Armbruster443422f2014-05-28 11:16:58 +0200238 qopts = qemu_opts_find(&empty_opts, NULL);
239 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
240 qemu_opts_reset(&empty_opts);
241
Max Reitzfd0fee32013-12-20 19:28:20 +0100242 if (optind == argc - 1) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200243 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100244 } else if (optind == argc) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200245 ret = openfile(NULL, flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100246 } else {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200247 qobject_unref(opts);
Eric Blake64ebf552017-06-05 15:38:42 -0500248 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200249 return -EINVAL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400250 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200251
252 if (ret) {
253 return -EINVAL;
254 }
255
256 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000257}
258
Max Reitzb32d7a32018-05-09 21:42:59 +0200259static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200260{
Max Reitzb444d0e2018-05-09 21:42:58 +0200261 quit_qemu_io = true;
Max Reitzb32d7a32018-05-09 21:42:59 +0200262 return 0;
Kevin Wolfe681be72013-06-05 14:19:34 +0200263}
264
265static const cmdinfo_t quit_cmd = {
266 .name = "quit",
267 .altname = "q",
268 .cfunc = quit_f,
269 .argmin = -1,
270 .argmax = -1,
271 .flags = CMD_FLAG_GLOBAL,
272 .oneline = "exit the program",
273};
274
aliguorie3aff4f2009-04-05 19:14:04 +0000275static void usage(const char *name)
276{
Devin Nakamura43642b32011-07-11 11:22:16 -0400277 printf(
Eric Blakee4e12bb2016-05-07 21:16:40 -0600278"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200279"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000280"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000281" --object OBJECTDEF define an object such as 'secret' for\n"
282" passwords and/or encryption keys\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600283" --image-opts treat file as option string\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400284" -c, --cmd STRING execute command with its arguments\n"
285" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100286" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000287" -r, --read-only export read-only\n"
288" -s, --snapshot use snapshot file\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600289" -n, --nocache disable host cache, short for -t none\n"
Eric Blake0f404442017-10-05 14:02:43 -0500290" -C, --copy-on-read enable copy-on-read\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000291" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200292" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200293" -t, --cache=MODE use the given cache mode for the image\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600294" -d, --discard=MODE use the given discard mode for the image\n"
Denis V. Luneve9a80852016-06-17 17:44:11 +0300295" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
296" specify tracing options\n"
297" see qemu-img(1) man page for full description\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800298" -U, --force-share force shared permissions\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000299" -h, --help display this help and exit\n"
300" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400301"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500302"See '%s -c help' for information on available commands.\n"
303"\n"
304QEMU_HELP_BOTTOM "\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400305 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000306}
307
Kevin Wolfd1174f12013-06-05 14:19:37 +0200308static char *get_prompt(void)
309{
310 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
311
312 if (!prompt[0]) {
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100313 snprintf(prompt, sizeof(prompt), "%s> ", error_get_progname());
Kevin Wolfd1174f12013-06-05 14:19:37 +0200314 }
315
316 return prompt;
317}
318
Stefan Weild5d15072014-01-25 18:18:23 +0100319static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
320 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200321{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100322 va_list ap;
323 va_start(ap, fmt);
324 vprintf(fmt, ap);
325 va_end(ap);
326}
327
328static void readline_flush_func(void *opaque)
329{
330 fflush(stdout);
331}
332
333static void readline_func(void *opaque, const char *str, void *readline_opaque)
334{
335 char **line = readline_opaque;
336 *line = g_strdup(str);
337}
338
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100339static void completion_match(const char *cmd, void *opaque)
340{
341 readline_add_completion(readline_state, cmd);
342}
343
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100344static void readline_completion_func(void *opaque, const char *str)
345{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100346 readline_set_completion_index(readline_state, strlen(str));
347 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100348}
349
350static char *fetchline_readline(void)
351{
352 char *line = NULL;
353
354 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
355 while (!line) {
356 int ch = getchar();
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000357 if (ttyEOF != 0x0 && ch == ttyEOF) {
358 printf("\n");
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100359 break;
360 }
361 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200362 }
363 return line;
364}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200365
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100366#define MAXREADLINESZ 1024
367static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200368{
369 char *p, *line = g_malloc(MAXREADLINESZ);
370
371 if (!fgets(line, MAXREADLINESZ, stdin)) {
372 g_free(line);
373 return NULL;
374 }
375
376 p = line + strlen(line);
377 if (p != line && p[-1] == '\n') {
378 p[-1] = '\0';
379 }
380
381 return line;
382}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100383
384static char *fetchline(void)
385{
386 if (readline_state) {
387 return fetchline_readline();
388 } else {
389 return fetchline_fgets();
390 }
391}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200392
393static void prep_fetchline(void *opaque)
394{
395 int *fetchable = opaque;
396
397 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
398 *fetchable= 1;
399}
400
Max Reitz6b3aa842018-05-09 21:43:00 +0200401static int command_loop(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200402{
Max Reitzb444d0e2018-05-09 21:42:58 +0200403 int i, fetchable = 0, prompted = 0;
Max Reitz6b3aa842018-05-09 21:43:00 +0200404 int ret, last_error = 0;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200405 char *input;
406
Max Reitzb444d0e2018-05-09 21:42:58 +0200407 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
Max Reitz6b3aa842018-05-09 21:43:00 +0200408 ret = qemuio_command(qemuio_blk, cmdline[i]);
409 if (ret < 0) {
410 last_error = ret;
411 }
Kevin Wolfd1174f12013-06-05 14:19:37 +0200412 }
413 if (cmdline) {
414 g_free(cmdline);
Max Reitz6b3aa842018-05-09 21:43:00 +0200415 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200416 }
417
Max Reitzb444d0e2018-05-09 21:42:58 +0200418 while (!quit_qemu_io) {
Kevin Wolfd1174f12013-06-05 14:19:37 +0200419 if (!prompted) {
420 printf("%s", get_prompt());
421 fflush(stdout);
422 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
423 prompted = 1;
424 }
425
426 main_loop_wait(false);
427
428 if (!fetchable) {
429 continue;
430 }
431
432 input = fetchline();
433 if (input == NULL) {
434 break;
435 }
Max Reitz6b3aa842018-05-09 21:43:00 +0200436 ret = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200437 g_free(input);
438
Max Reitz6b3aa842018-05-09 21:43:00 +0200439 if (ret < 0) {
440 last_error = ret;
441 }
442
Kevin Wolfd1174f12013-06-05 14:19:37 +0200443 prompted = 0;
444 fetchable = 0;
445 }
446 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
Max Reitz6b3aa842018-05-09 21:43:00 +0200447
448 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200449}
450
451static void add_user_command(char *optarg)
452{
Markus Armbruster5839e532014-08-19 10:31:08 +0200453 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200454 cmdline[ncmdline-1] = optarg;
455}
456
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100457static void reenable_tty_echo(void)
458{
459 qemu_set_tty_echo(STDIN_FILENO, true);
460}
461
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000462enum {
463 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000464 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000465};
466
467static QemuOptsList qemu_object_opts = {
468 .name = "object",
469 .implied_opt_name = "qom-type",
470 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
471 .desc = {
472 { }
473 },
474};
475
476
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000477static QemuOptsList file_opts = {
478 .name = "file",
479 .implied_opt_name = "file",
480 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
481 .desc = {
482 /* no elements => accept any params */
483 { /* end of list */ }
484 },
485};
486
aliguorie3aff4f2009-04-05 19:14:04 +0000487int main(int argc, char **argv)
488{
Devin Nakamura43642b32011-07-11 11:22:16 -0400489 int readonly = 0;
Eric Blake0f404442017-10-05 14:02:43 -0500490 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
Devin Nakamura43642b32011-07-11 11:22:16 -0400491 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000492 { "help", no_argument, NULL, 'h' },
493 { "version", no_argument, NULL, 'V' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000494 { "cmd", required_argument, NULL, 'c' },
495 { "format", required_argument, NULL, 'f' },
496 { "read-only", no_argument, NULL, 'r' },
497 { "snapshot", no_argument, NULL, 's' },
498 { "nocache", no_argument, NULL, 'n' },
Eric Blake0f404442017-10-05 14:02:43 -0500499 { "copy-on-read", no_argument, NULL, 'C' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000500 { "misalign", no_argument, NULL, 'm' },
501 { "native-aio", no_argument, NULL, 'k' },
502 { "discard", required_argument, NULL, 'd' },
503 { "cache", required_argument, NULL, 't' },
504 { "trace", required_argument, NULL, 'T' },
505 { "object", required_argument, NULL, OPTION_OBJECT },
506 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Fam Zheng459571f2017-05-03 00:35:41 +0800507 { "force-share", no_argument, 0, 'U'},
Devin Nakamura43642b32011-07-11 11:22:16 -0400508 { NULL, 0, NULL, 0 }
509 };
510 int c;
511 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100512 int flags = BDRV_O_UNMAP;
Max Reitz6b3aa842018-05-09 21:43:00 +0200513 int ret;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100514 bool writethrough = true;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300515 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500516 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000517 const char *format = NULL;
Denis V. Luneve9a80852016-06-17 17:44:11 +0300518 char *trace_file = NULL;
Fam Zheng459571f2017-05-03 00:35:41 +0800519 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000520
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900521#ifdef CONFIG_POSIX
522 signal(SIGPIPE, SIG_IGN);
523#endif
524
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100525 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100526 module_call_init(MODULE_INIT_TRACE);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800527 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000528
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300529 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100530
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000531 module_call_init(MODULE_INIT_QOM);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000532 qemu_add_opts(&qemu_object_opts);
Denis V. Luneve9a80852016-06-17 17:44:11 +0300533 qemu_add_opts(&qemu_trace_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100534 bdrv_init();
535
Devin Nakamura43642b32011-07-11 11:22:16 -0400536 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
537 switch (c) {
538 case 's':
539 flags |= BDRV_O_SNAPSHOT;
540 break;
541 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100542 flags |= BDRV_O_NOCACHE;
543 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400544 break;
Eric Blake0f404442017-10-05 14:02:43 -0500545 case 'C':
546 flags |= BDRV_O_COPY_ON_READ;
547 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100548 case 'd':
549 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
550 error_report("Invalid discard option: %s", optarg);
551 exit(1);
552 }
553 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100554 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000555 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100556 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400557 case 'c':
558 add_user_command(optarg);
559 break;
560 case 'r':
561 readonly = 1;
562 break;
563 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100564 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400565 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400566 case 'k':
567 flags |= BDRV_O_NATIVE_AIO;
568 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200569 case 't':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100570 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf592fa072012-04-18 12:07:39 +0200571 error_report("Invalid cache option: %s", optarg);
572 exit(1);
573 }
574 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000575 case 'T':
Denis V. Luneve9a80852016-06-17 17:44:11 +0300576 g_free(trace_file);
577 trace_file = trace_opt_parse(optarg);
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000578 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400579 case 'V':
Thomas Huth7e563bf2018-02-15 12:06:47 +0100580 printf("%s version " QEMU_FULL_VERSION "\n"
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100581 QEMU_COPYRIGHT "\n", error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400582 exit(0);
583 case 'h':
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100584 usage(error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400585 exit(0);
Fam Zheng459571f2017-05-03 00:35:41 +0800586 case 'U':
587 force_share = true;
588 break;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000589 case OPTION_OBJECT: {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000590 QemuOpts *qopts;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000591 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
592 optarg, true);
593 if (!qopts) {
594 exit(1);
595 }
596 } break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000597 case OPTION_IMAGE_OPTS:
598 imageOpts = true;
599 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400600 default:
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100601 usage(error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400602 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200603 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400604 }
aliguorie3aff4f2009-04-05 19:14:04 +0000605
Devin Nakamura43642b32011-07-11 11:22:16 -0400606 if ((argc - optind) > 1) {
Christophe Fergeau99e98d72019-01-31 17:46:13 +0100607 usage(error_get_progname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400608 exit(1);
609 }
aliguorie3aff4f2009-04-05 19:14:04 +0000610
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000611 if (format && imageOpts) {
612 error_report("--image-opts and -f are mutually exclusive");
613 exit(1);
614 }
615
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300616 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100617 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300618 exit(1);
619 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800620
Markus Armbruster7e1e0c12018-10-17 10:26:43 +0200621 qemu_opts_foreach(&qemu_object_opts,
622 user_creatable_add_opts_foreach,
623 NULL, &error_fatal);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000624
Denis V. Luneve9a80852016-06-17 17:44:11 +0300625 if (!trace_init_backends()) {
626 exit(1);
627 }
628 trace_init_file(trace_file);
629 qemu_set_log(LOG_TRACE);
630
Devin Nakamura43642b32011-07-11 11:22:16 -0400631 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200632 qemuio_add_command(&quit_cmd);
633 qemuio_add_command(&open_cmd);
634 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400635
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100636 if (isatty(STDIN_FILENO)) {
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000637 ttyEOF = get_eof_char();
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100638 readline_state = readline_init(readline_printf_func,
639 readline_flush_func,
640 NULL,
641 readline_completion_func);
642 qemu_set_tty_echo(STDIN_FILENO, false);
643 atexit(reenable_tty_echo);
644 }
645
Devin Nakamura43642b32011-07-11 11:22:16 -0400646 /* open the device */
647 if (!readonly) {
648 flags |= BDRV_O_RDWR;
649 }
650
651 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000652 if (imageOpts) {
653 QemuOpts *qopts = NULL;
654 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
655 if (!qopts) {
656 exit(1);
657 }
658 opts = qemu_opts_to_qdict(qopts, NULL);
Fam Zheng459571f2017-05-03 00:35:41 +0800659 if (openfile(NULL, flags, writethrough, force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200660 exit(1);
661 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000662 } else {
663 if (format) {
664 opts = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -0500665 qdict_put_str(opts, "driver", format);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000666 }
Fam Zheng459571f2017-05-03 00:35:41 +0800667 if (openfile(argv[optind], flags, writethrough,
668 force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200669 exit(1);
670 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000671 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400672 }
Max Reitz6b3aa842018-05-09 21:43:00 +0200673 ret = command_loop();
Devin Nakamura43642b32011-07-11 11:22:16 -0400674
675 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000676 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400677 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000678 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400679
Markus Armbruster26f54e92014-10-07 13:59:04 +0200680 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100681 g_free(readline_state);
Max Reitz6b3aa842018-05-09 21:43:00 +0200682
683 if (ret < 0) {
684 return 1;
685 } else {
686 return 0;
687 }
aliguorie3aff4f2009-04-05 19:14:04 +0000688}