Merge tag 'v2.10.0-rc0'
[qemu/ar7.git] / qemu-io.c
blob7c317e5f615f5e86d297049fa770b1fac2cc5211
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 */
10 #include "qemu/osdep.h"
11 #include <getopt.h>
12 #include <libgen.h>
14 #include "qapi/error.h"
15 #include "qemu-io.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/option.h"
19 #include "qemu/config-file.h"
20 #include "qemu/readline.h"
21 #include "qemu/log.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qapi/qmp/qbool.h"
24 #include "qom/object_interfaces.h"
25 #include "sysemu/block-backend.h"
26 #include "block/block_int.h"
27 #include "trace/control.h"
28 #include "crypto/init.h"
30 #define CMD_NOFILE_OK 0x01
32 static char *progname;
34 static BlockBackend *qemuio_blk;
36 /* qemu-io commands passed using -c */
37 static int ncmdline;
38 static char **cmdline;
39 static bool imageOpts;
41 static ReadLineState *readline_state;
43 static int close_f(BlockBackend *blk, int argc, char **argv)
45 blk_unref(qemuio_blk);
46 qemuio_blk = NULL;
47 return 0;
50 static const cmdinfo_t close_cmd = {
51 .name = "close",
52 .altname = "c",
53 .cfunc = close_f,
54 .oneline = "close the current open file",
57 static int openfile(char *name, int flags, bool writethrough, bool force_share,
58 QDict *opts)
60 Error *local_err = NULL;
62 if (qemuio_blk) {
63 error_report("file open already, try 'help close'");
64 QDECREF(opts);
65 return 1;
68 if (force_share) {
69 if (!opts) {
70 opts = qdict_new();
72 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
73 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
74 error_report("-U conflicts with image options");
75 QDECREF(opts);
76 return 1;
78 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
80 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
81 if (!qemuio_blk) {
82 error_reportf_err(local_err, "can't open%s%s: ",
83 name ? " device " : "", name ?: "");
84 return 1;
87 blk_set_enable_write_cache(qemuio_blk, !writethrough);
89 return 0;
92 static void open_help(void)
94 printf(
95 "\n"
96 " opens a new file in the requested mode\n"
97 "\n"
98 " Example:\n"
99 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
100 "\n"
101 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
102 " -r, -- open file read-only\n"
103 " -s, -- use snapshot file\n"
104 " -n, -- disable host cache, short for -t none\n"
105 " -U, -- force shared permissions\n"
106 " -k, -- use kernel AIO implementation (on Linux only)\n"
107 " -t, -- use the given cache mode for the image\n"
108 " -d, -- use the given discard mode for the image\n"
109 " -o, -- options to be given to the block driver"
110 "\n");
113 static int open_f(BlockBackend *blk, int argc, char **argv);
115 static const cmdinfo_t open_cmd = {
116 .name = "open",
117 .altname = "o",
118 .cfunc = open_f,
119 .argmin = 1,
120 .argmax = -1,
121 .flags = CMD_NOFILE_OK,
122 .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]",
123 .oneline = "open the file specified by path",
124 .help = open_help,
127 static QemuOptsList empty_opts = {
128 .name = "drive",
129 .merge_lists = true,
130 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
131 .desc = {
132 /* no elements => accept any params */
133 { /* end of list */ }
137 static int open_f(BlockBackend *blk, int argc, char **argv)
139 int flags = BDRV_O_UNMAP;
140 int readonly = 0;
141 bool writethrough = true;
142 int c;
143 QemuOpts *qopts;
144 QDict *opts;
145 bool force_share = false;
147 while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) {
148 switch (c) {
149 case 's':
150 flags |= BDRV_O_SNAPSHOT;
151 break;
152 case 'n':
153 flags |= BDRV_O_NOCACHE;
154 writethrough = false;
155 break;
156 case 'r':
157 readonly = 1;
158 break;
159 case 'k':
160 flags |= BDRV_O_NATIVE_AIO;
161 break;
162 case 't':
163 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
164 error_report("Invalid cache option: %s", optarg);
165 qemu_opts_reset(&empty_opts);
166 return 0;
168 break;
169 case 'd':
170 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
171 error_report("Invalid discard option: %s", optarg);
172 qemu_opts_reset(&empty_opts);
173 return 0;
175 break;
176 case 'o':
177 if (imageOpts) {
178 printf("--image-opts and 'open -o' are mutually exclusive\n");
179 qemu_opts_reset(&empty_opts);
180 return 0;
182 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
183 qemu_opts_reset(&empty_opts);
184 return 0;
186 break;
187 case 'U':
188 force_share = true;
189 break;
190 default:
191 qemu_opts_reset(&empty_opts);
192 return qemuio_command_usage(&open_cmd);
196 if (!readonly) {
197 flags |= BDRV_O_RDWR;
200 if (imageOpts && (optind == argc - 1)) {
201 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
202 qemu_opts_reset(&empty_opts);
203 return 0;
205 optind++;
208 qopts = qemu_opts_find(&empty_opts, NULL);
209 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
210 qemu_opts_reset(&empty_opts);
212 if (optind == argc - 1) {
213 openfile(argv[optind], flags, writethrough, force_share, opts);
214 } else if (optind == argc) {
215 openfile(NULL, flags, writethrough, force_share, opts);
216 } else {
217 QDECREF(opts);
218 qemuio_command_usage(&open_cmd);
220 return 0;
223 static int quit_f(BlockBackend *blk, int argc, char **argv)
225 return 1;
228 #if defined(CONFIG_FVD)
229 # include "qemu-io-sim.c"
230 #endif
232 static const cmdinfo_t quit_cmd = {
233 .name = "quit",
234 .altname = "q",
235 .cfunc = quit_f,
236 .argmin = -1,
237 .argmax = -1,
238 .flags = CMD_FLAG_GLOBAL,
239 .oneline = "exit the program",
242 static void usage(const char *name)
244 printf(
245 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
246 "QEMU Disk exerciser\n"
247 "\n"
248 " --object OBJECTDEF define an object such as 'secret' for\n"
249 " passwords and/or encryption keys\n"
250 " --image-opts treat file as option string\n"
251 " -c, --cmd STRING execute command with its arguments\n"
252 " from the given string\n"
253 " -f, --format FMT specifies the block driver to use\n"
254 " -r, --read-only export read-only\n"
255 " -s, --snapshot use snapshot file\n"
256 " -n, --nocache disable host cache, short for -t none\n"
257 " -m, --misalign misalign allocations for O_DIRECT\n"
258 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
259 " -t, --cache=MODE use the given cache mode for the image\n"
260 " -d, --discard=MODE use the given discard mode for the image\n"
261 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
262 " specify tracing options\n"
263 " see qemu-img(1) man page for full description\n"
264 " -U, --force-share force shared permissions\n"
265 " -h, --help display this help and exit\n"
266 " -V, --version output version information and exit\n"
267 "\n"
268 "See '%s -c help' for information on available commands."
269 "\n",
270 name, name);
273 static char *get_prompt(void)
275 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
277 if (!prompt[0]) {
278 snprintf(prompt, sizeof(prompt), "%s> ", progname);
281 return prompt;
284 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
285 const char *fmt, ...)
287 va_list ap;
288 va_start(ap, fmt);
289 vprintf(fmt, ap);
290 va_end(ap);
293 static void readline_flush_func(void *opaque)
295 fflush(stdout);
298 static void readline_func(void *opaque, const char *str, void *readline_opaque)
300 char **line = readline_opaque;
301 *line = g_strdup(str);
304 static void completion_match(const char *cmd, void *opaque)
306 readline_add_completion(readline_state, cmd);
309 static void readline_completion_func(void *opaque, const char *str)
311 readline_set_completion_index(readline_state, strlen(str));
312 qemuio_complete_command(str, completion_match, NULL);
315 static char *fetchline_readline(void)
317 char *line = NULL;
319 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
320 while (!line) {
321 int ch = getchar();
322 if (ch == EOF) {
323 break;
325 readline_handle_byte(readline_state, ch);
327 return line;
330 #define MAXREADLINESZ 1024
331 static char *fetchline_fgets(void)
333 char *p, *line = g_malloc(MAXREADLINESZ);
335 if (!fgets(line, MAXREADLINESZ, stdin)) {
336 g_free(line);
337 return NULL;
340 p = line + strlen(line);
341 if (p != line && p[-1] == '\n') {
342 p[-1] = '\0';
345 return line;
348 static char *fetchline(void)
350 if (readline_state) {
351 return fetchline_readline();
352 } else {
353 return fetchline_fgets();
357 static void prep_fetchline(void *opaque)
359 int *fetchable = opaque;
361 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
362 *fetchable= 1;
365 static void command_loop(void)
367 int i, done = 0, fetchable = 0, prompted = 0;
368 char *input;
370 for (i = 0; !done && i < ncmdline; i++) {
371 done = qemuio_command(qemuio_blk, cmdline[i]);
373 if (cmdline) {
374 g_free(cmdline);
375 return;
378 while (!done) {
379 if (!prompted) {
380 printf("%s", get_prompt());
381 fflush(stdout);
382 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
383 prompted = 1;
386 main_loop_wait(false);
388 if (!fetchable) {
389 continue;
392 input = fetchline();
393 if (input == NULL) {
394 break;
396 done = qemuio_command(qemuio_blk, input);
397 g_free(input);
399 prompted = 0;
400 fetchable = 0;
402 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
405 static void add_user_command(char *optarg)
407 cmdline = g_renew(char *, cmdline, ++ncmdline);
408 cmdline[ncmdline-1] = optarg;
411 static void reenable_tty_echo(void)
413 qemu_set_tty_echo(STDIN_FILENO, true);
416 enum {
417 OPTION_OBJECT = 256,
418 OPTION_IMAGE_OPTS = 257,
421 static QemuOptsList qemu_object_opts = {
422 .name = "object",
423 .implied_opt_name = "qom-type",
424 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
425 .desc = {
431 static QemuOptsList file_opts = {
432 .name = "file",
433 .implied_opt_name = "file",
434 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
435 .desc = {
436 /* no elements => accept any params */
437 { /* end of list */ }
441 int main(int argc, char **argv)
443 int readonly = 0;
444 const char *sopt = "hVc:d:f:rsnmkt:T:U";
445 const struct option lopt[] = {
446 { "help", no_argument, NULL, 'h' },
447 { "version", no_argument, NULL, 'V' },
448 { "cmd", required_argument, NULL, 'c' },
449 { "format", required_argument, NULL, 'f' },
450 { "read-only", no_argument, NULL, 'r' },
451 { "snapshot", no_argument, NULL, 's' },
452 { "nocache", no_argument, NULL, 'n' },
453 { "misalign", no_argument, NULL, 'm' },
454 { "native-aio", no_argument, NULL, 'k' },
455 { "discard", required_argument, NULL, 'd' },
456 { "cache", required_argument, NULL, 't' },
457 { "trace", required_argument, NULL, 'T' },
458 { "object", required_argument, NULL, OPTION_OBJECT },
459 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
460 { "force-share", no_argument, 0, 'U'},
461 { NULL, 0, NULL, 0 }
463 int c;
464 int opt_index = 0;
465 int flags = BDRV_O_UNMAP;
466 bool writethrough = true;
467 Error *local_error = NULL;
468 QDict *opts = NULL;
469 const char *format = NULL;
470 char *trace_file = NULL;
471 bool force_share = false;
473 #ifdef CONFIG_POSIX
474 signal(SIGPIPE, SIG_IGN);
475 #endif
477 module_call_init(MODULE_INIT_TRACE);
478 progname = basename(argv[0]);
479 qemu_init_exec_dir(argv[0]);
481 qcrypto_init(&error_fatal);
483 module_call_init(MODULE_INIT_QOM);
484 qemu_add_opts(&qemu_object_opts);
485 qemu_add_opts(&qemu_trace_opts);
486 bdrv_init();
488 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
489 switch (c) {
490 case 's':
491 flags |= BDRV_O_SNAPSHOT;
492 break;
493 case 'n':
494 flags |= BDRV_O_NOCACHE;
495 writethrough = false;
496 break;
497 case 'd':
498 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
499 error_report("Invalid discard option: %s", optarg);
500 exit(1);
502 break;
503 case 'f':
504 format = optarg;
505 break;
506 case 'c':
507 add_user_command(optarg);
508 break;
509 case 'r':
510 readonly = 1;
511 break;
512 case 'm':
513 qemuio_misalign = true;
514 break;
515 case 'k':
516 flags |= BDRV_O_NATIVE_AIO;
517 break;
518 case 't':
519 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
520 error_report("Invalid cache option: %s", optarg);
521 exit(1);
523 break;
524 case 'T':
525 g_free(trace_file);
526 trace_file = trace_opt_parse(optarg);
527 break;
528 case 'V':
529 printf("%s version %s\n", progname, QEMU_VERSION);
530 exit(0);
531 case 'h':
532 usage(progname);
533 exit(0);
534 case 'U':
535 force_share = true;
536 break;
537 case OPTION_OBJECT: {
538 QemuOpts *qopts;
539 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
540 optarg, true);
541 if (!qopts) {
542 exit(1);
544 } break;
545 case OPTION_IMAGE_OPTS:
546 imageOpts = true;
547 break;
548 default:
549 usage(progname);
550 exit(1);
554 if ((argc - optind) > 1) {
555 usage(progname);
556 exit(1);
559 if (format && imageOpts) {
560 error_report("--image-opts and -f are mutually exclusive");
561 exit(1);
564 if (qemu_init_main_loop(&local_error)) {
565 error_report_err(local_error);
566 exit(1);
569 if (qemu_opts_foreach(&qemu_object_opts,
570 user_creatable_add_opts_foreach,
571 NULL, NULL)) {
572 exit(1);
575 if (!trace_init_backends()) {
576 exit(1);
578 trace_init_file(trace_file);
579 qemu_set_log(LOG_TRACE);
581 /* initialize commands */
582 qemuio_add_command(&quit_cmd);
583 qemuio_add_command(&open_cmd);
584 qemuio_add_command(&close_cmd);
586 if (isatty(STDIN_FILENO)) {
587 readline_state = readline_init(readline_printf_func,
588 readline_flush_func,
589 NULL,
590 readline_completion_func);
591 qemu_set_tty_echo(STDIN_FILENO, false);
592 atexit(reenable_tty_echo);
595 /* open the device */
596 if (!readonly) {
597 flags |= BDRV_O_RDWR;
600 if ((argc - optind) == 1) {
601 if (imageOpts) {
602 QemuOpts *qopts = NULL;
603 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
604 if (!qopts) {
605 exit(1);
607 opts = qemu_opts_to_qdict(qopts, NULL);
608 if (openfile(NULL, flags, writethrough, force_share, opts)) {
609 exit(1);
611 } else {
612 if (format) {
613 opts = qdict_new();
614 qdict_put_str(opts, "driver", format);
616 if (openfile(argv[optind], flags, writethrough,
617 force_share, opts)) {
618 exit(1);
622 command_loop();
625 * Make sure all outstanding requests complete before the program exits.
627 bdrv_drain_all();
629 blk_unref(qemuio_blk);
630 g_free(readline_state);
631 return 0;