]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-io.c
Include qemu/module.h where needed, drop it from qemu-common.h
[mirror_qemu.git] / qemu-io.c
CommitLineData
e3aff4f6
AL
1/*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
452fcdbc 10
80c71a24 11#include "qemu/osdep.h"
e3aff4f6 12#include <getopt.h>
c32d766a 13#include <libgen.h>
0e448a05
DB
14#ifndef _WIN32
15#include <termios.h>
16#endif
e3aff4f6 17
da34e65c 18#include "qapi/error.h"
3d21994f 19#include "qemu-io.h"
d49b6836 20#include "qemu/error-report.h"
1de7afc9 21#include "qemu/main-loop.h"
0b8fa32f 22#include "qemu/module.h"
b543c5cd
HR
23#include "qemu/option.h"
24#include "qemu/config-file.h"
0cf17e18 25#include "qemu/readline.h"
e9a80859 26#include "qemu/log.h"
d49b6836 27#include "qapi/qmp/qstring.h"
452fcdbc 28#include "qapi/qmp/qdict.h"
9ba371b6 29#include "qom/object_interfaces.h"
26f54e9a 30#include "sysemu/block-backend.h"
737e150e 31#include "block/block_int.h"
d7bb72c8 32#include "trace/control.h"
c2297088 33#include "crypto/init.h"
4face32a 34#include "qemu-version.h"
e3aff4f6 35
43642b38 36#define CMD_NOFILE_OK 0x01
e3aff4f6 37
26f54e9a 38static BlockBackend *qemuio_blk;
b444d0e9 39static bool quit_qemu_io;
191c2890 40
d1174f13
KW
41/* qemu-io commands passed using -c */
42static int ncmdline;
43static char **cmdline;
499afa25 44static bool imageOpts;
d1174f13 45
0cf17e18
SH
46static ReadLineState *readline_state;
47
0e448a05
DB
48static int ttyEOF;
49
50static int get_eof_char(void)
51{
52#ifdef _WIN32
53 return 0x4; /* Ctrl-D */
54#else
55 struct termios tty;
56 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
57 if (errno == ENOTTY) {
58 return 0x0; /* just expect read() == 0 */
59 } else {
60 return 0x4; /* Ctrl-D */
61 }
62 }
63
64 return tty.c_cc[VEOF];
65#endif
66}
67
b32d7a39 68static int close_f(BlockBackend *blk, int argc, char **argv)
e3aff4f6 69{
26f54e9a 70 blk_unref(qemuio_blk);
26f54e9a 71 qemuio_blk = NULL;
b32d7a39 72 return 0;
e3aff4f6
AL
73}
74
75static const cmdinfo_t close_cmd = {
43642b38
DN
76 .name = "close",
77 .altname = "c",
78 .cfunc = close_f,
79 .oneline = "close the current open file",
e3aff4f6
AL
80};
81
459571f7
FZ
82static int openfile(char *name, int flags, bool writethrough, bool force_share,
83 QDict *opts)
e3aff4f6 84{
34b5d2c6
HR
85 Error *local_err = NULL;
86
1b58b438 87 if (qemuio_blk) {
b9884681 88 error_report("file open already, try 'help close'");
cb3e7f08 89 qobject_unref(opts);
43642b38
DN
90 return 1;
91 }
92
459571f7
FZ
93 if (force_share) {
94 if (!opts) {
95 opts = qdict_new();
96 }
97 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
2a01c01f 98 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
459571f7 99 error_report("-U conflicts with image options");
cb3e7f08 100 qobject_unref(opts);
459571f7
FZ
101 return 1;
102 }
2a01c01f 103 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
459571f7 104 }
efaa7c4e 105 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
1b58b438 106 if (!qemuio_blk) {
b9884681
MA
107 error_reportf_err(local_err, "can't open%s%s: ",
108 name ? " device " : "", name ?: "");
dbb651c4 109 return 1;
43642b38
DN
110 }
111
e151fc16 112 blk_set_enable_write_cache(qemuio_blk, !writethrough);
8caf0212 113
43642b38 114 return 0;
e3aff4f6
AL
115}
116
43642b38 117static void open_help(void)
e3aff4f6 118{
43642b38 119 printf(
e3aff4f6
AL
120"\n"
121" opens a new file in the requested mode\n"
122"\n"
123" Example:\n"
e4e12bb2 124" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
e3aff4f6
AL
125"\n"
126" Opens a file for subsequent use by all of the other qemu-io commands.\n"
e3aff4f6
AL
127" -r, -- open file read-only\n"
128" -s, -- use snapshot file\n"
0f40444c 129" -C, -- use copy-on-read\n"
b8d970f1 130" -n, -- disable host cache, short for -t none\n"
459571f7 131" -U, -- force shared permissions\n"
b8d970f1
EB
132" -k, -- use kernel AIO implementation (on Linux only)\n"
133" -t, -- use the given cache mode for the image\n"
134" -d, -- use the given discard mode for the image\n"
b543c5cd 135" -o, -- options to be given to the block driver"
e3aff4f6
AL
136"\n");
137}
138
b32d7a39 139static int open_f(BlockBackend *blk, int argc, char **argv);
22a2bdcb
BS
140
141static const cmdinfo_t open_cmd = {
43642b38
DN
142 .name = "open",
143 .altname = "o",
144 .cfunc = open_f,
145 .argmin = 1,
146 .argmax = -1,
147 .flags = CMD_NOFILE_OK,
0f40444c 148 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
43642b38
DN
149 .oneline = "open the file specified by path",
150 .help = open_help,
22a2bdcb 151};
e3aff4f6 152
b543c5cd
HR
153static QemuOptsList empty_opts = {
154 .name = "drive",
443422fd 155 .merge_lists = true,
b543c5cd
HR
156 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
157 .desc = {
158 /* no elements => accept any params */
159 { /* end of list */ }
160 },
161};
162
b32d7a39 163static int open_f(BlockBackend *blk, int argc, char **argv)
e3aff4f6 164{
b8d970f1 165 int flags = BDRV_O_UNMAP;
43642b38 166 int readonly = 0;
e151fc16 167 bool writethrough = true;
43642b38 168 int c;
b32d7a39 169 int ret;
b543c5cd 170 QemuOpts *qopts;
443422fd 171 QDict *opts;
459571f7 172 bool force_share = false;
43642b38 173
0f40444c 174 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
43642b38
DN
175 switch (c) {
176 case 's':
177 flags |= BDRV_O_SNAPSHOT;
178 break;
179 case 'n':
e151fc16
KW
180 flags |= BDRV_O_NOCACHE;
181 writethrough = false;
43642b38 182 break;
0f40444c
EB
183 case 'C':
184 flags |= BDRV_O_COPY_ON_READ;
185 break;
43642b38
DN
186 case 'r':
187 readonly = 1;
188 break;
b8d970f1
EB
189 case 'k':
190 flags |= BDRV_O_NATIVE_AIO;
191 break;
192 case 't':
193 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
194 error_report("Invalid cache option: %s", optarg);
195 qemu_opts_reset(&empty_opts);
b32d7a39 196 return -EINVAL;
b8d970f1
EB
197 }
198 break;
199 case 'd':
200 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
201 error_report("Invalid discard option: %s", optarg);
202 qemu_opts_reset(&empty_opts);
b32d7a39 203 return -EINVAL;
b8d970f1
EB
204 }
205 break;
b543c5cd 206 case 'o':
499afa25
DB
207 if (imageOpts) {
208 printf("--image-opts and 'open -o' are mutually exclusive\n");
b8d970f1 209 qemu_opts_reset(&empty_opts);
b32d7a39 210 return -EINVAL;
499afa25 211 }
70b94331 212 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
443422fd 213 qemu_opts_reset(&empty_opts);
b32d7a39 214 return -EINVAL;
b543c5cd 215 }
b543c5cd 216 break;
459571f7
FZ
217 case 'U':
218 force_share = true;
219 break;
43642b38 220 default:
443422fd 221 qemu_opts_reset(&empty_opts);
b444d0e9 222 qemuio_command_usage(&open_cmd);
b32d7a39 223 return -EINVAL;
f5edb014 224 }
43642b38
DN
225 }
226
227 if (!readonly) {
228 flags |= BDRV_O_RDWR;
229 }
e3aff4f6 230
499afa25
DB
231 if (imageOpts && (optind == argc - 1)) {
232 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
233 qemu_opts_reset(&empty_opts);
b32d7a39 234 return -EINVAL;
499afa25
DB
235 }
236 optind++;
237 }
238
443422fd
MA
239 qopts = qemu_opts_find(&empty_opts, NULL);
240 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
241 qemu_opts_reset(&empty_opts);
242
fd0fee34 243 if (optind == argc - 1) {
b32d7a39 244 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
fd0fee34 245 } else if (optind == argc) {
b32d7a39 246 ret = openfile(NULL, flags, writethrough, force_share, opts);
fd0fee34 247 } else {
cb3e7f08 248 qobject_unref(opts);
64ebf556 249 qemuio_command_usage(&open_cmd);
b32d7a39
HR
250 return -EINVAL;
251 }
252
253 if (ret) {
254 return -EINVAL;
43642b38 255 }
b32d7a39
HR
256
257 return 0;
e3aff4f6
AL
258}
259
b32d7a39 260static int quit_f(BlockBackend *blk, int argc, char **argv)
e681be7e 261{
b444d0e9 262 quit_qemu_io = true;
b32d7a39 263 return 0;
e681be7e
KW
264}
265
266static const cmdinfo_t quit_cmd = {
267 .name = "quit",
268 .altname = "q",
269 .cfunc = quit_f,
270 .argmin = -1,
271 .argmax = -1,
272 .flags = CMD_FLAG_GLOBAL,
273 .oneline = "exit the program",
274};
275
e3aff4f6
AL
276static void usage(const char *name)
277{
43642b38 278 printf(
e4e12bb2 279"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
84844a20 280"QEMU Disk exerciser\n"
e3aff4f6 281"\n"
9ba371b6
DB
282" --object OBJECTDEF define an object such as 'secret' for\n"
283" passwords and/or encryption keys\n"
e4e12bb2 284" --image-opts treat file as option string\n"
d208cc35
MK
285" -c, --cmd STRING execute command with its arguments\n"
286" from the given string\n"
be6273da 287" -f, --format FMT specifies the block driver to use\n"
e3aff4f6
AL
288" -r, --read-only export read-only\n"
289" -s, --snapshot use snapshot file\n"
e4e12bb2 290" -n, --nocache disable host cache, short for -t none\n"
0f40444c 291" -C, --copy-on-read enable copy-on-read\n"
e3aff4f6 292" -m, --misalign misalign allocations for O_DIRECT\n"
5c6c3a6c 293" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
592fa070 294" -t, --cache=MODE use the given cache mode for the image\n"
e4e12bb2 295" -d, --discard=MODE use the given discard mode for the image\n"
e9a80859
DL
296" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
297" specify tracing options\n"
298" see qemu-img(1) man page for full description\n"
459571f7 299" -U, --force-share force shared permissions\n"
e3aff4f6
AL
300" -h, --help display this help and exit\n"
301" -V, --version output version information and exit\n"
d208cc35 302"\n"
f5048cb7
EB
303"See '%s -c help' for information on available commands.\n"
304"\n"
305QEMU_HELP_BOTTOM "\n",
d208cc35 306 name, name);
e3aff4f6
AL
307}
308
d1174f13
KW
309static char *get_prompt(void)
310{
311 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
312
313 if (!prompt[0]) {
99e98d7c 314 snprintf(prompt, sizeof(prompt), "%s> ", error_get_progname());
d1174f13
KW
315 }
316
317 return prompt;
318}
319
d5d1507b
SW
320static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
321 const char *fmt, ...)
d1174f13 322{
0cf17e18
SH
323 va_list ap;
324 va_start(ap, fmt);
325 vprintf(fmt, ap);
326 va_end(ap);
d1174f13 327}
0cf17e18
SH
328
329static void readline_flush_func(void *opaque)
d1174f13 330{
0cf17e18 331 fflush(stdout);
d1174f13
KW
332}
333
0cf17e18 334static void readline_func(void *opaque, const char *str, void *readline_opaque)
d1174f13 335{
0cf17e18
SH
336 char **line = readline_opaque;
337 *line = g_strdup(str);
338}
339
4694020d
SH
340static void completion_match(const char *cmd, void *opaque)
341{
342 readline_add_completion(readline_state, cmd);
343}
344
0cf17e18
SH
345static void readline_completion_func(void *opaque, const char *str)
346{
4694020d
SH
347 readline_set_completion_index(readline_state, strlen(str));
348 qemuio_complete_command(str, completion_match, NULL);
0cf17e18
SH
349}
350
351static char *fetchline_readline(void)
352{
353 char *line = NULL;
354
355 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
356 while (!line) {
357 int ch = getchar();
0e448a05
DB
358 if (ttyEOF != 0x0 && ch == ttyEOF) {
359 printf("\n");
0cf17e18 360 break;
d1174f13 361 }
0cf17e18 362 readline_handle_byte(readline_state, ch);
d1174f13
KW
363 }
364 return line;
365}
0cf17e18
SH
366
367#define MAXREADLINESZ 1024
368static char *fetchline_fgets(void)
d1174f13
KW
369{
370 char *p, *line = g_malloc(MAXREADLINESZ);
371
372 if (!fgets(line, MAXREADLINESZ, stdin)) {
373 g_free(line);
374 return NULL;
375 }
376
377 p = line + strlen(line);
378 if (p != line && p[-1] == '\n') {
379 p[-1] = '\0';
380 }
381
382 return line;
383}
0cf17e18
SH
384
385static char *fetchline(void)
386{
387 if (readline_state) {
388 return fetchline_readline();
389 } else {
390 return fetchline_fgets();
391 }
392}
d1174f13
KW
393
394static void prep_fetchline(void *opaque)
395{
396 int *fetchable = opaque;
397
398 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
399 *fetchable= 1;
400}
401
6b3aa848 402static int command_loop(void)
d1174f13 403{
b444d0e9 404 int i, fetchable = 0, prompted = 0;
6b3aa848 405 int ret, last_error = 0;
d1174f13
KW
406 char *input;
407
b444d0e9 408 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
6b3aa848
HR
409 ret = qemuio_command(qemuio_blk, cmdline[i]);
410 if (ret < 0) {
411 last_error = ret;
412 }
d1174f13
KW
413 }
414 if (cmdline) {
415 g_free(cmdline);
6b3aa848 416 return last_error;
d1174f13
KW
417 }
418
b444d0e9 419 while (!quit_qemu_io) {
d1174f13
KW
420 if (!prompted) {
421 printf("%s", get_prompt());
422 fflush(stdout);
423 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
424 prompted = 1;
425 }
426
427 main_loop_wait(false);
428
429 if (!fetchable) {
430 continue;
431 }
432
433 input = fetchline();
434 if (input == NULL) {
435 break;
436 }
6b3aa848 437 ret = qemuio_command(qemuio_blk, input);
d1174f13
KW
438 g_free(input);
439
6b3aa848
HR
440 if (ret < 0) {
441 last_error = ret;
442 }
443
d1174f13
KW
444 prompted = 0;
445 fetchable = 0;
446 }
447 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
6b3aa848
HR
448
449 return last_error;
d1174f13
KW
450}
451
452static void add_user_command(char *optarg)
453{
5839e53b 454 cmdline = g_renew(char *, cmdline, ++ncmdline);
d1174f13
KW
455 cmdline[ncmdline-1] = optarg;
456}
457
0cf17e18
SH
458static void reenable_tty_echo(void)
459{
460 qemu_set_tty_echo(STDIN_FILENO, true);
461}
462
9ba371b6
DB
463enum {
464 OPTION_OBJECT = 256,
499afa25 465 OPTION_IMAGE_OPTS = 257,
9ba371b6
DB
466};
467
468static QemuOptsList qemu_object_opts = {
469 .name = "object",
470 .implied_opt_name = "qom-type",
471 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
472 .desc = {
473 { }
474 },
475};
476
477
499afa25
DB
478static QemuOptsList file_opts = {
479 .name = "file",
480 .implied_opt_name = "file",
481 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
482 .desc = {
483 /* no elements => accept any params */
484 { /* end of list */ }
485 },
486};
487
e3aff4f6
AL
488int main(int argc, char **argv)
489{
43642b38 490 int readonly = 0;
0f40444c 491 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
43642b38 492 const struct option lopt[] = {
a513416e
DB
493 { "help", no_argument, NULL, 'h' },
494 { "version", no_argument, NULL, 'V' },
a513416e
DB
495 { "cmd", required_argument, NULL, 'c' },
496 { "format", required_argument, NULL, 'f' },
497 { "read-only", no_argument, NULL, 'r' },
498 { "snapshot", no_argument, NULL, 's' },
499 { "nocache", no_argument, NULL, 'n' },
0f40444c 500 { "copy-on-read", no_argument, NULL, 'C' },
a513416e
DB
501 { "misalign", no_argument, NULL, 'm' },
502 { "native-aio", no_argument, NULL, 'k' },
503 { "discard", required_argument, NULL, 'd' },
504 { "cache", required_argument, NULL, 't' },
505 { "trace", required_argument, NULL, 'T' },
506 { "object", required_argument, NULL, OPTION_OBJECT },
507 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
459571f7 508 { "force-share", no_argument, 0, 'U'},
43642b38
DN
509 { NULL, 0, NULL, 0 }
510 };
511 int c;
512 int opt_index = 0;
9e8f1835 513 int flags = BDRV_O_UNMAP;
6b3aa848 514 int ret;
e151fc16 515 bool writethrough = true;
2f78e491 516 Error *local_error = NULL;
1b58b438 517 QDict *opts = NULL;
499afa25 518 const char *format = NULL;
e9a80859 519 char *trace_file = NULL;
459571f7 520 bool force_share = false;
43642b38 521
526eda14
MK
522#ifdef CONFIG_POSIX
523 signal(SIGPIPE, SIG_IGN);
524#endif
525
f5852efa 526 error_init(argv[0]);
fe4db84d 527 module_call_init(MODULE_INIT_TRACE);
10f5bff6 528 qemu_init_exec_dir(argv[0]);
43642b38 529
e8f2d272 530 qcrypto_init(&error_fatal);
c2297088 531
064097d9 532 module_call_init(MODULE_INIT_QOM);
9ba371b6 533 qemu_add_opts(&qemu_object_opts);
e9a80859 534 qemu_add_opts(&qemu_trace_opts);
be6273da
KW
535 bdrv_init();
536
43642b38
DN
537 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
538 switch (c) {
539 case 's':
540 flags |= BDRV_O_SNAPSHOT;
541 break;
542 case 'n':
e151fc16
KW
543 flags |= BDRV_O_NOCACHE;
544 writethrough = false;
43642b38 545 break;
0f40444c
EB
546 case 'C':
547 flags |= BDRV_O_COPY_ON_READ;
548 break;
9e8f1835
PB
549 case 'd':
550 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
551 error_report("Invalid discard option: %s", optarg);
552 exit(1);
553 }
554 break;
be6273da 555 case 'f':
499afa25 556 format = optarg;
be6273da 557 break;
43642b38
DN
558 case 'c':
559 add_user_command(optarg);
560 break;
561 case 'r':
562 readonly = 1;
563 break;
564 case 'm':
f9883880 565 qemuio_misalign = true;
43642b38 566 break;
43642b38
DN
567 case 'k':
568 flags |= BDRV_O_NATIVE_AIO;
569 break;
592fa070 570 case 't':
e151fc16 571 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
592fa070
KW
572 error_report("Invalid cache option: %s", optarg);
573 exit(1);
574 }
575 break;
d7bb72c8 576 case 'T':
e9a80859
DL
577 g_free(trace_file);
578 trace_file = trace_opt_parse(optarg);
d7bb72c8 579 break;
43642b38 580 case 'V':
7e563bfb 581 printf("%s version " QEMU_FULL_VERSION "\n"
99e98d7c 582 QEMU_COPYRIGHT "\n", error_get_progname());
43642b38
DN
583 exit(0);
584 case 'h':
99e98d7c 585 usage(error_get_progname());
43642b38 586 exit(0);
459571f7
FZ
587 case 'U':
588 force_share = true;
589 break;
9ba371b6 590 case OPTION_OBJECT: {
499afa25 591 QemuOpts *qopts;
9ba371b6
DB
592 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
593 optarg, true);
594 if (!qopts) {
595 exit(1);
596 }
597 } break;
499afa25
DB
598 case OPTION_IMAGE_OPTS:
599 imageOpts = true;
600 break;
43642b38 601 default:
99e98d7c 602 usage(error_get_progname());
43642b38 603 exit(1);
f5edb014 604 }
43642b38
DN
605 }
606
607 if ((argc - optind) > 1) {
99e98d7c 608 usage(error_get_progname());
43642b38
DN
609 exit(1);
610 }
e3aff4f6 611
499afa25
DB
612 if (format && imageOpts) {
613 error_report("--image-opts and -f are mutually exclusive");
614 exit(1);
615 }
616
2f78e491 617 if (qemu_init_main_loop(&local_error)) {
565f65d2 618 error_report_err(local_error);
2f78e491
CN
619 exit(1);
620 }
a57d1143 621
7e1e0c11
MA
622 qemu_opts_foreach(&qemu_object_opts,
623 user_creatable_add_opts_foreach,
624 NULL, &error_fatal);
9ba371b6 625
e9a80859
DL
626 if (!trace_init_backends()) {
627 exit(1);
628 }
629 trace_init_file(trace_file);
630 qemu_set_log(LOG_TRACE);
631
43642b38 632 /* initialize commands */
c2cdf5c5
KW
633 qemuio_add_command(&quit_cmd);
634 qemuio_add_command(&open_cmd);
635 qemuio_add_command(&close_cmd);
43642b38 636
0cf17e18 637 if (isatty(STDIN_FILENO)) {
0e448a05 638 ttyEOF = get_eof_char();
0cf17e18
SH
639 readline_state = readline_init(readline_printf_func,
640 readline_flush_func,
641 NULL,
642 readline_completion_func);
643 qemu_set_tty_echo(STDIN_FILENO, false);
644 atexit(reenable_tty_echo);
645 }
646
43642b38
DN
647 /* open the device */
648 if (!readonly) {
649 flags |= BDRV_O_RDWR;
650 }
651
652 if ((argc - optind) == 1) {
499afa25
DB
653 if (imageOpts) {
654 QemuOpts *qopts = NULL;
655 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
656 if (!qopts) {
657 exit(1);
658 }
659 opts = qemu_opts_to_qdict(qopts, NULL);
459571f7 660 if (openfile(NULL, flags, writethrough, force_share, opts)) {
b7aa1315
NS
661 exit(1);
662 }
499afa25
DB
663 } else {
664 if (format) {
665 opts = qdict_new();
46f5ac20 666 qdict_put_str(opts, "driver", format);
499afa25 667 }
459571f7
FZ
668 if (openfile(argv[optind], flags, writethrough,
669 force_share, opts)) {
b7aa1315
NS
670 exit(1);
671 }
499afa25 672 }
43642b38 673 }
6b3aa848 674 ret = command_loop();
e3aff4f6 675
43642b38 676 /*
922453bc 677 * Make sure all outstanding requests complete before the program exits.
43642b38 678 */
922453bc 679 bdrv_drain_all();
95533d5f 680
26f54e9a 681 blk_unref(qemuio_blk);
0cf17e18 682 g_free(readline_state);
6b3aa848
HR
683
684 if (ret < 0) {
685 return 1;
686 } else {
687 return 0;
688 }
e3aff4f6 689}