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