blob: bc129536e4dd6d94d9d9655b92cf304a04fce6ea [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
Markus Armbrusterda34e652016-03-14 09:01:28 +010014#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020015#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010016#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020018#include "qemu/option.h"
19#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010020#include "qemu/readline.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010021#include "qapi/qmp/qstring.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000022#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020023#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010024#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000025#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000026
Devin Nakamura43642b32011-07-11 11:22:16 -040027#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000028
Stefan Weilf9883882014-03-05 22:23:00 +010029static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000030
Markus Armbruster26f54e92014-10-07 13:59:04 +020031static BlockBackend *qemuio_blk;
Kevin Wolf191c2892010-09-16 13:18:08 +020032
Kevin Wolfd1174f12013-06-05 14:19:37 +020033/* qemu-io commands passed using -c */
34static int ncmdline;
35static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000036static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020037
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010038static ReadLineState *readline_state;
39
Max Reitz4c7b7e92015-02-05 13:58:22 -050040static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000041{
Markus Armbruster26f54e92014-10-07 13:59:04 +020042 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020043 qemuio_blk = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040044 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000045}
46
47static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040048 .name = "close",
49 .altname = "c",
50 .cfunc = close_f,
51 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000052};
53
Max Reitz10d9d752015-02-05 13:58:21 -050054static int openfile(char *name, int flags, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000055{
Max Reitz34b5d2c2013-09-05 14:45:29 +020056 Error *local_err = NULL;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010057 BlockDriverState *bs;
Max Reitz34b5d2c2013-09-05 14:45:29 +020058
Max Reitz1b58b432015-02-05 13:58:20 -050059 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010060 error_report("file open already, try 'help close'");
Markus Armbruster29f26012014-05-28 11:16:59 +020061 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040062 return 1;
63 }
aliguorie3aff4f2009-04-05 19:14:04 +000064
Max Reitzefaa7c42016-03-16 19:54:38 +010065 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -050066 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010067 error_reportf_err(local_err, "can't open%s%s: ",
68 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020069 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040070 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020071
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010072 bs = blk_bs(qemuio_blk);
73 if (bdrv_is_encrypted(bs)) {
74 char password[256];
75 printf("Disk image '%s' is encrypted.\n", name);
76 if (qemu_read_password(password, sizeof(password)) < 0) {
77 error_report("No password given");
78 goto error;
79 }
80 if (bdrv_set_key(bs, password) < 0) {
81 error_report("invalid password");
82 goto error;
83 }
84 }
85
86
Devin Nakamura43642b32011-07-11 11:22:16 -040087 return 0;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010088
89 error:
90 blk_unref(qemuio_blk);
91 qemuio_blk = NULL;
92 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +000093}
94
Devin Nakamura43642b32011-07-11 11:22:16 -040095static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000096{
Devin Nakamura43642b32011-07-11 11:22:16 -040097 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000098"\n"
99" opens a new file in the requested mode\n"
100"\n"
101" Example:\n"
102" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
103"\n"
104" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000105" -r, -- open file read-only\n"
106" -s, -- use snapshot file\n"
107" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200108" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000109"\n");
110}
111
Max Reitz4c7b7e92015-02-05 13:58:22 -0500112static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000113
114static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400115 .name = "open",
116 .altname = "o",
117 .cfunc = open_f,
118 .argmin = 1,
119 .argmax = -1,
120 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200121 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400122 .oneline = "open the file specified by path",
123 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000124};
aliguorie3aff4f2009-04-05 19:14:04 +0000125
Max Reitzb543c5c2013-10-11 14:02:10 +0200126static QemuOptsList empty_opts = {
127 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200128 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200129 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
130 .desc = {
131 /* no elements => accept any params */
132 { /* end of list */ }
133 },
134};
135
Max Reitz4c7b7e92015-02-05 13:58:22 -0500136static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000137{
Devin Nakamura43642b32011-07-11 11:22:16 -0400138 int flags = 0;
139 int readonly = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400140 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200141 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200142 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000143
Eric Blakeb062ad82015-05-12 09:10:56 -0600144 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400145 switch (c) {
146 case 's':
147 flags |= BDRV_O_SNAPSHOT;
148 break;
149 case 'n':
150 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
151 break;
152 case 'r':
153 readonly = 1;
154 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200155 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000156 if (imageOpts) {
157 printf("--image-opts and 'open -o' are mutually exclusive\n");
158 return 0;
159 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100160 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200161 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200162 return 0;
163 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200164 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400165 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200166 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200167 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200168 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400169 }
aliguorie3aff4f2009-04-05 19:14:04 +0000170
Devin Nakamura43642b32011-07-11 11:22:16 -0400171 if (!readonly) {
172 flags |= BDRV_O_RDWR;
173 }
aliguorie3aff4f2009-04-05 19:14:04 +0000174
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000175 if (imageOpts && (optind == argc - 1)) {
176 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
177 qemu_opts_reset(&empty_opts);
178 return 0;
179 }
180 optind++;
181 }
182
Markus Armbruster443422f2014-05-28 11:16:58 +0200183 qopts = qemu_opts_find(&empty_opts, NULL);
184 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
185 qemu_opts_reset(&empty_opts);
186
Max Reitzfd0fee32013-12-20 19:28:20 +0100187 if (optind == argc - 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500188 return openfile(argv[optind], flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100189 } else if (optind == argc) {
Max Reitz10d9d752015-02-05 13:58:21 -0500190 return openfile(NULL, flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100191 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200192 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200193 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400194 }
aliguorie3aff4f2009-04-05 19:14:04 +0000195}
196
Max Reitz4c7b7e92015-02-05 13:58:22 -0500197static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200198{
199 return 1;
200}
201
202static const cmdinfo_t quit_cmd = {
203 .name = "quit",
204 .altname = "q",
205 .cfunc = quit_f,
206 .argmin = -1,
207 .argmax = -1,
208 .flags = CMD_FLAG_GLOBAL,
209 .oneline = "exit the program",
210};
211
aliguorie3aff4f2009-04-05 19:14:04 +0000212static void usage(const char *name)
213{
Devin Nakamura43642b32011-07-11 11:22:16 -0400214 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100215"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200216"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000217"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000218" --object OBJECTDEF define an object such as 'secret' for\n"
219" passwords and/or encryption keys\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400220" -c, --cmd STRING execute command with its arguments\n"
221" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100222" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000223" -r, --read-only export read-only\n"
224" -s, --snapshot use snapshot file\n"
225" -n, --nocache disable host cache\n"
226" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200227" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200228" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000229" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000230" -h, --help display this help and exit\n"
231" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400232"\n"
233"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000234"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400235 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000236}
237
Kevin Wolfd1174f12013-06-05 14:19:37 +0200238static char *get_prompt(void)
239{
240 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
241
242 if (!prompt[0]) {
243 snprintf(prompt, sizeof(prompt), "%s> ", progname);
244 }
245
246 return prompt;
247}
248
Stefan Weild5d15072014-01-25 18:18:23 +0100249static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
250 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200251{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100252 va_list ap;
253 va_start(ap, fmt);
254 vprintf(fmt, ap);
255 va_end(ap);
256}
257
258static void readline_flush_func(void *opaque)
259{
260 fflush(stdout);
261}
262
263static void readline_func(void *opaque, const char *str, void *readline_opaque)
264{
265 char **line = readline_opaque;
266 *line = g_strdup(str);
267}
268
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100269static void completion_match(const char *cmd, void *opaque)
270{
271 readline_add_completion(readline_state, cmd);
272}
273
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100274static void readline_completion_func(void *opaque, const char *str)
275{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100276 readline_set_completion_index(readline_state, strlen(str));
277 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100278}
279
280static char *fetchline_readline(void)
281{
282 char *line = NULL;
283
284 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
285 while (!line) {
286 int ch = getchar();
287 if (ch == EOF) {
288 break;
289 }
290 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200291 }
292 return line;
293}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200294
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100295#define MAXREADLINESZ 1024
296static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200297{
298 char *p, *line = g_malloc(MAXREADLINESZ);
299
300 if (!fgets(line, MAXREADLINESZ, stdin)) {
301 g_free(line);
302 return NULL;
303 }
304
305 p = line + strlen(line);
306 if (p != line && p[-1] == '\n') {
307 p[-1] = '\0';
308 }
309
310 return line;
311}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100312
313static char *fetchline(void)
314{
315 if (readline_state) {
316 return fetchline_readline();
317 } else {
318 return fetchline_fgets();
319 }
320}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200321
322static void prep_fetchline(void *opaque)
323{
324 int *fetchable = opaque;
325
326 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
327 *fetchable= 1;
328}
329
330static void command_loop(void)
331{
332 int i, done = 0, fetchable = 0, prompted = 0;
333 char *input;
334
335 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500336 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200337 }
338 if (cmdline) {
339 g_free(cmdline);
340 return;
341 }
342
343 while (!done) {
344 if (!prompted) {
345 printf("%s", get_prompt());
346 fflush(stdout);
347 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
348 prompted = 1;
349 }
350
351 main_loop_wait(false);
352
353 if (!fetchable) {
354 continue;
355 }
356
357 input = fetchline();
358 if (input == NULL) {
359 break;
360 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500361 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200362 g_free(input);
363
364 prompted = 0;
365 fetchable = 0;
366 }
367 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
368}
369
370static void add_user_command(char *optarg)
371{
Markus Armbruster5839e532014-08-19 10:31:08 +0200372 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200373 cmdline[ncmdline-1] = optarg;
374}
375
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100376static void reenable_tty_echo(void)
377{
378 qemu_set_tty_echo(STDIN_FILENO, true);
379}
380
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000381enum {
382 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000383 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000384};
385
386static QemuOptsList qemu_object_opts = {
387 .name = "object",
388 .implied_opt_name = "qom-type",
389 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
390 .desc = {
391 { }
392 },
393};
394
395
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000396static QemuOptsList file_opts = {
397 .name = "file",
398 .implied_opt_name = "file",
399 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
400 .desc = {
401 /* no elements => accept any params */
402 { /* end of list */ }
403 },
404};
405
aliguorie3aff4f2009-04-05 19:14:04 +0000406int main(int argc, char **argv)
407{
Devin Nakamura43642b32011-07-11 11:22:16 -0400408 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100409 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400410 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000411 { "help", no_argument, NULL, 'h' },
412 { "version", no_argument, NULL, 'V' },
413 { "offset", required_argument, NULL, 'o' },
414 { "cmd", required_argument, NULL, 'c' },
415 { "format", required_argument, NULL, 'f' },
416 { "read-only", no_argument, NULL, 'r' },
417 { "snapshot", no_argument, NULL, 's' },
418 { "nocache", no_argument, NULL, 'n' },
419 { "misalign", no_argument, NULL, 'm' },
420 { "native-aio", no_argument, NULL, 'k' },
421 { "discard", required_argument, NULL, 'd' },
422 { "cache", required_argument, NULL, 't' },
423 { "trace", required_argument, NULL, 'T' },
424 { "object", required_argument, NULL, OPTION_OBJECT },
425 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Devin Nakamura43642b32011-07-11 11:22:16 -0400426 { NULL, 0, NULL, 0 }
427 };
428 int c;
429 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100430 int flags = BDRV_O_UNMAP;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300431 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500432 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000433 const char *format = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000434
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900435#ifdef CONFIG_POSIX
436 signal(SIGPIPE, SIG_IGN);
437#endif
438
Devin Nakamura43642b32011-07-11 11:22:16 -0400439 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800440 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000441
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000442 module_call_init(MODULE_INIT_QOM);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000443 qemu_add_opts(&qemu_object_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100444 bdrv_init();
445
Devin Nakamura43642b32011-07-11 11:22:16 -0400446 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
447 switch (c) {
448 case 's':
449 flags |= BDRV_O_SNAPSHOT;
450 break;
451 case 'n':
452 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
453 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100454 case 'd':
455 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
456 error_report("Invalid discard option: %s", optarg);
457 exit(1);
458 }
459 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100460 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000461 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100462 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400463 case 'c':
464 add_user_command(optarg);
465 break;
466 case 'r':
467 readonly = 1;
468 break;
469 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100470 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400471 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400472 case 'k':
473 flags |= BDRV_O_NATIVE_AIO;
474 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200475 case 't':
476 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
477 error_report("Invalid cache option: %s", optarg);
478 exit(1);
479 }
480 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000481 case 'T':
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300482 if (!trace_init_backends()) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000483 exit(1); /* error message will have been printed */
484 }
485 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400486 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200487 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400488 exit(0);
489 case 'h':
490 usage(progname);
491 exit(0);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000492 case OPTION_OBJECT: {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000493 QemuOpts *qopts;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000494 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
495 optarg, true);
496 if (!qopts) {
497 exit(1);
498 }
499 } break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000500 case OPTION_IMAGE_OPTS:
501 imageOpts = true;
502 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400503 default:
504 usage(progname);
505 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200506 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400507 }
aliguorie3aff4f2009-04-05 19:14:04 +0000508
Devin Nakamura43642b32011-07-11 11:22:16 -0400509 if ((argc - optind) > 1) {
510 usage(progname);
511 exit(1);
512 }
aliguorie3aff4f2009-04-05 19:14:04 +0000513
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000514 if (format && imageOpts) {
515 error_report("--image-opts and -f are mutually exclusive");
516 exit(1);
517 }
518
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300519 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100520 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300521 exit(1);
522 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800523
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000524 if (qemu_opts_foreach(&qemu_object_opts,
525 user_creatable_add_opts_foreach,
526 NULL, &local_error)) {
527 error_report_err(local_error);
528 exit(1);
529 }
530
Devin Nakamura43642b32011-07-11 11:22:16 -0400531 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200532 qemuio_add_command(&quit_cmd);
533 qemuio_add_command(&open_cmd);
534 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400535
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100536 if (isatty(STDIN_FILENO)) {
537 readline_state = readline_init(readline_printf_func,
538 readline_flush_func,
539 NULL,
540 readline_completion_func);
541 qemu_set_tty_echo(STDIN_FILENO, false);
542 atexit(reenable_tty_echo);
543 }
544
Devin Nakamura43642b32011-07-11 11:22:16 -0400545 /* open the device */
546 if (!readonly) {
547 flags |= BDRV_O_RDWR;
548 }
549
550 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000551 if (imageOpts) {
552 QemuOpts *qopts = NULL;
553 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
554 if (!qopts) {
555 exit(1);
556 }
557 opts = qemu_opts_to_qdict(qopts, NULL);
558 openfile(NULL, flags, opts);
559 } else {
560 if (format) {
561 opts = qdict_new();
562 qdict_put(opts, "driver", qstring_from_str(format));
563 }
564 openfile(argv[optind], flags, opts);
565 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400566 }
567 command_loop();
568
569 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000570 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400571 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000572 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400573
Markus Armbruster26f54e92014-10-07 13:59:04 +0200574 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100575 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400576 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000577}