master
c 696 lines 17.9 KB
Raw
1 /*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
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
11 #include "qemu/osdep.h"
12 #include <getopt.h>
13 #include <libgen.h>
14 #ifndef _WIN32
15 #include <termios.h>
16 #endif
17
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 "qobject/qstring.h"
31 #include "qobject/qdict.h"
32 #include "qom/object_interfaces.h"
33 #include "system/block-backend.h"
34 #include "block/block_int.h"
35 #include "trace/control.h"
36 #include "crypto/init.h"
37 #include "qemu-version.h"
38
39 #define CMD_NOFILE_OK 0x01
40
41 static BlockBackend *qemuio_blk;
42 static bool quit_qemu_io;
43
44 /* qemu-io commands passed using -c */
45 static int ncmdline;
46 static char **cmdline;
47 static bool imageOpts;
48
49 static ReadLineState *readline_state;
50
51 static int ttyEOF;
52
53 static int get_eof_char(void)
54 {
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 */
64 }
65 }
66
67 return tty.c_cc[VEOF];
68 #endif
69 }
70
71 static int close_f(BlockBackend *blk, int argc, char **argv, Error **errp)
72 {
73 blk_unref(qemuio_blk);
74 qemuio_blk = NULL;
75 return 0;
76 }
77
78 static const cmdinfo_t close_cmd = {
79 .name = "close",
80 .altname = "c",
81 .cfunc = close_f,
82 .oneline = "close the current open file",
83 };
84
85 static int openfile(char *name, int flags, bool writethrough, bool force_share,
86 QDict *opts)
87 {
88 Error *local_err = NULL;
89
90 if (qemuio_blk) {
91 error_report("file open already, try 'help close'");
92 qobject_unref(opts);
93 return 1;
94 }
95
96 if (force_share) {
97 if (!opts) {
98 opts = qdict_new();
99 }
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;
105 }
106 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
107 }
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;
113 }
114
115 blk_set_enable_write_cache(qemuio_blk, !writethrough);
116
117 return 0;
118 }
119
120 static void open_help(void)
121 {
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");
141 }
142
143 static int open_f(BlockBackend *blk, int argc, char **argv, Error **errp);
144
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,
155 };
156
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 */ }
164 },
165 };
166
167 static int open_f(BlockBackend *blk, int argc, char **argv, Error **errp)
168 {
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;
177
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;
201 }
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;
208 }
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;
215 }
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;
222 }
223 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
224 qemu_opts_reset(&empty_opts);
225 return -EINVAL;
226 }
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;
235 }
236 }
237
238 if (!readonly) {
239 flags |= BDRV_O_RDWR;
240 }
241
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;
246 }
247 optind++;
248 }
249
250 qopts = qemu_opts_find(&empty_opts, NULL);
251 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
252 qemu_opts_reset(&empty_opts);
253
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;
262 }
263
264 if (ret) {
265 return -EINVAL;
266 }
267
268 return 0;
269 }
270
271 static int quit_f(BlockBackend *blk, int argc, char **argv, Error **errp)
272 {
273 quit_qemu_io = true;
274 return 0;
275 }
276
277 static const cmdinfo_t quit_cmd = {
278 .name = "quit",
279 .altname = "q",
280 .cfunc = quit_f,
281 .argmin = -1,
282 .argmax = -1,
283 .flags = CMD_FLAG_GLOBAL,
284 .oneline = "exit the program",
285 };
286
287 static void usage(const char *name)
288 {
289 printf(
290 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
291 "QEMU Disk exerciser\n"
292 "\n"
293 " --object OBJECTDEF define an object such as 'secret' for\n"
294 " passwords and/or encryption keys\n"
295 " --image-opts treat file as option string\n"
296 " -c, --cmd STRING execute command with its arguments\n"
297 " from the given string\n"
298 " -f, --format FMT specifies the block driver to use\n"
299 " -r, --read-only export read-only\n"
300 " -s, --snapshot use snapshot file\n"
301 " -n, --nocache disable host cache, short for -t none\n"
302 " -C, --copy-on-read enable copy-on-read\n"
303 " -m, --misalign misalign allocations for O_DIRECT\n"
304 " -k, --native-aio use kernel AIO implementation\n"
305 " (Linux only, prefer use of -i)\n"
306 " -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
307 " -t, --cache=MODE use the given cache mode for the image\n"
308 " -d, --discard=MODE use the given discard mode for the image\n"
309 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
310 " specify tracing options\n"
311 " see qemu-img(1) man page for full description\n"
312 " -U, --force-share force shared permissions\n"
313 " -h, --help display this help and exit\n"
314 " -V, --version output version information and exit\n"
315 "\n"
316 "See '%s -c help' for information on available commands.\n"
317 "\n"
318 QEMU_HELP_BOTTOM "\n",
319 name, name);
320 }
321
322 static char *get_prompt(void)
323 {
324 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
325
326 if (!prompt[0]) {
327 snprintf(prompt, sizeof(prompt), "%s> ", g_get_prgname());
328 }
329
330 return prompt;
331 }
332
333 static void G_GNUC_PRINTF(2, 3) readline_printf_func(void *opaque,
334 const char *fmt, ...)
335 {
336 va_list ap;
337 va_start(ap, fmt);
338 vprintf(fmt, ap);
339 va_end(ap);
340 }
341
342 static void readline_flush_func(void *opaque)
343 {
344 fflush(stdout);
345 }
346
347 static void readline_func(void *opaque, const char *str, void *readline_opaque)
348 {
349 char **line = readline_opaque;
350 *line = g_strdup(str);
351 }
352
353 static void completion_match(const char *cmd, void *opaque)
354 {
355 readline_add_completion(readline_state, cmd);
356 }
357
358 static void readline_completion_func(void *opaque, const char *str)
359 {
360 readline_set_completion_index(readline_state, strlen(str));
361 qemuio_complete_command(str, completion_match, NULL);
362 }
363
364 static char *fetchline_readline(void)
365 {
366 char *line = NULL;
367
368 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
369 while (!line) {
370 int ch = getchar();
371 if (ttyEOF != 0x0 && ch == ttyEOF) {
372 printf("\n");
373 break;
374 }
375 readline_handle_byte(readline_state, ch);
376 }
377 return line;
378 }
379
380 #define MAXREADLINESZ 1024
381 static char *fetchline_fgets(void)
382 {
383 char *p, *line = g_malloc(MAXREADLINESZ);
384
385 if (!fgets(line, MAXREADLINESZ, stdin)) {
386 g_free(line);
387 return NULL;
388 }
389
390 p = line + strlen(line);
391 if (p != line && p[-1] == '\n') {
392 p[-1] = '\0';
393 }
394
395 return line;
396 }
397
398 static char *fetchline(void)
399 {
400 if (readline_state) {
401 return fetchline_readline();
402 } else {
403 return fetchline_fgets();
404 }
405 }
406
407 static void prep_fetchline(void *opaque)
408 {
409 int *fetchable = opaque;
410
411 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
412 *fetchable= 1;
413 }
414
415 static int do_qemuio_command(const char *cmd)
416 {
417 Error *local_err = NULL;
418 int ret;
419
420 ret = qemuio_command(qemuio_blk, cmd, &local_err);
421 if (local_err) {
422 error_report_err(local_err);
423 }
424 return ret;
425 }
426
427 static int command_loop(void)
428 {
429 int i, fetchable = 0, prompted = 0;
430 int ret, last_error = 0;
431 char *input;
432
433 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
434 ret = do_qemuio_command(cmdline[i]);
435 if (ret < 0) {
436 last_error = ret;
437 }
438 }
439 if (cmdline) {
440 g_free(cmdline);
441 return last_error;
442 }
443
444 while (!quit_qemu_io) {
445 if (!prompted) {
446 printf("%s", get_prompt());
447 fflush(stdout);
448 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
449 prompted = 1;
450 }
451
452 main_loop_wait(false);
453
454 if (!fetchable) {
455 continue;
456 }
457
458 input = fetchline();
459 if (input == NULL) {
460 break;
461 }
462 ret = do_qemuio_command(input);
463 g_free(input);
464
465 if (ret < 0) {
466 last_error = ret;
467 }
468
469 prompted = 0;
470 fetchable = 0;
471 }
472 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
473
474 return last_error;
475 }
476
477 static void add_user_command(char *user_cmd)
478 {
479 cmdline = g_renew(char *, cmdline, ++ncmdline);
480 cmdline[ncmdline - 1] = user_cmd;
481 }
482
483 static void reenable_tty_echo(void)
484 {
485 qemu_set_tty_echo(STDIN_FILENO, true);
486 }
487
488 enum {
489 OPTION_OBJECT = 256,
490 OPTION_IMAGE_OPTS = 257,
491 };
492
493 static QemuOptsList file_opts = {
494 .name = "file",
495 .implied_opt_name = "file",
496 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
497 .desc = {
498 /* no elements => accept any params */
499 { /* end of list */ }
500 },
501 };
502
503 int main(int argc, char **argv)
504 {
505 int readonly = 0;
506 const char *sopt = "hVc:d:f:rsnCmki:t:T:U";
507 const struct option lopt[] = {
508 { "help", no_argument, NULL, 'h' },
509 { "version", no_argument, NULL, 'V' },
510 { "cmd", required_argument, NULL, 'c' },
511 { "format", required_argument, NULL, 'f' },
512 { "read-only", no_argument, NULL, 'r' },
513 { "snapshot", no_argument, NULL, 's' },
514 { "nocache", no_argument, NULL, 'n' },
515 { "copy-on-read", no_argument, NULL, 'C' },
516 { "misalign", no_argument, NULL, 'm' },
517 { "native-aio", no_argument, NULL, 'k' },
518 { "aio", required_argument, NULL, 'i' },
519 { "discard", required_argument, NULL, 'd' },
520 { "cache", required_argument, NULL, 't' },
521 { "trace", required_argument, NULL, 'T' },
522 { "object", required_argument, NULL, OPTION_OBJECT },
523 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
524 { "force-share", no_argument, 0, 'U'},
525 { NULL, 0, NULL, 0 }
526 };
527 int c;
528 int opt_index = 0;
529 int flags = BDRV_O_UNMAP;
530 int ret;
531 bool writethrough = true;
532 QDict *opts = NULL;
533 const char *format = NULL;
534 bool force_share = false;
535
536 #ifdef CONFIG_POSIX
537 signal(SIGPIPE, SIG_IGN);
538 #endif
539
540 socket_init();
541 error_init(argv[0]);
542 module_call_init(MODULE_INIT_TRACE);
543 qemu_init_exec_dir(argv[0]);
544
545 qcrypto_init(&error_fatal);
546
547 module_call_init(MODULE_INIT_QOM);
548 qemu_add_opts(&qemu_trace_opts);
549 bdrv_init();
550
551 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
552 switch (c) {
553 case 's':
554 flags |= BDRV_O_SNAPSHOT;
555 break;
556 case 'n':
557 flags |= BDRV_O_NOCACHE;
558 writethrough = false;
559 break;
560 case 'C':
561 flags |= BDRV_O_COPY_ON_READ;
562 break;
563 case 'd':
564 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
565 error_report("Invalid discard option: %s", optarg);
566 exit(1);
567 }
568 break;
569 case 'f':
570 format = optarg;
571 break;
572 case 'c':
573 add_user_command(optarg);
574 break;
575 case 'r':
576 readonly = 1;
577 break;
578 case 'm':
579 qemuio_misalign = true;
580 break;
581 case 'k':
582 flags |= BDRV_O_NATIVE_AIO;
583 break;
584 case 'i':
585 if (bdrv_parse_aio(optarg, &flags) < 0) {
586 error_report("Invalid aio option: %s", optarg);
587 exit(1);
588 }
589 break;
590 case 't':
591 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
592 error_report("Invalid cache option: %s", optarg);
593 exit(1);
594 }
595 break;
596 case 'T':
597 trace_opt_parse(optarg);
598 break;
599 case 'V':
600 printf("%s version " QEMU_FULL_VERSION "\n"
601 QEMU_COPYRIGHT "\n", g_get_prgname());
602 exit(0);
603 case 'h':
604 usage(g_get_prgname());
605 exit(0);
606 case 'U':
607 force_share = true;
608 break;
609 case OPTION_OBJECT:
610 user_creatable_process_cmdline(optarg);
611 break;
612 case OPTION_IMAGE_OPTS:
613 imageOpts = true;
614 break;
615 default:
616 usage(g_get_prgname());
617 exit(1);
618 }
619 }
620
621 if ((argc - optind) > 1) {
622 usage(g_get_prgname());
623 exit(1);
624 }
625
626 if (format && imageOpts) {
627 error_report("--image-opts and -f are mutually exclusive");
628 exit(1);
629 }
630
631 qemu_init_main_loop(&error_fatal);
632
633 if (!trace_init_backends()) {
634 exit(1);
635 }
636 trace_init_file();
637 qemu_set_log(LOG_TRACE, &error_fatal);
638
639 /* initialize commands */
640 qemuio_add_command(&quit_cmd);
641 qemuio_add_command(&open_cmd);
642 qemuio_add_command(&close_cmd);
643
644 if (isatty(STDIN_FILENO)) {
645 ttyEOF = get_eof_char();
646 readline_state = readline_init(readline_printf_func,
647 readline_flush_func,
648 NULL,
649 readline_completion_func);
650 qemu_set_tty_echo(STDIN_FILENO, false);
651 atexit(reenable_tty_echo);
652 }
653
654 /* open the device */
655 if (!readonly) {
656 flags |= BDRV_O_RDWR;
657 }
658
659 if ((argc - optind) == 1) {
660 if (imageOpts) {
661 QemuOpts *qopts = NULL;
662 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
663 if (!qopts) {
664 exit(1);
665 }
666 opts = qemu_opts_to_qdict(qopts, NULL);
667 if (openfile(NULL, flags, writethrough, force_share, opts)) {
668 exit(1);
669 }
670 } else {
671 if (format) {
672 opts = qdict_new();
673 qdict_put_str(opts, "driver", format);
674 }
675 if (openfile(argv[optind], flags, writethrough,
676 force_share, opts)) {
677 exit(1);
678 }
679 }
680 }
681 ret = command_loop();
682
683 /*
684 * Make sure all outstanding requests complete before the program exits.
685 */
686 bdrv_drain_all();
687
688 blk_unref(qemuio_blk);
689 g_free(readline_state);
690
691 if (ret < 0) {
692 return 1;
693 } else {
694 return 0;
695 }
696 }