2
0

qemu-io.c 17 KB

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