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