2
0

qemu-io.c 18 KB

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