qemu-io.c 12 KB

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