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