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