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