2
0

qemu-io.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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> ", error_get_progname());
  294. }
  295. return prompt;
  296. }
  297. static void GCC_FMT_ATTR(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 command_loop(void)
  366. {
  367. int i, fetchable = 0, prompted = 0;
  368. int ret, last_error = 0;
  369. char *input;
  370. for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
  371. ret = qemuio_command(qemuio_blk, cmdline[i]);
  372. if (ret < 0) {
  373. last_error = ret;
  374. }
  375. }
  376. if (cmdline) {
  377. g_free(cmdline);
  378. return last_error;
  379. }
  380. while (!quit_qemu_io) {
  381. if (!prompted) {
  382. printf("%s", get_prompt());
  383. fflush(stdout);
  384. qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
  385. prompted = 1;
  386. }
  387. main_loop_wait(false);
  388. if (!fetchable) {
  389. continue;
  390. }
  391. input = fetchline();
  392. if (input == NULL) {
  393. break;
  394. }
  395. ret = qemuio_command(qemuio_blk, input);
  396. g_free(input);
  397. if (ret < 0) {
  398. last_error = ret;
  399. }
  400. prompted = 0;
  401. fetchable = 0;
  402. }
  403. qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
  404. return last_error;
  405. }
  406. static void add_user_command(char *optarg)
  407. {
  408. cmdline = g_renew(char *, cmdline, ++ncmdline);
  409. cmdline[ncmdline-1] = optarg;
  410. }
  411. static void reenable_tty_echo(void)
  412. {
  413. qemu_set_tty_echo(STDIN_FILENO, true);
  414. }
  415. enum {
  416. OPTION_OBJECT = 256,
  417. OPTION_IMAGE_OPTS = 257,
  418. };
  419. static QemuOptsList qemu_object_opts = {
  420. .name = "object",
  421. .implied_opt_name = "qom-type",
  422. .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
  423. .desc = {
  424. { }
  425. },
  426. };
  427. static bool qemu_io_object_print_help(const char *type, QemuOpts *opts)
  428. {
  429. if (user_creatable_print_help(type, opts)) {
  430. exit(0);
  431. }
  432. return true;
  433. }
  434. static QemuOptsList file_opts = {
  435. .name = "file",
  436. .implied_opt_name = "file",
  437. .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
  438. .desc = {
  439. /* no elements => accept any params */
  440. { /* end of list */ }
  441. },
  442. };
  443. int main(int argc, char **argv)
  444. {
  445. int readonly = 0;
  446. const char *sopt = "hVc:d:f:rsnCmki:t:T:U";
  447. const struct option lopt[] = {
  448. { "help", no_argument, NULL, 'h' },
  449. { "version", no_argument, NULL, 'V' },
  450. { "cmd", required_argument, NULL, 'c' },
  451. { "format", required_argument, NULL, 'f' },
  452. { "read-only", no_argument, NULL, 'r' },
  453. { "snapshot", no_argument, NULL, 's' },
  454. { "nocache", no_argument, NULL, 'n' },
  455. { "copy-on-read", no_argument, NULL, 'C' },
  456. { "misalign", no_argument, NULL, 'm' },
  457. { "native-aio", no_argument, NULL, 'k' },
  458. { "aio", required_argument, NULL, 'i' },
  459. { "discard", required_argument, NULL, 'd' },
  460. { "cache", required_argument, NULL, 't' },
  461. { "trace", required_argument, NULL, 'T' },
  462. { "object", required_argument, NULL, OPTION_OBJECT },
  463. { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
  464. { "force-share", no_argument, 0, 'U'},
  465. { NULL, 0, NULL, 0 }
  466. };
  467. int c;
  468. int opt_index = 0;
  469. int flags = BDRV_O_UNMAP;
  470. int ret;
  471. bool writethrough = true;
  472. Error *local_error = NULL;
  473. QDict *opts = NULL;
  474. const char *format = NULL;
  475. char *trace_file = NULL;
  476. bool force_share = false;
  477. #ifdef CONFIG_POSIX
  478. signal(SIGPIPE, SIG_IGN);
  479. #endif
  480. socket_init();
  481. error_init(argv[0]);
  482. module_call_init(MODULE_INIT_TRACE);
  483. qemu_init_exec_dir(argv[0]);
  484. qcrypto_init(&error_fatal);
  485. module_call_init(MODULE_INIT_QOM);
  486. qemu_add_opts(&qemu_object_opts);
  487. qemu_add_opts(&qemu_trace_opts);
  488. bdrv_init();
  489. while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
  490. switch (c) {
  491. case 's':
  492. flags |= BDRV_O_SNAPSHOT;
  493. break;
  494. case 'n':
  495. flags |= BDRV_O_NOCACHE;
  496. writethrough = false;
  497. break;
  498. case 'C':
  499. flags |= BDRV_O_COPY_ON_READ;
  500. break;
  501. case 'd':
  502. if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
  503. error_report("Invalid discard option: %s", optarg);
  504. exit(1);
  505. }
  506. break;
  507. case 'f':
  508. format = optarg;
  509. break;
  510. case 'c':
  511. add_user_command(optarg);
  512. break;
  513. case 'r':
  514. readonly = 1;
  515. break;
  516. case 'm':
  517. qemuio_misalign = true;
  518. break;
  519. case 'k':
  520. flags |= BDRV_O_NATIVE_AIO;
  521. break;
  522. case 'i':
  523. if (bdrv_parse_aio(optarg, &flags) < 0) {
  524. error_report("Invalid aio option: %s", optarg);
  525. exit(1);
  526. }
  527. break;
  528. case 't':
  529. if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
  530. error_report("Invalid cache option: %s", optarg);
  531. exit(1);
  532. }
  533. break;
  534. case 'T':
  535. g_free(trace_file);
  536. trace_file = trace_opt_parse(optarg);
  537. break;
  538. case 'V':
  539. printf("%s version " QEMU_FULL_VERSION "\n"
  540. QEMU_COPYRIGHT "\n", error_get_progname());
  541. exit(0);
  542. case 'h':
  543. usage(error_get_progname());
  544. exit(0);
  545. case 'U':
  546. force_share = true;
  547. break;
  548. case OPTION_OBJECT: {
  549. QemuOpts *qopts;
  550. qopts = qemu_opts_parse_noisily(&qemu_object_opts,
  551. optarg, true);
  552. if (!qopts) {
  553. exit(1);
  554. }
  555. } break;
  556. case OPTION_IMAGE_OPTS:
  557. imageOpts = true;
  558. break;
  559. default:
  560. usage(error_get_progname());
  561. exit(1);
  562. }
  563. }
  564. if ((argc - optind) > 1) {
  565. usage(error_get_progname());
  566. exit(1);
  567. }
  568. if (format && imageOpts) {
  569. error_report("--image-opts and -f are mutually exclusive");
  570. exit(1);
  571. }
  572. if (qemu_init_main_loop(&local_error)) {
  573. error_report_err(local_error);
  574. exit(1);
  575. }
  576. qemu_opts_foreach(&qemu_object_opts,
  577. user_creatable_add_opts_foreach,
  578. qemu_io_object_print_help, &error_fatal);
  579. if (!trace_init_backends()) {
  580. exit(1);
  581. }
  582. trace_init_file(trace_file);
  583. qemu_set_log(LOG_TRACE);
  584. /* initialize commands */
  585. qemuio_add_command(&quit_cmd);
  586. qemuio_add_command(&open_cmd);
  587. qemuio_add_command(&close_cmd);
  588. if (isatty(STDIN_FILENO)) {
  589. ttyEOF = get_eof_char();
  590. readline_state = readline_init(readline_printf_func,
  591. readline_flush_func,
  592. NULL,
  593. readline_completion_func);
  594. qemu_set_tty_echo(STDIN_FILENO, false);
  595. atexit(reenable_tty_echo);
  596. }
  597. /* open the device */
  598. if (!readonly) {
  599. flags |= BDRV_O_RDWR;
  600. }
  601. if ((argc - optind) == 1) {
  602. if (imageOpts) {
  603. QemuOpts *qopts = NULL;
  604. qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
  605. if (!qopts) {
  606. exit(1);
  607. }
  608. opts = qemu_opts_to_qdict(qopts, NULL);
  609. if (openfile(NULL, flags, writethrough, force_share, opts)) {
  610. exit(1);
  611. }
  612. } else {
  613. if (format) {
  614. opts = qdict_new();
  615. qdict_put_str(opts, "driver", format);
  616. }
  617. if (openfile(argv[optind], flags, writethrough,
  618. force_share, opts)) {
  619. exit(1);
  620. }
  621. }
  622. }
  623. ret = command_loop();
  624. /*
  625. * Make sure all outstanding requests complete before the program exits.
  626. */
  627. bdrv_drain_all();
  628. blk_unref(qemuio_blk);
  629. g_free(readline_state);
  630. if (ret < 0) {
  631. return 1;
  632. } else {
  633. return 0;
  634. }
  635. }