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