2
0

qemu-io.c 12 KB

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