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 #include "qemu/osdep.h" 11 #include <getopt.h> 12 #include <libgen.h> 13 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" 30 31 #define CMD_NOFILE_OK 0x01 32 33 static char *progname; 34 35 static BlockBackend *qemuio_blk; 36 37 /* qemu-io commands passed using -c */ 38 static int ncmdline; 39 static char **cmdline; 40 static bool imageOpts; 41 42 static ReadLineState *readline_state; 43 44 static int close_f(BlockBackend *blk, int argc, char **argv) 45 { 46 blk_unref(qemuio_blk); 47 qemuio_blk = NULL; 48 return 0; 49 } 50 51 static const cmdinfo_t close_cmd = { 52 .name = "close", 53 .altname = "c", 54 .cfunc = close_f, 55 .oneline = "close the current open file", 56 }; 57 58 static int openfile(char *name, int flags, bool writethrough, bool force_share, 59 QDict *opts) 60 { 61 Error *local_err = NULL; 62 63 if (qemuio_blk) { 64 error_report("file open already, try 'help close'"); 65 QDECREF(opts); 66 return 1; 67 } 68 69 if (force_share) { 70 if (!opts) { 71 opts = qdict_new(); 72 } 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; 78 } 79 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true); 80 } 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; 86 } 87 88 blk_set_enable_write_cache(qemuio_blk, !writethrough); 89 90 return 0; 91 } 92 93 static void open_help(void) 94 { 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"); 112 } 113 114 static int open_f(BlockBackend *blk, int argc, char **argv); 115 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, 126 }; 127 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 */ } 135 }, 136 }; 137 138 static int open_f(BlockBackend *blk, int argc, char **argv) 139 { 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; 147 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; 168 } 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; 175 } 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; 182 } 183 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) { 184 qemu_opts_reset(&empty_opts); 185 return 0; 186 } 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); 194 } 195 } 196 197 if (!readonly) { 198 flags |= BDRV_O_RDWR; 199 } 200 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; 205 } 206 optind++; 207 } 208 209 qopts = qemu_opts_find(&empty_opts, NULL); 210 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL; 211 qemu_opts_reset(&empty_opts); 212 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); 220 } 221 return 0; 222 } 223 224 static int quit_f(BlockBackend *blk, int argc, char **argv) 225 { 226 return 1; 227 } 228 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", 237 }; 238 239 static void usage(const char *name) 240 { 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); 269 } 270 271 static char *get_prompt(void) 272 { 273 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; 274 275 if (!prompt[0]) { 276 snprintf(prompt, sizeof(prompt), "%s> ", progname); 277 } 278 279 return prompt; 280 } 281 282 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, 283 const char *fmt, ...) 284 { 285 va_list ap; 286 va_start(ap, fmt); 287 vprintf(fmt, ap); 288 va_end(ap); 289 } 290 291 static void readline_flush_func(void *opaque) 292 { 293 fflush(stdout); 294 } 295 296 static void readline_func(void *opaque, const char *str, void *readline_opaque) 297 { 298 char **line = readline_opaque; 299 *line = g_strdup(str); 300 } 301 302 static void completion_match(const char *cmd, void *opaque) 303 { 304 readline_add_completion(readline_state, cmd); 305 } 306 307 static void readline_completion_func(void *opaque, const char *str) 308 { 309 readline_set_completion_index(readline_state, strlen(str)); 310 qemuio_complete_command(str, completion_match, NULL); 311 } 312 313 static char *fetchline_readline(void) 314 { 315 char *line = NULL; 316 317 readline_start(readline_state, get_prompt(), 0, readline_func, &line); 318 while (!line) { 319 int ch = getchar(); 320 if (ch == EOF) { 321 break; 322 } 323 readline_handle_byte(readline_state, ch); 324 } 325 return line; 326 } 327 328 #define MAXREADLINESZ 1024 329 static char *fetchline_fgets(void) 330 { 331 char *p, *line = g_malloc(MAXREADLINESZ); 332 333 if (!fgets(line, MAXREADLINESZ, stdin)) { 334 g_free(line); 335 return NULL; 336 } 337 338 p = line + strlen(line); 339 if (p != line && p[-1] == '\n') { 340 p[-1] = '\0'; 341 } 342 343 return line; 344 } 345 346 static char *fetchline(void) 347 { 348 if (readline_state) { 349 return fetchline_readline(); 350 } else { 351 return fetchline_fgets(); 352 } 353 } 354 355 static void prep_fetchline(void *opaque) 356 { 357 int *fetchable = opaque; 358 359 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); 360 *fetchable= 1; 361 } 362 363 static void command_loop(void) 364 { 365 int i, done = 0, fetchable = 0, prompted = 0; 366 char *input; 367 368 for (i = 0; !done && i < ncmdline; i++) { 369 done = qemuio_command(qemuio_blk, cmdline[i]); 370 } 371 if (cmdline) { 372 g_free(cmdline); 373 return; 374 } 375 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; 382 } 383 384 main_loop_wait(false); 385 386 if (!fetchable) { 387 continue; 388 } 389 390 input = fetchline(); 391 if (input == NULL) { 392 break; 393 } 394 done = qemuio_command(qemuio_blk, input); 395 g_free(input); 396 397 prompted = 0; 398 fetchable = 0; 399 } 400 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); 401 } 402 403 static void add_user_command(char *optarg) 404 { 405 cmdline = g_renew(char *, cmdline, ++ncmdline); 406 cmdline[ncmdline-1] = optarg; 407 } 408 409 static void reenable_tty_echo(void) 410 { 411 qemu_set_tty_echo(STDIN_FILENO, true); 412 } 413 414 enum { 415 OPTION_OBJECT = 256, 416 OPTION_IMAGE_OPTS = 257, 417 }; 418 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 = { 424 { } 425 }, 426 }; 427 428 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 */ } 436 }, 437 }; 438 439 int main(int argc, char **argv) 440 { 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 } 460 }; 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; 470 471 #ifdef CONFIG_POSIX 472 signal(SIGPIPE, SIG_IGN); 473 #endif 474 475 module_call_init(MODULE_INIT_TRACE); 476 progname = basename(argv[0]); 477 qemu_init_exec_dir(argv[0]); 478 479 qcrypto_init(&error_fatal); 480 481 module_call_init(MODULE_INIT_QOM); 482 qemu_add_opts(&qemu_object_opts); 483 qemu_add_opts(&qemu_trace_opts); 484 bdrv_init(); 485 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); 499 } 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); 520 } 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); 542 } 543 } break; 544 case OPTION_IMAGE_OPTS: 545 imageOpts = true; 546 break; 547 default: 548 usage(progname); 549 exit(1); 550 } 551 } 552 553 if ((argc - optind) > 1) { 554 usage(progname); 555 exit(1); 556 } 557 558 if (format && imageOpts) { 559 error_report("--image-opts and -f are mutually exclusive"); 560 exit(1); 561 } 562 563 if (qemu_init_main_loop(&local_error)) { 564 error_report_err(local_error); 565 exit(1); 566 } 567 568 if (qemu_opts_foreach(&qemu_object_opts, 569 user_creatable_add_opts_foreach, 570 NULL, NULL)) { 571 exit(1); 572 } 573 574 if (!trace_init_backends()) { 575 exit(1); 576 } 577 trace_init_file(trace_file); 578 qemu_set_log(LOG_TRACE); 579 580 /* initialize commands */ 581 qemuio_add_command(&quit_cmd); 582 qemuio_add_command(&open_cmd); 583 qemuio_add_command(&close_cmd); 584 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); 592 } 593 594 /* open the device */ 595 if (!readonly) { 596 flags |= BDRV_O_RDWR; 597 } 598 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); 605 } 606 opts = qemu_opts_to_qdict(qopts, NULL); 607 if (openfile(NULL, flags, writethrough, force_share, opts)) { 608 exit(1); 609 } 610 } else { 611 if (format) { 612 opts = qdict_new(); 613 qdict_put_str(opts, "driver", format); 614 } 615 if (openfile(argv[optind], flags, writethrough, 616 force_share, opts)) { 617 exit(1); 618 } 619 } 620 } 621 command_loop(); 622 623 /* 624 * Make sure all outstanding requests complete before the program exits. 625 */ 626 bdrv_drain_all(); 627 628 blk_unref(qemuio_blk); 629 g_free(readline_state); 630 return 0; 631 } 632