blob: 969c8bfdead7e1f36cb99a633c0bbce5e548c33a [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 */
Peter Maydell80c71a22016-01-18 18:01:42 +000010#include "qemu/osdep.h"
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020012#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000013
Kevin Wolf3d219942013-06-05 14:19:39 +020014#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010015#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020017#include "qemu/option.h"
18#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010019#include "qemu/readline.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010020#include "qapi/qmp/qstring.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000021#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020022#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010023#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000024#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000025
Devin Nakamura43642b32011-07-11 11:22:16 -040026#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000027
Stefan Weilf9883882014-03-05 22:23:00 +010028static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000029
Markus Armbruster26f54e92014-10-07 13:59:04 +020030static BlockBackend *qemuio_blk;
Kevin Wolf191c2892010-09-16 13:18:08 +020031
Kevin Wolfd1174f12013-06-05 14:19:37 +020032/* qemu-io commands passed using -c */
33static int ncmdline;
34static char **cmdline;
35
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010036static ReadLineState *readline_state;
37
Max Reitz4c7b7e92015-02-05 13:58:22 -050038static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000039{
Markus Armbruster26f54e92014-10-07 13:59:04 +020040 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020041 qemuio_blk = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040042 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000043}
44
45static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040046 .name = "close",
47 .altname = "c",
48 .cfunc = close_f,
49 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000050};
51
Max Reitz10d9d752015-02-05 13:58:21 -050052static int openfile(char *name, int flags, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000053{
Max Reitz34b5d2c2013-09-05 14:45:29 +020054 Error *local_err = NULL;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010055 BlockDriverState *bs;
Max Reitz34b5d2c2013-09-05 14:45:29 +020056
Max Reitz1b58b432015-02-05 13:58:20 -050057 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010058 error_report("file open already, try 'help close'");
Markus Armbruster29f26012014-05-28 11:16:59 +020059 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040060 return 1;
61 }
aliguorie3aff4f2009-04-05 19:14:04 +000062
Max Reitz1b58b432015-02-05 13:58:20 -050063 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
64 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010065 error_reportf_err(local_err, "can't open%s%s: ",
66 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020067 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040068 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020069
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010070 bs = blk_bs(qemuio_blk);
71 if (bdrv_is_encrypted(bs)) {
72 char password[256];
73 printf("Disk image '%s' is encrypted.\n", name);
74 if (qemu_read_password(password, sizeof(password)) < 0) {
75 error_report("No password given");
76 goto error;
77 }
78 if (bdrv_set_key(bs, password) < 0) {
79 error_report("invalid password");
80 goto error;
81 }
82 }
83
84
Devin Nakamura43642b32011-07-11 11:22:16 -040085 return 0;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010086
87 error:
88 blk_unref(qemuio_blk);
89 qemuio_blk = NULL;
90 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +000091}
92
Devin Nakamura43642b32011-07-11 11:22:16 -040093static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000094{
Devin Nakamura43642b32011-07-11 11:22:16 -040095 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000096"\n"
97" opens a new file in the requested mode\n"
98"\n"
99" Example:\n"
100" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
101"\n"
102" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000103" -r, -- open file read-only\n"
104" -s, -- use snapshot file\n"
105" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200106" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000107"\n");
108}
109
Max Reitz4c7b7e92015-02-05 13:58:22 -0500110static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000111
112static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400113 .name = "open",
114 .altname = "o",
115 .cfunc = open_f,
116 .argmin = 1,
117 .argmax = -1,
118 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200119 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400120 .oneline = "open the file specified by path",
121 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000122};
aliguorie3aff4f2009-04-05 19:14:04 +0000123
Max Reitzb543c5c2013-10-11 14:02:10 +0200124static QemuOptsList empty_opts = {
125 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200126 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200127 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
128 .desc = {
129 /* no elements => accept any params */
130 { /* end of list */ }
131 },
132};
133
Max Reitz4c7b7e92015-02-05 13:58:22 -0500134static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000135{
Devin Nakamura43642b32011-07-11 11:22:16 -0400136 int flags = 0;
137 int readonly = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400138 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200139 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200140 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000141
Eric Blakeb062ad82015-05-12 09:10:56 -0600142 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400143 switch (c) {
144 case 's':
145 flags |= BDRV_O_SNAPSHOT;
146 break;
147 case 'n':
148 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
149 break;
150 case 'r':
151 readonly = 1;
152 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200153 case 'o':
Markus Armbruster70b94332015-02-13 12:50:26 +0100154 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200155 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200156 return 0;
157 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200158 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400159 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200160 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200161 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200162 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400163 }
aliguorie3aff4f2009-04-05 19:14:04 +0000164
Devin Nakamura43642b32011-07-11 11:22:16 -0400165 if (!readonly) {
166 flags |= BDRV_O_RDWR;
167 }
aliguorie3aff4f2009-04-05 19:14:04 +0000168
Markus Armbruster443422f2014-05-28 11:16:58 +0200169 qopts = qemu_opts_find(&empty_opts, NULL);
170 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
171 qemu_opts_reset(&empty_opts);
172
Max Reitzfd0fee32013-12-20 19:28:20 +0100173 if (optind == argc - 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500174 return openfile(argv[optind], flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100175 } else if (optind == argc) {
Max Reitz10d9d752015-02-05 13:58:21 -0500176 return openfile(NULL, flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100177 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200178 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200179 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400180 }
aliguorie3aff4f2009-04-05 19:14:04 +0000181}
182
Max Reitz4c7b7e92015-02-05 13:58:22 -0500183static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200184{
185 return 1;
186}
187
188static const cmdinfo_t quit_cmd = {
189 .name = "quit",
190 .altname = "q",
191 .cfunc = quit_f,
192 .argmin = -1,
193 .argmax = -1,
194 .flags = CMD_FLAG_GLOBAL,
195 .oneline = "exit the program",
196};
197
aliguorie3aff4f2009-04-05 19:14:04 +0000198static void usage(const char *name)
199{
Devin Nakamura43642b32011-07-11 11:22:16 -0400200 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100201"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200202"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000203"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000204" --object OBJECTDEF define an object such as 'secret' for\n"
205" passwords and/or encryption keys\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400206" -c, --cmd STRING execute command with its arguments\n"
207" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100208" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000209" -r, --read-only export read-only\n"
210" -s, --snapshot use snapshot file\n"
211" -n, --nocache disable host cache\n"
212" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200213" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200214" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000215" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000216" -h, --help display this help and exit\n"
217" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400218"\n"
219"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000220"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400221 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000222}
223
Kevin Wolfd1174f12013-06-05 14:19:37 +0200224static char *get_prompt(void)
225{
226 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
227
228 if (!prompt[0]) {
229 snprintf(prompt, sizeof(prompt), "%s> ", progname);
230 }
231
232 return prompt;
233}
234
Stefan Weild5d15072014-01-25 18:18:23 +0100235static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
236 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200237{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100238 va_list ap;
239 va_start(ap, fmt);
240 vprintf(fmt, ap);
241 va_end(ap);
242}
243
244static void readline_flush_func(void *opaque)
245{
246 fflush(stdout);
247}
248
249static void readline_func(void *opaque, const char *str, void *readline_opaque)
250{
251 char **line = readline_opaque;
252 *line = g_strdup(str);
253}
254
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100255static void completion_match(const char *cmd, void *opaque)
256{
257 readline_add_completion(readline_state, cmd);
258}
259
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100260static void readline_completion_func(void *opaque, const char *str)
261{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100262 readline_set_completion_index(readline_state, strlen(str));
263 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100264}
265
266static char *fetchline_readline(void)
267{
268 char *line = NULL;
269
270 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
271 while (!line) {
272 int ch = getchar();
273 if (ch == EOF) {
274 break;
275 }
276 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200277 }
278 return line;
279}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200280
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100281#define MAXREADLINESZ 1024
282static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200283{
284 char *p, *line = g_malloc(MAXREADLINESZ);
285
286 if (!fgets(line, MAXREADLINESZ, stdin)) {
287 g_free(line);
288 return NULL;
289 }
290
291 p = line + strlen(line);
292 if (p != line && p[-1] == '\n') {
293 p[-1] = '\0';
294 }
295
296 return line;
297}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100298
299static char *fetchline(void)
300{
301 if (readline_state) {
302 return fetchline_readline();
303 } else {
304 return fetchline_fgets();
305 }
306}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200307
308static void prep_fetchline(void *opaque)
309{
310 int *fetchable = opaque;
311
312 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
313 *fetchable= 1;
314}
315
316static void command_loop(void)
317{
318 int i, done = 0, fetchable = 0, prompted = 0;
319 char *input;
320
321 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500322 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200323 }
324 if (cmdline) {
325 g_free(cmdline);
326 return;
327 }
328
329 while (!done) {
330 if (!prompted) {
331 printf("%s", get_prompt());
332 fflush(stdout);
333 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
334 prompted = 1;
335 }
336
337 main_loop_wait(false);
338
339 if (!fetchable) {
340 continue;
341 }
342
343 input = fetchline();
344 if (input == NULL) {
345 break;
346 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500347 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200348 g_free(input);
349
350 prompted = 0;
351 fetchable = 0;
352 }
353 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
354}
355
356static void add_user_command(char *optarg)
357{
Markus Armbruster5839e532014-08-19 10:31:08 +0200358 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200359 cmdline[ncmdline-1] = optarg;
360}
361
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100362static void reenable_tty_echo(void)
363{
364 qemu_set_tty_echo(STDIN_FILENO, true);
365}
366
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000367enum {
368 OPTION_OBJECT = 256,
369};
370
371static QemuOptsList qemu_object_opts = {
372 .name = "object",
373 .implied_opt_name = "qom-type",
374 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
375 .desc = {
376 { }
377 },
378};
379
380
aliguorie3aff4f2009-04-05 19:14:04 +0000381int main(int argc, char **argv)
382{
Devin Nakamura43642b32011-07-11 11:22:16 -0400383 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100384 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400385 const struct option lopt[] = {
386 { "help", 0, NULL, 'h' },
387 { "version", 0, NULL, 'V' },
388 { "offset", 1, NULL, 'o' },
389 { "cmd", 1, NULL, 'c' },
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100390 { "format", 1, NULL, 'f' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400391 { "read-only", 0, NULL, 'r' },
392 { "snapshot", 0, NULL, 's' },
393 { "nocache", 0, NULL, 'n' },
394 { "misalign", 0, NULL, 'm' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400395 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100396 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200397 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000398 { "trace", 1, NULL, 'T' },
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000399 { "object", 1, NULL, OPTION_OBJECT },
Devin Nakamura43642b32011-07-11 11:22:16 -0400400 { NULL, 0, NULL, 0 }
401 };
402 int c;
403 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100404 int flags = BDRV_O_UNMAP;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300405 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500406 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000407
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900408#ifdef CONFIG_POSIX
409 signal(SIGPIPE, SIG_IGN);
410#endif
411
Devin Nakamura43642b32011-07-11 11:22:16 -0400412 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800413 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000414
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000415 module_call_init(MODULE_INIT_QOM);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000416 qemu_add_opts(&qemu_object_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100417 bdrv_init();
418
Devin Nakamura43642b32011-07-11 11:22:16 -0400419 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
420 switch (c) {
421 case 's':
422 flags |= BDRV_O_SNAPSHOT;
423 break;
424 case 'n':
425 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
426 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100427 case 'd':
428 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
429 error_report("Invalid discard option: %s", optarg);
430 exit(1);
431 }
432 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100433 case 'f':
Max Reitz1b58b432015-02-05 13:58:20 -0500434 if (!opts) {
435 opts = qdict_new();
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100436 }
Max Reitz1b58b432015-02-05 13:58:20 -0500437 qdict_put(opts, "driver", qstring_from_str(optarg));
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100438 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400439 case 'c':
440 add_user_command(optarg);
441 break;
442 case 'r':
443 readonly = 1;
444 break;
445 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100446 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400447 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400448 case 'k':
449 flags |= BDRV_O_NATIVE_AIO;
450 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200451 case 't':
452 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
453 error_report("Invalid cache option: %s", optarg);
454 exit(1);
455 }
456 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000457 case 'T':
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300458 if (!trace_init_backends()) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000459 exit(1); /* error message will have been printed */
460 }
461 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400462 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200463 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400464 exit(0);
465 case 'h':
466 usage(progname);
467 exit(0);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000468 case OPTION_OBJECT: {
469 QemuOpts *qopts = NULL;
470 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
471 optarg, true);
472 if (!qopts) {
473 exit(1);
474 }
475 } break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400476 default:
477 usage(progname);
478 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200479 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400480 }
aliguorie3aff4f2009-04-05 19:14:04 +0000481
Devin Nakamura43642b32011-07-11 11:22:16 -0400482 if ((argc - optind) > 1) {
483 usage(progname);
484 exit(1);
485 }
aliguorie3aff4f2009-04-05 19:14:04 +0000486
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300487 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100488 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300489 exit(1);
490 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800491
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000492 if (qemu_opts_foreach(&qemu_object_opts,
493 user_creatable_add_opts_foreach,
494 NULL, &local_error)) {
495 error_report_err(local_error);
496 exit(1);
497 }
498
Devin Nakamura43642b32011-07-11 11:22:16 -0400499 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200500 qemuio_add_command(&quit_cmd);
501 qemuio_add_command(&open_cmd);
502 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400503
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100504 if (isatty(STDIN_FILENO)) {
505 readline_state = readline_init(readline_printf_func,
506 readline_flush_func,
507 NULL,
508 readline_completion_func);
509 qemu_set_tty_echo(STDIN_FILENO, false);
510 atexit(reenable_tty_echo);
511 }
512
Devin Nakamura43642b32011-07-11 11:22:16 -0400513 /* open the device */
514 if (!readonly) {
515 flags |= BDRV_O_RDWR;
516 }
517
518 if ((argc - optind) == 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500519 openfile(argv[optind], flags, opts);
Devin Nakamura43642b32011-07-11 11:22:16 -0400520 }
521 command_loop();
522
523 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000524 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400525 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000526 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400527
Markus Armbruster26f54e92014-10-07 13:59:04 +0200528 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100529 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400530 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000531}