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