Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blob7d359fc94c359afeef5577019a564297feebd792
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;
41 /* qemu-io commands passed using -c */
42 static int ncmdline;
43 static char **cmdline;
44 static bool imageOpts;
46 static ReadLineState *readline_state;
48 static int ttyEOF;
50 static int get_eof_char(void)
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 */
64 return tty.c_cc[VEOF];
65 #endif
68 static int close_f(BlockBackend *blk, int argc, char **argv)
70 blk_unref(qemuio_blk);
71 qemuio_blk = NULL;
72 return 0;
75 static const cmdinfo_t close_cmd = {
76 .name = "close",
77 .altname = "c",
78 .cfunc = close_f,
79 .oneline = "close the current open file",
82 static int openfile(char *name, int flags, bool writethrough, bool force_share,
83 QDict *opts)
85 Error *local_err = NULL;
87 if (qemuio_blk) {
88 error_report("file open already, try 'help close'");
89 QDECREF(opts);
90 return 1;
93 if (force_share) {
94 if (!opts) {
95 opts = qdict_new();
97 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
98 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
99 error_report("-U conflicts with image options");
100 QDECREF(opts);
101 return 1;
103 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
105 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
106 if (!qemuio_blk) {
107 error_reportf_err(local_err, "can't open%s%s: ",
108 name ? " device " : "", name ?: "");
109 return 1;
112 blk_set_enable_write_cache(qemuio_blk, !writethrough);
114 return 0;
117 static void open_help(void)
119 printf(
120 "\n"
121 " opens a new file in the requested mode\n"
122 "\n"
123 " Example:\n"
124 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
125 "\n"
126 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
127 " -r, -- open file read-only\n"
128 " -s, -- use snapshot file\n"
129 " -C, -- use copy-on-read\n"
130 " -n, -- disable host cache, short for -t none\n"
131 " -U, -- force shared permissions\n"
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"
135 " -o, -- options to be given to the block driver"
136 "\n");
139 static int open_f(BlockBackend *blk, int argc, char **argv);
141 static const cmdinfo_t open_cmd = {
142 .name = "open",
143 .altname = "o",
144 .cfunc = open_f,
145 .argmin = 1,
146 .argmax = -1,
147 .flags = CMD_NOFILE_OK,
148 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
149 .oneline = "open the file specified by path",
150 .help = open_help,
153 static QemuOptsList empty_opts = {
154 .name = "drive",
155 .merge_lists = true,
156 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
157 .desc = {
158 /* no elements => accept any params */
159 { /* end of list */ }
163 static int open_f(BlockBackend *blk, int argc, char **argv)
165 int flags = BDRV_O_UNMAP;
166 int readonly = 0;
167 bool writethrough = true;
168 int c;
169 QemuOpts *qopts;
170 QDict *opts;
171 bool force_share = false;
173 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
174 switch (c) {
175 case 's':
176 flags |= BDRV_O_SNAPSHOT;
177 break;
178 case 'n':
179 flags |= BDRV_O_NOCACHE;
180 writethrough = false;
181 break;
182 case 'C':
183 flags |= BDRV_O_COPY_ON_READ;
184 break;
185 case 'r':
186 readonly = 1;
187 break;
188 case 'k':
189 flags |= BDRV_O_NATIVE_AIO;
190 break;
191 case 't':
192 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
193 error_report("Invalid cache option: %s", optarg);
194 qemu_opts_reset(&empty_opts);
195 return 0;
197 break;
198 case 'd':
199 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
200 error_report("Invalid discard option: %s", optarg);
201 qemu_opts_reset(&empty_opts);
202 return 0;
204 break;
205 case 'o':
206 if (imageOpts) {
207 printf("--image-opts and 'open -o' are mutually exclusive\n");
208 qemu_opts_reset(&empty_opts);
209 return 0;
211 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
212 qemu_opts_reset(&empty_opts);
213 return 0;
215 break;
216 case 'U':
217 force_share = true;
218 break;
219 default:
220 qemu_opts_reset(&empty_opts);
221 return qemuio_command_usage(&open_cmd);
225 if (!readonly) {
226 flags |= BDRV_O_RDWR;
229 if (imageOpts && (optind == argc - 1)) {
230 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
231 qemu_opts_reset(&empty_opts);
232 return 0;
234 optind++;
237 qopts = qemu_opts_find(&empty_opts, NULL);
238 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
239 qemu_opts_reset(&empty_opts);
241 if (optind == argc - 1) {
242 openfile(argv[optind], flags, writethrough, force_share, opts);
243 } else if (optind == argc) {
244 openfile(NULL, flags, writethrough, force_share, opts);
245 } else {
246 QDECREF(opts);
247 qemuio_command_usage(&open_cmd);
249 return 0;
252 static int quit_f(BlockBackend *blk, int argc, char **argv)
254 return 1;
257 #if defined(CONFIG_FVD)
258 # include "qemu-io-sim.c"
259 #endif
261 static const cmdinfo_t quit_cmd = {
262 .name = "quit",
263 .altname = "q",
264 .cfunc = quit_f,
265 .argmin = -1,
266 .argmax = -1,
267 .flags = CMD_FLAG_GLOBAL,
268 .oneline = "exit the program",
271 static void usage(const char *name)
273 printf(
274 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
275 "QEMU Disk exerciser\n"
276 "\n"
277 " --object OBJECTDEF define an object such as 'secret' for\n"
278 " passwords and/or encryption keys\n"
279 " --image-opts treat file as option string\n"
280 " -c, --cmd STRING execute command with its arguments\n"
281 " from the given string\n"
282 " -f, --format FMT specifies the block driver to use\n"
283 " -r, --read-only export read-only\n"
284 " -s, --snapshot use snapshot file\n"
285 " -n, --nocache disable host cache, short for -t none\n"
286 " -C, --copy-on-read enable copy-on-read\n"
287 " -m, --misalign misalign allocations for O_DIRECT\n"
288 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
289 " -t, --cache=MODE use the given cache mode for the image\n"
290 " -d, --discard=MODE use the given discard mode for the image\n"
291 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
292 " specify tracing options\n"
293 " see qemu-img(1) man page for full description\n"
294 " -U, --force-share force shared permissions\n"
295 " -h, --help display this help and exit\n"
296 " -V, --version output version information and exit\n"
297 "\n"
298 "See '%s -c help' for information on available commands.\n"
299 "\n"
300 QEMU_HELP_BOTTOM "\n",
301 name, name);
304 static char *get_prompt(void)
306 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
308 if (!prompt[0]) {
309 snprintf(prompt, sizeof(prompt), "%s> ", progname);
312 return prompt;
315 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
316 const char *fmt, ...)
318 va_list ap;
319 va_start(ap, fmt);
320 vprintf(fmt, ap);
321 va_end(ap);
324 static void readline_flush_func(void *opaque)
326 fflush(stdout);
329 static void readline_func(void *opaque, const char *str, void *readline_opaque)
331 char **line = readline_opaque;
332 *line = g_strdup(str);
335 static void completion_match(const char *cmd, void *opaque)
337 readline_add_completion(readline_state, cmd);
340 static void readline_completion_func(void *opaque, const char *str)
342 readline_set_completion_index(readline_state, strlen(str));
343 qemuio_complete_command(str, completion_match, NULL);
346 static char *fetchline_readline(void)
348 char *line = NULL;
350 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
351 while (!line) {
352 int ch = getchar();
353 if (ttyEOF != 0x0 && ch == ttyEOF) {
354 printf("\n");
355 break;
357 readline_handle_byte(readline_state, ch);
359 return line;
362 #define MAXREADLINESZ 1024
363 static char *fetchline_fgets(void)
365 char *p, *line = g_malloc(MAXREADLINESZ);
367 if (!fgets(line, MAXREADLINESZ, stdin)) {
368 g_free(line);
369 return NULL;
372 p = line + strlen(line);
373 if (p != line && p[-1] == '\n') {
374 p[-1] = '\0';
377 return line;
380 static char *fetchline(void)
382 if (readline_state) {
383 return fetchline_readline();
384 } else {
385 return fetchline_fgets();
389 static void prep_fetchline(void *opaque)
391 int *fetchable = opaque;
393 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
394 *fetchable= 1;
397 static void command_loop(void)
399 int i, done = 0, fetchable = 0, prompted = 0;
400 char *input;
402 for (i = 0; !done && i < ncmdline; i++) {
403 done = qemuio_command(qemuio_blk, cmdline[i]);
405 if (cmdline) {
406 g_free(cmdline);
407 return;
410 while (!done) {
411 if (!prompted) {
412 printf("%s", get_prompt());
413 fflush(stdout);
414 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
415 prompted = 1;
418 main_loop_wait(false);
420 if (!fetchable) {
421 continue;
424 input = fetchline();
425 if (input == NULL) {
426 break;
428 done = qemuio_command(qemuio_blk, input);
429 g_free(input);
431 prompted = 0;
432 fetchable = 0;
434 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
437 static void add_user_command(char *optarg)
439 cmdline = g_renew(char *, cmdline, ++ncmdline);
440 cmdline[ncmdline-1] = optarg;
443 static void reenable_tty_echo(void)
445 qemu_set_tty_echo(STDIN_FILENO, true);
448 enum {
449 OPTION_OBJECT = 256,
450 OPTION_IMAGE_OPTS = 257,
453 static QemuOptsList qemu_object_opts = {
454 .name = "object",
455 .implied_opt_name = "qom-type",
456 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
457 .desc = {
463 static QemuOptsList file_opts = {
464 .name = "file",
465 .implied_opt_name = "file",
466 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
467 .desc = {
468 /* no elements => accept any params */
469 { /* end of list */ }
473 int main(int argc, char **argv)
475 int readonly = 0;
476 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
477 const struct option lopt[] = {
478 { "help", no_argument, NULL, 'h' },
479 { "version", no_argument, NULL, 'V' },
480 { "cmd", required_argument, NULL, 'c' },
481 { "format", required_argument, NULL, 'f' },
482 { "read-only", no_argument, NULL, 'r' },
483 { "snapshot", no_argument, NULL, 's' },
484 { "nocache", no_argument, NULL, 'n' },
485 { "copy-on-read", no_argument, NULL, 'C' },
486 { "misalign", no_argument, NULL, 'm' },
487 { "native-aio", no_argument, NULL, 'k' },
488 { "discard", required_argument, NULL, 'd' },
489 { "cache", required_argument, NULL, 't' },
490 { "trace", required_argument, NULL, 'T' },
491 { "object", required_argument, NULL, OPTION_OBJECT },
492 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
493 { "force-share", no_argument, 0, 'U'},
494 { NULL, 0, NULL, 0 }
496 int c;
497 int opt_index = 0;
498 int flags = BDRV_O_UNMAP;
499 bool writethrough = true;
500 Error *local_error = NULL;
501 QDict *opts = NULL;
502 const char *format = NULL;
503 char *trace_file = NULL;
504 bool force_share = false;
506 #ifdef CONFIG_POSIX
507 signal(SIGPIPE, SIG_IGN);
508 #endif
510 module_call_init(MODULE_INIT_TRACE);
511 progname = g_path_get_basename(argv[0]);
512 qemu_init_exec_dir(argv[0]);
514 qcrypto_init(&error_fatal);
516 module_call_init(MODULE_INIT_QOM);
517 qemu_add_opts(&qemu_object_opts);
518 qemu_add_opts(&qemu_trace_opts);
519 bdrv_init();
521 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
522 switch (c) {
523 case 's':
524 flags |= BDRV_O_SNAPSHOT;
525 break;
526 case 'n':
527 flags |= BDRV_O_NOCACHE;
528 writethrough = false;
529 break;
530 case 'C':
531 flags |= BDRV_O_COPY_ON_READ;
532 break;
533 case 'd':
534 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
535 error_report("Invalid discard option: %s", optarg);
536 exit(1);
538 break;
539 case 'f':
540 format = optarg;
541 break;
542 case 'c':
543 add_user_command(optarg);
544 break;
545 case 'r':
546 readonly = 1;
547 break;
548 case 'm':
549 qemuio_misalign = true;
550 break;
551 case 'k':
552 flags |= BDRV_O_NATIVE_AIO;
553 break;
554 case 't':
555 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
556 error_report("Invalid cache option: %s", optarg);
557 exit(1);
559 break;
560 case 'T':
561 g_free(trace_file);
562 trace_file = trace_opt_parse(optarg);
563 break;
564 case 'V':
565 printf("%s version " QEMU_FULL_VERSION "\n"
566 QEMU_COPYRIGHT "\n", progname);
567 exit(0);
568 case 'h':
569 usage(progname);
570 exit(0);
571 case 'U':
572 force_share = true;
573 break;
574 case OPTION_OBJECT: {
575 QemuOpts *qopts;
576 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
577 optarg, true);
578 if (!qopts) {
579 exit(1);
581 } break;
582 case OPTION_IMAGE_OPTS:
583 imageOpts = true;
584 break;
585 default:
586 usage(progname);
587 exit(1);
591 if ((argc - optind) > 1) {
592 usage(progname);
593 exit(1);
596 if (format && imageOpts) {
597 error_report("--image-opts and -f are mutually exclusive");
598 exit(1);
601 if (qemu_init_main_loop(&local_error)) {
602 error_report_err(local_error);
603 exit(1);
606 if (qemu_opts_foreach(&qemu_object_opts,
607 user_creatable_add_opts_foreach,
608 NULL, NULL)) {
609 exit(1);
612 if (!trace_init_backends()) {
613 exit(1);
615 trace_init_file(trace_file);
616 qemu_set_log(LOG_TRACE);
618 /* initialize commands */
619 qemuio_add_command(&quit_cmd);
620 qemuio_add_command(&open_cmd);
621 qemuio_add_command(&close_cmd);
623 if (isatty(STDIN_FILENO)) {
624 ttyEOF = get_eof_char();
625 readline_state = readline_init(readline_printf_func,
626 readline_flush_func,
627 NULL,
628 readline_completion_func);
629 qemu_set_tty_echo(STDIN_FILENO, false);
630 atexit(reenable_tty_echo);
633 /* open the device */
634 if (!readonly) {
635 flags |= BDRV_O_RDWR;
638 if ((argc - optind) == 1) {
639 if (imageOpts) {
640 QemuOpts *qopts = NULL;
641 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
642 if (!qopts) {
643 exit(1);
645 opts = qemu_opts_to_qdict(qopts, NULL);
646 if (openfile(NULL, flags, writethrough, force_share, opts)) {
647 exit(1);
649 } else {
650 if (format) {
651 opts = qdict_new();
652 qdict_put_str(opts, "driver", format);
654 if (openfile(argv[optind], flags, writethrough,
655 force_share, opts)) {
656 exit(1);
660 command_loop();
663 * Make sure all outstanding requests complete before the program exits.
665 bdrv_drain_all();
667 blk_unref(qemuio_blk);
668 g_free(readline_state);
669 return 0;