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