Merge tag 'v2.10.0-rc4'
[qemu/ar7.git] / qemu-io.c
blobaa19d31107e1d7d502d5a9ac43aaab469149d9f1
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"
29 #include "qemu-version.h"
31 #define CMD_NOFILE_OK 0x01
33 static char *progname;
35 static BlockBackend *qemuio_blk;
37 /* qemu-io commands passed using -c */
38 static int ncmdline;
39 static char **cmdline;
40 static bool imageOpts;
42 static ReadLineState *readline_state;
44 static int close_f(BlockBackend *blk, int argc, char **argv)
46 blk_unref(qemuio_blk);
47 qemuio_blk = NULL;
48 return 0;
51 static const cmdinfo_t close_cmd = {
52 .name = "close",
53 .altname = "c",
54 .cfunc = close_f,
55 .oneline = "close the current open file",
58 static int openfile(char *name, int flags, bool writethrough, bool force_share,
59 QDict *opts)
61 Error *local_err = NULL;
63 if (qemuio_blk) {
64 error_report("file open already, try 'help close'");
65 QDECREF(opts);
66 return 1;
69 if (force_share) {
70 if (!opts) {
71 opts = qdict_new();
73 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
74 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
75 error_report("-U conflicts with image options");
76 QDECREF(opts);
77 return 1;
79 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
81 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
82 if (!qemuio_blk) {
83 error_reportf_err(local_err, "can't open%s%s: ",
84 name ? " device " : "", name ?: "");
85 return 1;
88 blk_set_enable_write_cache(qemuio_blk, !writethrough);
90 return 0;
93 static void open_help(void)
95 printf(
96 "\n"
97 " opens a new file in the requested mode\n"
98 "\n"
99 " Example:\n"
100 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
101 "\n"
102 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
103 " -r, -- open file read-only\n"
104 " -s, -- use snapshot file\n"
105 " -n, -- disable host cache, short for -t none\n"
106 " -U, -- force shared permissions\n"
107 " -k, -- use kernel AIO implementation (on Linux only)\n"
108 " -t, -- use the given cache mode for the image\n"
109 " -d, -- use the given discard mode for the image\n"
110 " -o, -- options to be given to the block driver"
111 "\n");
114 static int open_f(BlockBackend *blk, int argc, char **argv);
116 static const cmdinfo_t open_cmd = {
117 .name = "open",
118 .altname = "o",
119 .cfunc = open_f,
120 .argmin = 1,
121 .argmax = -1,
122 .flags = CMD_NOFILE_OK,
123 .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]",
124 .oneline = "open the file specified by path",
125 .help = open_help,
128 static QemuOptsList empty_opts = {
129 .name = "drive",
130 .merge_lists = true,
131 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
132 .desc = {
133 /* no elements => accept any params */
134 { /* end of list */ }
138 static int open_f(BlockBackend *blk, int argc, char **argv)
140 int flags = BDRV_O_UNMAP;
141 int readonly = 0;
142 bool writethrough = true;
143 int c;
144 QemuOpts *qopts;
145 QDict *opts;
146 bool force_share = false;
148 while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) {
149 switch (c) {
150 case 's':
151 flags |= BDRV_O_SNAPSHOT;
152 break;
153 case 'n':
154 flags |= BDRV_O_NOCACHE;
155 writethrough = false;
156 break;
157 case 'r':
158 readonly = 1;
159 break;
160 case 'k':
161 flags |= BDRV_O_NATIVE_AIO;
162 break;
163 case 't':
164 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
165 error_report("Invalid cache option: %s", optarg);
166 qemu_opts_reset(&empty_opts);
167 return 0;
169 break;
170 case 'd':
171 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
172 error_report("Invalid discard option: %s", optarg);
173 qemu_opts_reset(&empty_opts);
174 return 0;
176 break;
177 case 'o':
178 if (imageOpts) {
179 printf("--image-opts and 'open -o' are mutually exclusive\n");
180 qemu_opts_reset(&empty_opts);
181 return 0;
183 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
184 qemu_opts_reset(&empty_opts);
185 return 0;
187 break;
188 case 'U':
189 force_share = true;
190 break;
191 default:
192 qemu_opts_reset(&empty_opts);
193 return qemuio_command_usage(&open_cmd);
197 if (!readonly) {
198 flags |= BDRV_O_RDWR;
201 if (imageOpts && (optind == argc - 1)) {
202 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
203 qemu_opts_reset(&empty_opts);
204 return 0;
206 optind++;
209 qopts = qemu_opts_find(&empty_opts, NULL);
210 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
211 qemu_opts_reset(&empty_opts);
213 if (optind == argc - 1) {
214 openfile(argv[optind], flags, writethrough, force_share, opts);
215 } else if (optind == argc) {
216 openfile(NULL, flags, writethrough, force_share, opts);
217 } else {
218 QDECREF(opts);
219 qemuio_command_usage(&open_cmd);
221 return 0;
224 static int quit_f(BlockBackend *blk, int argc, char **argv)
226 return 1;
229 #if defined(CONFIG_FVD)
230 # include "qemu-io-sim.c"
231 #endif
233 static const cmdinfo_t quit_cmd = {
234 .name = "quit",
235 .altname = "q",
236 .cfunc = quit_f,
237 .argmin = -1,
238 .argmax = -1,
239 .flags = CMD_FLAG_GLOBAL,
240 .oneline = "exit the program",
243 static void usage(const char *name)
245 printf(
246 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
247 "QEMU Disk exerciser\n"
248 "\n"
249 " --object OBJECTDEF define an object such as 'secret' for\n"
250 " passwords and/or encryption keys\n"
251 " --image-opts treat file as option string\n"
252 " -c, --cmd STRING execute command with its arguments\n"
253 " from the given string\n"
254 " -f, --format FMT specifies the block driver to use\n"
255 " -r, --read-only export read-only\n"
256 " -s, --snapshot use snapshot file\n"
257 " -n, --nocache disable host cache, short for -t none\n"
258 " -m, --misalign misalign allocations for O_DIRECT\n"
259 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
260 " -t, --cache=MODE use the given cache mode for the image\n"
261 " -d, --discard=MODE use the given discard mode for the image\n"
262 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
263 " specify tracing options\n"
264 " see qemu-img(1) man page for full description\n"
265 " -U, --force-share force shared permissions\n"
266 " -h, --help display this help and exit\n"
267 " -V, --version output version information and exit\n"
268 "\n"
269 "See '%s -c help' for information on available commands.\n"
270 "\n"
271 QEMU_HELP_BOTTOM "\n",
272 name, name);
275 static char *get_prompt(void)
277 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
279 if (!prompt[0]) {
280 snprintf(prompt, sizeof(prompt), "%s> ", progname);
283 return prompt;
286 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
287 const char *fmt, ...)
289 va_list ap;
290 va_start(ap, fmt);
291 vprintf(fmt, ap);
292 va_end(ap);
295 static void readline_flush_func(void *opaque)
297 fflush(stdout);
300 static void readline_func(void *opaque, const char *str, void *readline_opaque)
302 char **line = readline_opaque;
303 *line = g_strdup(str);
306 static void completion_match(const char *cmd, void *opaque)
308 readline_add_completion(readline_state, cmd);
311 static void readline_completion_func(void *opaque, const char *str)
313 readline_set_completion_index(readline_state, strlen(str));
314 qemuio_complete_command(str, completion_match, NULL);
317 static char *fetchline_readline(void)
319 char *line = NULL;
321 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
322 while (!line) {
323 int ch = getchar();
324 if (ch == EOF) {
325 break;
327 readline_handle_byte(readline_state, ch);
329 return line;
332 #define MAXREADLINESZ 1024
333 static char *fetchline_fgets(void)
335 char *p, *line = g_malloc(MAXREADLINESZ);
337 if (!fgets(line, MAXREADLINESZ, stdin)) {
338 g_free(line);
339 return NULL;
342 p = line + strlen(line);
343 if (p != line && p[-1] == '\n') {
344 p[-1] = '\0';
347 return line;
350 static char *fetchline(void)
352 if (readline_state) {
353 return fetchline_readline();
354 } else {
355 return fetchline_fgets();
359 static void prep_fetchline(void *opaque)
361 int *fetchable = opaque;
363 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
364 *fetchable= 1;
367 static void command_loop(void)
369 int i, done = 0, fetchable = 0, prompted = 0;
370 char *input;
372 for (i = 0; !done && i < ncmdline; i++) {
373 done = qemuio_command(qemuio_blk, cmdline[i]);
375 if (cmdline) {
376 g_free(cmdline);
377 return;
380 while (!done) {
381 if (!prompted) {
382 printf("%s", get_prompt());
383 fflush(stdout);
384 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
385 prompted = 1;
388 main_loop_wait(false);
390 if (!fetchable) {
391 continue;
394 input = fetchline();
395 if (input == NULL) {
396 break;
398 done = qemuio_command(qemuio_blk, input);
399 g_free(input);
401 prompted = 0;
402 fetchable = 0;
404 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
407 static void add_user_command(char *optarg)
409 cmdline = g_renew(char *, cmdline, ++ncmdline);
410 cmdline[ncmdline-1] = optarg;
413 static void reenable_tty_echo(void)
415 qemu_set_tty_echo(STDIN_FILENO, true);
418 enum {
419 OPTION_OBJECT = 256,
420 OPTION_IMAGE_OPTS = 257,
423 static QemuOptsList qemu_object_opts = {
424 .name = "object",
425 .implied_opt_name = "qom-type",
426 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
427 .desc = {
433 static QemuOptsList file_opts = {
434 .name = "file",
435 .implied_opt_name = "file",
436 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
437 .desc = {
438 /* no elements => accept any params */
439 { /* end of list */ }
443 int main(int argc, char **argv)
445 int readonly = 0;
446 const char *sopt = "hVc:d:f:rsnmkt:T:U";
447 const struct option lopt[] = {
448 { "help", no_argument, NULL, 'h' },
449 { "version", no_argument, NULL, 'V' },
450 { "cmd", required_argument, NULL, 'c' },
451 { "format", required_argument, NULL, 'f' },
452 { "read-only", no_argument, NULL, 'r' },
453 { "snapshot", no_argument, NULL, 's' },
454 { "nocache", no_argument, NULL, 'n' },
455 { "misalign", no_argument, NULL, 'm' },
456 { "native-aio", no_argument, NULL, 'k' },
457 { "discard", required_argument, NULL, 'd' },
458 { "cache", required_argument, NULL, 't' },
459 { "trace", required_argument, NULL, 'T' },
460 { "object", required_argument, NULL, OPTION_OBJECT },
461 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
462 { "force-share", no_argument, 0, 'U'},
463 { NULL, 0, NULL, 0 }
465 int c;
466 int opt_index = 0;
467 int flags = BDRV_O_UNMAP;
468 bool writethrough = true;
469 Error *local_error = NULL;
470 QDict *opts = NULL;
471 const char *format = NULL;
472 char *trace_file = NULL;
473 bool force_share = false;
475 #ifdef CONFIG_POSIX
476 signal(SIGPIPE, SIG_IGN);
477 #endif
479 module_call_init(MODULE_INIT_TRACE);
480 progname = basename(argv[0]);
481 qemu_init_exec_dir(argv[0]);
483 qcrypto_init(&error_fatal);
485 module_call_init(MODULE_INIT_QOM);
486 qemu_add_opts(&qemu_object_opts);
487 qemu_add_opts(&qemu_trace_opts);
488 bdrv_init();
490 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
491 switch (c) {
492 case 's':
493 flags |= BDRV_O_SNAPSHOT;
494 break;
495 case 'n':
496 flags |= BDRV_O_NOCACHE;
497 writethrough = false;
498 break;
499 case 'd':
500 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
501 error_report("Invalid discard option: %s", optarg);
502 exit(1);
504 break;
505 case 'f':
506 format = optarg;
507 break;
508 case 'c':
509 add_user_command(optarg);
510 break;
511 case 'r':
512 readonly = 1;
513 break;
514 case 'm':
515 qemuio_misalign = true;
516 break;
517 case 'k':
518 flags |= BDRV_O_NATIVE_AIO;
519 break;
520 case 't':
521 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
522 error_report("Invalid cache option: %s", optarg);
523 exit(1);
525 break;
526 case 'T':
527 g_free(trace_file);
528 trace_file = trace_opt_parse(optarg);
529 break;
530 case 'V':
531 printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n"
532 QEMU_COPYRIGHT "\n", progname);
533 exit(0);
534 case 'h':
535 usage(progname);
536 exit(0);
537 case 'U':
538 force_share = true;
539 break;
540 case OPTION_OBJECT: {
541 QemuOpts *qopts;
542 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
543 optarg, true);
544 if (!qopts) {
545 exit(1);
547 } break;
548 case OPTION_IMAGE_OPTS:
549 imageOpts = true;
550 break;
551 default:
552 usage(progname);
553 exit(1);
557 if ((argc - optind) > 1) {
558 usage(progname);
559 exit(1);
562 if (format && imageOpts) {
563 error_report("--image-opts and -f are mutually exclusive");
564 exit(1);
567 if (qemu_init_main_loop(&local_error)) {
568 error_report_err(local_error);
569 exit(1);
572 if (qemu_opts_foreach(&qemu_object_opts,
573 user_creatable_add_opts_foreach,
574 NULL, NULL)) {
575 exit(1);
578 if (!trace_init_backends()) {
579 exit(1);
581 trace_init_file(trace_file);
582 qemu_set_log(LOG_TRACE);
584 /* initialize commands */
585 qemuio_add_command(&quit_cmd);
586 qemuio_add_command(&open_cmd);
587 qemuio_add_command(&close_cmd);
589 if (isatty(STDIN_FILENO)) {
590 readline_state = readline_init(readline_printf_func,
591 readline_flush_func,
592 NULL,
593 readline_completion_func);
594 qemu_set_tty_echo(STDIN_FILENO, false);
595 atexit(reenable_tty_echo);
598 /* open the device */
599 if (!readonly) {
600 flags |= BDRV_O_RDWR;
603 if ((argc - optind) == 1) {
604 if (imageOpts) {
605 QemuOpts *qopts = NULL;
606 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
607 if (!qopts) {
608 exit(1);
610 opts = qemu_opts_to_qdict(qopts, NULL);
611 if (openfile(NULL, flags, writethrough, force_share, opts)) {
612 exit(1);
614 } else {
615 if (format) {
616 opts = qdict_new();
617 qdict_put_str(opts, "driver", format);
619 if (openfile(argv[optind], flags, writethrough,
620 force_share, opts)) {
621 exit(1);
625 command_loop();
628 * Make sure all outstanding requests complete before the program exits.
630 bdrv_drain_all();
632 blk_unref(qemuio_blk);
633 g_free(readline_state);
634 return 0;