Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blobb564d5d9b116861d1280691204175bad0861bdfc
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 -Cn /tmp/data' - creates/opens data file read-write and 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\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 = "[-Crsn] [-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 = 0;
141 int readonly = 0;
142 bool writethrough = true;
143 int c;
144 QemuOpts *qopts;
145 QDict *opts;
147 while ((c = getopt(argc, argv, "snrgo:")) != -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 'o':
160 if (imageOpts) {
161 printf("--image-opts and 'open -o' are mutually exclusive\n");
162 return 0;
164 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
165 qemu_opts_reset(&empty_opts);
166 return 0;
168 break;
169 default:
170 qemu_opts_reset(&empty_opts);
171 return qemuio_command_usage(&open_cmd);
175 if (!readonly) {
176 flags |= BDRV_O_RDWR;
179 if (imageOpts && (optind == argc - 1)) {
180 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
181 qemu_opts_reset(&empty_opts);
182 return 0;
184 optind++;
187 qopts = qemu_opts_find(&empty_opts, NULL);
188 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
189 qemu_opts_reset(&empty_opts);
191 if (optind == argc - 1) {
192 return openfile(argv[optind], flags, writethrough, opts);
193 } else if (optind == argc) {
194 return openfile(NULL, flags, writethrough, opts);
195 } else {
196 QDECREF(opts);
197 return qemuio_command_usage(&open_cmd);
201 static int quit_f(BlockBackend *blk, int argc, char **argv)
203 return 1;
206 #if defined(CONFIG_FVD)
207 # include "qemu-io-sim.c"
208 #endif
210 static const cmdinfo_t quit_cmd = {
211 .name = "quit",
212 .altname = "q",
213 .cfunc = quit_f,
214 .argmin = -1,
215 .argmax = -1,
216 .flags = CMD_FLAG_GLOBAL,
217 .oneline = "exit the program",
220 static void usage(const char *name)
222 printf(
223 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
224 "QEMU Disk exerciser\n"
225 "\n"
226 " --object OBJECTDEF define an object such as 'secret' for\n"
227 " passwords and/or encryption keys\n"
228 " -c, --cmd STRING execute command with its arguments\n"
229 " from the given string\n"
230 " -f, --format FMT specifies the block driver to use\n"
231 " -r, --read-only export read-only\n"
232 " -s, --snapshot use snapshot file\n"
233 " -n, --nocache disable host cache\n"
234 " -m, --misalign misalign allocations for O_DIRECT\n"
235 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
236 " -t, --cache=MODE use the given cache mode for the image\n"
237 " -T, --trace FILE enable trace events listed in the given file\n"
238 " -h, --help display this help and exit\n"
239 " -V, --version output version information and exit\n"
240 "\n"
241 "See '%s -c help' for information on available commands."
242 "\n",
243 name, name);
246 static char *get_prompt(void)
248 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
250 if (!prompt[0]) {
251 snprintf(prompt, sizeof(prompt), "%s> ", progname);
254 return prompt;
257 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
258 const char *fmt, ...)
260 va_list ap;
261 va_start(ap, fmt);
262 vprintf(fmt, ap);
263 va_end(ap);
266 static void readline_flush_func(void *opaque)
268 fflush(stdout);
271 static void readline_func(void *opaque, const char *str, void *readline_opaque)
273 char **line = readline_opaque;
274 *line = g_strdup(str);
277 static void completion_match(const char *cmd, void *opaque)
279 readline_add_completion(readline_state, cmd);
282 static void readline_completion_func(void *opaque, const char *str)
284 readline_set_completion_index(readline_state, strlen(str));
285 qemuio_complete_command(str, completion_match, NULL);
288 static char *fetchline_readline(void)
290 char *line = NULL;
292 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
293 while (!line) {
294 int ch = getchar();
295 if (ch == EOF) {
296 break;
298 readline_handle_byte(readline_state, ch);
300 return line;
303 #define MAXREADLINESZ 1024
304 static char *fetchline_fgets(void)
306 char *p, *line = g_malloc(MAXREADLINESZ);
308 if (!fgets(line, MAXREADLINESZ, stdin)) {
309 g_free(line);
310 return NULL;
313 p = line + strlen(line);
314 if (p != line && p[-1] == '\n') {
315 p[-1] = '\0';
318 return line;
321 static char *fetchline(void)
323 if (readline_state) {
324 return fetchline_readline();
325 } else {
326 return fetchline_fgets();
330 static void prep_fetchline(void *opaque)
332 int *fetchable = opaque;
334 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
335 *fetchable= 1;
338 static void command_loop(void)
340 int i, done = 0, fetchable = 0, prompted = 0;
341 char *input;
343 for (i = 0; !done && i < ncmdline; i++) {
344 done = qemuio_command(qemuio_blk, cmdline[i]);
346 if (cmdline) {
347 g_free(cmdline);
348 return;
351 while (!done) {
352 if (!prompted) {
353 printf("%s", get_prompt());
354 fflush(stdout);
355 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
356 prompted = 1;
359 main_loop_wait(false);
361 if (!fetchable) {
362 continue;
365 input = fetchline();
366 if (input == NULL) {
367 break;
369 done = qemuio_command(qemuio_blk, input);
370 g_free(input);
372 prompted = 0;
373 fetchable = 0;
375 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
378 static void add_user_command(char *optarg)
380 cmdline = g_renew(char *, cmdline, ++ncmdline);
381 cmdline[ncmdline-1] = optarg;
384 static void reenable_tty_echo(void)
386 qemu_set_tty_echo(STDIN_FILENO, true);
389 enum {
390 OPTION_OBJECT = 256,
391 OPTION_IMAGE_OPTS = 257,
394 static QemuOptsList qemu_object_opts = {
395 .name = "object",
396 .implied_opt_name = "qom-type",
397 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
398 .desc = {
404 static QemuOptsList file_opts = {
405 .name = "file",
406 .implied_opt_name = "file",
407 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
408 .desc = {
409 /* no elements => accept any params */
410 { /* end of list */ }
414 int main(int argc, char **argv)
416 int readonly = 0;
417 const char *sopt = "hVc:d:f:rsnmgkt:T:";
418 const struct option lopt[] = {
419 { "help", no_argument, NULL, 'h' },
420 { "version", no_argument, NULL, 'V' },
421 { "offset", required_argument, NULL, 'o' },
422 { "cmd", required_argument, NULL, 'c' },
423 { "format", required_argument, NULL, 'f' },
424 { "read-only", no_argument, NULL, 'r' },
425 { "snapshot", no_argument, NULL, 's' },
426 { "nocache", no_argument, NULL, 'n' },
427 { "misalign", no_argument, NULL, 'm' },
428 { "native-aio", no_argument, NULL, 'k' },
429 { "discard", required_argument, NULL, 'd' },
430 { "cache", required_argument, NULL, 't' },
431 { "trace", required_argument, NULL, 'T' },
432 { "object", required_argument, NULL, OPTION_OBJECT },
433 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
434 { NULL, 0, NULL, 0 }
436 int c;
437 int opt_index = 0;
438 int flags = BDRV_O_UNMAP;
439 bool writethrough = true;
440 Error *local_error = NULL;
441 QDict *opts = NULL;
442 const char *format = NULL;
444 #ifdef CONFIG_POSIX
445 signal(SIGPIPE, SIG_IGN);
446 #endif
448 progname = basename(argv[0]);
449 qemu_init_exec_dir(argv[0]);
451 if (qcrypto_init(&local_error) < 0) {
452 error_reportf_err(local_error, "cannot initialize crypto: ");
453 exit(1);
456 module_call_init(MODULE_INIT_QOM);
457 qemu_add_opts(&qemu_object_opts);
458 bdrv_init();
460 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
461 switch (c) {
462 case 's':
463 flags |= BDRV_O_SNAPSHOT;
464 break;
465 case 'n':
466 flags |= BDRV_O_NOCACHE;
467 writethrough = false;
468 break;
469 case 'd':
470 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
471 error_report("Invalid discard option: %s", optarg);
472 exit(1);
474 break;
475 case 'f':
476 format = optarg;
477 break;
478 case 'c':
479 add_user_command(optarg);
480 break;
481 case 'r':
482 readonly = 1;
483 break;
484 case 'm':
485 qemuio_misalign = true;
486 break;
487 case 'k':
488 flags |= BDRV_O_NATIVE_AIO;
489 break;
490 case 't':
491 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
492 error_report("Invalid cache option: %s", optarg);
493 exit(1);
495 break;
496 case 'T':
497 if (!trace_init_backends()) {
498 exit(1); /* error message will have been printed */
500 break;
501 case 'V':
502 printf("%s version %s\n", progname, QEMU_VERSION);
503 exit(0);
504 case 'h':
505 usage(progname);
506 exit(0);
507 case OPTION_OBJECT: {
508 QemuOpts *qopts;
509 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
510 optarg, true);
511 if (!qopts) {
512 exit(1);
514 } break;
515 case OPTION_IMAGE_OPTS:
516 imageOpts = true;
517 break;
518 default:
519 usage(progname);
520 exit(1);
524 if ((argc - optind) > 1) {
525 usage(progname);
526 exit(1);
529 if (format && imageOpts) {
530 error_report("--image-opts and -f are mutually exclusive");
531 exit(1);
534 if (qemu_init_main_loop(&local_error)) {
535 error_report_err(local_error);
536 exit(1);
539 if (qemu_opts_foreach(&qemu_object_opts,
540 user_creatable_add_opts_foreach,
541 NULL, NULL)) {
542 exit(1);
545 /* initialize commands */
546 qemuio_add_command(&quit_cmd);
547 qemuio_add_command(&open_cmd);
548 qemuio_add_command(&close_cmd);
550 if (isatty(STDIN_FILENO)) {
551 readline_state = readline_init(readline_printf_func,
552 readline_flush_func,
553 NULL,
554 readline_completion_func);
555 qemu_set_tty_echo(STDIN_FILENO, false);
556 atexit(reenable_tty_echo);
559 /* open the device */
560 if (!readonly) {
561 flags |= BDRV_O_RDWR;
564 if ((argc - optind) == 1) {
565 if (imageOpts) {
566 QemuOpts *qopts = NULL;
567 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
568 if (!qopts) {
569 exit(1);
571 opts = qemu_opts_to_qdict(qopts, NULL);
572 openfile(NULL, flags, writethrough, opts);
573 } else {
574 if (format) {
575 opts = qdict_new();
576 qdict_put(opts, "driver", qstring_from_str(format));
578 openfile(argv[optind], flags, writethrough, opts);
581 command_loop();
584 * Make sure all outstanding requests complete before the program exits.
586 bdrv_drain_all();
588 blk_unref(qemuio_blk);
589 g_free(readline_state);
590 return 0;