2
0

qemu-io.c 18 KB

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