Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blob51ec637573d62f386193328c7bba2a3efd930be9
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;
61 BlockDriverState *bs;
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 bs = blk_bs(qemuio_blk);
89 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs)) {
90 char password[256];
91 printf("Disk image '%s' is encrypted.\n", name);
92 if (qemu_read_password(password, sizeof(password)) < 0) {
93 error_report("No password given");
94 goto error;
96 if (bdrv_set_key(bs, password) < 0) {
97 error_report("invalid password");
98 goto error;
102 blk_set_enable_write_cache(qemuio_blk, !writethrough);
104 return 0;
106 error:
107 blk_unref(qemuio_blk);
108 qemuio_blk = NULL;
109 return 1;
112 static void open_help(void)
114 printf(
115 "\n"
116 " opens a new file in the requested mode\n"
117 "\n"
118 " Example:\n"
119 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
120 "\n"
121 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
122 " -r, -- open file read-only\n"
123 " -s, -- use snapshot file\n"
124 " -n, -- disable host cache, short for -t none\n"
125 " -U, -- force shared permissions\n"
126 " -k, -- use kernel AIO implementation (on Linux only)\n"
127 " -t, -- use the given cache mode for the image\n"
128 " -d, -- use the given discard mode for the image\n"
129 " -o, -- options to be given to the block driver"
130 "\n");
133 static int open_f(BlockBackend *blk, int argc, char **argv);
135 static const cmdinfo_t open_cmd = {
136 .name = "open",
137 .altname = "o",
138 .cfunc = open_f,
139 .argmin = 1,
140 .argmax = -1,
141 .flags = CMD_NOFILE_OK,
142 .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]",
143 .oneline = "open the file specified by path",
144 .help = open_help,
147 static QemuOptsList empty_opts = {
148 .name = "drive",
149 .merge_lists = true,
150 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
151 .desc = {
152 /* no elements => accept any params */
153 { /* end of list */ }
157 static int open_f(BlockBackend *blk, int argc, char **argv)
159 int flags = BDRV_O_UNMAP;
160 int readonly = 0;
161 bool writethrough = true;
162 int c;
163 QemuOpts *qopts;
164 QDict *opts;
165 bool force_share = false;
167 while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) {
168 switch (c) {
169 case 's':
170 flags |= BDRV_O_SNAPSHOT;
171 break;
172 case 'n':
173 flags |= BDRV_O_NOCACHE;
174 writethrough = false;
175 break;
176 case 'r':
177 readonly = 1;
178 break;
179 case 'k':
180 flags |= BDRV_O_NATIVE_AIO;
181 break;
182 case 't':
183 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
184 error_report("Invalid cache option: %s", optarg);
185 qemu_opts_reset(&empty_opts);
186 return 0;
188 break;
189 case 'd':
190 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
191 error_report("Invalid discard option: %s", optarg);
192 qemu_opts_reset(&empty_opts);
193 return 0;
195 break;
196 case 'o':
197 if (imageOpts) {
198 printf("--image-opts and 'open -o' are mutually exclusive\n");
199 qemu_opts_reset(&empty_opts);
200 return 0;
202 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
203 qemu_opts_reset(&empty_opts);
204 return 0;
206 break;
207 case 'U':
208 force_share = true;
209 break;
210 default:
211 qemu_opts_reset(&empty_opts);
212 return qemuio_command_usage(&open_cmd);
216 if (!readonly) {
217 flags |= BDRV_O_RDWR;
220 if (imageOpts && (optind == argc - 1)) {
221 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
222 qemu_opts_reset(&empty_opts);
223 return 0;
225 optind++;
228 qopts = qemu_opts_find(&empty_opts, NULL);
229 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
230 qemu_opts_reset(&empty_opts);
232 if (optind == argc - 1) {
233 return openfile(argv[optind], flags, writethrough, force_share, opts);
234 } else if (optind == argc) {
235 return openfile(NULL, flags, writethrough, force_share, opts);
236 } else {
237 QDECREF(opts);
238 return qemuio_command_usage(&open_cmd);
242 static int quit_f(BlockBackend *blk, int argc, char **argv)
244 return 1;
247 #if defined(CONFIG_FVD)
248 # include "qemu-io-sim.c"
249 #endif
251 static const cmdinfo_t quit_cmd = {
252 .name = "quit",
253 .altname = "q",
254 .cfunc = quit_f,
255 .argmin = -1,
256 .argmax = -1,
257 .flags = CMD_FLAG_GLOBAL,
258 .oneline = "exit the program",
261 static void usage(const char *name)
263 printf(
264 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
265 "QEMU Disk exerciser\n"
266 "\n"
267 " --object OBJECTDEF define an object such as 'secret' for\n"
268 " passwords and/or encryption keys\n"
269 " --image-opts treat file as option string\n"
270 " -c, --cmd STRING execute command with its arguments\n"
271 " from the given string\n"
272 " -f, --format FMT specifies the block driver to use\n"
273 " -r, --read-only export read-only\n"
274 " -s, --snapshot use snapshot file\n"
275 " -n, --nocache disable host cache, short for -t none\n"
276 " -m, --misalign misalign allocations for O_DIRECT\n"
277 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
278 " -t, --cache=MODE use the given cache mode for the image\n"
279 " -d, --discard=MODE use the given discard mode for the image\n"
280 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
281 " specify tracing options\n"
282 " see qemu-img(1) man page for full description\n"
283 " -U, --force-share force shared permissions\n"
284 " -h, --help display this help and exit\n"
285 " -V, --version output version information and exit\n"
286 "\n"
287 "See '%s -c help' for information on available commands."
288 "\n",
289 name, name);
292 static char *get_prompt(void)
294 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
296 if (!prompt[0]) {
297 snprintf(prompt, sizeof(prompt), "%s> ", progname);
300 return prompt;
303 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
304 const char *fmt, ...)
306 va_list ap;
307 va_start(ap, fmt);
308 vprintf(fmt, ap);
309 va_end(ap);
312 static void readline_flush_func(void *opaque)
314 fflush(stdout);
317 static void readline_func(void *opaque, const char *str, void *readline_opaque)
319 char **line = readline_opaque;
320 *line = g_strdup(str);
323 static void completion_match(const char *cmd, void *opaque)
325 readline_add_completion(readline_state, cmd);
328 static void readline_completion_func(void *opaque, const char *str)
330 readline_set_completion_index(readline_state, strlen(str));
331 qemuio_complete_command(str, completion_match, NULL);
334 static char *fetchline_readline(void)
336 char *line = NULL;
338 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
339 while (!line) {
340 int ch = getchar();
341 if (ch == EOF) {
342 break;
344 readline_handle_byte(readline_state, ch);
346 return line;
349 #define MAXREADLINESZ 1024
350 static char *fetchline_fgets(void)
352 char *p, *line = g_malloc(MAXREADLINESZ);
354 if (!fgets(line, MAXREADLINESZ, stdin)) {
355 g_free(line);
356 return NULL;
359 p = line + strlen(line);
360 if (p != line && p[-1] == '\n') {
361 p[-1] = '\0';
364 return line;
367 static char *fetchline(void)
369 if (readline_state) {
370 return fetchline_readline();
371 } else {
372 return fetchline_fgets();
376 static void prep_fetchline(void *opaque)
378 int *fetchable = opaque;
380 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
381 *fetchable= 1;
384 static void command_loop(void)
386 int i, done = 0, fetchable = 0, prompted = 0;
387 char *input;
389 for (i = 0; !done && i < ncmdline; i++) {
390 done = qemuio_command(qemuio_blk, cmdline[i]);
392 if (cmdline) {
393 g_free(cmdline);
394 return;
397 while (!done) {
398 if (!prompted) {
399 printf("%s", get_prompt());
400 fflush(stdout);
401 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
402 prompted = 1;
405 main_loop_wait(false);
407 if (!fetchable) {
408 continue;
411 input = fetchline();
412 if (input == NULL) {
413 break;
415 done = qemuio_command(qemuio_blk, input);
416 g_free(input);
418 prompted = 0;
419 fetchable = 0;
421 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
424 static void add_user_command(char *optarg)
426 cmdline = g_renew(char *, cmdline, ++ncmdline);
427 cmdline[ncmdline-1] = optarg;
430 static void reenable_tty_echo(void)
432 qemu_set_tty_echo(STDIN_FILENO, true);
435 enum {
436 OPTION_OBJECT = 256,
437 OPTION_IMAGE_OPTS = 257,
440 static QemuOptsList qemu_object_opts = {
441 .name = "object",
442 .implied_opt_name = "qom-type",
443 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
444 .desc = {
450 static QemuOptsList file_opts = {
451 .name = "file",
452 .implied_opt_name = "file",
453 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
454 .desc = {
455 /* no elements => accept any params */
456 { /* end of list */ }
460 int main(int argc, char **argv)
462 int readonly = 0;
463 const char *sopt = "hVc:d:f:rsnmkt:T:U";
464 const struct option lopt[] = {
465 { "help", no_argument, NULL, 'h' },
466 { "version", no_argument, NULL, 'V' },
467 { "cmd", required_argument, NULL, 'c' },
468 { "format", required_argument, NULL, 'f' },
469 { "read-only", no_argument, NULL, 'r' },
470 { "snapshot", no_argument, NULL, 's' },
471 { "nocache", no_argument, NULL, 'n' },
472 { "misalign", no_argument, NULL, 'm' },
473 { "native-aio", no_argument, NULL, 'k' },
474 { "discard", required_argument, NULL, 'd' },
475 { "cache", required_argument, NULL, 't' },
476 { "trace", required_argument, NULL, 'T' },
477 { "object", required_argument, NULL, OPTION_OBJECT },
478 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
479 { "force-share", no_argument, 0, 'U'},
480 { NULL, 0, NULL, 0 }
482 int c;
483 int opt_index = 0;
484 int flags = BDRV_O_UNMAP;
485 bool writethrough = true;
486 Error *local_error = NULL;
487 QDict *opts = NULL;
488 const char *format = NULL;
489 char *trace_file = NULL;
490 bool force_share = false;
492 #ifdef CONFIG_POSIX
493 signal(SIGPIPE, SIG_IGN);
494 #endif
496 module_call_init(MODULE_INIT_TRACE);
497 progname = basename(argv[0]);
498 qemu_init_exec_dir(argv[0]);
500 qcrypto_init(&error_fatal);
502 module_call_init(MODULE_INIT_QOM);
503 qemu_add_opts(&qemu_object_opts);
504 qemu_add_opts(&qemu_trace_opts);
505 bdrv_init();
507 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
508 switch (c) {
509 case 's':
510 flags |= BDRV_O_SNAPSHOT;
511 break;
512 case 'n':
513 flags |= BDRV_O_NOCACHE;
514 writethrough = false;
515 break;
516 case 'd':
517 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
518 error_report("Invalid discard option: %s", optarg);
519 exit(1);
521 break;
522 case 'f':
523 format = optarg;
524 break;
525 case 'c':
526 add_user_command(optarg);
527 break;
528 case 'r':
529 readonly = 1;
530 break;
531 case 'm':
532 qemuio_misalign = true;
533 break;
534 case 'k':
535 flags |= BDRV_O_NATIVE_AIO;
536 break;
537 case 't':
538 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
539 error_report("Invalid cache option: %s", optarg);
540 exit(1);
542 break;
543 case 'T':
544 g_free(trace_file);
545 trace_file = trace_opt_parse(optarg);
546 break;
547 case 'V':
548 printf("%s version %s\n", progname, QEMU_VERSION);
549 exit(0);
550 case 'h':
551 usage(progname);
552 exit(0);
553 case 'U':
554 force_share = true;
555 break;
556 case OPTION_OBJECT: {
557 QemuOpts *qopts;
558 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
559 optarg, true);
560 if (!qopts) {
561 exit(1);
563 } break;
564 case OPTION_IMAGE_OPTS:
565 imageOpts = true;
566 break;
567 default:
568 usage(progname);
569 exit(1);
573 if ((argc - optind) > 1) {
574 usage(progname);
575 exit(1);
578 if (format && imageOpts) {
579 error_report("--image-opts and -f are mutually exclusive");
580 exit(1);
583 if (qemu_init_main_loop(&local_error)) {
584 error_report_err(local_error);
585 exit(1);
588 if (qemu_opts_foreach(&qemu_object_opts,
589 user_creatable_add_opts_foreach,
590 NULL, NULL)) {
591 exit(1);
594 if (!trace_init_backends()) {
595 exit(1);
597 trace_init_file(trace_file);
598 qemu_set_log(LOG_TRACE);
600 /* initialize commands */
601 qemuio_add_command(&quit_cmd);
602 qemuio_add_command(&open_cmd);
603 qemuio_add_command(&close_cmd);
605 if (isatty(STDIN_FILENO)) {
606 readline_state = readline_init(readline_printf_func,
607 readline_flush_func,
608 NULL,
609 readline_completion_func);
610 qemu_set_tty_echo(STDIN_FILENO, false);
611 atexit(reenable_tty_echo);
614 /* open the device */
615 if (!readonly) {
616 flags |= BDRV_O_RDWR;
619 if ((argc - optind) == 1) {
620 if (imageOpts) {
621 QemuOpts *qopts = NULL;
622 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
623 if (!qopts) {
624 exit(1);
626 opts = qemu_opts_to_qdict(qopts, NULL);
627 if (openfile(NULL, flags, writethrough, force_share, opts)) {
628 exit(1);
630 } else {
631 if (format) {
632 opts = qdict_new();
633 qdict_put_str(opts, "driver", format);
635 if (openfile(argv[optind], flags, writethrough,
636 force_share, opts)) {
637 exit(1);
641 command_loop();
644 * Make sure all outstanding requests complete before the program exits.
646 bdrv_drain_all();
648 blk_unref(qemuio_blk);
649 g_free(readline_state);
650 return 0;