Merge tag 'v3.1.0-rc0'
[qemu/ar7.git] / qemu-io.c
blobc932ee661bc303ba367effbc5c4dd7f7c6646512
1 /*
2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
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 */
11 #include "qemu/osdep.h"
12 #include <getopt.h>
13 #include <libgen.h>
14 #ifndef _WIN32
15 #include <termios.h>
16 #endif
18 #include "qapi/error.h"
19 #include "qemu-io.h"
20 #include "qemu/error-report.h"
21 #include "qemu/main-loop.h"
22 #include "qemu/option.h"
23 #include "qemu/config-file.h"
24 #include "qemu/readline.h"
25 #include "qemu/log.h"
26 #include "qapi/qmp/qstring.h"
27 #include "qapi/qmp/qdict.h"
28 #include "qom/object_interfaces.h"
29 #include "sysemu/block-backend.h"
30 #include "block/block_int.h"
31 #include "trace/control.h"
32 #include "crypto/init.h"
33 #include "qemu-version.h"
35 #define CMD_NOFILE_OK 0x01
37 static char *progname;
39 static BlockBackend *qemuio_blk;
40 static bool quit_qemu_io;
42 /* qemu-io commands passed using -c */
43 static int ncmdline;
44 static char **cmdline;
45 static bool imageOpts;
47 static ReadLineState *readline_state;
49 static int ttyEOF;
51 static int get_eof_char(void)
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 */
65 return tty.c_cc[VEOF];
66 #endif
69 static int close_f(BlockBackend *blk, int argc, char **argv)
71 blk_unref(qemuio_blk);
72 qemuio_blk = NULL;
73 return 0;
76 static const cmdinfo_t close_cmd = {
77 .name = "close",
78 .altname = "c",
79 .cfunc = close_f,
80 .oneline = "close the current open file",
83 static int openfile(char *name, int flags, bool writethrough, bool force_share,
84 QDict *opts)
86 Error *local_err = NULL;
88 if (qemuio_blk) {
89 error_report("file open already, try 'help close'");
90 qobject_unref(opts);
91 return 1;
94 if (force_share) {
95 if (!opts) {
96 opts = qdict_new();
98 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
99 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
100 error_report("-U conflicts with image options");
101 qobject_unref(opts);
102 return 1;
104 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
106 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
107 if (!qemuio_blk) {
108 error_reportf_err(local_err, "can't open%s%s: ",
109 name ? " device " : "", name ?: "");
110 return 1;
113 blk_set_enable_write_cache(qemuio_blk, !writethrough);
115 return 0;
118 static void open_help(void)
120 printf(
121 "\n"
122 " opens a new file in the requested mode\n"
123 "\n"
124 " Example:\n"
125 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
126 "\n"
127 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
128 " -r, -- open file read-only\n"
129 " -s, -- use snapshot file\n"
130 " -C, -- use copy-on-read\n"
131 " -n, -- disable host cache, short for -t none\n"
132 " -U, -- force shared permissions\n"
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"
136 " -o, -- options to be given to the block driver"
137 "\n");
140 static int open_f(BlockBackend *blk, int argc, char **argv);
142 static const cmdinfo_t open_cmd = {
143 .name = "open",
144 .altname = "o",
145 .cfunc = open_f,
146 .argmin = 1,
147 .argmax = -1,
148 .flags = CMD_NOFILE_OK,
149 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
150 .oneline = "open the file specified by path",
151 .help = open_help,
154 static QemuOptsList empty_opts = {
155 .name = "drive",
156 .merge_lists = true,
157 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
158 .desc = {
159 /* no elements => accept any params */
160 { /* end of list */ }
164 static int open_f(BlockBackend *blk, int argc, char **argv)
166 int flags = BDRV_O_UNMAP;
167 int readonly = 0;
168 bool writethrough = true;
169 int c;
170 int ret;
171 QemuOpts *qopts;
172 QDict *opts;
173 bool force_share = false;
175 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
176 switch (c) {
177 case 's':
178 flags |= BDRV_O_SNAPSHOT;
179 break;
180 case 'n':
181 flags |= BDRV_O_NOCACHE;
182 writethrough = false;
183 break;
184 case 'C':
185 flags |= BDRV_O_COPY_ON_READ;
186 break;
187 case 'r':
188 readonly = 1;
189 break;
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);
197 return -EINVAL;
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);
204 return -EINVAL;
206 break;
207 case 'o':
208 if (imageOpts) {
209 printf("--image-opts and 'open -o' are mutually exclusive\n");
210 qemu_opts_reset(&empty_opts);
211 return -EINVAL;
213 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
214 qemu_opts_reset(&empty_opts);
215 return -EINVAL;
217 break;
218 case 'U':
219 force_share = true;
220 break;
221 default:
222 qemu_opts_reset(&empty_opts);
223 qemuio_command_usage(&open_cmd);
224 return -EINVAL;
228 if (!readonly) {
229 flags |= BDRV_O_RDWR;
232 if (imageOpts && (optind == argc - 1)) {
233 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
234 qemu_opts_reset(&empty_opts);
235 return -EINVAL;
237 optind++;
240 qopts = qemu_opts_find(&empty_opts, NULL);
241 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
242 qemu_opts_reset(&empty_opts);
244 if (optind == argc - 1) {
245 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
246 } else if (optind == argc) {
247 ret = openfile(NULL, flags, writethrough, force_share, opts);
248 } else {
249 qobject_unref(opts);
250 qemuio_command_usage(&open_cmd);
251 return -EINVAL;
254 if (ret) {
255 return -EINVAL;
258 return 0;
261 static int quit_f(BlockBackend *blk, int argc, char **argv)
263 quit_qemu_io = true;
264 return 0;
267 #if defined(CONFIG_FVD)
268 # include "qemu-io-sim.c"
269 #endif
271 static const cmdinfo_t quit_cmd = {
272 .name = "quit",
273 .altname = "q",
274 .cfunc = quit_f,
275 .argmin = -1,
276 .argmax = -1,
277 .flags = CMD_FLAG_GLOBAL,
278 .oneline = "exit the program",
281 static void usage(const char *name)
283 printf(
284 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
285 "QEMU Disk exerciser\n"
286 "\n"
287 " --object OBJECTDEF define an object such as 'secret' for\n"
288 " passwords and/or encryption keys\n"
289 " --image-opts treat file as option string\n"
290 " -c, --cmd STRING execute command with its arguments\n"
291 " from the given string\n"
292 " -f, --format FMT specifies the block driver to use\n"
293 " -r, --read-only export read-only\n"
294 " -s, --snapshot use snapshot file\n"
295 " -n, --nocache disable host cache, short for -t none\n"
296 " -C, --copy-on-read enable copy-on-read\n"
297 " -m, --misalign misalign allocations for O_DIRECT\n"
298 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
299 " -t, --cache=MODE use the given cache mode for the image\n"
300 " -d, --discard=MODE use the given discard mode for the image\n"
301 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
302 " specify tracing options\n"
303 " see qemu-img(1) man page for full description\n"
304 " -U, --force-share force shared permissions\n"
305 " -h, --help display this help and exit\n"
306 " -V, --version output version information and exit\n"
307 "\n"
308 "See '%s -c help' for information on available commands.\n"
309 "\n"
310 QEMU_HELP_BOTTOM "\n",
311 name, name);
314 static char *get_prompt(void)
316 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
318 if (!prompt[0]) {
319 snprintf(prompt, sizeof(prompt), "%s> ", progname);
322 return prompt;
325 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
326 const char *fmt, ...)
328 va_list ap;
329 va_start(ap, fmt);
330 vprintf(fmt, ap);
331 va_end(ap);
334 static void readline_flush_func(void *opaque)
336 fflush(stdout);
339 static void readline_func(void *opaque, const char *str, void *readline_opaque)
341 char **line = readline_opaque;
342 *line = g_strdup(str);
345 static void completion_match(const char *cmd, void *opaque)
347 readline_add_completion(readline_state, cmd);
350 static void readline_completion_func(void *opaque, const char *str)
352 readline_set_completion_index(readline_state, strlen(str));
353 qemuio_complete_command(str, completion_match, NULL);
356 static char *fetchline_readline(void)
358 char *line = NULL;
360 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
361 while (!line) {
362 int ch = getchar();
363 if (ttyEOF != 0x0 && ch == ttyEOF) {
364 printf("\n");
365 break;
367 readline_handle_byte(readline_state, ch);
369 return line;
372 #define MAXREADLINESZ 1024
373 static char *fetchline_fgets(void)
375 char *p, *line = g_malloc(MAXREADLINESZ);
377 if (!fgets(line, MAXREADLINESZ, stdin)) {
378 g_free(line);
379 return NULL;
382 p = line + strlen(line);
383 if (p != line && p[-1] == '\n') {
384 p[-1] = '\0';
387 return line;
390 static char *fetchline(void)
392 if (readline_state) {
393 return fetchline_readline();
394 } else {
395 return fetchline_fgets();
399 static void prep_fetchline(void *opaque)
401 int *fetchable = opaque;
403 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
404 *fetchable= 1;
407 static int command_loop(void)
409 int i, fetchable = 0, prompted = 0;
410 int ret, last_error = 0;
411 char *input;
413 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
414 ret = qemuio_command(qemuio_blk, cmdline[i]);
415 if (ret < 0) {
416 last_error = ret;
419 if (cmdline) {
420 g_free(cmdline);
421 return last_error;
424 while (!quit_qemu_io) {
425 if (!prompted) {
426 printf("%s", get_prompt());
427 fflush(stdout);
428 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
429 prompted = 1;
432 main_loop_wait(false);
434 if (!fetchable) {
435 continue;
438 input = fetchline();
439 if (input == NULL) {
440 break;
442 ret = qemuio_command(qemuio_blk, input);
443 g_free(input);
445 if (ret < 0) {
446 last_error = ret;
449 prompted = 0;
450 fetchable = 0;
452 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
454 return last_error;
457 static void add_user_command(char *optarg)
459 cmdline = g_renew(char *, cmdline, ++ncmdline);
460 cmdline[ncmdline-1] = optarg;
463 static void reenable_tty_echo(void)
465 qemu_set_tty_echo(STDIN_FILENO, true);
468 enum {
469 OPTION_OBJECT = 256,
470 OPTION_IMAGE_OPTS = 257,
473 static QemuOptsList qemu_object_opts = {
474 .name = "object",
475 .implied_opt_name = "qom-type",
476 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
477 .desc = {
483 static QemuOptsList file_opts = {
484 .name = "file",
485 .implied_opt_name = "file",
486 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
487 .desc = {
488 /* no elements => accept any params */
489 { /* end of list */ }
493 int main(int argc, char **argv)
495 int readonly = 0;
496 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
497 const struct option lopt[] = {
498 { "help", no_argument, NULL, 'h' },
499 { "version", no_argument, NULL, 'V' },
500 { "cmd", required_argument, NULL, 'c' },
501 { "format", required_argument, NULL, 'f' },
502 { "read-only", no_argument, NULL, 'r' },
503 { "snapshot", no_argument, NULL, 's' },
504 { "nocache", no_argument, NULL, 'n' },
505 { "copy-on-read", no_argument, NULL, 'C' },
506 { "misalign", no_argument, NULL, 'm' },
507 { "native-aio", no_argument, NULL, 'k' },
508 { "discard", required_argument, NULL, 'd' },
509 { "cache", required_argument, NULL, 't' },
510 { "trace", required_argument, NULL, 'T' },
511 { "object", required_argument, NULL, OPTION_OBJECT },
512 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
513 { "force-share", no_argument, 0, 'U'},
514 { NULL, 0, NULL, 0 }
516 int c;
517 int opt_index = 0;
518 int flags = BDRV_O_UNMAP;
519 int ret;
520 bool writethrough = true;
521 Error *local_error = NULL;
522 QDict *opts = NULL;
523 const char *format = NULL;
524 char *trace_file = NULL;
525 bool force_share = false;
527 #ifdef CONFIG_POSIX
528 signal(SIGPIPE, SIG_IGN);
529 #endif
531 module_call_init(MODULE_INIT_TRACE);
532 progname = g_path_get_basename(argv[0]);
533 qemu_init_exec_dir(argv[0]);
535 qcrypto_init(&error_fatal);
537 module_call_init(MODULE_INIT_QOM);
538 qemu_add_opts(&qemu_object_opts);
539 qemu_add_opts(&qemu_trace_opts);
540 bdrv_init();
542 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
543 switch (c) {
544 case 's':
545 flags |= BDRV_O_SNAPSHOT;
546 break;
547 case 'n':
548 flags |= BDRV_O_NOCACHE;
549 writethrough = false;
550 break;
551 case 'C':
552 flags |= BDRV_O_COPY_ON_READ;
553 break;
554 case 'd':
555 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
556 error_report("Invalid discard option: %s", optarg);
557 exit(1);
559 break;
560 case 'f':
561 format = optarg;
562 break;
563 case 'c':
564 add_user_command(optarg);
565 break;
566 case 'r':
567 readonly = 1;
568 break;
569 case 'm':
570 qemuio_misalign = true;
571 break;
572 case 'k':
573 flags |= BDRV_O_NATIVE_AIO;
574 break;
575 case 't':
576 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
577 error_report("Invalid cache option: %s", optarg);
578 exit(1);
580 break;
581 case 'T':
582 g_free(trace_file);
583 trace_file = trace_opt_parse(optarg);
584 break;
585 case 'V':
586 printf("%s version " QEMU_FULL_VERSION "\n"
587 QEMU_COPYRIGHT "\n", progname);
588 exit(0);
589 case 'h':
590 usage(progname);
591 exit(0);
592 case 'U':
593 force_share = true;
594 break;
595 case OPTION_OBJECT: {
596 QemuOpts *qopts;
597 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
598 optarg, true);
599 if (!qopts) {
600 exit(1);
602 } break;
603 case OPTION_IMAGE_OPTS:
604 imageOpts = true;
605 break;
606 default:
607 usage(progname);
608 exit(1);
612 if ((argc - optind) > 1) {
613 usage(progname);
614 exit(1);
617 if (format && imageOpts) {
618 error_report("--image-opts and -f are mutually exclusive");
619 exit(1);
622 if (qemu_init_main_loop(&local_error)) {
623 error_report_err(local_error);
624 exit(1);
627 qemu_opts_foreach(&qemu_object_opts,
628 user_creatable_add_opts_foreach,
629 NULL, &error_fatal);
631 if (!trace_init_backends()) {
632 exit(1);
634 trace_init_file(trace_file);
635 qemu_set_log(LOG_TRACE);
637 /* initialize commands */
638 qemuio_add_command(&quit_cmd);
639 qemuio_add_command(&open_cmd);
640 qemuio_add_command(&close_cmd);
642 if (isatty(STDIN_FILENO)) {
643 ttyEOF = get_eof_char();
644 readline_state = readline_init(readline_printf_func,
645 readline_flush_func,
646 NULL,
647 readline_completion_func);
648 qemu_set_tty_echo(STDIN_FILENO, false);
649 atexit(reenable_tty_echo);
652 /* open the device */
653 if (!readonly) {
654 flags |= BDRV_O_RDWR;
657 if ((argc - optind) == 1) {
658 if (imageOpts) {
659 QemuOpts *qopts = NULL;
660 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
661 if (!qopts) {
662 exit(1);
664 opts = qemu_opts_to_qdict(qopts, NULL);
665 if (openfile(NULL, flags, writethrough, force_share, opts)) {
666 exit(1);
668 } else {
669 if (format) {
670 opts = qdict_new();
671 qdict_put_str(opts, "driver", format);
673 if (openfile(argv[optind], flags, writethrough,
674 force_share, opts)) {
675 exit(1);
679 ret = command_loop();
682 * Make sure all outstanding requests complete before the program exits.
684 bdrv_drain_all();
686 blk_unref(qemuio_blk);
687 g_free(readline_state);
689 if (ret < 0) {
690 return 1;
691 } else {
692 return 0;