qemu-io.c 16 KB

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