root / qemu-io.c @ 34b5d2c6
History | View | Annotate | Download (10.1 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 | 737e150e | Paolo Bonzini | #include "block/block_int.h" |
20 | d7bb72c8 | Stefan Hajnoczi | #include "trace/control.h" |
21 | e3aff4f6 | aliguori | |
22 | 43642b38 | Devin Nakamura | #define CMD_NOFILE_OK 0x01 |
23 | e3aff4f6 | aliguori | |
24 | e3aff4f6 | aliguori | char *progname;
|
25 | e3aff4f6 | aliguori | |
26 | 734c3b85 | Kevin Wolf | BlockDriverState *qemuio_bs; |
27 | 797ac58c | Kevin Wolf | extern int qemuio_misalign; |
28 | 191c2890 | Kevin Wolf | |
29 | d1174f13 | Kevin Wolf | /* qemu-io commands passed using -c */
|
30 | d1174f13 | Kevin Wolf | static int ncmdline; |
31 | d1174f13 | Kevin Wolf | static char **cmdline; |
32 | d1174f13 | Kevin Wolf | |
33 | 734c3b85 | Kevin Wolf | static int close_f(BlockDriverState *bs, int argc, char **argv) |
34 | e3aff4f6 | aliguori | { |
35 | 4f6fd349 | Fam Zheng | bdrv_unref(bs); |
36 | 734c3b85 | Kevin Wolf | qemuio_bs = NULL;
|
37 | 43642b38 | Devin Nakamura | return 0; |
38 | e3aff4f6 | aliguori | } |
39 | e3aff4f6 | aliguori | |
40 | e3aff4f6 | aliguori | static const cmdinfo_t close_cmd = { |
41 | 43642b38 | Devin Nakamura | .name = "close",
|
42 | 43642b38 | Devin Nakamura | .altname = "c",
|
43 | 43642b38 | Devin Nakamura | .cfunc = close_f, |
44 | 43642b38 | Devin Nakamura | .oneline = "close the current open file",
|
45 | e3aff4f6 | aliguori | }; |
46 | e3aff4f6 | aliguori | |
47 | 9c4bab26 | Christoph Hellwig | static int openfile(char *name, int flags, int growable) |
48 | e3aff4f6 | aliguori | { |
49 | 34b5d2c6 | Max Reitz | Error *local_err = NULL;
|
50 | 34b5d2c6 | Max Reitz | |
51 | 734c3b85 | Kevin Wolf | if (qemuio_bs) {
|
52 | 43642b38 | Devin Nakamura | fprintf(stderr, "file open already, try 'help close'\n");
|
53 | 43642b38 | Devin Nakamura | return 1; |
54 | 43642b38 | Devin Nakamura | } |
55 | 43642b38 | Devin Nakamura | |
56 | 43642b38 | Devin Nakamura | if (growable) {
|
57 | 34b5d2c6 | Max Reitz | if (bdrv_file_open(&qemuio_bs, name, NULL, flags, &local_err)) { |
58 | 34b5d2c6 | Max Reitz | fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
|
59 | 34b5d2c6 | Max Reitz | error_get_pretty(local_err)); |
60 | 34b5d2c6 | Max Reitz | error_free(local_err); |
61 | 43642b38 | Devin Nakamura | return 1; |
62 | 43642b38 | Devin Nakamura | } |
63 | 43642b38 | Devin Nakamura | } else {
|
64 | 734c3b85 | Kevin Wolf | qemuio_bs = bdrv_new("hda");
|
65 | 43642b38 | Devin Nakamura | |
66 | 34b5d2c6 | Max Reitz | if (bdrv_open(qemuio_bs, name, NULL, flags, NULL, &local_err) < 0) { |
67 | 34b5d2c6 | Max Reitz | fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
|
68 | 34b5d2c6 | Max Reitz | error_get_pretty(local_err)); |
69 | 34b5d2c6 | Max Reitz | error_free(local_err); |
70 | 4f6fd349 | Fam Zheng | bdrv_unref(qemuio_bs); |
71 | 734c3b85 | Kevin Wolf | qemuio_bs = NULL;
|
72 | 43642b38 | Devin Nakamura | return 1; |
73 | 43642b38 | Devin Nakamura | } |
74 | 43642b38 | Devin Nakamura | } |
75 | 43642b38 | Devin Nakamura | |
76 | 43642b38 | Devin Nakamura | return 0; |
77 | e3aff4f6 | aliguori | } |
78 | e3aff4f6 | aliguori | |
79 | 43642b38 | Devin Nakamura | static void open_help(void) |
80 | e3aff4f6 | aliguori | { |
81 | 43642b38 | Devin Nakamura | printf( |
82 | e3aff4f6 | aliguori | "\n"
|
83 | e3aff4f6 | aliguori | " opens a new file in the requested mode\n"
|
84 | e3aff4f6 | aliguori | "\n"
|
85 | e3aff4f6 | aliguori | " Example:\n"
|
86 | e3aff4f6 | aliguori | " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
|
87 | e3aff4f6 | aliguori | "\n"
|
88 | e3aff4f6 | aliguori | " Opens a file for subsequent use by all of the other qemu-io commands.\n"
|
89 | e3aff4f6 | aliguori | " -r, -- open file read-only\n"
|
90 | e3aff4f6 | aliguori | " -s, -- use snapshot file\n"
|
91 | e3aff4f6 | aliguori | " -n, -- disable host cache\n"
|
92 | 9c4bab26 | Christoph Hellwig | " -g, -- allow file to grow (only applies to protocols)"
|
93 | e3aff4f6 | aliguori | "\n");
|
94 | e3aff4f6 | aliguori | } |
95 | e3aff4f6 | aliguori | |
96 | 734c3b85 | Kevin Wolf | static int open_f(BlockDriverState *bs, int argc, char **argv); |
97 | 22a2bdcb | Blue Swirl | |
98 | 22a2bdcb | Blue Swirl | static const cmdinfo_t open_cmd = { |
99 | 43642b38 | Devin Nakamura | .name = "open",
|
100 | 43642b38 | Devin Nakamura | .altname = "o",
|
101 | 43642b38 | Devin Nakamura | .cfunc = open_f, |
102 | 43642b38 | Devin Nakamura | .argmin = 1,
|
103 | 43642b38 | Devin Nakamura | .argmax = -1,
|
104 | 43642b38 | Devin Nakamura | .flags = CMD_NOFILE_OK, |
105 | 43642b38 | Devin Nakamura | .args = "[-Crsn] [path]",
|
106 | 43642b38 | Devin Nakamura | .oneline = "open the file specified by path",
|
107 | 43642b38 | Devin Nakamura | .help = open_help, |
108 | 22a2bdcb | Blue Swirl | }; |
109 | e3aff4f6 | aliguori | |
110 | 734c3b85 | Kevin Wolf | static int open_f(BlockDriverState *bs, int argc, char **argv) |
111 | e3aff4f6 | aliguori | { |
112 | 43642b38 | Devin Nakamura | int flags = 0; |
113 | 43642b38 | Devin Nakamura | int readonly = 0; |
114 | 43642b38 | Devin Nakamura | int growable = 0; |
115 | 43642b38 | Devin Nakamura | int c;
|
116 | 43642b38 | Devin Nakamura | |
117 | 43642b38 | Devin Nakamura | while ((c = getopt(argc, argv, "snrg")) != EOF) { |
118 | 43642b38 | Devin Nakamura | switch (c) {
|
119 | 43642b38 | Devin Nakamura | case 's': |
120 | 43642b38 | Devin Nakamura | flags |= BDRV_O_SNAPSHOT; |
121 | 43642b38 | Devin Nakamura | break;
|
122 | 43642b38 | Devin Nakamura | case 'n': |
123 | 43642b38 | Devin Nakamura | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
124 | 43642b38 | Devin Nakamura | break;
|
125 | 43642b38 | Devin Nakamura | case 'r': |
126 | 43642b38 | Devin Nakamura | readonly = 1;
|
127 | 43642b38 | Devin Nakamura | break;
|
128 | 43642b38 | Devin Nakamura | case 'g': |
129 | 43642b38 | Devin Nakamura | growable = 1;
|
130 | 43642b38 | Devin Nakamura | break;
|
131 | 43642b38 | Devin Nakamura | default:
|
132 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&open_cmd);
|
133 | f5edb014 | Naphtali Sprei | } |
134 | 43642b38 | Devin Nakamura | } |
135 | 43642b38 | Devin Nakamura | |
136 | 43642b38 | Devin Nakamura | if (!readonly) {
|
137 | 43642b38 | Devin Nakamura | flags |= BDRV_O_RDWR; |
138 | 43642b38 | Devin Nakamura | } |
139 | e3aff4f6 | aliguori | |
140 | 43642b38 | Devin Nakamura | if (optind != argc - 1) { |
141 | c2cdf5c5 | Kevin Wolf | return qemuio_command_usage(&open_cmd);
|
142 | 43642b38 | Devin Nakamura | } |
143 | e3aff4f6 | aliguori | |
144 | 43642b38 | Devin Nakamura | return openfile(argv[optind], flags, growable);
|
145 | e3aff4f6 | aliguori | } |
146 | e3aff4f6 | aliguori | |
147 | e681be7e | Kevin Wolf | static int quit_f(BlockDriverState *bs, int argc, char **argv) |
148 | e681be7e | Kevin Wolf | { |
149 | e681be7e | Kevin Wolf | return 1; |
150 | e681be7e | Kevin Wolf | } |
151 | e681be7e | Kevin Wolf | |
152 | e681be7e | Kevin Wolf | static const cmdinfo_t quit_cmd = { |
153 | e681be7e | Kevin Wolf | .name = "quit",
|
154 | e681be7e | Kevin Wolf | .altname = "q",
|
155 | e681be7e | Kevin Wolf | .cfunc = quit_f, |
156 | e681be7e | Kevin Wolf | .argmin = -1,
|
157 | e681be7e | Kevin Wolf | .argmax = -1,
|
158 | e681be7e | Kevin Wolf | .flags = CMD_FLAG_GLOBAL, |
159 | e681be7e | Kevin Wolf | .oneline = "exit the program",
|
160 | e681be7e | Kevin Wolf | }; |
161 | e681be7e | Kevin Wolf | |
162 | e3aff4f6 | aliguori | static void usage(const char *name) |
163 | e3aff4f6 | aliguori | { |
164 | 43642b38 | Devin Nakamura | printf( |
165 | 9a2d77ad | Christoph Hellwig | "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
|
166 | 84844a20 | Stefan Weil | "QEMU Disk exerciser\n"
|
167 | e3aff4f6 | aliguori | "\n"
|
168 | e3aff4f6 | aliguori | " -c, --cmd command to execute\n"
|
169 | e3aff4f6 | aliguori | " -r, --read-only export read-only\n"
|
170 | e3aff4f6 | aliguori | " -s, --snapshot use snapshot file\n"
|
171 | e3aff4f6 | aliguori | " -n, --nocache disable host cache\n"
|
172 | 1db6947d | Christoph Hellwig | " -g, --growable allow file to grow (only applies to protocols)\n"
|
173 | e3aff4f6 | aliguori | " -m, --misalign misalign allocations for O_DIRECT\n"
|
174 | 5c6c3a6c | Christoph Hellwig | " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
|
175 | 592fa070 | Kevin Wolf | " -t, --cache=MODE use the given cache mode for the image\n"
|
176 | d7bb72c8 | Stefan Hajnoczi | " -T, --trace FILE enable trace events listed in the given file\n"
|
177 | e3aff4f6 | aliguori | " -h, --help display this help and exit\n"
|
178 | e3aff4f6 | aliguori | " -V, --version output version information and exit\n"
|
179 | e3aff4f6 | aliguori | "\n",
|
180 | 43642b38 | Devin Nakamura | name); |
181 | e3aff4f6 | aliguori | } |
182 | e3aff4f6 | aliguori | |
183 | e3aff4f6 | aliguori | |
184 | d1174f13 | Kevin Wolf | #if defined(ENABLE_READLINE)
|
185 | d1174f13 | Kevin Wolf | # include <readline/history.h> |
186 | d1174f13 | Kevin Wolf | # include <readline/readline.h> |
187 | d1174f13 | Kevin Wolf | #elif defined(ENABLE_EDITLINE)
|
188 | d1174f13 | Kevin Wolf | # include <histedit.h> |
189 | d1174f13 | Kevin Wolf | #endif
|
190 | d1174f13 | Kevin Wolf | |
191 | d1174f13 | Kevin Wolf | static char *get_prompt(void) |
192 | d1174f13 | Kevin Wolf | { |
193 | d1174f13 | Kevin Wolf | static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ]; |
194 | d1174f13 | Kevin Wolf | |
195 | d1174f13 | Kevin Wolf | if (!prompt[0]) { |
196 | d1174f13 | Kevin Wolf | snprintf(prompt, sizeof(prompt), "%s> ", progname); |
197 | d1174f13 | Kevin Wolf | } |
198 | d1174f13 | Kevin Wolf | |
199 | d1174f13 | Kevin Wolf | return prompt;
|
200 | d1174f13 | Kevin Wolf | } |
201 | d1174f13 | Kevin Wolf | |
202 | d1174f13 | Kevin Wolf | #if defined(ENABLE_READLINE)
|
203 | d1174f13 | Kevin Wolf | static char *fetchline(void) |
204 | d1174f13 | Kevin Wolf | { |
205 | d1174f13 | Kevin Wolf | char *line = readline(get_prompt());
|
206 | d1174f13 | Kevin Wolf | if (line && *line) {
|
207 | d1174f13 | Kevin Wolf | add_history(line); |
208 | d1174f13 | Kevin Wolf | } |
209 | d1174f13 | Kevin Wolf | return line;
|
210 | d1174f13 | Kevin Wolf | } |
211 | d1174f13 | Kevin Wolf | #elif defined(ENABLE_EDITLINE)
|
212 | d1174f13 | Kevin Wolf | static char *el_get_prompt(EditLine *e) |
213 | d1174f13 | Kevin Wolf | { |
214 | d1174f13 | Kevin Wolf | return get_prompt();
|
215 | d1174f13 | Kevin Wolf | } |
216 | d1174f13 | Kevin Wolf | |
217 | d1174f13 | Kevin Wolf | static char *fetchline(void) |
218 | d1174f13 | Kevin Wolf | { |
219 | d1174f13 | Kevin Wolf | static EditLine *el;
|
220 | d1174f13 | Kevin Wolf | static History *hist;
|
221 | d1174f13 | Kevin Wolf | HistEvent hevent; |
222 | d1174f13 | Kevin Wolf | char *line;
|
223 | d1174f13 | Kevin Wolf | int count;
|
224 | d1174f13 | Kevin Wolf | |
225 | d1174f13 | Kevin Wolf | if (!el) {
|
226 | d1174f13 | Kevin Wolf | hist = history_init(); |
227 | d1174f13 | Kevin Wolf | history(hist, &hevent, H_SETSIZE, 100);
|
228 | d1174f13 | Kevin Wolf | el = el_init(progname, stdin, stdout, stderr); |
229 | d1174f13 | Kevin Wolf | el_source(el, NULL);
|
230 | d1174f13 | Kevin Wolf | el_set(el, EL_SIGNAL, 1);
|
231 | d1174f13 | Kevin Wolf | el_set(el, EL_PROMPT, el_get_prompt); |
232 | d1174f13 | Kevin Wolf | el_set(el, EL_HIST, history, (const char *)hist); |
233 | d1174f13 | Kevin Wolf | } |
234 | d1174f13 | Kevin Wolf | line = strdup(el_gets(el, &count)); |
235 | d1174f13 | Kevin Wolf | if (line) {
|
236 | d1174f13 | Kevin Wolf | if (count > 0) { |
237 | d1174f13 | Kevin Wolf | line[count-1] = '\0'; |
238 | d1174f13 | Kevin Wolf | } |
239 | d1174f13 | Kevin Wolf | if (*line) {
|
240 | d1174f13 | Kevin Wolf | history(hist, &hevent, H_ENTER, line); |
241 | d1174f13 | Kevin Wolf | } |
242 | d1174f13 | Kevin Wolf | } |
243 | d1174f13 | Kevin Wolf | return line;
|
244 | d1174f13 | Kevin Wolf | } |
245 | d1174f13 | Kevin Wolf | #else
|
246 | d1174f13 | Kevin Wolf | # define MAXREADLINESZ 1024 |
247 | d1174f13 | Kevin Wolf | static char *fetchline(void) |
248 | d1174f13 | Kevin Wolf | { |
249 | d1174f13 | Kevin Wolf | char *p, *line = g_malloc(MAXREADLINESZ);
|
250 | d1174f13 | Kevin Wolf | |
251 | d1174f13 | Kevin Wolf | if (!fgets(line, MAXREADLINESZ, stdin)) {
|
252 | d1174f13 | Kevin Wolf | g_free(line); |
253 | d1174f13 | Kevin Wolf | return NULL; |
254 | d1174f13 | Kevin Wolf | } |
255 | d1174f13 | Kevin Wolf | |
256 | d1174f13 | Kevin Wolf | p = line + strlen(line); |
257 | d1174f13 | Kevin Wolf | if (p != line && p[-1] == '\n') { |
258 | d1174f13 | Kevin Wolf | p[-1] = '\0'; |
259 | d1174f13 | Kevin Wolf | } |
260 | d1174f13 | Kevin Wolf | |
261 | d1174f13 | Kevin Wolf | return line;
|
262 | d1174f13 | Kevin Wolf | } |
263 | d1174f13 | Kevin Wolf | #endif
|
264 | d1174f13 | Kevin Wolf | |
265 | d1174f13 | Kevin Wolf | static void prep_fetchline(void *opaque) |
266 | d1174f13 | Kevin Wolf | { |
267 | d1174f13 | Kevin Wolf | int *fetchable = opaque;
|
268 | d1174f13 | Kevin Wolf | |
269 | d1174f13 | Kevin Wolf | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
270 | d1174f13 | Kevin Wolf | *fetchable= 1;
|
271 | d1174f13 | Kevin Wolf | } |
272 | d1174f13 | Kevin Wolf | |
273 | d1174f13 | Kevin Wolf | static void command_loop(void) |
274 | d1174f13 | Kevin Wolf | { |
275 | d1174f13 | Kevin Wolf | int i, done = 0, fetchable = 0, prompted = 0; |
276 | d1174f13 | Kevin Wolf | char *input;
|
277 | d1174f13 | Kevin Wolf | |
278 | d1174f13 | Kevin Wolf | for (i = 0; !done && i < ncmdline; i++) { |
279 | 3d21994f | Kevin Wolf | done = qemuio_command(qemuio_bs, cmdline[i]); |
280 | d1174f13 | Kevin Wolf | } |
281 | d1174f13 | Kevin Wolf | if (cmdline) {
|
282 | d1174f13 | Kevin Wolf | g_free(cmdline); |
283 | d1174f13 | Kevin Wolf | return;
|
284 | d1174f13 | Kevin Wolf | } |
285 | d1174f13 | Kevin Wolf | |
286 | d1174f13 | Kevin Wolf | while (!done) {
|
287 | d1174f13 | Kevin Wolf | if (!prompted) {
|
288 | d1174f13 | Kevin Wolf | printf("%s", get_prompt());
|
289 | d1174f13 | Kevin Wolf | fflush(stdout); |
290 | d1174f13 | Kevin Wolf | qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
|
291 | d1174f13 | Kevin Wolf | prompted = 1;
|
292 | d1174f13 | Kevin Wolf | } |
293 | d1174f13 | Kevin Wolf | |
294 | d1174f13 | Kevin Wolf | main_loop_wait(false);
|
295 | d1174f13 | Kevin Wolf | |
296 | d1174f13 | Kevin Wolf | if (!fetchable) {
|
297 | d1174f13 | Kevin Wolf | continue;
|
298 | d1174f13 | Kevin Wolf | } |
299 | d1174f13 | Kevin Wolf | |
300 | d1174f13 | Kevin Wolf | input = fetchline(); |
301 | d1174f13 | Kevin Wolf | if (input == NULL) { |
302 | d1174f13 | Kevin Wolf | break;
|
303 | d1174f13 | Kevin Wolf | } |
304 | 3d21994f | Kevin Wolf | done = qemuio_command(qemuio_bs, input); |
305 | d1174f13 | Kevin Wolf | g_free(input); |
306 | d1174f13 | Kevin Wolf | |
307 | d1174f13 | Kevin Wolf | prompted = 0;
|
308 | d1174f13 | Kevin Wolf | fetchable = 0;
|
309 | d1174f13 | Kevin Wolf | } |
310 | d1174f13 | Kevin Wolf | qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL); |
311 | d1174f13 | Kevin Wolf | } |
312 | d1174f13 | Kevin Wolf | |
313 | d1174f13 | Kevin Wolf | static void add_user_command(char *optarg) |
314 | d1174f13 | Kevin Wolf | { |
315 | d1174f13 | Kevin Wolf | cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *)); |
316 | d1174f13 | Kevin Wolf | cmdline[ncmdline-1] = optarg;
|
317 | d1174f13 | Kevin Wolf | } |
318 | d1174f13 | Kevin Wolf | |
319 | e3aff4f6 | aliguori | int main(int argc, char **argv) |
320 | e3aff4f6 | aliguori | { |
321 | 43642b38 | Devin Nakamura | int readonly = 0; |
322 | 43642b38 | Devin Nakamura | int growable = 0; |
323 | 9e8f1835 | Paolo Bonzini | const char *sopt = "hVc:d:rsnmgkt:T:"; |
324 | 43642b38 | Devin Nakamura | const struct option lopt[] = { |
325 | 43642b38 | Devin Nakamura | { "help", 0, NULL, 'h' }, |
326 | 43642b38 | Devin Nakamura | { "version", 0, NULL, 'V' }, |
327 | 43642b38 | Devin Nakamura | { "offset", 1, NULL, 'o' }, |
328 | 43642b38 | Devin Nakamura | { "cmd", 1, NULL, 'c' }, |
329 | 43642b38 | Devin Nakamura | { "read-only", 0, NULL, 'r' }, |
330 | 43642b38 | Devin Nakamura | { "snapshot", 0, NULL, 's' }, |
331 | 43642b38 | Devin Nakamura | { "nocache", 0, NULL, 'n' }, |
332 | 43642b38 | Devin Nakamura | { "misalign", 0, NULL, 'm' }, |
333 | 43642b38 | Devin Nakamura | { "growable", 0, NULL, 'g' }, |
334 | 43642b38 | Devin Nakamura | { "native-aio", 0, NULL, 'k' }, |
335 | 9e8f1835 | Paolo Bonzini | { "discard", 1, NULL, 'd' }, |
336 | 592fa070 | Kevin Wolf | { "cache", 1, NULL, 't' }, |
337 | d7bb72c8 | Stefan Hajnoczi | { "trace", 1, NULL, 'T' }, |
338 | 43642b38 | Devin Nakamura | { NULL, 0, NULL, 0 } |
339 | 43642b38 | Devin Nakamura | }; |
340 | 43642b38 | Devin Nakamura | int c;
|
341 | 43642b38 | Devin Nakamura | int opt_index = 0; |
342 | 9e8f1835 | Paolo Bonzini | int flags = BDRV_O_UNMAP;
|
343 | 43642b38 | Devin Nakamura | |
344 | 526eda14 | MORITA Kazutaka | #ifdef CONFIG_POSIX
|
345 | 526eda14 | MORITA Kazutaka | signal(SIGPIPE, SIG_IGN); |
346 | 526eda14 | MORITA Kazutaka | #endif
|
347 | 526eda14 | MORITA Kazutaka | |
348 | 43642b38 | Devin Nakamura | progname = basename(argv[0]);
|
349 | 43642b38 | Devin Nakamura | |
350 | 43642b38 | Devin Nakamura | while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) { |
351 | 43642b38 | Devin Nakamura | switch (c) {
|
352 | 43642b38 | Devin Nakamura | case 's': |
353 | 43642b38 | Devin Nakamura | flags |= BDRV_O_SNAPSHOT; |
354 | 43642b38 | Devin Nakamura | break;
|
355 | 43642b38 | Devin Nakamura | case 'n': |
356 | 43642b38 | Devin Nakamura | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
357 | 43642b38 | Devin Nakamura | break;
|
358 | 9e8f1835 | Paolo Bonzini | case 'd': |
359 | 9e8f1835 | Paolo Bonzini | if (bdrv_parse_discard_flags(optarg, &flags) < 0) { |
360 | 9e8f1835 | Paolo Bonzini | error_report("Invalid discard option: %s", optarg);
|
361 | 9e8f1835 | Paolo Bonzini | exit(1);
|
362 | 9e8f1835 | Paolo Bonzini | } |
363 | 9e8f1835 | Paolo Bonzini | break;
|
364 | 43642b38 | Devin Nakamura | case 'c': |
365 | 43642b38 | Devin Nakamura | add_user_command(optarg); |
366 | 43642b38 | Devin Nakamura | break;
|
367 | 43642b38 | Devin Nakamura | case 'r': |
368 | 43642b38 | Devin Nakamura | readonly = 1;
|
369 | 43642b38 | Devin Nakamura | break;
|
370 | 43642b38 | Devin Nakamura | case 'm': |
371 | 797ac58c | Kevin Wolf | qemuio_misalign = 1;
|
372 | 43642b38 | Devin Nakamura | break;
|
373 | 43642b38 | Devin Nakamura | case 'g': |
374 | 43642b38 | Devin Nakamura | growable = 1;
|
375 | 43642b38 | Devin Nakamura | break;
|
376 | 43642b38 | Devin Nakamura | case 'k': |
377 | 43642b38 | Devin Nakamura | flags |= BDRV_O_NATIVE_AIO; |
378 | 43642b38 | Devin Nakamura | break;
|
379 | 592fa070 | Kevin Wolf | case 't': |
380 | 592fa070 | Kevin Wolf | if (bdrv_parse_cache_flags(optarg, &flags) < 0) { |
381 | 592fa070 | Kevin Wolf | error_report("Invalid cache option: %s", optarg);
|
382 | 592fa070 | Kevin Wolf | exit(1);
|
383 | 592fa070 | Kevin Wolf | } |
384 | 592fa070 | Kevin Wolf | break;
|
385 | d7bb72c8 | Stefan Hajnoczi | case 'T': |
386 | d7bb72c8 | Stefan Hajnoczi | if (!trace_backend_init(optarg, NULL)) { |
387 | d7bb72c8 | Stefan Hajnoczi | exit(1); /* error message will have been printed */ |
388 | d7bb72c8 | Stefan Hajnoczi | } |
389 | d7bb72c8 | Stefan Hajnoczi | break;
|
390 | 43642b38 | Devin Nakamura | case 'V': |
391 | 02da386a | Kevin Wolf | printf("%s version %s\n", progname, QEMU_VERSION);
|
392 | 43642b38 | Devin Nakamura | exit(0);
|
393 | 43642b38 | Devin Nakamura | case 'h': |
394 | 43642b38 | Devin Nakamura | usage(progname); |
395 | 43642b38 | Devin Nakamura | exit(0);
|
396 | 43642b38 | Devin Nakamura | default:
|
397 | 43642b38 | Devin Nakamura | usage(progname); |
398 | 43642b38 | Devin Nakamura | exit(1);
|
399 | f5edb014 | Naphtali Sprei | } |
400 | 43642b38 | Devin Nakamura | } |
401 | 43642b38 | Devin Nakamura | |
402 | 43642b38 | Devin Nakamura | if ((argc - optind) > 1) { |
403 | 43642b38 | Devin Nakamura | usage(progname); |
404 | 43642b38 | Devin Nakamura | exit(1);
|
405 | 43642b38 | Devin Nakamura | } |
406 | e3aff4f6 | aliguori | |
407 | a57d1143 | Zhi Yong Wu | qemu_init_main_loop(); |
408 | 2592c59a | Paolo Bonzini | bdrv_init(); |
409 | a57d1143 | Zhi Yong Wu | |
410 | 43642b38 | Devin Nakamura | /* initialize commands */
|
411 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&quit_cmd); |
412 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&open_cmd); |
413 | c2cdf5c5 | Kevin Wolf | qemuio_add_command(&close_cmd); |
414 | 43642b38 | Devin Nakamura | |
415 | 43642b38 | Devin Nakamura | /* open the device */
|
416 | 43642b38 | Devin Nakamura | if (!readonly) {
|
417 | 43642b38 | Devin Nakamura | flags |= BDRV_O_RDWR; |
418 | 43642b38 | Devin Nakamura | } |
419 | 43642b38 | Devin Nakamura | |
420 | 43642b38 | Devin Nakamura | if ((argc - optind) == 1) { |
421 | 43642b38 | Devin Nakamura | openfile(argv[optind], flags, growable); |
422 | 43642b38 | Devin Nakamura | } |
423 | 43642b38 | Devin Nakamura | command_loop(); |
424 | e3aff4f6 | aliguori | |
425 | 43642b38 | Devin Nakamura | /*
|
426 | 922453bc | Stefan Hajnoczi | * Make sure all outstanding requests complete before the program exits.
|
427 | 43642b38 | Devin Nakamura | */
|
428 | 922453bc | Stefan Hajnoczi | bdrv_drain_all(); |
429 | 95533d5f | Christoph Hellwig | |
430 | 734c3b85 | Kevin Wolf | if (qemuio_bs) {
|
431 | 4f6fd349 | Fam Zheng | bdrv_unref(qemuio_bs); |
432 | 43642b38 | Devin Nakamura | } |
433 | 43642b38 | Devin Nakamura | return 0; |
434 | e3aff4f6 | aliguori | } |