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