Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
bloba8a24e85c5359cc25f00d03321a797065e67b491
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>
15 #include "qapi/error.h"
16 #include "qemu-io.h"
17 #include "qemu/error-report.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/option.h"
20 #include "qemu/config-file.h"
21 #include "qemu/readline.h"
22 #include "qemu/log.h"
23 #include "qapi/qmp/qstring.h"
24 #include "qapi/qmp/qdict.h"
25 #include "qom/object_interfaces.h"
26 #include "sysemu/block-backend.h"
27 #include "block/block_int.h"
28 #include "trace/control.h"
29 #include "crypto/init.h"
30 #include "qemu-version.h"
32 #define CMD_NOFILE_OK 0x01
34 static char *progname;
36 static BlockBackend *qemuio_blk;
38 /* qemu-io commands passed using -c */
39 static int ncmdline;
40 static char **cmdline;
41 static bool imageOpts;
43 static ReadLineState *readline_state;
45 static int close_f(BlockBackend *blk, int argc, char **argv)
47 blk_unref(qemuio_blk);
48 qemuio_blk = NULL;
49 return 0;
52 static const cmdinfo_t close_cmd = {
53 .name = "close",
54 .altname = "c",
55 .cfunc = close_f,
56 .oneline = "close the current open file",
59 static int openfile(char *name, int flags, bool writethrough, bool force_share,
60 QDict *opts)
62 Error *local_err = NULL;
64 if (qemuio_blk) {
65 error_report("file open already, try 'help close'");
66 QDECREF(opts);
67 return 1;
70 if (force_share) {
71 if (!opts) {
72 opts = qdict_new();
74 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
75 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
76 error_report("-U conflicts with image options");
77 QDECREF(opts);
78 return 1;
80 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
82 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
83 if (!qemuio_blk) {
84 error_reportf_err(local_err, "can't open%s%s: ",
85 name ? " device " : "", name ?: "");
86 return 1;
89 blk_set_enable_write_cache(qemuio_blk, !writethrough);
91 return 0;
94 static void open_help(void)
96 printf(
97 "\n"
98 " opens a new file in the requested mode\n"
99 "\n"
100 " Example:\n"
101 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
102 "\n"
103 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
104 " -r, -- open file read-only\n"
105 " -s, -- use snapshot file\n"
106 " -C, -- use copy-on-read\n"
107 " -n, -- disable host cache, short for -t none\n"
108 " -U, -- force shared permissions\n"
109 " -k, -- use kernel AIO implementation (on Linux only)\n"
110 " -t, -- use the given cache mode for the image\n"
111 " -d, -- use the given discard mode for the image\n"
112 " -o, -- options to be given to the block driver"
113 "\n");
116 static int open_f(BlockBackend *blk, int argc, char **argv);
118 static const cmdinfo_t open_cmd = {
119 .name = "open",
120 .altname = "o",
121 .cfunc = open_f,
122 .argmin = 1,
123 .argmax = -1,
124 .flags = CMD_NOFILE_OK,
125 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
126 .oneline = "open the file specified by path",
127 .help = open_help,
130 static QemuOptsList empty_opts = {
131 .name = "drive",
132 .merge_lists = true,
133 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
134 .desc = {
135 /* no elements => accept any params */
136 { /* end of list */ }
140 static int open_f(BlockBackend *blk, int argc, char **argv)
142 int flags = BDRV_O_UNMAP;
143 int readonly = 0;
144 bool writethrough = true;
145 int c;
146 QemuOpts *qopts;
147 QDict *opts;
148 bool force_share = false;
150 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
151 switch (c) {
152 case 's':
153 flags |= BDRV_O_SNAPSHOT;
154 break;
155 case 'n':
156 flags |= BDRV_O_NOCACHE;
157 writethrough = false;
158 break;
159 case 'C':
160 flags |= BDRV_O_COPY_ON_READ;
161 break;
162 case 'r':
163 readonly = 1;
164 break;
165 case 'k':
166 flags |= BDRV_O_NATIVE_AIO;
167 break;
168 case 't':
169 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
170 error_report("Invalid cache option: %s", optarg);
171 qemu_opts_reset(&empty_opts);
172 return 0;
174 break;
175 case 'd':
176 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
177 error_report("Invalid discard option: %s", optarg);
178 qemu_opts_reset(&empty_opts);
179 return 0;
181 break;
182 case 'o':
183 if (imageOpts) {
184 printf("--image-opts and 'open -o' are mutually exclusive\n");
185 qemu_opts_reset(&empty_opts);
186 return 0;
188 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
189 qemu_opts_reset(&empty_opts);
190 return 0;
192 break;
193 case 'U':
194 force_share = true;
195 break;
196 default:
197 qemu_opts_reset(&empty_opts);
198 return qemuio_command_usage(&open_cmd);
202 if (!readonly) {
203 flags |= BDRV_O_RDWR;
206 if (imageOpts && (optind == argc - 1)) {
207 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
208 qemu_opts_reset(&empty_opts);
209 return 0;
211 optind++;
214 qopts = qemu_opts_find(&empty_opts, NULL);
215 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
216 qemu_opts_reset(&empty_opts);
218 if (optind == argc - 1) {
219 openfile(argv[optind], flags, writethrough, force_share, opts);
220 } else if (optind == argc) {
221 openfile(NULL, flags, writethrough, force_share, opts);
222 } else {
223 QDECREF(opts);
224 qemuio_command_usage(&open_cmd);
226 return 0;
229 static int quit_f(BlockBackend *blk, int argc, char **argv)
231 return 1;
234 #if defined(CONFIG_FVD)
235 # include "qemu-io-sim.c"
236 #endif
238 static const cmdinfo_t quit_cmd = {
239 .name = "quit",
240 .altname = "q",
241 .cfunc = quit_f,
242 .argmin = -1,
243 .argmax = -1,
244 .flags = CMD_FLAG_GLOBAL,
245 .oneline = "exit the program",
248 static void usage(const char *name)
250 printf(
251 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
252 "QEMU Disk exerciser\n"
253 "\n"
254 " --object OBJECTDEF define an object such as 'secret' for\n"
255 " passwords and/or encryption keys\n"
256 " --image-opts treat file as option string\n"
257 " -c, --cmd STRING execute command with its arguments\n"
258 " from the given string\n"
259 " -f, --format FMT specifies the block driver to use\n"
260 " -r, --read-only export read-only\n"
261 " -s, --snapshot use snapshot file\n"
262 " -n, --nocache disable host cache, short for -t none\n"
263 " -C, --copy-on-read enable copy-on-read\n"
264 " -m, --misalign misalign allocations for O_DIRECT\n"
265 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
266 " -t, --cache=MODE use the given cache mode for the image\n"
267 " -d, --discard=MODE use the given discard mode for the image\n"
268 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
269 " specify tracing options\n"
270 " see qemu-img(1) man page for full description\n"
271 " -U, --force-share force shared permissions\n"
272 " -h, --help display this help and exit\n"
273 " -V, --version output version information and exit\n"
274 "\n"
275 "See '%s -c help' for information on available commands.\n"
276 "\n"
277 QEMU_HELP_BOTTOM "\n",
278 name, name);
281 static char *get_prompt(void)
283 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
285 if (!prompt[0]) {
286 snprintf(prompt, sizeof(prompt), "%s> ", progname);
289 return prompt;
292 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
293 const char *fmt, ...)
295 va_list ap;
296 va_start(ap, fmt);
297 vprintf(fmt, ap);
298 va_end(ap);
301 static void readline_flush_func(void *opaque)
303 fflush(stdout);
306 static void readline_func(void *opaque, const char *str, void *readline_opaque)
308 char **line = readline_opaque;
309 *line = g_strdup(str);
312 static void completion_match(const char *cmd, void *opaque)
314 readline_add_completion(readline_state, cmd);
317 static void readline_completion_func(void *opaque, const char *str)
319 readline_set_completion_index(readline_state, strlen(str));
320 qemuio_complete_command(str, completion_match, NULL);
323 static char *fetchline_readline(void)
325 char *line = NULL;
327 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
328 while (!line) {
329 int ch = getchar();
330 if (ch == EOF) {
331 break;
333 readline_handle_byte(readline_state, ch);
335 return line;
338 #define MAXREADLINESZ 1024
339 static char *fetchline_fgets(void)
341 char *p, *line = g_malloc(MAXREADLINESZ);
343 if (!fgets(line, MAXREADLINESZ, stdin)) {
344 g_free(line);
345 return NULL;
348 p = line + strlen(line);
349 if (p != line && p[-1] == '\n') {
350 p[-1] = '\0';
353 return line;
356 static char *fetchline(void)
358 if (readline_state) {
359 return fetchline_readline();
360 } else {
361 return fetchline_fgets();
365 static void prep_fetchline(void *opaque)
367 int *fetchable = opaque;
369 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
370 *fetchable= 1;
373 static void command_loop(void)
375 int i, done = 0, fetchable = 0, prompted = 0;
376 char *input;
378 for (i = 0; !done && i < ncmdline; i++) {
379 done = qemuio_command(qemuio_blk, cmdline[i]);
381 if (cmdline) {
382 g_free(cmdline);
383 return;
386 while (!done) {
387 if (!prompted) {
388 printf("%s", get_prompt());
389 fflush(stdout);
390 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
391 prompted = 1;
394 main_loop_wait(false);
396 if (!fetchable) {
397 continue;
400 input = fetchline();
401 if (input == NULL) {
402 break;
404 done = qemuio_command(qemuio_blk, input);
405 g_free(input);
407 prompted = 0;
408 fetchable = 0;
410 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
413 static void add_user_command(char *optarg)
415 cmdline = g_renew(char *, cmdline, ++ncmdline);
416 cmdline[ncmdline-1] = optarg;
419 static void reenable_tty_echo(void)
421 qemu_set_tty_echo(STDIN_FILENO, true);
424 enum {
425 OPTION_OBJECT = 256,
426 OPTION_IMAGE_OPTS = 257,
429 static QemuOptsList qemu_object_opts = {
430 .name = "object",
431 .implied_opt_name = "qom-type",
432 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
433 .desc = {
439 static QemuOptsList file_opts = {
440 .name = "file",
441 .implied_opt_name = "file",
442 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
443 .desc = {
444 /* no elements => accept any params */
445 { /* end of list */ }
449 int main(int argc, char **argv)
451 int readonly = 0;
452 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
453 const struct option lopt[] = {
454 { "help", no_argument, NULL, 'h' },
455 { "version", no_argument, NULL, 'V' },
456 { "cmd", required_argument, NULL, 'c' },
457 { "format", required_argument, NULL, 'f' },
458 { "read-only", no_argument, NULL, 'r' },
459 { "snapshot", no_argument, NULL, 's' },
460 { "nocache", no_argument, NULL, 'n' },
461 { "copy-on-read", no_argument, NULL, 'C' },
462 { "misalign", no_argument, NULL, 'm' },
463 { "native-aio", no_argument, NULL, 'k' },
464 { "discard", required_argument, NULL, 'd' },
465 { "cache", required_argument, NULL, 't' },
466 { "trace", required_argument, NULL, 'T' },
467 { "object", required_argument, NULL, OPTION_OBJECT },
468 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
469 { "force-share", no_argument, 0, 'U'},
470 { NULL, 0, NULL, 0 }
472 int c;
473 int opt_index = 0;
474 int flags = BDRV_O_UNMAP;
475 bool writethrough = true;
476 Error *local_error = NULL;
477 QDict *opts = NULL;
478 const char *format = NULL;
479 char *trace_file = NULL;
480 bool force_share = false;
482 #ifdef CONFIG_POSIX
483 signal(SIGPIPE, SIG_IGN);
484 #endif
486 module_call_init(MODULE_INIT_TRACE);
487 progname = basename(argv[0]);
488 qemu_init_exec_dir(argv[0]);
490 qcrypto_init(&error_fatal);
492 module_call_init(MODULE_INIT_QOM);
493 qemu_add_opts(&qemu_object_opts);
494 qemu_add_opts(&qemu_trace_opts);
495 bdrv_init();
497 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
498 switch (c) {
499 case 's':
500 flags |= BDRV_O_SNAPSHOT;
501 break;
502 case 'n':
503 flags |= BDRV_O_NOCACHE;
504 writethrough = false;
505 break;
506 case 'C':
507 flags |= BDRV_O_COPY_ON_READ;
508 break;
509 case 'd':
510 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
511 error_report("Invalid discard option: %s", optarg);
512 exit(1);
514 break;
515 case 'f':
516 format = optarg;
517 break;
518 case 'c':
519 add_user_command(optarg);
520 break;
521 case 'r':
522 readonly = 1;
523 break;
524 case 'm':
525 qemuio_misalign = true;
526 break;
527 case 'k':
528 flags |= BDRV_O_NATIVE_AIO;
529 break;
530 case 't':
531 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
532 error_report("Invalid cache option: %s", optarg);
533 exit(1);
535 break;
536 case 'T':
537 g_free(trace_file);
538 trace_file = trace_opt_parse(optarg);
539 break;
540 case 'V':
541 printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n"
542 QEMU_COPYRIGHT "\n", progname);
543 exit(0);
544 case 'h':
545 usage(progname);
546 exit(0);
547 case 'U':
548 force_share = true;
549 break;
550 case OPTION_OBJECT: {
551 QemuOpts *qopts;
552 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
553 optarg, true);
554 if (!qopts) {
555 exit(1);
557 } break;
558 case OPTION_IMAGE_OPTS:
559 imageOpts = true;
560 break;
561 default:
562 usage(progname);
563 exit(1);
567 if ((argc - optind) > 1) {
568 usage(progname);
569 exit(1);
572 if (format && imageOpts) {
573 error_report("--image-opts and -f are mutually exclusive");
574 exit(1);
577 if (qemu_init_main_loop(&local_error)) {
578 error_report_err(local_error);
579 exit(1);
582 if (qemu_opts_foreach(&qemu_object_opts,
583 user_creatable_add_opts_foreach,
584 NULL, NULL)) {
585 exit(1);
588 if (!trace_init_backends()) {
589 exit(1);
591 trace_init_file(trace_file);
592 qemu_set_log(LOG_TRACE);
594 /* initialize commands */
595 qemuio_add_command(&quit_cmd);
596 qemuio_add_command(&open_cmd);
597 qemuio_add_command(&close_cmd);
599 if (isatty(STDIN_FILENO)) {
600 readline_state = readline_init(readline_printf_func,
601 readline_flush_func,
602 NULL,
603 readline_completion_func);
604 qemu_set_tty_echo(STDIN_FILENO, false);
605 atexit(reenable_tty_echo);
608 /* open the device */
609 if (!readonly) {
610 flags |= BDRV_O_RDWR;
613 if ((argc - optind) == 1) {
614 if (imageOpts) {
615 QemuOpts *qopts = NULL;
616 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
617 if (!qopts) {
618 exit(1);
620 opts = qemu_opts_to_qdict(qopts, NULL);
621 if (openfile(NULL, flags, writethrough, force_share, opts)) {
622 exit(1);
624 } else {
625 if (format) {
626 opts = qdict_new();
627 qdict_put_str(opts, "driver", format);
629 if (openfile(argv[optind], flags, writethrough,
630 force_share, opts)) {
631 exit(1);
635 command_loop();
638 * Make sure all outstanding requests complete before the program exits.
640 bdrv_drain_all();
642 blk_unref(qemuio_blk);
643 g_free(readline_state);
644 return 0;