Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blob3548e20a39faae63c7d427c754a5dc79f04fa4b7
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 "qapi/qmp/qstring.h"
22 #include "qom/object_interfaces.h"
23 #include "sysemu/block-backend.h"
24 #include "block/block_int.h"
25 #include "trace/control.h"
26 #include "crypto/init.h"
28 #define CMD_NOFILE_OK 0x01
30 static char *progname;
32 static BlockBackend *qemuio_blk;
34 /* qemu-io commands passed using -c */
35 static int ncmdline;
36 static char **cmdline;
37 static bool imageOpts;
39 static ReadLineState *readline_state;
41 static int close_f(BlockBackend *blk, int argc, char **argv)
43 blk_unref(qemuio_blk);
44 qemuio_blk = NULL;
45 return 0;
48 static const cmdinfo_t close_cmd = {
49 .name = "close",
50 .altname = "c",
51 .cfunc = close_f,
52 .oneline = "close the current open file",
55 static int openfile(char *name, int flags, bool writethrough, QDict *opts)
57 Error *local_err = NULL;
58 BlockDriverState *bs;
60 if (qemuio_blk) {
61 error_report("file open already, try 'help close'");
62 QDECREF(opts);
63 return 1;
66 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
67 if (!qemuio_blk) {
68 error_reportf_err(local_err, "can't open%s%s: ",
69 name ? " device " : "", name ?: "");
70 return 1;
73 bs = blk_bs(qemuio_blk);
74 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs)) {
75 char password[256];
76 printf("Disk image '%s' is encrypted.\n", name);
77 if (qemu_read_password(password, sizeof(password)) < 0) {
78 error_report("No password given");
79 goto error;
81 if (bdrv_set_key(bs, password) < 0) {
82 error_report("invalid password");
83 goto error;
87 blk_set_enable_write_cache(qemuio_blk, !writethrough);
89 return 0;
91 error:
92 blk_unref(qemuio_blk);
93 qemuio_blk = NULL;
94 return 1;
97 static void open_help(void)
99 printf(
100 "\n"
101 " opens a new file in the requested mode\n"
102 "\n"
103 " Example:\n"
104 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
105 "\n"
106 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
107 " -r, -- open file read-only\n"
108 " -s, -- use snapshot file\n"
109 " -n, -- disable host cache, short for -t none\n"
110 " -k, -- use kernel AIO implementation (on Linux only)\n"
111 " -t, -- use the given cache mode for the image\n"
112 " -d, -- use the given discard mode for the image\n"
113 " -o, -- options to be given to the block driver"
114 "\n");
117 static int open_f(BlockBackend *blk, int argc, char **argv);
119 static const cmdinfo_t open_cmd = {
120 .name = "open",
121 .altname = "o",
122 .cfunc = open_f,
123 .argmin = 1,
124 .argmax = -1,
125 .flags = CMD_NOFILE_OK,
126 .args = "[-rsnk] [-t cache] [-d discard] [-o options] [path]",
127 .oneline = "open the file specified by path",
128 .help = open_help,
131 static QemuOptsList empty_opts = {
132 .name = "drive",
133 .merge_lists = true,
134 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
135 .desc = {
136 /* no elements => accept any params */
137 { /* end of list */ }
141 static int open_f(BlockBackend *blk, int argc, char **argv)
143 int flags = BDRV_O_UNMAP;
144 int readonly = 0;
145 bool writethrough = true;
146 int c;
147 QemuOpts *qopts;
148 QDict *opts;
150 while ((c = getopt(argc, argv, "snro:kt:d:")) != -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 'r':
160 readonly = 1;
161 break;
162 case 'k':
163 flags |= BDRV_O_NATIVE_AIO;
164 break;
165 case 't':
166 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
167 error_report("Invalid cache option: %s", optarg);
168 qemu_opts_reset(&empty_opts);
169 return 0;
171 break;
172 case 'd':
173 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
174 error_report("Invalid discard option: %s", optarg);
175 qemu_opts_reset(&empty_opts);
176 return 0;
178 break;
179 case 'o':
180 if (imageOpts) {
181 printf("--image-opts and 'open -o' are mutually exclusive\n");
182 qemu_opts_reset(&empty_opts);
183 return 0;
185 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
186 qemu_opts_reset(&empty_opts);
187 return 0;
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 return openfile(argv[optind], flags, writethrough, opts);
214 } else if (optind == argc) {
215 return openfile(NULL, flags, writethrough, opts);
216 } else {
217 QDECREF(opts);
218 return qemuio_command_usage(&open_cmd);
222 static int quit_f(BlockBackend *blk, int argc, char **argv)
224 return 1;
227 #if defined(CONFIG_FVD)
228 # include "qemu-io-sim.c"
229 #endif
231 static const cmdinfo_t quit_cmd = {
232 .name = "quit",
233 .altname = "q",
234 .cfunc = quit_f,
235 .argmin = -1,
236 .argmax = -1,
237 .flags = CMD_FLAG_GLOBAL,
238 .oneline = "exit the program",
241 static void usage(const char *name)
243 printf(
244 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
245 "QEMU Disk exerciser\n"
246 "\n"
247 " --object OBJECTDEF define an object such as 'secret' for\n"
248 " passwords and/or encryption keys\n"
249 " --image-opts treat file as option string\n"
250 " -c, --cmd STRING execute command with its arguments\n"
251 " from the given string\n"
252 " -f, --format FMT specifies the block driver to use\n"
253 " -r, --read-only export read-only\n"
254 " -s, --snapshot use snapshot file\n"
255 " -n, --nocache disable host cache, short for -t none\n"
256 " -m, --misalign misalign allocations for O_DIRECT\n"
257 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
258 " -t, --cache=MODE use the given cache mode for the image\n"
259 " -d, --discard=MODE use the given discard mode for the image\n"
260 " -T, --trace FILE enable trace events listed in the given file\n"
261 " -h, --help display this help and exit\n"
262 " -V, --version output version information and exit\n"
263 "\n"
264 "See '%s -c help' for information on available commands."
265 "\n",
266 name, name);
269 static char *get_prompt(void)
271 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
273 if (!prompt[0]) {
274 snprintf(prompt, sizeof(prompt), "%s> ", progname);
277 return prompt;
280 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
281 const char *fmt, ...)
283 va_list ap;
284 va_start(ap, fmt);
285 vprintf(fmt, ap);
286 va_end(ap);
289 static void readline_flush_func(void *opaque)
291 fflush(stdout);
294 static void readline_func(void *opaque, const char *str, void *readline_opaque)
296 char **line = readline_opaque;
297 *line = g_strdup(str);
300 static void completion_match(const char *cmd, void *opaque)
302 readline_add_completion(readline_state, cmd);
305 static void readline_completion_func(void *opaque, const char *str)
307 readline_set_completion_index(readline_state, strlen(str));
308 qemuio_complete_command(str, completion_match, NULL);
311 static char *fetchline_readline(void)
313 char *line = NULL;
315 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
316 while (!line) {
317 int ch = getchar();
318 if (ch == EOF) {
319 break;
321 readline_handle_byte(readline_state, ch);
323 return line;
326 #define MAXREADLINESZ 1024
327 static char *fetchline_fgets(void)
329 char *p, *line = g_malloc(MAXREADLINESZ);
331 if (!fgets(line, MAXREADLINESZ, stdin)) {
332 g_free(line);
333 return NULL;
336 p = line + strlen(line);
337 if (p != line && p[-1] == '\n') {
338 p[-1] = '\0';
341 return line;
344 static char *fetchline(void)
346 if (readline_state) {
347 return fetchline_readline();
348 } else {
349 return fetchline_fgets();
353 static void prep_fetchline(void *opaque)
355 int *fetchable = opaque;
357 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
358 *fetchable= 1;
361 static void command_loop(void)
363 int i, done = 0, fetchable = 0, prompted = 0;
364 char *input;
366 for (i = 0; !done && i < ncmdline; i++) {
367 done = qemuio_command(qemuio_blk, cmdline[i]);
369 if (cmdline) {
370 g_free(cmdline);
371 return;
374 while (!done) {
375 if (!prompted) {
376 printf("%s", get_prompt());
377 fflush(stdout);
378 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
379 prompted = 1;
382 main_loop_wait(false);
384 if (!fetchable) {
385 continue;
388 input = fetchline();
389 if (input == NULL) {
390 break;
392 done = qemuio_command(qemuio_blk, input);
393 g_free(input);
395 prompted = 0;
396 fetchable = 0;
398 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
401 static void add_user_command(char *optarg)
403 cmdline = g_renew(char *, cmdline, ++ncmdline);
404 cmdline[ncmdline-1] = optarg;
407 static void reenable_tty_echo(void)
409 qemu_set_tty_echo(STDIN_FILENO, true);
412 enum {
413 OPTION_OBJECT = 256,
414 OPTION_IMAGE_OPTS = 257,
417 static QemuOptsList qemu_object_opts = {
418 .name = "object",
419 .implied_opt_name = "qom-type",
420 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
421 .desc = {
427 static QemuOptsList file_opts = {
428 .name = "file",
429 .implied_opt_name = "file",
430 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
431 .desc = {
432 /* no elements => accept any params */
433 { /* end of list */ }
437 int main(int argc, char **argv)
439 int readonly = 0;
440 const char *sopt = "hVc:d:f:rsnmkt:T:";
441 const struct option lopt[] = {
442 { "help", no_argument, NULL, 'h' },
443 { "version", no_argument, NULL, 'V' },
444 { "cmd", required_argument, NULL, 'c' },
445 { "format", required_argument, NULL, 'f' },
446 { "read-only", no_argument, NULL, 'r' },
447 { "snapshot", no_argument, NULL, 's' },
448 { "nocache", no_argument, NULL, 'n' },
449 { "misalign", no_argument, NULL, 'm' },
450 { "native-aio", no_argument, NULL, 'k' },
451 { "discard", required_argument, NULL, 'd' },
452 { "cache", required_argument, NULL, 't' },
453 { "trace", required_argument, NULL, 'T' },
454 { "object", required_argument, NULL, OPTION_OBJECT },
455 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
456 { NULL, 0, NULL, 0 }
458 int c;
459 int opt_index = 0;
460 int flags = BDRV_O_UNMAP;
461 bool writethrough = true;
462 Error *local_error = NULL;
463 QDict *opts = NULL;
464 const char *format = NULL;
466 #ifdef CONFIG_POSIX
467 signal(SIGPIPE, SIG_IGN);
468 #endif
470 progname = basename(argv[0]);
471 qemu_init_exec_dir(argv[0]);
473 if (qcrypto_init(&local_error) < 0) {
474 error_reportf_err(local_error, "cannot initialize crypto: ");
475 exit(1);
478 module_call_init(MODULE_INIT_QOM);
479 qemu_add_opts(&qemu_object_opts);
480 bdrv_init();
482 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
483 switch (c) {
484 case 's':
485 flags |= BDRV_O_SNAPSHOT;
486 break;
487 case 'n':
488 flags |= BDRV_O_NOCACHE;
489 writethrough = false;
490 break;
491 case 'd':
492 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
493 error_report("Invalid discard option: %s", optarg);
494 exit(1);
496 break;
497 case 'f':
498 format = optarg;
499 break;
500 case 'c':
501 add_user_command(optarg);
502 break;
503 case 'r':
504 readonly = 1;
505 break;
506 case 'm':
507 qemuio_misalign = true;
508 break;
509 case 'k':
510 flags |= BDRV_O_NATIVE_AIO;
511 break;
512 case 't':
513 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
514 error_report("Invalid cache option: %s", optarg);
515 exit(1);
517 break;
518 case 'T':
519 if (!trace_init_backends()) {
520 exit(1); /* error message will have been printed */
522 break;
523 case 'V':
524 printf("%s version %s\n", progname, QEMU_VERSION);
525 exit(0);
526 case 'h':
527 usage(progname);
528 exit(0);
529 case OPTION_OBJECT: {
530 QemuOpts *qopts;
531 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
532 optarg, true);
533 if (!qopts) {
534 exit(1);
536 } break;
537 case OPTION_IMAGE_OPTS:
538 imageOpts = true;
539 break;
540 default:
541 usage(progname);
542 exit(1);
546 if ((argc - optind) > 1) {
547 usage(progname);
548 exit(1);
551 if (format && imageOpts) {
552 error_report("--image-opts and -f are mutually exclusive");
553 exit(1);
556 if (qemu_init_main_loop(&local_error)) {
557 error_report_err(local_error);
558 exit(1);
561 if (qemu_opts_foreach(&qemu_object_opts,
562 user_creatable_add_opts_foreach,
563 NULL, NULL)) {
564 exit(1);
567 /* initialize commands */
568 qemuio_add_command(&quit_cmd);
569 qemuio_add_command(&open_cmd);
570 qemuio_add_command(&close_cmd);
572 if (isatty(STDIN_FILENO)) {
573 readline_state = readline_init(readline_printf_func,
574 readline_flush_func,
575 NULL,
576 readline_completion_func);
577 qemu_set_tty_echo(STDIN_FILENO, false);
578 atexit(reenable_tty_echo);
581 /* open the device */
582 if (!readonly) {
583 flags |= BDRV_O_RDWR;
586 if ((argc - optind) == 1) {
587 if (imageOpts) {
588 QemuOpts *qopts = NULL;
589 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
590 if (!qopts) {
591 exit(1);
593 opts = qemu_opts_to_qdict(qopts, NULL);
594 openfile(NULL, flags, writethrough, opts);
595 } else {
596 if (format) {
597 opts = qdict_new();
598 qdict_put(opts, "driver", qstring_from_str(format));
600 openfile(argv[optind], flags, writethrough, opts);
603 command_loop();
606 * Make sure all outstanding requests complete before the program exits.
608 bdrv_drain_all();
610 blk_unref(qemuio_blk);
611 g_free(readline_state);
612 return 0;