qemu-io.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. /* 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. QDECREF(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. QDECREF(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. QemuOpts *qopts;
  148. QDict *opts;
  149. bool force_share = false;
  150. while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
  151. switch (c) {
  152. case 's':
  153. flags |= BDRV_O_SNAPSHOT;
  154. break;
  155. case 'n':
  156. flags |= BDRV_O_NOCACHE;
  157. writethrough = false;
  158. break;
  159. case 'C':
  160. flags |= BDRV_O_COPY_ON_READ;
  161. break;
  162. case 'r':
  163. readonly = 1;
  164. break;
  165. case 'k':
  166. flags |= BDRV_O_NATIVE_AIO;
  167. break;
  168. case 't':
  169. if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
  170. error_report("Invalid cache option: %s", optarg);
  171. qemu_opts_reset(&empty_opts);
  172. return 0;
  173. }
  174. break;
  175. case 'd':
  176. if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
  177. error_report("Invalid discard option: %s", optarg);
  178. qemu_opts_reset(&empty_opts);
  179. return 0;
  180. }
  181. break;
  182. case 'o':
  183. if (imageOpts) {
  184. printf("--image-opts and 'open -o' are mutually exclusive\n");
  185. qemu_opts_reset(&empty_opts);
  186. return 0;
  187. }
  188. if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
  189. qemu_opts_reset(&empty_opts);
  190. return 0;
  191. }
  192. break;
  193. case 'U':
  194. force_share = true;
  195. break;
  196. default:
  197. qemu_opts_reset(&empty_opts);
  198. return qemuio_command_usage(&open_cmd);
  199. }
  200. }
  201. if (!readonly) {
  202. flags |= BDRV_O_RDWR;
  203. }
  204. if (imageOpts && (optind == argc - 1)) {
  205. if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
  206. qemu_opts_reset(&empty_opts);
  207. return 0;
  208. }
  209. optind++;
  210. }
  211. qopts = qemu_opts_find(&empty_opts, NULL);
  212. opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
  213. qemu_opts_reset(&empty_opts);
  214. if (optind == argc - 1) {
  215. openfile(argv[optind], flags, writethrough, force_share, opts);
  216. } else if (optind == argc) {
  217. openfile(NULL, flags, writethrough, force_share, opts);
  218. } else {
  219. QDECREF(opts);
  220. qemuio_command_usage(&open_cmd);
  221. }
  222. return 0;
  223. }
  224. static int quit_f(BlockBackend *blk, int argc, char **argv)
  225. {
  226. return 1;
  227. }
  228. static const cmdinfo_t quit_cmd = {
  229. .name = "quit",
  230. .altname = "q",
  231. .cfunc = quit_f,
  232. .argmin = -1,
  233. .argmax = -1,
  234. .flags = CMD_FLAG_GLOBAL,
  235. .oneline = "exit the program",
  236. };
  237. static void usage(const char *name)
  238. {
  239. printf(
  240. "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
  241. "QEMU Disk exerciser\n"
  242. "\n"
  243. " --object OBJECTDEF define an object such as 'secret' for\n"
  244. " passwords and/or encryption keys\n"
  245. " --image-opts treat file as option string\n"
  246. " -c, --cmd STRING execute command with its arguments\n"
  247. " from the given string\n"
  248. " -f, --format FMT specifies the block driver to use\n"
  249. " -r, --read-only export read-only\n"
  250. " -s, --snapshot use snapshot file\n"
  251. " -n, --nocache disable host cache, short for -t none\n"
  252. " -C, --copy-on-read enable copy-on-read\n"
  253. " -m, --misalign misalign allocations for O_DIRECT\n"
  254. " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
  255. " -t, --cache=MODE use the given cache mode for the image\n"
  256. " -d, --discard=MODE use the given discard mode for the image\n"
  257. " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
  258. " specify tracing options\n"
  259. " see qemu-img(1) man page for full description\n"
  260. " -U, --force-share force shared permissions\n"
  261. " -h, --help display this help and exit\n"
  262. " -V, --version output version information and exit\n"
  263. "\n"
  264. "See '%s -c help' for information on available commands.\n"
  265. "\n"
  266. QEMU_HELP_BOTTOM "\n",
  267. name, name);
  268. }
  269. static char *get_prompt(void)
  270. {
  271. static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
  272. if (!prompt[0]) {
  273. snprintf(prompt, sizeof(prompt), "%s> ", progname);
  274. }
  275. return prompt;
  276. }
  277. static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
  278. const char *fmt, ...)
  279. {
  280. va_list ap;
  281. va_start(ap, fmt);
  282. vprintf(fmt, ap);
  283. va_end(ap);
  284. }
  285. static void readline_flush_func(void *opaque)
  286. {
  287. fflush(stdout);
  288. }
  289. static void readline_func(void *opaque, const char *str, void *readline_opaque)
  290. {
  291. char **line = readline_opaque;
  292. *line = g_strdup(str);
  293. }
  294. static void completion_match(const char *cmd, void *opaque)
  295. {
  296. readline_add_completion(readline_state, cmd);
  297. }
  298. static void readline_completion_func(void *opaque, const char *str)
  299. {
  300. readline_set_completion_index(readline_state, strlen(str));
  301. qemuio_complete_command(str, completion_match, NULL);
  302. }
  303. static char *fetchline_readline(void)
  304. {
  305. char *line = NULL;
  306. readline_start(readline_state, get_prompt(), 0, readline_func, &line);
  307. while (!line) {
  308. int ch = getchar();
  309. if (ttyEOF != 0x0 && ch == ttyEOF) {
  310. printf("\n");
  311. break;
  312. }
  313. readline_handle_byte(readline_state, ch);
  314. }
  315. return line;
  316. }
  317. #define MAXREADLINESZ 1024
  318. static char *fetchline_fgets(void)
  319. {
  320. char *p, *line = g_malloc(MAXREADLINESZ);
  321. if (!fgets(line, MAXREADLINESZ, stdin)) {
  322. g_free(line);
  323. return NULL;
  324. }
  325. p = line + strlen(line);
  326. if (p != line && p[-1] == '\n') {
  327. p[-1] = '\0';
  328. }
  329. return line;
  330. }
  331. static char *fetchline(void)
  332. {
  333. if (readline_state) {
  334. return fetchline_readline();
  335. } else {
  336. return fetchline_fgets();
  337. }
  338. }
  339. static void prep_fetchline(void *opaque)
  340. {
  341. int *fetchable = opaque;
  342. qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
  343. *fetchable= 1;
  344. }
  345. static void command_loop(void)
  346. {
  347. int i, done = 0, fetchable = 0, prompted = 0;
  348. char *input;
  349. for (i = 0; !done && i < ncmdline; i++) {
  350. done = qemuio_command(qemuio_blk, cmdline[i]);
  351. }
  352. if (cmdline) {
  353. g_free(cmdline);
  354. return;
  355. }
  356. while (!done) {
  357. if (!prompted) {
  358. printf("%s", get_prompt());
  359. fflush(stdout);
  360. qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
  361. prompted = 1;
  362. }
  363. main_loop_wait(false);
  364. if (!fetchable) {
  365. continue;
  366. }
  367. input = fetchline();
  368. if (input == NULL) {
  369. break;
  370. }
  371. done = qemuio_command(qemuio_blk, input);
  372. g_free(input);
  373. prompted = 0;
  374. fetchable = 0;
  375. }
  376. qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
  377. }
  378. static void add_user_command(char *optarg)
  379. {
  380. cmdline = g_renew(char *, cmdline, ++ncmdline);
  381. cmdline[ncmdline-1] = optarg;
  382. }
  383. static void reenable_tty_echo(void)
  384. {
  385. qemu_set_tty_echo(STDIN_FILENO, true);
  386. }
  387. enum {
  388. OPTION_OBJECT = 256,
  389. OPTION_IMAGE_OPTS = 257,
  390. };
  391. static QemuOptsList qemu_object_opts = {
  392. .name = "object",
  393. .implied_opt_name = "qom-type",
  394. .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
  395. .desc = {
  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. int main(int argc, char **argv)
  409. {
  410. int readonly = 0;
  411. const char *sopt = "hVc:d:f:rsnCmkt:T:U";
  412. const struct option lopt[] = {
  413. { "help", no_argument, NULL, 'h' },
  414. { "version", no_argument, NULL, 'V' },
  415. { "cmd", required_argument, NULL, 'c' },
  416. { "format", required_argument, NULL, 'f' },
  417. { "read-only", no_argument, NULL, 'r' },
  418. { "snapshot", no_argument, NULL, 's' },
  419. { "nocache", no_argument, NULL, 'n' },
  420. { "copy-on-read", no_argument, NULL, 'C' },
  421. { "misalign", no_argument, NULL, 'm' },
  422. { "native-aio", no_argument, NULL, 'k' },
  423. { "discard", required_argument, NULL, 'd' },
  424. { "cache", required_argument, NULL, 't' },
  425. { "trace", required_argument, NULL, 'T' },
  426. { "object", required_argument, NULL, OPTION_OBJECT },
  427. { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
  428. { "force-share", no_argument, 0, 'U'},
  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. char *trace_file = NULL;
  439. bool force_share = false;
  440. #ifdef CONFIG_POSIX
  441. signal(SIGPIPE, SIG_IGN);
  442. #endif
  443. module_call_init(MODULE_INIT_TRACE);
  444. progname = g_path_get_basename(argv[0]);
  445. qemu_init_exec_dir(argv[0]);
  446. qcrypto_init(&error_fatal);
  447. module_call_init(MODULE_INIT_QOM);
  448. qemu_add_opts(&qemu_object_opts);
  449. qemu_add_opts(&qemu_trace_opts);
  450. bdrv_init();
  451. while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
  452. switch (c) {
  453. case 's':
  454. flags |= BDRV_O_SNAPSHOT;
  455. break;
  456. case 'n':
  457. flags |= BDRV_O_NOCACHE;
  458. writethrough = false;
  459. break;
  460. case 'C':
  461. flags |= BDRV_O_COPY_ON_READ;
  462. break;
  463. case 'd':
  464. if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
  465. error_report("Invalid discard option: %s", optarg);
  466. exit(1);
  467. }
  468. break;
  469. case 'f':
  470. format = optarg;
  471. break;
  472. case 'c':
  473. add_user_command(optarg);
  474. break;
  475. case 'r':
  476. readonly = 1;
  477. break;
  478. case 'm':
  479. qemuio_misalign = true;
  480. break;
  481. case 'k':
  482. flags |= BDRV_O_NATIVE_AIO;
  483. break;
  484. case 't':
  485. if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
  486. error_report("Invalid cache option: %s", optarg);
  487. exit(1);
  488. }
  489. break;
  490. case 'T':
  491. g_free(trace_file);
  492. trace_file = trace_opt_parse(optarg);
  493. break;
  494. case 'V':
  495. printf("%s version " QEMU_FULL_VERSION "\n"
  496. QEMU_COPYRIGHT "\n", progname);
  497. exit(0);
  498. case 'h':
  499. usage(progname);
  500. exit(0);
  501. case 'U':
  502. force_share = true;
  503. break;
  504. case OPTION_OBJECT: {
  505. QemuOpts *qopts;
  506. qopts = qemu_opts_parse_noisily(&qemu_object_opts,
  507. optarg, true);
  508. if (!qopts) {
  509. exit(1);
  510. }
  511. } break;
  512. case OPTION_IMAGE_OPTS:
  513. imageOpts = true;
  514. break;
  515. default:
  516. usage(progname);
  517. exit(1);
  518. }
  519. }
  520. if ((argc - optind) > 1) {
  521. usage(progname);
  522. exit(1);
  523. }
  524. if (format && imageOpts) {
  525. error_report("--image-opts and -f are mutually exclusive");
  526. exit(1);
  527. }
  528. if (qemu_init_main_loop(&local_error)) {
  529. error_report_err(local_error);
  530. exit(1);
  531. }
  532. if (qemu_opts_foreach(&qemu_object_opts,
  533. user_creatable_add_opts_foreach,
  534. NULL, NULL)) {
  535. exit(1);
  536. }
  537. if (!trace_init_backends()) {
  538. exit(1);
  539. }
  540. trace_init_file(trace_file);
  541. qemu_set_log(LOG_TRACE);
  542. /* initialize commands */
  543. qemuio_add_command(&quit_cmd);
  544. qemuio_add_command(&open_cmd);
  545. qemuio_add_command(&close_cmd);
  546. if (isatty(STDIN_FILENO)) {
  547. ttyEOF = get_eof_char();
  548. readline_state = readline_init(readline_printf_func,
  549. readline_flush_func,
  550. NULL,
  551. readline_completion_func);
  552. qemu_set_tty_echo(STDIN_FILENO, false);
  553. atexit(reenable_tty_echo);
  554. }
  555. /* open the device */
  556. if (!readonly) {
  557. flags |= BDRV_O_RDWR;
  558. }
  559. if ((argc - optind) == 1) {
  560. if (imageOpts) {
  561. QemuOpts *qopts = NULL;
  562. qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
  563. if (!qopts) {
  564. exit(1);
  565. }
  566. opts = qemu_opts_to_qdict(qopts, NULL);
  567. if (openfile(NULL, flags, writethrough, force_share, opts)) {
  568. exit(1);
  569. }
  570. } else {
  571. if (format) {
  572. opts = qdict_new();
  573. qdict_put_str(opts, "driver", format);
  574. }
  575. if (openfile(argv[optind], flags, writethrough,
  576. force_share, opts)) {
  577. exit(1);
  578. }
  579. }
  580. }
  581. command_loop();
  582. /*
  583. * Make sure all outstanding requests complete before the program exits.
  584. */
  585. bdrv_drain_all();
  586. blk_unref(qemuio_blk);
  587. g_free(readline_state);
  588. return 0;
  589. }