Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blob5ce38d9167dd526a547d727ab0042f734dc06535
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"
27 #define CMD_NOFILE_OK 0x01
29 static char *progname;
31 static BlockBackend *qemuio_blk;
33 /* qemu-io commands passed using -c */
34 static int ncmdline;
35 static char **cmdline;
36 static bool imageOpts;
38 static ReadLineState *readline_state;
40 static int close_f(BlockBackend *blk, int argc, char **argv)
42 blk_unref(qemuio_blk);
43 qemuio_blk = NULL;
44 return 0;
47 static const cmdinfo_t close_cmd = {
48 .name = "close",
49 .altname = "c",
50 .cfunc = close_f,
51 .oneline = "close the current open file",
54 static int openfile(char *name, int flags, bool writethrough, QDict *opts)
56 Error *local_err = NULL;
57 BlockDriverState *bs;
59 if (qemuio_blk) {
60 error_report("file open already, try 'help close'");
61 QDECREF(opts);
62 return 1;
65 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
66 if (!qemuio_blk) {
67 error_reportf_err(local_err, "can't open%s%s: ",
68 name ? " device " : "", name ?: "");
69 return 1;
72 bs = blk_bs(qemuio_blk);
73 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs)) {
74 char password[256];
75 printf("Disk image '%s' is encrypted.\n", name);
76 if (qemu_read_password(password, sizeof(password)) < 0) {
77 error_report("No password given");
78 goto error;
80 if (bdrv_set_key(bs, password) < 0) {
81 error_report("invalid password");
82 goto error;
86 blk_set_enable_write_cache(qemuio_blk, !writethrough);
88 return 0;
90 error:
91 blk_unref(qemuio_blk);
92 qemuio_blk = NULL;
93 return 1;
96 static void open_help(void)
98 printf(
99 "\n"
100 " opens a new file in the requested mode\n"
101 "\n"
102 " Example:\n"
103 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
104 "\n"
105 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
106 " -r, -- open file read-only\n"
107 " -s, -- use snapshot file\n"
108 " -n, -- disable host cache\n"
109 " -o, -- options to be given to the block driver"
110 "\n");
113 static int open_f(BlockBackend *blk, int argc, char **argv);
115 static const cmdinfo_t open_cmd = {
116 .name = "open",
117 .altname = "o",
118 .cfunc = open_f,
119 .argmin = 1,
120 .argmax = -1,
121 .flags = CMD_NOFILE_OK,
122 .args = "[-Crsn] [-o options] [path]",
123 .oneline = "open the file specified by path",
124 .help = open_help,
127 static QemuOptsList empty_opts = {
128 .name = "drive",
129 .merge_lists = true,
130 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
131 .desc = {
132 /* no elements => accept any params */
133 { /* end of list */ }
137 static int open_f(BlockBackend *blk, int argc, char **argv)
139 int flags = 0;
140 int readonly = 0;
141 bool writethrough = true;
142 int c;
143 QemuOpts *qopts;
144 QDict *opts;
146 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
147 switch (c) {
148 case 's':
149 flags |= BDRV_O_SNAPSHOT;
150 break;
151 case 'n':
152 flags |= BDRV_O_NOCACHE;
153 writethrough = false;
154 break;
155 case 'r':
156 readonly = 1;
157 break;
158 case 'o':
159 if (imageOpts) {
160 printf("--image-opts and 'open -o' are mutually exclusive\n");
161 return 0;
163 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
164 qemu_opts_reset(&empty_opts);
165 return 0;
167 break;
168 default:
169 qemu_opts_reset(&empty_opts);
170 return qemuio_command_usage(&open_cmd);
174 if (!readonly) {
175 flags |= BDRV_O_RDWR;
178 if (imageOpts && (optind == argc - 1)) {
179 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
180 qemu_opts_reset(&empty_opts);
181 return 0;
183 optind++;
186 qopts = qemu_opts_find(&empty_opts, NULL);
187 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
188 qemu_opts_reset(&empty_opts);
190 if (optind == argc - 1) {
191 return openfile(argv[optind], flags, writethrough, opts);
192 } else if (optind == argc) {
193 return openfile(NULL, flags, writethrough, opts);
194 } else {
195 QDECREF(opts);
196 return qemuio_command_usage(&open_cmd);
200 static int quit_f(BlockBackend *blk, int argc, char **argv)
202 return 1;
205 #if defined(CONFIG_FVD)
206 # include "qemu-io-sim.c"
207 #endif
209 static const cmdinfo_t quit_cmd = {
210 .name = "quit",
211 .altname = "q",
212 .cfunc = quit_f,
213 .argmin = -1,
214 .argmax = -1,
215 .flags = CMD_FLAG_GLOBAL,
216 .oneline = "exit the program",
219 static void usage(const char *name)
221 printf(
222 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
223 "QEMU Disk exerciser\n"
224 "\n"
225 " --object OBJECTDEF define an object such as 'secret' for\n"
226 " passwords and/or encryption keys\n"
227 " -c, --cmd STRING execute command with its arguments\n"
228 " from the given string\n"
229 " -f, --format FMT specifies the block driver to use\n"
230 " -r, --read-only export read-only\n"
231 " -s, --snapshot use snapshot file\n"
232 " -n, --nocache disable host cache\n"
233 " -m, --misalign misalign allocations for O_DIRECT\n"
234 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
235 " -t, --cache=MODE use the given cache mode for the image\n"
236 " -T, --trace FILE enable trace events listed in the given file\n"
237 " -h, --help display this help and exit\n"
238 " -V, --version output version information and exit\n"
239 "\n"
240 "See '%s -c help' for information on available commands."
241 "\n",
242 name, name);
245 static char *get_prompt(void)
247 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
249 if (!prompt[0]) {
250 snprintf(prompt, sizeof(prompt), "%s> ", progname);
253 return prompt;
256 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
257 const char *fmt, ...)
259 va_list ap;
260 va_start(ap, fmt);
261 vprintf(fmt, ap);
262 va_end(ap);
265 static void readline_flush_func(void *opaque)
267 fflush(stdout);
270 static void readline_func(void *opaque, const char *str, void *readline_opaque)
272 char **line = readline_opaque;
273 *line = g_strdup(str);
276 static void completion_match(const char *cmd, void *opaque)
278 readline_add_completion(readline_state, cmd);
281 static void readline_completion_func(void *opaque, const char *str)
283 readline_set_completion_index(readline_state, strlen(str));
284 qemuio_complete_command(str, completion_match, NULL);
287 static char *fetchline_readline(void)
289 char *line = NULL;
291 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
292 while (!line) {
293 int ch = getchar();
294 if (ch == EOF) {
295 break;
297 readline_handle_byte(readline_state, ch);
299 return line;
302 #define MAXREADLINESZ 1024
303 static char *fetchline_fgets(void)
305 char *p, *line = g_malloc(MAXREADLINESZ);
307 if (!fgets(line, MAXREADLINESZ, stdin)) {
308 g_free(line);
309 return NULL;
312 p = line + strlen(line);
313 if (p != line && p[-1] == '\n') {
314 p[-1] = '\0';
317 return line;
320 static char *fetchline(void)
322 if (readline_state) {
323 return fetchline_readline();
324 } else {
325 return fetchline_fgets();
329 static void prep_fetchline(void *opaque)
331 int *fetchable = opaque;
333 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
334 *fetchable= 1;
337 static void command_loop(void)
339 int i, done = 0, fetchable = 0, prompted = 0;
340 char *input;
342 for (i = 0; !done && i < ncmdline; i++) {
343 done = qemuio_command(qemuio_blk, cmdline[i]);
345 if (cmdline) {
346 g_free(cmdline);
347 return;
350 while (!done) {
351 if (!prompted) {
352 printf("%s", get_prompt());
353 fflush(stdout);
354 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
355 prompted = 1;
358 main_loop_wait(false);
360 if (!fetchable) {
361 continue;
364 input = fetchline();
365 if (input == NULL) {
366 break;
368 done = qemuio_command(qemuio_blk, input);
369 g_free(input);
371 prompted = 0;
372 fetchable = 0;
374 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
377 static void add_user_command(char *optarg)
379 cmdline = g_renew(char *, cmdline, ++ncmdline);
380 cmdline[ncmdline-1] = optarg;
383 static void reenable_tty_echo(void)
385 qemu_set_tty_echo(STDIN_FILENO, true);
388 enum {
389 OPTION_OBJECT = 256,
390 OPTION_IMAGE_OPTS = 257,
393 static QemuOptsList qemu_object_opts = {
394 .name = "object",
395 .implied_opt_name = "qom-type",
396 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
397 .desc = {
403 static QemuOptsList file_opts = {
404 .name = "file",
405 .implied_opt_name = "file",
406 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
407 .desc = {
408 /* no elements => accept any params */
409 { /* end of list */ }
413 int main(int argc, char **argv)
415 int readonly = 0;
416 const char *sopt = "hVc:d:f:rsnmgkt:T:";
417 const struct option lopt[] = {
418 { "help", no_argument, NULL, 'h' },
419 { "version", no_argument, NULL, 'V' },
420 { "offset", required_argument, NULL, 'o' },
421 { "cmd", required_argument, NULL, 'c' },
422 { "format", required_argument, NULL, 'f' },
423 { "read-only", no_argument, NULL, 'r' },
424 { "snapshot", no_argument, NULL, 's' },
425 { "nocache", no_argument, NULL, 'n' },
426 { "misalign", no_argument, NULL, 'm' },
427 { "native-aio", no_argument, NULL, 'k' },
428 { "discard", required_argument, NULL, 'd' },
429 { "cache", required_argument, NULL, 't' },
430 { "trace", required_argument, NULL, 'T' },
431 { "object", required_argument, NULL, OPTION_OBJECT },
432 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
433 { NULL, 0, NULL, 0 }
435 int c;
436 int opt_index = 0;
437 int flags = BDRV_O_UNMAP;
438 bool writethrough = true;
439 Error *local_error = NULL;
440 QDict *opts = NULL;
441 const char *format = NULL;
443 #ifdef CONFIG_POSIX
444 signal(SIGPIPE, SIG_IGN);
445 #endif
447 progname = basename(argv[0]);
448 qemu_init_exec_dir(argv[0]);
450 module_call_init(MODULE_INIT_QOM);
451 qemu_add_opts(&qemu_object_opts);
452 bdrv_init();
454 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
455 switch (c) {
456 case 's':
457 flags |= BDRV_O_SNAPSHOT;
458 break;
459 case 'n':
460 flags |= BDRV_O_NOCACHE;
461 writethrough = false;
462 break;
463 case 'd':
464 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
465 error_report("Invalid discard option: %s", optarg);
466 exit(1);
468 break;
469 case 'f':
470 format = optarg;
471 break;
472 case 'c':
473 add_user_command(optarg);
474 break;
475 case 'r':
476 readonly = 1;
477 break;
478 case 'm':
479 qemuio_misalign = true;
480 break;
481 case 'k':
482 flags |= BDRV_O_NATIVE_AIO;
483 break;
484 case 't':
485 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
486 error_report("Invalid cache option: %s", optarg);
487 exit(1);
489 break;
490 case 'T':
491 if (!trace_init_backends()) {
492 exit(1); /* error message will have been printed */
494 break;
495 case 'V':
496 printf("%s version %s\n", progname, QEMU_VERSION);
497 exit(0);
498 case 'h':
499 usage(progname);
500 exit(0);
501 case OPTION_OBJECT: {
502 QemuOpts *qopts;
503 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
504 optarg, true);
505 if (!qopts) {
506 exit(1);
508 } break;
509 case OPTION_IMAGE_OPTS:
510 imageOpts = true;
511 break;
512 default:
513 usage(progname);
514 exit(1);
518 if ((argc - optind) > 1) {
519 usage(progname);
520 exit(1);
523 if (format && imageOpts) {
524 error_report("--image-opts and -f are mutually exclusive");
525 exit(1);
528 if (qemu_init_main_loop(&local_error)) {
529 error_report_err(local_error);
530 exit(1);
533 if (qemu_opts_foreach(&qemu_object_opts,
534 user_creatable_add_opts_foreach,
535 NULL, &local_error)) {
536 error_report_err(local_error);
537 exit(1);
540 /* initialize commands */
541 qemuio_add_command(&quit_cmd);
542 qemuio_add_command(&open_cmd);
543 qemuio_add_command(&close_cmd);
545 if (isatty(STDIN_FILENO)) {
546 readline_state = readline_init(readline_printf_func,
547 readline_flush_func,
548 NULL,
549 readline_completion_func);
550 qemu_set_tty_echo(STDIN_FILENO, false);
551 atexit(reenable_tty_echo);
554 /* open the device */
555 if (!readonly) {
556 flags |= BDRV_O_RDWR;
559 if ((argc - optind) == 1) {
560 if (imageOpts) {
561 QemuOpts *qopts = NULL;
562 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
563 if (!qopts) {
564 exit(1);
566 opts = qemu_opts_to_qdict(qopts, NULL);
567 openfile(NULL, flags, writethrough, opts);
568 } else {
569 if (format) {
570 opts = qdict_new();
571 qdict_put(opts, "driver", qstring_from_str(format));
573 openfile(argv[optind], flags, writethrough, opts);
576 command_loop();
579 * Make sure all outstanding requests complete before the program exits.
581 bdrv_drain_all();
583 blk_unref(qemuio_blk);
584 g_free(readline_state);
585 return 0;