You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

689 lines
18KB

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