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