migratiom: Remove max_item_age parameter
[qemu/ar7.git] / qemu-io.c
blobc70bde3eb1cd3b2721df18f78cd492226927eeb6
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 " -C, -- use copy-on-read\n"
106 " -n, -- disable host cache, short for -t none\n"
107 " -U, -- force shared permissions\n"
108 " -k, -- use kernel AIO implementation (on Linux only)\n"
109 " -t, -- use the given cache mode for the image\n"
110 " -d, -- use the given discard mode for the image\n"
111 " -o, -- options to be given to the block driver"
112 "\n");
115 static int open_f(BlockBackend *blk, int argc, char **argv);
117 static const cmdinfo_t open_cmd = {
118 .name = "open",
119 .altname = "o",
120 .cfunc = open_f,
121 .argmin = 1,
122 .argmax = -1,
123 .flags = CMD_NOFILE_OK,
124 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
125 .oneline = "open the file specified by path",
126 .help = open_help,
129 static QemuOptsList empty_opts = {
130 .name = "drive",
131 .merge_lists = true,
132 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
133 .desc = {
134 /* no elements => accept any params */
135 { /* end of list */ }
139 static int open_f(BlockBackend *blk, int argc, char **argv)
141 int flags = BDRV_O_UNMAP;
142 int readonly = 0;
143 bool writethrough = true;
144 int c;
145 QemuOpts *qopts;
146 QDict *opts;
147 bool force_share = false;
149 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
150 switch (c) {
151 case 's':
152 flags |= BDRV_O_SNAPSHOT;
153 break;
154 case 'n':
155 flags |= BDRV_O_NOCACHE;
156 writethrough = false;
157 break;
158 case 'C':
159 flags |= BDRV_O_COPY_ON_READ;
160 break;
161 case 'r':
162 readonly = 1;
163 break;
164 case 'k':
165 flags |= BDRV_O_NATIVE_AIO;
166 break;
167 case 't':
168 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
169 error_report("Invalid cache option: %s", optarg);
170 qemu_opts_reset(&empty_opts);
171 return 0;
173 break;
174 case 'd':
175 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
176 error_report("Invalid discard option: %s", optarg);
177 qemu_opts_reset(&empty_opts);
178 return 0;
180 break;
181 case 'o':
182 if (imageOpts) {
183 printf("--image-opts and 'open -o' are mutually exclusive\n");
184 qemu_opts_reset(&empty_opts);
185 return 0;
187 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
188 qemu_opts_reset(&empty_opts);
189 return 0;
191 break;
192 case 'U':
193 force_share = true;
194 break;
195 default:
196 qemu_opts_reset(&empty_opts);
197 return qemuio_command_usage(&open_cmd);
201 if (!readonly) {
202 flags |= BDRV_O_RDWR;
205 if (imageOpts && (optind == argc - 1)) {
206 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
207 qemu_opts_reset(&empty_opts);
208 return 0;
210 optind++;
213 qopts = qemu_opts_find(&empty_opts, NULL);
214 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
215 qemu_opts_reset(&empty_opts);
217 if (optind == argc - 1) {
218 openfile(argv[optind], flags, writethrough, force_share, opts);
219 } else if (optind == argc) {
220 openfile(NULL, flags, writethrough, force_share, opts);
221 } else {
222 QDECREF(opts);
223 qemuio_command_usage(&open_cmd);
225 return 0;
228 static int quit_f(BlockBackend *blk, int argc, char **argv)
230 return 1;
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 " -C, --copy-on-read enable copy-on-read\n"
259 " -m, --misalign misalign allocations for O_DIRECT\n"
260 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
261 " -t, --cache=MODE use the given cache mode for the image\n"
262 " -d, --discard=MODE use the given discard mode for the image\n"
263 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
264 " specify tracing options\n"
265 " see qemu-img(1) man page for full description\n"
266 " -U, --force-share force shared permissions\n"
267 " -h, --help display this help and exit\n"
268 " -V, --version output version information and exit\n"
269 "\n"
270 "See '%s -c help' for information on available commands.\n"
271 "\n"
272 QEMU_HELP_BOTTOM "\n",
273 name, name);
276 static char *get_prompt(void)
278 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
280 if (!prompt[0]) {
281 snprintf(prompt, sizeof(prompt), "%s> ", progname);
284 return prompt;
287 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
288 const char *fmt, ...)
290 va_list ap;
291 va_start(ap, fmt);
292 vprintf(fmt, ap);
293 va_end(ap);
296 static void readline_flush_func(void *opaque)
298 fflush(stdout);
301 static void readline_func(void *opaque, const char *str, void *readline_opaque)
303 char **line = readline_opaque;
304 *line = g_strdup(str);
307 static void completion_match(const char *cmd, void *opaque)
309 readline_add_completion(readline_state, cmd);
312 static void readline_completion_func(void *opaque, const char *str)
314 readline_set_completion_index(readline_state, strlen(str));
315 qemuio_complete_command(str, completion_match, NULL);
318 static char *fetchline_readline(void)
320 char *line = NULL;
322 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
323 while (!line) {
324 int ch = getchar();
325 if (ch == EOF) {
326 break;
328 readline_handle_byte(readline_state, ch);
330 return line;
333 #define MAXREADLINESZ 1024
334 static char *fetchline_fgets(void)
336 char *p, *line = g_malloc(MAXREADLINESZ);
338 if (!fgets(line, MAXREADLINESZ, stdin)) {
339 g_free(line);
340 return NULL;
343 p = line + strlen(line);
344 if (p != line && p[-1] == '\n') {
345 p[-1] = '\0';
348 return line;
351 static char *fetchline(void)
353 if (readline_state) {
354 return fetchline_readline();
355 } else {
356 return fetchline_fgets();
360 static void prep_fetchline(void *opaque)
362 int *fetchable = opaque;
364 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
365 *fetchable= 1;
368 static void command_loop(void)
370 int i, done = 0, fetchable = 0, prompted = 0;
371 char *input;
373 for (i = 0; !done && i < ncmdline; i++) {
374 done = qemuio_command(qemuio_blk, cmdline[i]);
376 if (cmdline) {
377 g_free(cmdline);
378 return;
381 while (!done) {
382 if (!prompted) {
383 printf("%s", get_prompt());
384 fflush(stdout);
385 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
386 prompted = 1;
389 main_loop_wait(false);
391 if (!fetchable) {
392 continue;
395 input = fetchline();
396 if (input == NULL) {
397 break;
399 done = qemuio_command(qemuio_blk, input);
400 g_free(input);
402 prompted = 0;
403 fetchable = 0;
405 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
408 static void add_user_command(char *optarg)
410 cmdline = g_renew(char *, cmdline, ++ncmdline);
411 cmdline[ncmdline-1] = optarg;
414 static void reenable_tty_echo(void)
416 qemu_set_tty_echo(STDIN_FILENO, true);
419 enum {
420 OPTION_OBJECT = 256,
421 OPTION_IMAGE_OPTS = 257,
424 static QemuOptsList qemu_object_opts = {
425 .name = "object",
426 .implied_opt_name = "qom-type",
427 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
428 .desc = {
434 static QemuOptsList file_opts = {
435 .name = "file",
436 .implied_opt_name = "file",
437 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
438 .desc = {
439 /* no elements => accept any params */
440 { /* end of list */ }
444 int main(int argc, char **argv)
446 int readonly = 0;
447 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
448 const struct option lopt[] = {
449 { "help", no_argument, NULL, 'h' },
450 { "version", no_argument, NULL, 'V' },
451 { "cmd", required_argument, NULL, 'c' },
452 { "format", required_argument, NULL, 'f' },
453 { "read-only", no_argument, NULL, 'r' },
454 { "snapshot", no_argument, NULL, 's' },
455 { "nocache", no_argument, NULL, 'n' },
456 { "copy-on-read", no_argument, NULL, 'C' },
457 { "misalign", no_argument, NULL, 'm' },
458 { "native-aio", no_argument, NULL, 'k' },
459 { "discard", required_argument, NULL, 'd' },
460 { "cache", required_argument, NULL, 't' },
461 { "trace", required_argument, NULL, 'T' },
462 { "object", required_argument, NULL, OPTION_OBJECT },
463 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
464 { "force-share", no_argument, 0, 'U'},
465 { NULL, 0, NULL, 0 }
467 int c;
468 int opt_index = 0;
469 int flags = BDRV_O_UNMAP;
470 bool writethrough = true;
471 Error *local_error = NULL;
472 QDict *opts = NULL;
473 const char *format = NULL;
474 char *trace_file = NULL;
475 bool force_share = false;
477 #ifdef CONFIG_POSIX
478 signal(SIGPIPE, SIG_IGN);
479 #endif
481 module_call_init(MODULE_INIT_TRACE);
482 progname = basename(argv[0]);
483 qemu_init_exec_dir(argv[0]);
485 qcrypto_init(&error_fatal);
487 module_call_init(MODULE_INIT_QOM);
488 qemu_add_opts(&qemu_object_opts);
489 qemu_add_opts(&qemu_trace_opts);
490 bdrv_init();
492 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
493 switch (c) {
494 case 's':
495 flags |= BDRV_O_SNAPSHOT;
496 break;
497 case 'n':
498 flags |= BDRV_O_NOCACHE;
499 writethrough = false;
500 break;
501 case 'C':
502 flags |= BDRV_O_COPY_ON_READ;
503 break;
504 case 'd':
505 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
506 error_report("Invalid discard option: %s", optarg);
507 exit(1);
509 break;
510 case 'f':
511 format = optarg;
512 break;
513 case 'c':
514 add_user_command(optarg);
515 break;
516 case 'r':
517 readonly = 1;
518 break;
519 case 'm':
520 qemuio_misalign = true;
521 break;
522 case 'k':
523 flags |= BDRV_O_NATIVE_AIO;
524 break;
525 case 't':
526 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
527 error_report("Invalid cache option: %s", optarg);
528 exit(1);
530 break;
531 case 'T':
532 g_free(trace_file);
533 trace_file = trace_opt_parse(optarg);
534 break;
535 case 'V':
536 printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n"
537 QEMU_COPYRIGHT "\n", progname);
538 exit(0);
539 case 'h':
540 usage(progname);
541 exit(0);
542 case 'U':
543 force_share = true;
544 break;
545 case OPTION_OBJECT: {
546 QemuOpts *qopts;
547 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
548 optarg, true);
549 if (!qopts) {
550 exit(1);
552 } break;
553 case OPTION_IMAGE_OPTS:
554 imageOpts = true;
555 break;
556 default:
557 usage(progname);
558 exit(1);
562 if ((argc - optind) > 1) {
563 usage(progname);
564 exit(1);
567 if (format && imageOpts) {
568 error_report("--image-opts and -f are mutually exclusive");
569 exit(1);
572 if (qemu_init_main_loop(&local_error)) {
573 error_report_err(local_error);
574 exit(1);
577 if (qemu_opts_foreach(&qemu_object_opts,
578 user_creatable_add_opts_foreach,
579 NULL, NULL)) {
580 exit(1);
583 if (!trace_init_backends()) {
584 exit(1);
586 trace_init_file(trace_file);
587 qemu_set_log(LOG_TRACE);
589 /* initialize commands */
590 qemuio_add_command(&quit_cmd);
591 qemuio_add_command(&open_cmd);
592 qemuio_add_command(&close_cmd);
594 if (isatty(STDIN_FILENO)) {
595 readline_state = readline_init(readline_printf_func,
596 readline_flush_func,
597 NULL,
598 readline_completion_func);
599 qemu_set_tty_echo(STDIN_FILENO, false);
600 atexit(reenable_tty_echo);
603 /* open the device */
604 if (!readonly) {
605 flags |= BDRV_O_RDWR;
608 if ((argc - optind) == 1) {
609 if (imageOpts) {
610 QemuOpts *qopts = NULL;
611 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
612 if (!qopts) {
613 exit(1);
615 opts = qemu_opts_to_qdict(qopts, NULL);
616 if (openfile(NULL, flags, writethrough, force_share, opts)) {
617 exit(1);
619 } else {
620 if (format) {
621 opts = qdict_new();
622 qdict_put_str(opts, "driver", format);
624 if (openfile(argv[optind], flags, writethrough,
625 force_share, opts)) {
626 exit(1);
630 command_loop();
633 * Make sure all outstanding requests complete before the program exits.
635 bdrv_drain_all();
637 blk_unref(qemuio_blk);
638 g_free(readline_state);
639 return 0;