docs: Fix typo (wphx => whpx)
[qemu/ar7.git] / qemu-io.c
blob52e5736e4f7bc11cfec4ebc8c30d1b134ad7589c
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 */
11 #include "qemu/osdep.h"
12 #include <getopt.h>
13 #include <libgen.h>
14 #ifndef _WIN32
15 #include <termios.h>
16 #endif
18 #include "qemu/help-texts.h"
19 #include "qemu/cutils.h"
20 #include "qapi/error.h"
21 #include "qemu-io.h"
22 #include "qemu/error-report.h"
23 #include "qemu/main-loop.h"
24 #include "qemu/module.h"
25 #include "qemu/option.h"
26 #include "qemu/config-file.h"
27 #include "qemu/readline.h"
28 #include "qemu/log.h"
29 #include "qemu/sockets.h"
30 #include "qapi/qmp/qstring.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qom/object_interfaces.h"
33 #include "sysemu/block-backend.h"
34 #include "block/block_int.h"
35 #include "trace/control.h"
36 #include "crypto/init.h"
37 #include "qemu-version.h"
39 #define CMD_NOFILE_OK 0x01
41 static BlockBackend *qemuio_blk;
42 static bool quit_qemu_io;
44 /* qemu-io commands passed using -c */
45 static int ncmdline;
46 static char **cmdline;
47 static bool imageOpts;
49 static ReadLineState *readline_state;
51 static int ttyEOF;
53 static int get_eof_char(void)
55 #ifdef _WIN32
56 return 0x4; /* Ctrl-D */
57 #else
58 struct termios tty;
59 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
60 if (errno == ENOTTY) {
61 return 0x0; /* just expect read() == 0 */
62 } else {
63 return 0x4; /* Ctrl-D */
67 return tty.c_cc[VEOF];
68 #endif
71 static int close_f(BlockBackend *blk, int argc, char **argv)
73 blk_unref(qemuio_blk);
74 qemuio_blk = NULL;
75 return 0;
78 static const cmdinfo_t close_cmd = {
79 .name = "close",
80 .altname = "c",
81 .cfunc = close_f,
82 .oneline = "close the current open file",
85 static int openfile(char *name, int flags, bool writethrough, bool force_share,
86 QDict *opts)
88 Error *local_err = NULL;
90 if (qemuio_blk) {
91 error_report("file open already, try 'help close'");
92 qobject_unref(opts);
93 return 1;
96 if (force_share) {
97 if (!opts) {
98 opts = qdict_new();
100 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
101 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
102 error_report("-U conflicts with image options");
103 qobject_unref(opts);
104 return 1;
106 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
108 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
109 if (!qemuio_blk) {
110 error_reportf_err(local_err, "can't open%s%s: ",
111 name ? " device " : "", name ?: "");
112 return 1;
115 blk_set_enable_write_cache(qemuio_blk, !writethrough);
117 return 0;
120 static void open_help(void)
122 printf(
123 "\n"
124 " opens a new file in the requested mode\n"
125 "\n"
126 " Example:\n"
127 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
128 "\n"
129 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
130 " -r, -- open file read-only\n"
131 " -s, -- use snapshot file\n"
132 " -C, -- use copy-on-read\n"
133 " -n, -- disable host cache, short for -t none\n"
134 " -U, -- force shared permissions\n"
135 " -k, -- use kernel AIO implementation (Linux only, prefer use of -i)\n"
136 " -i, -- use AIO mode (threads, native or io_uring)\n"
137 " -t, -- use the given cache mode for the image\n"
138 " -d, -- use the given discard mode for the image\n"
139 " -o, -- options to be given to the block driver"
140 "\n");
143 static int open_f(BlockBackend *blk, int argc, char **argv);
145 static const cmdinfo_t open_cmd = {
146 .name = "open",
147 .altname = "o",
148 .cfunc = open_f,
149 .argmin = 1,
150 .argmax = -1,
151 .flags = CMD_NOFILE_OK,
152 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
153 .oneline = "open the file specified by path",
154 .help = open_help,
157 static QemuOptsList empty_opts = {
158 .name = "drive",
159 .merge_lists = true,
160 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
161 .desc = {
162 /* no elements => accept any params */
163 { /* end of list */ }
167 static int open_f(BlockBackend *blk, int argc, char **argv)
169 int flags = BDRV_O_UNMAP;
170 int readonly = 0;
171 bool writethrough = true;
172 int c;
173 int ret;
174 QemuOpts *qopts;
175 QDict *opts;
176 bool force_share = false;
178 while ((c = getopt(argc, argv, "snCro:ki:t:d:U")) != -1) {
179 switch (c) {
180 case 's':
181 flags |= BDRV_O_SNAPSHOT;
182 break;
183 case 'n':
184 flags |= BDRV_O_NOCACHE;
185 writethrough = false;
186 break;
187 case 'C':
188 flags |= BDRV_O_COPY_ON_READ;
189 break;
190 case 'r':
191 readonly = 1;
192 break;
193 case 'k':
194 flags |= BDRV_O_NATIVE_AIO;
195 break;
196 case 't':
197 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
198 error_report("Invalid cache option: %s", optarg);
199 qemu_opts_reset(&empty_opts);
200 return -EINVAL;
202 break;
203 case 'd':
204 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
205 error_report("Invalid discard option: %s", optarg);
206 qemu_opts_reset(&empty_opts);
207 return -EINVAL;
209 break;
210 case 'i':
211 if (bdrv_parse_aio(optarg, &flags) < 0) {
212 error_report("Invalid aio option: %s", optarg);
213 qemu_opts_reset(&empty_opts);
214 return -EINVAL;
216 break;
217 case 'o':
218 if (imageOpts) {
219 printf("--image-opts and 'open -o' are mutually exclusive\n");
220 qemu_opts_reset(&empty_opts);
221 return -EINVAL;
223 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
224 qemu_opts_reset(&empty_opts);
225 return -EINVAL;
227 break;
228 case 'U':
229 force_share = true;
230 break;
231 default:
232 qemu_opts_reset(&empty_opts);
233 qemuio_command_usage(&open_cmd);
234 return -EINVAL;
238 if (!readonly) {
239 flags |= BDRV_O_RDWR;
242 if (imageOpts && (optind == argc - 1)) {
243 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
244 qemu_opts_reset(&empty_opts);
245 return -EINVAL;
247 optind++;
250 qopts = qemu_opts_find(&empty_opts, NULL);
251 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
252 qemu_opts_reset(&empty_opts);
254 if (optind == argc - 1) {
255 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
256 } else if (optind == argc) {
257 ret = openfile(NULL, flags, writethrough, force_share, opts);
258 } else {
259 qobject_unref(opts);
260 qemuio_command_usage(&open_cmd);
261 return -EINVAL;
264 if (ret) {
265 return -EINVAL;
268 return 0;
271 static int quit_f(BlockBackend *blk, int argc, char **argv)
273 quit_qemu_io = true;
274 return 0;
277 #if defined(CONFIG_FVD)
278 # include "qemu-io-sim.c"
279 #endif
281 static const cmdinfo_t quit_cmd = {
282 .name = "quit",
283 .altname = "q",
284 .cfunc = quit_f,
285 .argmin = -1,
286 .argmax = -1,
287 .flags = CMD_FLAG_GLOBAL,
288 .oneline = "exit the program",
291 static void usage(const char *name)
293 printf(
294 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
295 "QEMU Disk exerciser\n"
296 "\n"
297 " --object OBJECTDEF define an object such as 'secret' for\n"
298 " passwords and/or encryption keys\n"
299 " --image-opts treat file as option string\n"
300 " -c, --cmd STRING execute command with its arguments\n"
301 " from the given string\n"
302 " -f, --format FMT specifies the block driver to use\n"
303 " -r, --read-only export read-only\n"
304 " -s, --snapshot use snapshot file\n"
305 " -n, --nocache disable host cache, short for -t none\n"
306 " -C, --copy-on-read enable copy-on-read\n"
307 " -m, --misalign misalign allocations for O_DIRECT\n"
308 " -k, --native-aio use kernel AIO implementation\n"
309 " (Linux only, prefer use of -i)\n"
310 " -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
311 " -t, --cache=MODE use the given cache mode for the image\n"
312 " -d, --discard=MODE use the given discard mode for the image\n"
313 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
314 " specify tracing options\n"
315 " see qemu-img(1) man page for full description\n"
316 " -U, --force-share force shared permissions\n"
317 " -h, --help display this help and exit\n"
318 " -V, --version output version information and exit\n"
319 "\n"
320 "See '%s -c help' for information on available commands.\n"
321 "\n"
322 QEMU_HELP_BOTTOM "\n",
323 name, name);
326 static char *get_prompt(void)
328 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
330 if (!prompt[0]) {
331 snprintf(prompt, sizeof(prompt), "%s> ", g_get_prgname());
334 return prompt;
337 static void G_GNUC_PRINTF(2, 3) readline_printf_func(void *opaque,
338 const char *fmt, ...)
340 va_list ap;
341 va_start(ap, fmt);
342 vprintf(fmt, ap);
343 va_end(ap);
346 static void readline_flush_func(void *opaque)
348 fflush(stdout);
351 static void readline_func(void *opaque, const char *str, void *readline_opaque)
353 char **line = readline_opaque;
354 *line = g_strdup(str);
357 static void completion_match(const char *cmd, void *opaque)
359 readline_add_completion(readline_state, cmd);
362 static void readline_completion_func(void *opaque, const char *str)
364 readline_set_completion_index(readline_state, strlen(str));
365 qemuio_complete_command(str, completion_match, NULL);
368 static char *fetchline_readline(void)
370 char *line = NULL;
372 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
373 while (!line) {
374 int ch = getchar();
375 if (ttyEOF != 0x0 && ch == ttyEOF) {
376 printf("\n");
377 break;
379 readline_handle_byte(readline_state, ch);
381 return line;
384 #define MAXREADLINESZ 1024
385 static char *fetchline_fgets(void)
387 char *p, *line = g_malloc(MAXREADLINESZ);
389 if (!fgets(line, MAXREADLINESZ, stdin)) {
390 g_free(line);
391 return NULL;
394 p = line + strlen(line);
395 if (p != line && p[-1] == '\n') {
396 p[-1] = '\0';
399 return line;
402 static char *fetchline(void)
404 if (readline_state) {
405 return fetchline_readline();
406 } else {
407 return fetchline_fgets();
411 static void prep_fetchline(void *opaque)
413 int *fetchable = opaque;
415 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
416 *fetchable= 1;
419 static int do_qemuio_command(const char *cmd)
421 int ret;
422 AioContext *ctx =
423 qemuio_blk ? blk_get_aio_context(qemuio_blk) : qemu_get_aio_context();
425 aio_context_acquire(ctx);
426 ret = qemuio_command(qemuio_blk, cmd);
427 aio_context_release(ctx);
429 return ret;
432 static int command_loop(void)
434 int i, fetchable = 0, prompted = 0;
435 int ret, last_error = 0;
436 char *input;
438 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
439 ret = do_qemuio_command(cmdline[i]);
440 if (ret < 0) {
441 last_error = ret;
444 if (cmdline) {
445 g_free(cmdline);
446 return last_error;
449 while (!quit_qemu_io) {
450 if (!prompted) {
451 printf("%s", get_prompt());
452 fflush(stdout);
453 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
454 prompted = 1;
457 main_loop_wait(false);
459 if (!fetchable) {
460 continue;
463 input = fetchline();
464 if (input == NULL) {
465 break;
467 ret = do_qemuio_command(input);
468 g_free(input);
470 if (ret < 0) {
471 last_error = ret;
474 prompted = 0;
475 fetchable = 0;
477 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
479 return last_error;
482 static void add_user_command(char *optarg)
484 cmdline = g_renew(char *, cmdline, ++ncmdline);
485 cmdline[ncmdline-1] = optarg;
488 static void reenable_tty_echo(void)
490 qemu_set_tty_echo(STDIN_FILENO, true);
493 enum {
494 OPTION_OBJECT = 256,
495 OPTION_IMAGE_OPTS = 257,
498 static QemuOptsList file_opts = {
499 .name = "file",
500 .implied_opt_name = "file",
501 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
502 .desc = {
503 /* no elements => accept any params */
504 { /* end of list */ }
508 int main(int argc, char **argv)
510 int readonly = 0;
511 const char *sopt = "hVc:d:f:rsnCmki:t:T:U";
512 const struct option lopt[] = {
513 { "help", no_argument, NULL, 'h' },
514 { "version", no_argument, NULL, 'V' },
515 { "cmd", required_argument, NULL, 'c' },
516 { "format", required_argument, NULL, 'f' },
517 { "read-only", no_argument, NULL, 'r' },
518 { "snapshot", no_argument, NULL, 's' },
519 { "nocache", no_argument, NULL, 'n' },
520 { "copy-on-read", no_argument, NULL, 'C' },
521 { "misalign", no_argument, NULL, 'm' },
522 { "native-aio", no_argument, NULL, 'k' },
523 { "aio", required_argument, NULL, 'i' },
524 { "discard", required_argument, NULL, 'd' },
525 { "cache", required_argument, NULL, 't' },
526 { "trace", required_argument, NULL, 'T' },
527 { "object", required_argument, NULL, OPTION_OBJECT },
528 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
529 { "force-share", no_argument, 0, 'U'},
530 { NULL, 0, NULL, 0 }
532 int c;
533 int opt_index = 0;
534 int flags = BDRV_O_UNMAP;
535 int ret;
536 bool writethrough = true;
537 QDict *opts = NULL;
538 const char *format = NULL;
539 bool force_share = false;
541 #ifdef CONFIG_POSIX
542 signal(SIGPIPE, SIG_IGN);
543 #endif
545 socket_init();
546 error_init(argv[0]);
547 module_call_init(MODULE_INIT_TRACE);
548 qemu_init_exec_dir(argv[0]);
550 qcrypto_init(&error_fatal);
552 module_call_init(MODULE_INIT_QOM);
553 qemu_add_opts(&qemu_trace_opts);
554 bdrv_init();
556 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
557 switch (c) {
558 case 's':
559 flags |= BDRV_O_SNAPSHOT;
560 break;
561 case 'n':
562 flags |= BDRV_O_NOCACHE;
563 writethrough = false;
564 break;
565 case 'C':
566 flags |= BDRV_O_COPY_ON_READ;
567 break;
568 case 'd':
569 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
570 error_report("Invalid discard option: %s", optarg);
571 exit(1);
573 break;
574 case 'f':
575 format = optarg;
576 break;
577 case 'c':
578 add_user_command(optarg);
579 break;
580 case 'r':
581 readonly = 1;
582 break;
583 case 'm':
584 qemuio_misalign = true;
585 break;
586 case 'k':
587 flags |= BDRV_O_NATIVE_AIO;
588 break;
589 case 'i':
590 if (bdrv_parse_aio(optarg, &flags) < 0) {
591 error_report("Invalid aio option: %s", optarg);
592 exit(1);
594 break;
595 case 't':
596 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
597 error_report("Invalid cache option: %s", optarg);
598 exit(1);
600 break;
601 case 'T':
602 trace_opt_parse(optarg);
603 break;
604 case 'V':
605 printf("%s version " QEMU_FULL_VERSION "\n"
606 QEMU_COPYRIGHT "\n", g_get_prgname());
607 exit(0);
608 case 'h':
609 usage(g_get_prgname());
610 exit(0);
611 case 'U':
612 force_share = true;
613 break;
614 case OPTION_OBJECT:
615 user_creatable_process_cmdline(optarg);
616 break;
617 case OPTION_IMAGE_OPTS:
618 imageOpts = true;
619 break;
620 default:
621 usage(g_get_prgname());
622 exit(1);
626 if ((argc - optind) > 1) {
627 usage(g_get_prgname());
628 exit(1);
631 if (format && imageOpts) {
632 error_report("--image-opts and -f are mutually exclusive");
633 exit(1);
636 qemu_init_main_loop(&error_fatal);
638 if (!trace_init_backends()) {
639 exit(1);
641 trace_init_file();
642 qemu_set_log(LOG_TRACE, &error_fatal);
644 /* initialize commands */
645 qemuio_add_command(&quit_cmd);
646 qemuio_add_command(&open_cmd);
647 qemuio_add_command(&close_cmd);
649 if (isatty(STDIN_FILENO)) {
650 ttyEOF = get_eof_char();
651 readline_state = readline_init(readline_printf_func,
652 readline_flush_func,
653 NULL,
654 readline_completion_func);
655 qemu_set_tty_echo(STDIN_FILENO, false);
656 atexit(reenable_tty_echo);
659 /* open the device */
660 if (!readonly) {
661 flags |= BDRV_O_RDWR;
664 if ((argc - optind) == 1) {
665 if (imageOpts) {
666 QemuOpts *qopts = NULL;
667 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
668 if (!qopts) {
669 exit(1);
671 opts = qemu_opts_to_qdict(qopts, NULL);
672 if (openfile(NULL, flags, writethrough, force_share, opts)) {
673 exit(1);
675 } else {
676 if (format) {
677 opts = qdict_new();
678 qdict_put_str(opts, "driver", format);
680 if (openfile(argv[optind], flags, writethrough,
681 force_share, opts)) {
682 exit(1);
686 ret = command_loop();
689 * Make sure all outstanding requests complete before the program exits.
691 bdrv_drain_all();
693 blk_unref(qemuio_blk);
694 g_free(readline_state);
696 if (ret < 0) {
697 return 1;
698 } else {
699 return 0;