qemu-io.c 18 KB

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