spapr_pci: handle FDT creation errors with _FDT()
[qemu.git] / qemu-io.c
blob265445ad8994589de1158863693aba029f20a9fc
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 " -n, -- disable host cache, short for -t none\n"
106 " -U, -- force shared permissions\n"
107 " -k, -- use kernel AIO implementation (on Linux only)\n"
108 " -t, -- use the given cache mode for the image\n"
109 " -d, -- use the given discard mode for the image\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 = "[-rsnkU] [-t cache] [-d discard] [-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 = BDRV_O_UNMAP;
141 int readonly = 0;
142 bool writethrough = true;
143 int c;
144 QemuOpts *qopts;
145 QDict *opts;
146 bool force_share = false;
148 while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) {
149 switch (c) {
150 case 's':
151 flags |= BDRV_O_SNAPSHOT;
152 break;
153 case 'n':
154 flags |= BDRV_O_NOCACHE;
155 writethrough = false;
156 break;
157 case 'r':
158 readonly = 1;
159 break;
160 case 'k':
161 flags |= BDRV_O_NATIVE_AIO;
162 break;
163 case 't':
164 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
165 error_report("Invalid cache option: %s", optarg);
166 qemu_opts_reset(&empty_opts);
167 return 0;
169 break;
170 case 'd':
171 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
172 error_report("Invalid discard option: %s", optarg);
173 qemu_opts_reset(&empty_opts);
174 return 0;
176 break;
177 case 'o':
178 if (imageOpts) {
179 printf("--image-opts and 'open -o' are mutually exclusive\n");
180 qemu_opts_reset(&empty_opts);
181 return 0;
183 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
184 qemu_opts_reset(&empty_opts);
185 return 0;
187 break;
188 case 'U':
189 force_share = true;
190 break;
191 default:
192 qemu_opts_reset(&empty_opts);
193 return qemuio_command_usage(&open_cmd);
197 if (!readonly) {
198 flags |= BDRV_O_RDWR;
201 if (imageOpts && (optind == argc - 1)) {
202 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
203 qemu_opts_reset(&empty_opts);
204 return 0;
206 optind++;
209 qopts = qemu_opts_find(&empty_opts, NULL);
210 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
211 qemu_opts_reset(&empty_opts);
213 if (optind == argc - 1) {
214 openfile(argv[optind], flags, writethrough, force_share, opts);
215 } else if (optind == argc) {
216 openfile(NULL, flags, writethrough, force_share, opts);
217 } else {
218 QDECREF(opts);
219 qemuio_command_usage(&open_cmd);
221 return 0;
224 static int quit_f(BlockBackend *blk, int argc, char **argv)
226 return 1;
229 static const cmdinfo_t quit_cmd = {
230 .name = "quit",
231 .altname = "q",
232 .cfunc = quit_f,
233 .argmin = -1,
234 .argmax = -1,
235 .flags = CMD_FLAG_GLOBAL,
236 .oneline = "exit the program",
239 static void usage(const char *name)
241 printf(
242 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
243 "QEMU Disk exerciser\n"
244 "\n"
245 " --object OBJECTDEF define an object such as 'secret' for\n"
246 " passwords and/or encryption keys\n"
247 " --image-opts treat file as option string\n"
248 " -c, --cmd STRING execute command with its arguments\n"
249 " from the given string\n"
250 " -f, --format FMT specifies the block driver to use\n"
251 " -r, --read-only export read-only\n"
252 " -s, --snapshot use snapshot file\n"
253 " -n, --nocache disable host cache, short for -t none\n"
254 " -m, --misalign misalign allocations for O_DIRECT\n"
255 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
256 " -t, --cache=MODE use the given cache mode for the image\n"
257 " -d, --discard=MODE use the given discard mode for the image\n"
258 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
259 " specify tracing options\n"
260 " see qemu-img(1) man page for full description\n"
261 " -U, --force-share force shared permissions\n"
262 " -h, --help display this help and exit\n"
263 " -V, --version output version information and exit\n"
264 "\n"
265 "See '%s -c help' for information on available commands.\n"
266 "\n"
267 QEMU_HELP_BOTTOM "\n",
268 name, name);
271 static char *get_prompt(void)
273 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
275 if (!prompt[0]) {
276 snprintf(prompt, sizeof(prompt), "%s> ", progname);
279 return prompt;
282 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
283 const char *fmt, ...)
285 va_list ap;
286 va_start(ap, fmt);
287 vprintf(fmt, ap);
288 va_end(ap);
291 static void readline_flush_func(void *opaque)
293 fflush(stdout);
296 static void readline_func(void *opaque, const char *str, void *readline_opaque)
298 char **line = readline_opaque;
299 *line = g_strdup(str);
302 static void completion_match(const char *cmd, void *opaque)
304 readline_add_completion(readline_state, cmd);
307 static void readline_completion_func(void *opaque, const char *str)
309 readline_set_completion_index(readline_state, strlen(str));
310 qemuio_complete_command(str, completion_match, NULL);
313 static char *fetchline_readline(void)
315 char *line = NULL;
317 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
318 while (!line) {
319 int ch = getchar();
320 if (ch == EOF) {
321 break;
323 readline_handle_byte(readline_state, ch);
325 return line;
328 #define MAXREADLINESZ 1024
329 static char *fetchline_fgets(void)
331 char *p, *line = g_malloc(MAXREADLINESZ);
333 if (!fgets(line, MAXREADLINESZ, stdin)) {
334 g_free(line);
335 return NULL;
338 p = line + strlen(line);
339 if (p != line && p[-1] == '\n') {
340 p[-1] = '\0';
343 return line;
346 static char *fetchline(void)
348 if (readline_state) {
349 return fetchline_readline();
350 } else {
351 return fetchline_fgets();
355 static void prep_fetchline(void *opaque)
357 int *fetchable = opaque;
359 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
360 *fetchable= 1;
363 static void command_loop(void)
365 int i, done = 0, fetchable = 0, prompted = 0;
366 char *input;
368 for (i = 0; !done && i < ncmdline; i++) {
369 done = qemuio_command(qemuio_blk, cmdline[i]);
371 if (cmdline) {
372 g_free(cmdline);
373 return;
376 while (!done) {
377 if (!prompted) {
378 printf("%s", get_prompt());
379 fflush(stdout);
380 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
381 prompted = 1;
384 main_loop_wait(false);
386 if (!fetchable) {
387 continue;
390 input = fetchline();
391 if (input == NULL) {
392 break;
394 done = qemuio_command(qemuio_blk, input);
395 g_free(input);
397 prompted = 0;
398 fetchable = 0;
400 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
403 static void add_user_command(char *optarg)
405 cmdline = g_renew(char *, cmdline, ++ncmdline);
406 cmdline[ncmdline-1] = optarg;
409 static void reenable_tty_echo(void)
411 qemu_set_tty_echo(STDIN_FILENO, true);
414 enum {
415 OPTION_OBJECT = 256,
416 OPTION_IMAGE_OPTS = 257,
419 static QemuOptsList qemu_object_opts = {
420 .name = "object",
421 .implied_opt_name = "qom-type",
422 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
423 .desc = {
429 static QemuOptsList file_opts = {
430 .name = "file",
431 .implied_opt_name = "file",
432 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
433 .desc = {
434 /* no elements => accept any params */
435 { /* end of list */ }
439 int main(int argc, char **argv)
441 int readonly = 0;
442 const char *sopt = "hVc:d:f:rsnmkt:T:U";
443 const struct option lopt[] = {
444 { "help", no_argument, NULL, 'h' },
445 { "version", no_argument, NULL, 'V' },
446 { "cmd", required_argument, NULL, 'c' },
447 { "format", required_argument, NULL, 'f' },
448 { "read-only", no_argument, NULL, 'r' },
449 { "snapshot", no_argument, NULL, 's' },
450 { "nocache", no_argument, NULL, 'n' },
451 { "misalign", no_argument, NULL, 'm' },
452 { "native-aio", no_argument, NULL, 'k' },
453 { "discard", required_argument, NULL, 'd' },
454 { "cache", required_argument, NULL, 't' },
455 { "trace", required_argument, NULL, 'T' },
456 { "object", required_argument, NULL, OPTION_OBJECT },
457 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
458 { "force-share", no_argument, 0, 'U'},
459 { NULL, 0, NULL, 0 }
461 int c;
462 int opt_index = 0;
463 int flags = BDRV_O_UNMAP;
464 bool writethrough = true;
465 Error *local_error = NULL;
466 QDict *opts = NULL;
467 const char *format = NULL;
468 char *trace_file = NULL;
469 bool force_share = false;
471 #ifdef CONFIG_POSIX
472 signal(SIGPIPE, SIG_IGN);
473 #endif
475 module_call_init(MODULE_INIT_TRACE);
476 progname = basename(argv[0]);
477 qemu_init_exec_dir(argv[0]);
479 qcrypto_init(&error_fatal);
481 module_call_init(MODULE_INIT_QOM);
482 qemu_add_opts(&qemu_object_opts);
483 qemu_add_opts(&qemu_trace_opts);
484 bdrv_init();
486 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
487 switch (c) {
488 case 's':
489 flags |= BDRV_O_SNAPSHOT;
490 break;
491 case 'n':
492 flags |= BDRV_O_NOCACHE;
493 writethrough = false;
494 break;
495 case 'd':
496 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
497 error_report("Invalid discard option: %s", optarg);
498 exit(1);
500 break;
501 case 'f':
502 format = optarg;
503 break;
504 case 'c':
505 add_user_command(optarg);
506 break;
507 case 'r':
508 readonly = 1;
509 break;
510 case 'm':
511 qemuio_misalign = true;
512 break;
513 case 'k':
514 flags |= BDRV_O_NATIVE_AIO;
515 break;
516 case 't':
517 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
518 error_report("Invalid cache option: %s", optarg);
519 exit(1);
521 break;
522 case 'T':
523 g_free(trace_file);
524 trace_file = trace_opt_parse(optarg);
525 break;
526 case 'V':
527 printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n"
528 QEMU_COPYRIGHT "\n", progname);
529 exit(0);
530 case 'h':
531 usage(progname);
532 exit(0);
533 case 'U':
534 force_share = true;
535 break;
536 case OPTION_OBJECT: {
537 QemuOpts *qopts;
538 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
539 optarg, true);
540 if (!qopts) {
541 exit(1);
543 } break;
544 case OPTION_IMAGE_OPTS:
545 imageOpts = true;
546 break;
547 default:
548 usage(progname);
549 exit(1);
553 if ((argc - optind) > 1) {
554 usage(progname);
555 exit(1);
558 if (format && imageOpts) {
559 error_report("--image-opts and -f are mutually exclusive");
560 exit(1);
563 if (qemu_init_main_loop(&local_error)) {
564 error_report_err(local_error);
565 exit(1);
568 if (qemu_opts_foreach(&qemu_object_opts,
569 user_creatable_add_opts_foreach,
570 NULL, NULL)) {
571 exit(1);
574 if (!trace_init_backends()) {
575 exit(1);
577 trace_init_file(trace_file);
578 qemu_set_log(LOG_TRACE);
580 /* initialize commands */
581 qemuio_add_command(&quit_cmd);
582 qemuio_add_command(&open_cmd);
583 qemuio_add_command(&close_cmd);
585 if (isatty(STDIN_FILENO)) {
586 readline_state = readline_init(readline_printf_func,
587 readline_flush_func,
588 NULL,
589 readline_completion_func);
590 qemu_set_tty_echo(STDIN_FILENO, false);
591 atexit(reenable_tty_echo);
594 /* open the device */
595 if (!readonly) {
596 flags |= BDRV_O_RDWR;
599 if ((argc - optind) == 1) {
600 if (imageOpts) {
601 QemuOpts *qopts = NULL;
602 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
603 if (!qopts) {
604 exit(1);
606 opts = qemu_opts_to_qdict(qopts, NULL);
607 if (openfile(NULL, flags, writethrough, force_share, opts)) {
608 exit(1);
610 } else {
611 if (format) {
612 opts = qdict_new();
613 qdict_put_str(opts, "driver", format);
615 if (openfile(argv[optind], flags, writethrough,
616 force_share, opts)) {
617 exit(1);
621 command_loop();
624 * Make sure all outstanding requests complete before the program exits.
626 bdrv_drain_all();
628 blk_unref(qemuio_blk);
629 g_free(readline_state);
630 return 0;