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 "qapi/error.h" 19 #include "qemu-io.h" 20 #include "qemu/error-report.h" 21 #include "qemu/main-loop.h" 22 #include "qemu/option.h" 23 #include "qemu/config-file.h" 24 #include "qemu/readline.h" 25 #include "qemu/log.h" 26 #include "qapi/qmp/qstring.h" 27 #include "qapi/qmp/qdict.h" 28 #include "qom/object_interfaces.h" 29 #include "sysemu/block-backend.h" 30 #include "block/block_int.h" 31 #include "trace/control.h" 32 #include "crypto/init.h" 33 #include "qemu-version.h" 34 35 #define CMD_NOFILE_OK 0x01 36 37 static char *progname; 38 39 static BlockBackend *qemuio_blk; 40 static bool quit_qemu_io; 41 42 /* qemu-io commands passed using -c */ 43 static int ncmdline; 44 static char **cmdline; 45 static bool imageOpts; 46 47 static ReadLineState *readline_state; 48 49 static int ttyEOF; 50 51 static int get_eof_char(void) 52 { 53 #ifdef _WIN32 54 return 0x4; /* Ctrl-D */ 55 #else 56 struct termios tty; 57 if (tcgetattr(STDIN_FILENO, &tty) != 0) { 58 if (errno == ENOTTY) { 59 return 0x0; /* just expect read() == 0 */ 60 } else { 61 return 0x4; /* Ctrl-D */ 62 } 63 } 64 65 return tty.c_cc[VEOF]; 66 #endif 67 } 68 69 static int close_f(BlockBackend *blk, int argc, char **argv) 70 { 71 blk_unref(qemuio_blk); 72 qemuio_blk = NULL; 73 return 0; 74 } 75 76 static const cmdinfo_t close_cmd = { 77 .name = "close", 78 .altname = "c", 79 .cfunc = close_f, 80 .oneline = "close the current open file", 81 }; 82 83 static int openfile(char *name, int flags, bool writethrough, bool force_share, 84 QDict *opts) 85 { 86 Error *local_err = NULL; 87 88 if (qemuio_blk) { 89 error_report("file open already, try 'help close'"); 90 qobject_unref(opts); 91 return 1; 92 } 93 94 if (force_share) { 95 if (!opts) { 96 opts = qdict_new(); 97 } 98 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE) 99 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) { 100 error_report("-U conflicts with image options"); 101 qobject_unref(opts); 102 return 1; 103 } 104 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on"); 105 } 106 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err); 107 if (!qemuio_blk) { 108 error_reportf_err(local_err, "can't open%s%s: ", 109 name ? " device " : "", name ?: ""); 110 return 1; 111 } 112 113 blk_set_enable_write_cache(qemuio_blk, !writethrough); 114 115 return 0; 116 } 117 118 static void open_help(void) 119 { 120 printf( 121 "\n" 122 " opens a new file in the requested mode\n" 123 "\n" 124 " Example:\n" 125 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n" 126 "\n" 127 " Opens a file for subsequent use by all of the other qemu-io commands.\n" 128 " -r, -- open file read-only\n" 129 " -s, -- use snapshot file\n" 130 " -C, -- use copy-on-read\n" 131 " -n, -- disable host cache, short for -t none\n" 132 " -U, -- force shared permissions\n" 133 " -k, -- use kernel AIO implementation (on Linux only)\n" 134 " -t, -- use the given cache mode for the image\n" 135 " -d, -- use the given discard mode for the image\n" 136 " -o, -- options to be given to the block driver" 137 "\n"); 138 } 139 140 static int open_f(BlockBackend *blk, int argc, char **argv); 141 142 static const cmdinfo_t open_cmd = { 143 .name = "open", 144 .altname = "o", 145 .cfunc = open_f, 146 .argmin = 1, 147 .argmax = -1, 148 .flags = CMD_NOFILE_OK, 149 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]", 150 .oneline = "open the file specified by path", 151 .help = open_help, 152 }; 153 154 static QemuOptsList empty_opts = { 155 .name = "drive", 156 .merge_lists = true, 157 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head), 158 .desc = { 159 /* no elements => accept any params */ 160 { /* end of list */ } 161 }, 162 }; 163 164 static int open_f(BlockBackend *blk, int argc, char **argv) 165 { 166 int flags = BDRV_O_UNMAP; 167 int readonly = 0; 168 bool writethrough = true; 169 int c; 170 int ret; 171 QemuOpts *qopts; 172 QDict *opts; 173 bool force_share = false; 174 175 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) { 176 switch (c) { 177 case 's': 178 flags |= BDRV_O_SNAPSHOT; 179 break; 180 case 'n': 181 flags |= BDRV_O_NOCACHE; 182 writethrough = false; 183 break; 184 case 'C': 185 flags |= BDRV_O_COPY_ON_READ; 186 break; 187 case 'r': 188 readonly = 1; 189 break; 190 case 'k': 191 flags |= BDRV_O_NATIVE_AIO; 192 break; 193 case 't': 194 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { 195 error_report("Invalid cache option: %s", optarg); 196 qemu_opts_reset(&empty_opts); 197 return -EINVAL; 198 } 199 break; 200 case 'd': 201 if (bdrv_parse_discard_flags(optarg, &flags) < 0) { 202 error_report("Invalid discard option: %s", optarg); 203 qemu_opts_reset(&empty_opts); 204 return -EINVAL; 205 } 206 break; 207 case 'o': 208 if (imageOpts) { 209 printf("--image-opts and 'open -o' are mutually exclusive\n"); 210 qemu_opts_reset(&empty_opts); 211 return -EINVAL; 212 } 213 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) { 214 qemu_opts_reset(&empty_opts); 215 return -EINVAL; 216 } 217 break; 218 case 'U': 219 force_share = true; 220 break; 221 default: 222 qemu_opts_reset(&empty_opts); 223 qemuio_command_usage(&open_cmd); 224 return -EINVAL; 225 } 226 } 227 228 if (!readonly) { 229 flags |= BDRV_O_RDWR; 230 } 231 232 if (imageOpts && (optind == argc - 1)) { 233 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) { 234 qemu_opts_reset(&empty_opts); 235 return -EINVAL; 236 } 237 optind++; 238 } 239 240 qopts = qemu_opts_find(&empty_opts, NULL); 241 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; 242 qemu_opts_reset(&empty_opts); 243 244 if (optind == argc - 1) { 245 ret = openfile(argv[optind], flags, writethrough, force_share, opts); 246 } else if (optind == argc) { 247 ret = openfile(NULL, flags, writethrough, force_share, opts); 248 } else { 249 qobject_unref(opts); 250 qemuio_command_usage(&open_cmd); 251 return -EINVAL; 252 } 253 254 if (ret) { 255 return -EINVAL; 256 } 257 258 return 0; 259 } 260 261 static int quit_f(BlockBackend *blk, int argc, char **argv) 262 { 263 quit_qemu_io = true; 264 return 0; 265 } 266 267 static const cmdinfo_t quit_cmd = { 268 .name = "quit", 269 .altname = "q", 270 .cfunc = quit_f, 271 .argmin = -1, 272 .argmax = -1, 273 .flags = CMD_FLAG_GLOBAL, 274 .oneline = "exit the program", 275 }; 276 277 static void usage(const char *name) 278 { 279 printf( 280 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n" 281 "QEMU Disk exerciser\n" 282 "\n" 283 " --object OBJECTDEF define an object such as 'secret' for\n" 284 " passwords and/or encryption keys\n" 285 " --image-opts treat file as option string\n" 286 " -c, --cmd STRING execute command with its arguments\n" 287 " from the given string\n" 288 " -f, --format FMT specifies the block driver to use\n" 289 " -r, --read-only export read-only\n" 290 " -s, --snapshot use snapshot file\n" 291 " -n, --nocache disable host cache, short for -t none\n" 292 " -C, --copy-on-read enable copy-on-read\n" 293 " -m, --misalign misalign allocations for O_DIRECT\n" 294 " -k, --native-aio use kernel AIO implementation (on Linux only)\n" 295 " -t, --cache=MODE use the given cache mode for the image\n" 296 " -d, --discard=MODE use the given discard mode for the image\n" 297 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" 298 " specify tracing options\n" 299 " see qemu-img(1) man page for full description\n" 300 " -U, --force-share force shared permissions\n" 301 " -h, --help display this help and exit\n" 302 " -V, --version output version information and exit\n" 303 "\n" 304 "See '%s -c help' for information on available commands.\n" 305 "\n" 306 QEMU_HELP_BOTTOM "\n", 307 name, name); 308 } 309 310 static char *get_prompt(void) 311 { 312 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; 313 314 if (!prompt[0]) { 315 snprintf(prompt, sizeof(prompt), "%s> ", progname); 316 } 317 318 return prompt; 319 } 320 321 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, 322 const char *fmt, ...) 323 { 324 va_list ap; 325 va_start(ap, fmt); 326 vprintf(fmt, ap); 327 va_end(ap); 328 } 329 330 static void readline_flush_func(void *opaque) 331 { 332 fflush(stdout); 333 } 334 335 static void readline_func(void *opaque, const char *str, void *readline_opaque) 336 { 337 char **line = readline_opaque; 338 *line = g_strdup(str); 339 } 340 341 static void completion_match(const char *cmd, void *opaque) 342 { 343 readline_add_completion(readline_state, cmd); 344 } 345 346 static void readline_completion_func(void *opaque, const char *str) 347 { 348 readline_set_completion_index(readline_state, strlen(str)); 349 qemuio_complete_command(str, completion_match, NULL); 350 } 351 352 static char *fetchline_readline(void) 353 { 354 char *line = NULL; 355 356 readline_start(readline_state, get_prompt(), 0, readline_func, &line); 357 while (!line) { 358 int ch = getchar(); 359 if (ttyEOF != 0x0 && ch == ttyEOF) { 360 printf("\n"); 361 break; 362 } 363 readline_handle_byte(readline_state, ch); 364 } 365 return line; 366 } 367 368 #define MAXREADLINESZ 1024 369 static char *fetchline_fgets(void) 370 { 371 char *p, *line = g_malloc(MAXREADLINESZ); 372 373 if (!fgets(line, MAXREADLINESZ, stdin)) { 374 g_free(line); 375 return NULL; 376 } 377 378 p = line + strlen(line); 379 if (p != line && p[-1] == '\n') { 380 p[-1] = '\0'; 381 } 382 383 return line; 384 } 385 386 static char *fetchline(void) 387 { 388 if (readline_state) { 389 return fetchline_readline(); 390 } else { 391 return fetchline_fgets(); 392 } 393 } 394 395 static void prep_fetchline(void *opaque) 396 { 397 int *fetchable = opaque; 398 399 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); 400 *fetchable= 1; 401 } 402 403 static void command_loop(void) 404 { 405 int i, fetchable = 0, prompted = 0; 406 char *input; 407 408 for (i = 0; !quit_qemu_io && i < ncmdline; i++) { 409 qemuio_command(qemuio_blk, cmdline[i]); 410 } 411 if (cmdline) { 412 g_free(cmdline); 413 return; 414 } 415 416 while (!quit_qemu_io) { 417 if (!prompted) { 418 printf("%s", get_prompt()); 419 fflush(stdout); 420 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable); 421 prompted = 1; 422 } 423 424 main_loop_wait(false); 425 426 if (!fetchable) { 427 continue; 428 } 429 430 input = fetchline(); 431 if (input == NULL) { 432 break; 433 } 434 qemuio_command(qemuio_blk, input); 435 g_free(input); 436 437 prompted = 0; 438 fetchable = 0; 439 } 440 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); 441 } 442 443 static void add_user_command(char *optarg) 444 { 445 cmdline = g_renew(char *, cmdline, ++ncmdline); 446 cmdline[ncmdline-1] = optarg; 447 } 448 449 static void reenable_tty_echo(void) 450 { 451 qemu_set_tty_echo(STDIN_FILENO, true); 452 } 453 454 enum { 455 OPTION_OBJECT = 256, 456 OPTION_IMAGE_OPTS = 257, 457 }; 458 459 static QemuOptsList qemu_object_opts = { 460 .name = "object", 461 .implied_opt_name = "qom-type", 462 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), 463 .desc = { 464 { } 465 }, 466 }; 467 468 469 static QemuOptsList file_opts = { 470 .name = "file", 471 .implied_opt_name = "file", 472 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head), 473 .desc = { 474 /* no elements => accept any params */ 475 { /* end of list */ } 476 }, 477 }; 478 479 int main(int argc, char **argv) 480 { 481 int readonly = 0; 482 const char *sopt = "hVc:d:f:rsnCmkt:T:U"; 483 const struct option lopt[] = { 484 { "help", no_argument, NULL, 'h' }, 485 { "version", no_argument, NULL, 'V' }, 486 { "cmd", required_argument, NULL, 'c' }, 487 { "format", required_argument, NULL, 'f' }, 488 { "read-only", no_argument, NULL, 'r' }, 489 { "snapshot", no_argument, NULL, 's' }, 490 { "nocache", no_argument, NULL, 'n' }, 491 { "copy-on-read", no_argument, NULL, 'C' }, 492 { "misalign", no_argument, NULL, 'm' }, 493 { "native-aio", no_argument, NULL, 'k' }, 494 { "discard", required_argument, NULL, 'd' }, 495 { "cache", required_argument, NULL, 't' }, 496 { "trace", required_argument, NULL, 'T' }, 497 { "object", required_argument, NULL, OPTION_OBJECT }, 498 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS }, 499 { "force-share", no_argument, 0, 'U'}, 500 { NULL, 0, NULL, 0 } 501 }; 502 int c; 503 int opt_index = 0; 504 int flags = BDRV_O_UNMAP; 505 bool writethrough = true; 506 Error *local_error = NULL; 507 QDict *opts = NULL; 508 const char *format = NULL; 509 char *trace_file = NULL; 510 bool force_share = false; 511 512 #ifdef CONFIG_POSIX 513 signal(SIGPIPE, SIG_IGN); 514 #endif 515 516 module_call_init(MODULE_INIT_TRACE); 517 progname = g_path_get_basename(argv[0]); 518 qemu_init_exec_dir(argv[0]); 519 520 qcrypto_init(&error_fatal); 521 522 module_call_init(MODULE_INIT_QOM); 523 qemu_add_opts(&qemu_object_opts); 524 qemu_add_opts(&qemu_trace_opts); 525 bdrv_init(); 526 527 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { 528 switch (c) { 529 case 's': 530 flags |= BDRV_O_SNAPSHOT; 531 break; 532 case 'n': 533 flags |= BDRV_O_NOCACHE; 534 writethrough = false; 535 break; 536 case 'C': 537 flags |= BDRV_O_COPY_ON_READ; 538 break; 539 case 'd': 540 if (bdrv_parse_discard_flags(optarg, &flags) < 0) { 541 error_report("Invalid discard option: %s", optarg); 542 exit(1); 543 } 544 break; 545 case 'f': 546 format = optarg; 547 break; 548 case 'c': 549 add_user_command(optarg); 550 break; 551 case 'r': 552 readonly = 1; 553 break; 554 case 'm': 555 qemuio_misalign = true; 556 break; 557 case 'k': 558 flags |= BDRV_O_NATIVE_AIO; 559 break; 560 case 't': 561 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { 562 error_report("Invalid cache option: %s", optarg); 563 exit(1); 564 } 565 break; 566 case 'T': 567 g_free(trace_file); 568 trace_file = trace_opt_parse(optarg); 569 break; 570 case 'V': 571 printf("%s version " QEMU_FULL_VERSION "\n" 572 QEMU_COPYRIGHT "\n", progname); 573 exit(0); 574 case 'h': 575 usage(progname); 576 exit(0); 577 case 'U': 578 force_share = true; 579 break; 580 case OPTION_OBJECT: { 581 QemuOpts *qopts; 582 qopts = qemu_opts_parse_noisily(&qemu_object_opts, 583 optarg, true); 584 if (!qopts) { 585 exit(1); 586 } 587 } break; 588 case OPTION_IMAGE_OPTS: 589 imageOpts = true; 590 break; 591 default: 592 usage(progname); 593 exit(1); 594 } 595 } 596 597 if ((argc - optind) > 1) { 598 usage(progname); 599 exit(1); 600 } 601 602 if (format && imageOpts) { 603 error_report("--image-opts and -f are mutually exclusive"); 604 exit(1); 605 } 606 607 if (qemu_init_main_loop(&local_error)) { 608 error_report_err(local_error); 609 exit(1); 610 } 611 612 if (qemu_opts_foreach(&qemu_object_opts, 613 user_creatable_add_opts_foreach, 614 NULL, NULL)) { 615 exit(1); 616 } 617 618 if (!trace_init_backends()) { 619 exit(1); 620 } 621 trace_init_file(trace_file); 622 qemu_set_log(LOG_TRACE); 623 624 /* initialize commands */ 625 qemuio_add_command(&quit_cmd); 626 qemuio_add_command(&open_cmd); 627 qemuio_add_command(&close_cmd); 628 629 if (isatty(STDIN_FILENO)) { 630 ttyEOF = get_eof_char(); 631 readline_state = readline_init(readline_printf_func, 632 readline_flush_func, 633 NULL, 634 readline_completion_func); 635 qemu_set_tty_echo(STDIN_FILENO, false); 636 atexit(reenable_tty_echo); 637 } 638 639 /* open the device */ 640 if (!readonly) { 641 flags |= BDRV_O_RDWR; 642 } 643 644 if ((argc - optind) == 1) { 645 if (imageOpts) { 646 QemuOpts *qopts = NULL; 647 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false); 648 if (!qopts) { 649 exit(1); 650 } 651 opts = qemu_opts_to_qdict(qopts, NULL); 652 if (openfile(NULL, flags, writethrough, force_share, opts)) { 653 exit(1); 654 } 655 } else { 656 if (format) { 657 opts = qdict_new(); 658 qdict_put_str(opts, "driver", format); 659 } 660 if (openfile(argv[optind], flags, writethrough, 661 force_share, opts)) { 662 exit(1); 663 } 664 } 665 } 666 command_loop(); 667 668 /* 669 * Make sure all outstanding requests complete before the program exits. 670 */ 671 bdrv_drain_all(); 672 673 blk_unref(qemuio_blk); 674 g_free(readline_state); 675 return 0; 676 } 677