2
0

qemu-io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 <sys/time.h>
  11. #include <sys/types.h>
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <getopt.h>
  15. #include <libgen.h>
  16. #include "qemu-io.h"
  17. #include "qemu/main-loop.h"
  18. #include "qemu/option.h"
  19. #include "qemu/config-file.h"
  20. #include "qemu/readline.h"
  21. #include "sysemu/block-backend.h"
  22. #include "block/block_int.h"
  23. #include "trace/control.h"
  24. #define CMD_NOFILE_OK 0x01
  25. static char *progname;
  26. static BlockBackend *qemuio_blk;
  27. static BlockDriverState *qemuio_bs;
  28. /* qemu-io commands passed using -c */
  29. static int ncmdline;
  30. static char **cmdline;
  31. static ReadLineState *readline_state;
  32. static int close_f(BlockDriverState *bs, int argc, char **argv)
  33. {
  34. blk_unref(qemuio_blk);
  35. qemuio_bs = NULL;
  36. qemuio_blk = NULL;
  37. return 0;
  38. }
  39. static const cmdinfo_t close_cmd = {
  40. .name = "close",
  41. .altname = "c",
  42. .cfunc = close_f,
  43. .oneline = "close the current open file",
  44. };
  45. static int openfile(char *name, int flags, int growable, QDict *opts)
  46. {
  47. Error *local_err = NULL;
  48. if (qemuio_bs) {
  49. fprintf(stderr, "file open already, try 'help close'\n");
  50. QDECREF(opts);
  51. return 1;
  52. }
  53. qemuio_blk = blk_new_with_bs("hda", &error_abort);
  54. qemuio_bs = blk_bs(qemuio_blk);
  55. if (growable) {
  56. flags |= BDRV_O_PROTOCOL;
  57. }
  58. if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err) < 0) {
  59. fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
  60. name ? " device " : "", name ?: "",
  61. error_get_pretty(local_err));
  62. error_free(local_err);
  63. blk_unref(qemuio_blk);
  64. qemuio_bs = NULL;
  65. qemuio_blk = NULL;
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. static void open_help(void)
  71. {
  72. printf(
  73. "\n"
  74. " opens a new file in the requested mode\n"
  75. "\n"
  76. " Example:\n"
  77. " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
  78. "\n"
  79. " Opens a file for subsequent use by all of the other qemu-io commands.\n"
  80. " -r, -- open file read-only\n"
  81. " -s, -- use snapshot file\n"
  82. " -n, -- disable host cache\n"
  83. " -g, -- allow file to grow (only applies to protocols)\n"
  84. " -o, -- options to be given to the block driver"
  85. "\n");
  86. }
  87. static int open_f(BlockDriverState *bs, int argc, char **argv);
  88. static const cmdinfo_t open_cmd = {
  89. .name = "open",
  90. .altname = "o",
  91. .cfunc = open_f,
  92. .argmin = 1,
  93. .argmax = -1,
  94. .flags = CMD_NOFILE_OK,
  95. .args = "[-Crsn] [-o options] [path]",
  96. .oneline = "open the file specified by path",
  97. .help = open_help,
  98. };
  99. static QemuOptsList empty_opts = {
  100. .name = "drive",
  101. .merge_lists = true,
  102. .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
  103. .desc = {
  104. /* no elements => accept any params */
  105. { /* end of list */ }
  106. },
  107. };
  108. static int open_f(BlockDriverState *bs, int argc, char **argv)
  109. {
  110. int flags = 0;
  111. int readonly = 0;
  112. int growable = 0;
  113. int c;
  114. QemuOpts *qopts;
  115. QDict *opts;
  116. while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
  117. switch (c) {
  118. case 's':
  119. flags |= BDRV_O_SNAPSHOT;
  120. break;
  121. case 'n':
  122. flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
  123. break;
  124. case 'r':
  125. readonly = 1;
  126. break;
  127. case 'g':
  128. growable = 1;
  129. break;
  130. case 'o':
  131. if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
  132. printf("could not parse option list -- %s\n", optarg);
  133. qemu_opts_reset(&empty_opts);
  134. return 0;
  135. }
  136. break;
  137. default:
  138. qemu_opts_reset(&empty_opts);
  139. return qemuio_command_usage(&open_cmd);
  140. }
  141. }
  142. if (!readonly) {
  143. flags |= BDRV_O_RDWR;
  144. }
  145. qopts = qemu_opts_find(&empty_opts, NULL);
  146. opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
  147. qemu_opts_reset(&empty_opts);
  148. if (optind == argc - 1) {
  149. return openfile(argv[optind], flags, growable, opts);
  150. } else if (optind == argc) {
  151. return openfile(NULL, flags, growable, opts);
  152. } else {
  153. QDECREF(opts);
  154. return qemuio_command_usage(&open_cmd);
  155. }
  156. }
  157. static int quit_f(BlockDriverState *bs, int argc, char **argv)
  158. {
  159. return 1;
  160. }
  161. static const cmdinfo_t quit_cmd = {
  162. .name = "quit",
  163. .altname = "q",
  164. .cfunc = quit_f,
  165. .argmin = -1,
  166. .argmax = -1,
  167. .flags = CMD_FLAG_GLOBAL,
  168. .oneline = "exit the program",
  169. };
  170. static void usage(const char *name)
  171. {
  172. printf(
  173. "Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
  174. "QEMU Disk exerciser\n"
  175. "\n"
  176. " -c, --cmd STRING execute command with its arguments\n"
  177. " from the given string\n"
  178. " -r, --read-only export read-only\n"
  179. " -s, --snapshot use snapshot file\n"
  180. " -n, --nocache disable host cache\n"
  181. " -g, --growable allow file to grow (only applies to protocols)\n"
  182. " -m, --misalign misalign allocations for O_DIRECT\n"
  183. " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
  184. " -t, --cache=MODE use the given cache mode for the image\n"
  185. " -T, --trace FILE enable trace events listed in the given file\n"
  186. " -h, --help display this help and exit\n"
  187. " -V, --version output version information and exit\n"
  188. "\n"
  189. "See '%s -c help' for information on available commands."
  190. "\n",
  191. name, name);
  192. }
  193. static char *get_prompt(void)
  194. {
  195. static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
  196. if (!prompt[0]) {
  197. snprintf(prompt, sizeof(prompt), "%s> ", progname);
  198. }
  199. return prompt;
  200. }
  201. static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
  202. const char *fmt, ...)
  203. {
  204. va_list ap;
  205. va_start(ap, fmt);
  206. vprintf(fmt, ap);
  207. va_end(ap);
  208. }
  209. static void readline_flush_func(void *opaque)
  210. {
  211. fflush(stdout);
  212. }
  213. static void readline_func(void *opaque, const char *str, void *readline_opaque)
  214. {
  215. char **line = readline_opaque;
  216. *line = g_strdup(str);
  217. }
  218. static void completion_match(const char *cmd, void *opaque)
  219. {
  220. readline_add_completion(readline_state, cmd);
  221. }
  222. static void readline_completion_func(void *opaque, const char *str)
  223. {
  224. readline_set_completion_index(readline_state, strlen(str));
  225. qemuio_complete_command(str, completion_match, NULL);
  226. }
  227. static char *fetchline_readline(void)
  228. {
  229. char *line = NULL;
  230. readline_start(readline_state, get_prompt(), 0, readline_func, &line);
  231. while (!line) {
  232. int ch = getchar();
  233. if (ch == EOF) {
  234. break;
  235. }
  236. readline_handle_byte(readline_state, ch);
  237. }
  238. return line;
  239. }
  240. #define MAXREADLINESZ 1024
  241. static char *fetchline_fgets(void)
  242. {
  243. char *p, *line = g_malloc(MAXREADLINESZ);
  244. if (!fgets(line, MAXREADLINESZ, stdin)) {
  245. g_free(line);
  246. return NULL;
  247. }
  248. p = line + strlen(line);
  249. if (p != line && p[-1] == '\n') {
  250. p[-1] = '\0';
  251. }
  252. return line;
  253. }
  254. static char *fetchline(void)
  255. {
  256. if (readline_state) {
  257. return fetchline_readline();
  258. } else {
  259. return fetchline_fgets();
  260. }
  261. }
  262. static void prep_fetchline(void *opaque)
  263. {
  264. int *fetchable = opaque;
  265. qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
  266. *fetchable= 1;
  267. }
  268. static void command_loop(void)
  269. {
  270. int i, done = 0, fetchable = 0, prompted = 0;
  271. char *input;
  272. for (i = 0; !done && i < ncmdline; i++) {
  273. done = qemuio_command(qemuio_bs, cmdline[i]);
  274. }
  275. if (cmdline) {
  276. g_free(cmdline);
  277. return;
  278. }
  279. while (!done) {
  280. if (!prompted) {
  281. printf("%s", get_prompt());
  282. fflush(stdout);
  283. qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
  284. prompted = 1;
  285. }
  286. main_loop_wait(false);
  287. if (!fetchable) {
  288. continue;
  289. }
  290. input = fetchline();
  291. if (input == NULL) {
  292. break;
  293. }
  294. done = qemuio_command(qemuio_bs, input);
  295. g_free(input);
  296. prompted = 0;
  297. fetchable = 0;
  298. }
  299. qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
  300. }
  301. static void add_user_command(char *optarg)
  302. {
  303. cmdline = g_renew(char *, cmdline, ++ncmdline);
  304. cmdline[ncmdline-1] = optarg;
  305. }
  306. static void reenable_tty_echo(void)
  307. {
  308. qemu_set_tty_echo(STDIN_FILENO, true);
  309. }
  310. int main(int argc, char **argv)
  311. {
  312. int readonly = 0;
  313. int growable = 0;
  314. const char *sopt = "hVc:d:rsnmgkt:T:";
  315. const struct option lopt[] = {
  316. { "help", 0, NULL, 'h' },
  317. { "version", 0, NULL, 'V' },
  318. { "offset", 1, NULL, 'o' },
  319. { "cmd", 1, NULL, 'c' },
  320. { "read-only", 0, NULL, 'r' },
  321. { "snapshot", 0, NULL, 's' },
  322. { "nocache", 0, NULL, 'n' },
  323. { "misalign", 0, NULL, 'm' },
  324. { "growable", 0, NULL, 'g' },
  325. { "native-aio", 0, NULL, 'k' },
  326. { "discard", 1, NULL, 'd' },
  327. { "cache", 1, NULL, 't' },
  328. { "trace", 1, NULL, 'T' },
  329. { NULL, 0, NULL, 0 }
  330. };
  331. int c;
  332. int opt_index = 0;
  333. int flags = BDRV_O_UNMAP;
  334. Error *local_error = NULL;
  335. #ifdef CONFIG_POSIX
  336. signal(SIGPIPE, SIG_IGN);
  337. #endif
  338. progname = basename(argv[0]);
  339. qemu_init_exec_dir(argv[0]);
  340. while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
  341. switch (c) {
  342. case 's':
  343. flags |= BDRV_O_SNAPSHOT;
  344. break;
  345. case 'n':
  346. flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
  347. break;
  348. case 'd':
  349. if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
  350. error_report("Invalid discard option: %s", optarg);
  351. exit(1);
  352. }
  353. break;
  354. case 'c':
  355. add_user_command(optarg);
  356. break;
  357. case 'r':
  358. readonly = 1;
  359. break;
  360. case 'm':
  361. qemuio_misalign = true;
  362. break;
  363. case 'g':
  364. growable = 1;
  365. break;
  366. case 'k':
  367. flags |= BDRV_O_NATIVE_AIO;
  368. break;
  369. case 't':
  370. if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
  371. error_report("Invalid cache option: %s", optarg);
  372. exit(1);
  373. }
  374. break;
  375. case 'T':
  376. if (!trace_init_backends(optarg, NULL)) {
  377. exit(1); /* error message will have been printed */
  378. }
  379. break;
  380. case 'V':
  381. printf("%s version %s\n", progname, QEMU_VERSION);
  382. exit(0);
  383. case 'h':
  384. usage(progname);
  385. exit(0);
  386. default:
  387. usage(progname);
  388. exit(1);
  389. }
  390. }
  391. if ((argc - optind) > 1) {
  392. usage(progname);
  393. exit(1);
  394. }
  395. if (qemu_init_main_loop(&local_error)) {
  396. error_report("%s", error_get_pretty(local_error));
  397. error_free(local_error);
  398. exit(1);
  399. }
  400. bdrv_init();
  401. /* initialize commands */
  402. qemuio_add_command(&quit_cmd);
  403. qemuio_add_command(&open_cmd);
  404. qemuio_add_command(&close_cmd);
  405. if (isatty(STDIN_FILENO)) {
  406. readline_state = readline_init(readline_printf_func,
  407. readline_flush_func,
  408. NULL,
  409. readline_completion_func);
  410. qemu_set_tty_echo(STDIN_FILENO, false);
  411. atexit(reenable_tty_echo);
  412. }
  413. /* open the device */
  414. if (!readonly) {
  415. flags |= BDRV_O_RDWR;
  416. }
  417. if ((argc - optind) == 1) {
  418. openfile(argv[optind], flags, growable, NULL);
  419. }
  420. command_loop();
  421. /*
  422. * Make sure all outstanding requests complete before the program exits.
  423. */
  424. bdrv_drain_all();
  425. blk_unref(qemuio_blk);
  426. g_free(readline_state);
  427. return 0;
  428. }