]>
git.proxmox.com Git - mirror_qemu.git/blob - qemu-io.c
2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
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.
10 #include <sys/types.h>
15 #include "qemu-common.h"
16 #include "block_int.h"
19 #define VERSION "0.0.1"
21 #define CMD_NOFILE_OK 0x01
24 static BlockDriverState
*bs
;
29 * Memory allocation helpers.
31 * Make sure memory is aligned by default, or purposefully misaligned if
32 * that is specified on the command line.
35 #define MISALIGN_OFFSET 16
36 static void *qemu_io_alloc(size_t len
, int pattern
)
41 len
+= MISALIGN_OFFSET
;
42 buf
= qemu_memalign(512, len
);
43 memset(buf
, pattern
, len
);
45 buf
+= MISALIGN_OFFSET
;
49 static void qemu_io_free(void *p
)
57 dump_buffer(const void *buffer
, int64_t offset
, int len
)
62 for (i
= 0, p
= buffer
; i
< len
; i
+= 16) {
65 printf("%08llx: ", (unsigned long long)offset
+ i
);
66 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, p
++)
69 for (j
= 0; j
< 16 && i
+ j
< len
; j
++, s
++) {
80 print_report(const char *op
, struct timeval
*t
, int64_t offset
,
81 int count
, int total
, int cnt
, int Cflag
)
83 char s1
[64], s2
[64], ts
[64];
85 timestr(t
, ts
, sizeof(ts
), Cflag
? VERBOSE_FIXED_TIME
: 0);
87 cvtstr((double)total
, s1
, sizeof(s1
));
88 cvtstr(tdiv((double)total
, *t
), s2
, sizeof(s2
));
89 printf("%s %d/%d bytes at offset %lld\n",
90 op
, total
, count
, (long long)offset
);
91 printf("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n",
92 s1
, cnt
, ts
, s2
, tdiv((double)cnt
, *t
));
93 } else {/* bytes,ops,time,bytes/sec,ops/sec */
94 printf("%d,%d,%s,%.3f,%.3f\n",
96 tdiv((double)total
, *t
),
97 tdiv((double)cnt
, *t
));
102 * Parse multiple length statements for vectored I/O, and construct an I/O
103 * vector matching it.
106 create_iovec(QEMUIOVector
*qiov
, char **argv
, int nr_iov
, int pattern
)
108 size_t *sizes
= calloc(nr_iov
, sizeof(size_t));
113 for (i
= 0; i
< nr_iov
; i
++) {
119 printf("non-numeric length argument -- %s\n", arg
);
123 /* should be SIZE_T_MAX, but that doesn't exist */
124 if (len
> UINT_MAX
) {
125 printf("too large length argument -- %s\n", arg
);
130 printf("length argument %lld is not sector aligned\n",
139 qemu_iovec_init(qiov
, nr_iov
);
141 buf
= p
= qemu_io_alloc(count
, pattern
);
143 for (i
= 0; i
< nr_iov
; i
++) {
144 qemu_iovec_add(qiov
, p
, sizes
[i
]);
152 static int do_read(char *buf
, int64_t offset
, int count
, int *total
)
156 ret
= bdrv_read(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
163 static int do_write(char *buf
, int64_t offset
, int count
, int *total
)
167 ret
= bdrv_write(bs
, offset
>> 9, (uint8_t *)buf
, count
>> 9);
174 static int do_pread(char *buf
, int64_t offset
, int count
, int *total
)
176 *total
= bdrv_pread(bs
, offset
, (uint8_t *)buf
, count
);
182 static int do_pwrite(char *buf
, int64_t offset
, int count
, int *total
)
184 *total
= bdrv_pwrite(bs
, offset
, (uint8_t *)buf
, count
);
190 #define NOT_DONE 0x7fffffff
191 static void aio_rw_done(void *opaque
, int ret
)
193 *(int *)opaque
= ret
;
196 static int do_aio_readv(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
198 BlockDriverAIOCB
*acb
;
199 int async_ret
= NOT_DONE
;
201 acb
= bdrv_aio_readv(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
202 aio_rw_done
, &async_ret
);
206 while (async_ret
== NOT_DONE
)
210 return async_ret
< 0 ? async_ret
: 1;
213 static int do_aio_writev(QEMUIOVector
*qiov
, int64_t offset
, int *total
)
215 BlockDriverAIOCB
*acb
;
216 int async_ret
= NOT_DONE
;
218 acb
= bdrv_aio_writev(bs
, offset
>> 9, qiov
, qiov
->size
>> 9,
219 aio_rw_done
, &async_ret
);
223 while (async_ret
== NOT_DONE
)
227 return async_ret
< 0 ? async_ret
: 1;
231 static const cmdinfo_t read_cmd
;
238 " reads a range of bytes from the given offset\n"
241 " 'read -v 512 1k' - dumps 1 kilobyte read from 512 bytes into the file\n"
243 " Reads a segment of the currently open file, optionally dumping it to the\n"
244 " standard output stream (with -v option) for subsequent inspection.\n"
245 " -C, -- report statistics in a machine parsable format\n"
246 " -l, -- length for pattern verification (only with -P)\n"
247 " -p, -- use bdrv_pread to read the file\n"
248 " -P, -- use a pattern to verify read data\n"
249 " -q, -- quite mode, do not show I/O statistics\n"
250 " -s, -- start offset for pattern verification (only with -P)\n"
251 " -v, -- dump buffer to standard output\n"
256 read_f(int argc
, char **argv
)
258 struct timeval t1
, t2
;
259 int Cflag
= 0, pflag
= 0, qflag
= 0, vflag
= 0;
260 int Pflag
= 0, sflag
= 0, lflag
= 0;
265 /* Some compilers get confused and warn if this is not initialized. */
267 int pattern
= 0, pattern_offset
= 0, pattern_count
= 0;
269 while ((c
= getopt(argc
, argv
, "Cl:pP:qs:v")) != EOF
) {
276 pattern_count
= cvtnum(optarg
);
277 if (pattern_count
< 0) {
278 printf("non-numeric length argument -- %s\n", optarg
);
287 pattern
= atoi(optarg
);
294 pattern_offset
= cvtnum(optarg
);
295 if (pattern_offset
< 0) {
296 printf("non-numeric length argument -- %s\n", optarg
);
304 return command_usage(&read_cmd
);
308 if (optind
!= argc
- 2)
309 return command_usage(&read_cmd
);
311 offset
= cvtnum(argv
[optind
]);
313 printf("non-numeric length argument -- %s\n", argv
[optind
]);
318 count
= cvtnum(argv
[optind
]);
320 printf("non-numeric length argument -- %s\n", argv
[optind
]);
324 if (!Pflag
&& (lflag
|| sflag
)) {
325 return command_usage(&read_cmd
);
329 pattern_count
= count
- pattern_offset
;
332 if ((pattern_count
< 0) || (pattern_count
+ pattern_offset
> count
)) {
333 printf("pattern verfication range exceeds end of read data\n");
338 if (offset
& 0x1ff) {
339 printf("offset %lld is not sector aligned\n",
344 printf("count %d is not sector aligned\n",
350 buf
= qemu_io_alloc(count
, 0xab);
352 gettimeofday(&t1
, NULL
);
354 cnt
= do_pread(buf
, offset
, count
, &total
);
356 cnt
= do_read(buf
, offset
, count
, &total
);
357 gettimeofday(&t2
, NULL
);
360 printf("read failed: %s\n", strerror(-cnt
));
365 void* cmp_buf
= malloc(pattern_count
);
366 memset(cmp_buf
, pattern
, pattern_count
);
367 if (memcmp(buf
+ pattern_offset
, cmp_buf
, pattern_count
)) {
368 printf("Pattern verification failed at offset %lld, "
370 (long long) offset
+ pattern_offset
, pattern_count
);
379 dump_buffer(buf
, offset
, count
);
381 /* Finally, report back -- -C gives a parsable format */
383 print_report("read", &t2
, offset
, count
, total
, cnt
, Cflag
);
390 static const cmdinfo_t read_cmd
= {
396 .args
= "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
397 .oneline
= "reads a number of bytes at a specified offset",
401 static const cmdinfo_t readv_cmd
;
408 " reads a range of bytes from the given offset into multiple buffers\n"
411 " 'readv -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
413 " Reads a segment of the currently open file, optionally dumping it to the\n"
414 " standard output stream (with -v option) for subsequent inspection.\n"
415 " Uses multiple iovec buffers if more than one byte range is specified.\n"
416 " -C, -- report statistics in a machine parsable format\n"
417 " -P, -- use a pattern to verify read data\n"
418 " -v, -- dump buffer to standard output\n"
419 " -q, -- quite mode, do not show I/O statistics\n"
424 readv_f(int argc
, char **argv
)
426 struct timeval t1
, t2
;
427 int Cflag
= 0, qflag
= 0, vflag
= 0;
437 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
444 pattern
= atoi(optarg
);
453 return command_usage(&readv_cmd
);
457 if (optind
> argc
- 2)
458 return command_usage(&readv_cmd
);
461 offset
= cvtnum(argv
[optind
]);
463 printf("non-numeric length argument -- %s\n", argv
[optind
]);
468 if (offset
& 0x1ff) {
469 printf("offset %lld is not sector aligned\n",
474 nr_iov
= argc
- optind
;
475 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, 0xab);
477 gettimeofday(&t1
, NULL
);
478 cnt
= do_aio_readv(&qiov
, offset
, &total
);
479 gettimeofday(&t2
, NULL
);
482 printf("readv failed: %s\n", strerror(-cnt
));
487 void* cmp_buf
= malloc(qiov
.size
);
488 memset(cmp_buf
, pattern
, qiov
.size
);
489 if (memcmp(buf
, cmp_buf
, qiov
.size
)) {
490 printf("Pattern verification failed at offset %lld, "
492 (long long) offset
, qiov
.size
);
501 dump_buffer(buf
, offset
, qiov
.size
);
503 /* Finally, report back -- -C gives a parsable format */
505 print_report("read", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
512 static const cmdinfo_t readv_cmd
= {
517 .args
= "[-Cqv] [-P pattern ] off len [len..]",
518 .oneline
= "reads a number of bytes at a specified offset",
522 static const cmdinfo_t write_cmd
;
529 " writes a range of bytes from the given offset\n"
532 " 'write 512 1k' - writes 1 kilobyte at 512 bytes into the open file\n"
534 " Writes into a segment of the currently open file, using a buffer\n"
535 " filled with a set pattern (0xcdcdcdcd).\n"
536 " -p, -- use bdrv_pwrite to write the file\n"
537 " -P, -- use different pattern to fill file\n"
538 " -C, -- report statistics in a machine parsable format\n"
539 " -q, -- quite mode, do not show I/O statistics\n"
544 write_f(int argc
, char **argv
)
546 struct timeval t1
, t2
;
547 int Cflag
= 0, pflag
= 0, qflag
= 0;
552 /* Some compilers get confused and warn if this is not initialized. */
556 while ((c
= getopt(argc
, argv
, "CpP:q")) != EOF
) {
565 pattern
= atoi(optarg
);
571 return command_usage(&write_cmd
);
575 if (optind
!= argc
- 2)
576 return command_usage(&write_cmd
);
578 offset
= cvtnum(argv
[optind
]);
580 printf("non-numeric length argument -- %s\n", argv
[optind
]);
585 count
= cvtnum(argv
[optind
]);
587 printf("non-numeric length argument -- %s\n", argv
[optind
]);
592 if (offset
& 0x1ff) {
593 printf("offset %lld is not sector aligned\n",
599 printf("count %d is not sector aligned\n",
605 buf
= qemu_io_alloc(count
, pattern
);
607 gettimeofday(&t1
, NULL
);
609 cnt
= do_pwrite(buf
, offset
, count
, &total
);
611 cnt
= do_write(buf
, offset
, count
, &total
);
612 gettimeofday(&t2
, NULL
);
615 printf("write failed: %s\n", strerror(-cnt
));
622 /* Finally, report back -- -C gives a parsable format */
624 print_report("wrote", &t2
, offset
, count
, total
, cnt
, Cflag
);
631 static const cmdinfo_t write_cmd
= {
637 .args
= "[-aCpq] [-P pattern ] off len",
638 .oneline
= "writes a number of bytes at a specified offset",
642 static const cmdinfo_t writev_cmd
;
649 " writes a range of bytes from the given offset source from multiple buffers\n"
652 " 'write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
654 " Writes into a segment of the currently open file, using a buffer\n"
655 " filled with a set pattern (0xcdcdcdcd).\n"
656 " -P, -- use different pattern to fill file\n"
657 " -C, -- report statistics in a machine parsable format\n"
658 " -q, -- quite mode, do not show I/O statistics\n"
663 writev_f(int argc
, char **argv
)
665 struct timeval t1
, t2
;
666 int Cflag
= 0, qflag
= 0;
675 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
684 pattern
= atoi(optarg
);
687 return command_usage(&writev_cmd
);
691 if (optind
> argc
- 2)
692 return command_usage(&writev_cmd
);
694 offset
= cvtnum(argv
[optind
]);
696 printf("non-numeric length argument -- %s\n", argv
[optind
]);
701 if (offset
& 0x1ff) {
702 printf("offset %lld is not sector aligned\n",
707 nr_iov
= argc
- optind
;
708 buf
= create_iovec(&qiov
, &argv
[optind
], nr_iov
, pattern
);
710 gettimeofday(&t1
, NULL
);
711 cnt
= do_aio_writev(&qiov
, offset
, &total
);
712 gettimeofday(&t2
, NULL
);
715 printf("writev failed: %s\n", strerror(-cnt
));
722 /* Finally, report back -- -C gives a parsable format */
724 print_report("wrote", &t2
, offset
, qiov
.size
, total
, cnt
, Cflag
);
731 static const cmdinfo_t writev_cmd
= {
736 .args
= "[-Cq] [-P pattern ] off len [len..]",
737 .oneline
= "writes a number of bytes at a specified offset",
754 aio_write_done(void *opaque
, int ret
)
756 struct aio_ctx
*ctx
= opaque
;
759 gettimeofday(&t2
, NULL
);
763 printf("aio_write failed: %s\n", strerror(-ret
));
771 /* Finally, report back -- -C gives a parsable format */
772 t2
= tsub(t2
, ctx
->t1
);
773 print_report("wrote", &t2
, ctx
->offset
, ctx
->qiov
.size
,
774 ctx
->qiov
.size
, 1, ctx
->Cflag
);
776 qemu_io_free(ctx
->buf
);
780 static const cmdinfo_t aio_read_cmd
;
783 aio_read_done(void *opaque
, int ret
)
785 struct aio_ctx
*ctx
= opaque
;
788 gettimeofday(&t2
, NULL
);
791 printf("readv failed: %s\n", strerror(-ret
));
796 void *cmp_buf
= malloc(ctx
->qiov
.size
);
798 memset(cmp_buf
, ctx
->pattern
, ctx
->qiov
.size
);
799 if (memcmp(ctx
->buf
, cmp_buf
, ctx
->qiov
.size
)) {
800 printf("Pattern verification failed at offset %lld, "
802 (long long) ctx
->offset
, ctx
->qiov
.size
);
812 dump_buffer(ctx
->buf
, ctx
->offset
, ctx
->qiov
.size
);
815 /* Finally, report back -- -C gives a parsable format */
816 t2
= tsub(t2
, ctx
->t1
);
817 print_report("read", &t2
, ctx
->offset
, ctx
->qiov
.size
,
818 ctx
->qiov
.size
, 1, ctx
->Cflag
);
820 qemu_io_free(ctx
->buf
);
829 " asynchronously reads a range of bytes from the given offset\n"
832 " 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n"
834 " Reads a segment of the currently open file, optionally dumping it to the\n"
835 " standard output stream (with -v option) for subsequent inspection.\n"
836 " The read is performed asynchronously and should the aio_flush command \n"
837 " should be used to ensure all outstanding aio requests have been completed\n"
838 " -C, -- report statistics in a machine parsable format\n"
839 " -P, -- use a pattern to verify read data\n"
840 " -v, -- dump buffer to standard output\n"
841 " -q, -- quite mode, do not show I/O statistics\n"
846 aio_read_f(int argc
, char **argv
)
849 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
850 BlockDriverAIOCB
*acb
;
852 while ((c
= getopt(argc
, argv
, "CP:qv")) != EOF
) {
859 ctx
->pattern
= atoi(optarg
);
868 return command_usage(&aio_read_cmd
);
872 if (optind
> argc
- 2)
873 return command_usage(&aio_read_cmd
);
876 ctx
->offset
= cvtnum(argv
[optind
]);
877 if (ctx
->offset
< 0) {
878 printf("non-numeric length argument -- %s\n", argv
[optind
]);
883 if (ctx
->offset
& 0x1ff) {
884 printf("offset %lld is not sector aligned\n",
885 (long long)ctx
->offset
);
889 nr_iov
= argc
- optind
;
890 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, 0xab);
892 gettimeofday(&ctx
->t1
, NULL
);
893 acb
= bdrv_aio_readv(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
894 ctx
->qiov
.size
>> 9, aio_read_done
, ctx
);
901 static const cmdinfo_t aio_read_cmd
= {
906 .args
= "[-Cqv] [-P pattern ] off len [len..]",
907 .oneline
= "asynchronously reads a number of bytes",
908 .help
= aio_read_help
,
911 static const cmdinfo_t aio_write_cmd
;
918 " asynchronously writes a range of bytes from the given offset source \n"
919 " from multiple buffers\n"
922 " 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n"
924 " Writes into a segment of the currently open file, using a buffer\n"
925 " filled with a set pattern (0xcdcdcdcd).\n"
926 " The write is performed asynchronously and should the aio_flush command \n"
927 " should be used to ensure all outstanding aio requests have been completed\n"
928 " -P, -- use different pattern to fill file\n"
929 " -C, -- report statistics in a machine parsable format\n"
930 " -q, -- quite mode, do not show I/O statistics\n"
936 aio_write_f(int argc
, char **argv
)
940 struct aio_ctx
*ctx
= calloc(1, sizeof(struct aio_ctx
));
941 BlockDriverAIOCB
*acb
;
943 while ((c
= getopt(argc
, argv
, "CqP:")) != EOF
) {
952 pattern
= atoi(optarg
);
955 return command_usage(&aio_write_cmd
);
959 if (optind
> argc
- 2)
960 return command_usage(&aio_write_cmd
);
962 ctx
->offset
= cvtnum(argv
[optind
]);
963 if (ctx
->offset
< 0) {
964 printf("non-numeric length argument -- %s\n", argv
[optind
]);
969 if (ctx
->offset
& 0x1ff) {
970 printf("offset %lld is not sector aligned\n",
971 (long long)ctx
->offset
);
975 nr_iov
= argc
- optind
;
976 ctx
->buf
= create_iovec(&ctx
->qiov
, &argv
[optind
], nr_iov
, pattern
);
978 gettimeofday(&ctx
->t1
, NULL
);
979 acb
= bdrv_aio_writev(bs
, ctx
->offset
>> 9, &ctx
->qiov
,
980 ctx
->qiov
.size
>> 9, aio_write_done
, ctx
);
987 static const cmdinfo_t aio_write_cmd
= {
989 .cfunc
= aio_write_f
,
992 .args
= "[-Cq] [-P pattern ] off len [len..]",
993 .oneline
= "asynchronously writes a number of bytes",
994 .help
= aio_write_help
,
998 aio_flush_f(int argc
, char **argv
)
1004 static const cmdinfo_t aio_flush_cmd
= {
1005 .name
= "aio_flush",
1006 .cfunc
= aio_flush_f
,
1007 .oneline
= "completes all outstanding aio requets"
1011 flush_f(int argc
, char **argv
)
1017 static const cmdinfo_t flush_cmd
= {
1021 .oneline
= "flush all in-core file state to disk",
1025 truncate_f(int argc
, char **argv
)
1030 offset
= cvtnum(argv
[1]);
1032 printf("non-numeric truncate argument -- %s\n", argv
[1]);
1036 ret
= bdrv_truncate(bs
, offset
);
1038 printf("truncate: %s", strerror(ret
));
1045 static const cmdinfo_t truncate_cmd
= {
1048 .cfunc
= truncate_f
,
1052 .oneline
= "truncates the current file at the given offset",
1056 length_f(int argc
, char **argv
)
1061 size
= bdrv_getlength(bs
);
1063 printf("getlength: %s", strerror(size
));
1067 cvtstr(size
, s1
, sizeof(s1
));
1073 static const cmdinfo_t length_cmd
= {
1077 .oneline
= "gets the length of the current file",
1082 info_f(int argc
, char **argv
)
1084 BlockDriverInfo bdi
;
1085 char s1
[64], s2
[64];
1088 if (bs
->drv
&& bs
->drv
->format_name
)
1089 printf("format name: %s\n", bs
->drv
->format_name
);
1090 if (bs
->drv
&& bs
->drv
->protocol_name
)
1091 printf("format name: %s\n", bs
->drv
->protocol_name
);
1093 ret
= bdrv_get_info(bs
, &bdi
);
1097 cvtstr(bdi
.cluster_size
, s1
, sizeof(s1
));
1098 cvtstr(bdi
.vm_state_offset
, s2
, sizeof(s2
));
1100 printf("cluster size: %s\n", s1
);
1101 printf("vm state offset: %s\n", s2
);
1108 static const cmdinfo_t info_cmd
= {
1112 .oneline
= "prints information about the current file",
1116 alloc_f(int argc
, char **argv
)
1125 offset
= cvtnum(argv
[1]);
1126 if (offset
& 0x1ff) {
1127 printf("offset %lld is not sector aligned\n",
1133 nb_sectors
= cvtnum(argv
[2]);
1137 ret
= bdrv_is_allocated(bs
, offset
>> 9, nb_sectors
, &num
);
1139 cvtstr(offset
, s1
, sizeof(s1
));
1141 retstr
= ret
? "allocated" : "not allocated";
1142 if (nb_sectors
== 1)
1143 printf("sector %s at offset %s\n", retstr
, s1
);
1145 printf("%d/%d sectors %s at offset %s\n",
1146 num
, nb_sectors
, retstr
, s1
);
1150 static const cmdinfo_t alloc_cmd
= {
1156 .args
= "off [sectors]",
1157 .oneline
= "checks if a sector is present in the file",
1161 close_f(int argc
, char **argv
)
1168 static const cmdinfo_t close_cmd
= {
1172 .oneline
= "close the current open file",
1175 static int openfile(char *name
, int flags
, int growable
)
1178 fprintf(stderr
, "file open already, try 'help close'\n");
1182 bs
= bdrv_new("hda");
1186 if (bdrv_open(bs
, name
, flags
) == -1) {
1187 fprintf(stderr
, "%s: can't open device %s\n", progname
, name
);
1194 if (!bs
->drv
|| !bs
->drv
->protocol_name
) {
1196 "%s: only protocols can be opened growable\n",
1211 " opens a new file in the requested mode\n"
1214 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
1216 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
1217 " -C, -- create new file if it doesn't exist\n"
1218 " -r, -- open file read-only\n"
1219 " -s, -- use snapshot file\n"
1220 " -n, -- disable host cache\n"
1221 " -g, -- allow file to grow (only applies to protocols)"
1225 static const cmdinfo_t open_cmd
;
1228 open_f(int argc
, char **argv
)
1235 while ((c
= getopt(argc
, argv
, "snCrg")) != EOF
) {
1238 flags
|= BDRV_O_SNAPSHOT
;
1241 flags
|= BDRV_O_NOCACHE
;
1244 flags
|= BDRV_O_CREAT
;
1253 return command_usage(&open_cmd
);
1258 flags
|= BDRV_O_RDONLY
;
1260 flags
|= BDRV_O_RDWR
;
1262 if (optind
!= argc
- 1)
1263 return command_usage(&open_cmd
);
1265 return openfile(argv
[optind
], flags
, growable
);
1268 static const cmdinfo_t open_cmd
= {
1274 .flags
= CMD_NOFILE_OK
,
1275 .args
= "[-Crsn] [path]",
1276 .oneline
= "open the file specified by path",
1284 /* only one device allowed so far */
1292 const cmdinfo_t
*ct
)
1294 if (ct
->flags
& CMD_FLAG_GLOBAL
)
1296 if (!(ct
->flags
& CMD_NOFILE_OK
) && !bs
) {
1297 fprintf(stderr
, "no file open, try 'help open'\n");
1303 static void usage(const char *name
)
1306 "Usage: %s [-h] [-V] [-Crsnm] [-c cmd] ... [file]\n"
1307 "QEMU Disk exerciser\n"
1309 " -C, --create create new file if it doesn't exist\n"
1310 " -c, --cmd command to execute\n"
1311 " -r, --read-only export read-only\n"
1312 " -s, --snapshot use snapshot file\n"
1313 " -n, --nocache disable host cache\n"
1314 " -m, --misalign misalign allocations for O_DIRECT\n"
1315 " -h, --help display this help and exit\n"
1316 " -V, --version output version information and exit\n"
1322 int main(int argc
, char **argv
)
1326 const char *sopt
= "hVc:Crsnmg";
1327 struct option lopt
[] = {
1328 { "help", 0, 0, 'h' },
1329 { "version", 0, 0, 'V' },
1330 { "offset", 1, 0, 'o' },
1331 { "cmd", 1, 0, 'c' },
1332 { "create", 0, 0, 'C' },
1333 { "read-only", 0, 0, 'r' },
1334 { "snapshot", 0, 0, 's' },
1335 { "nocache", 0, 0, 'n' },
1336 { "misalign", 0, 0, 'm' },
1337 { "growable", 0, 0, 'g' },
1344 progname
= basename(argv
[0]);
1346 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
1349 flags
|= BDRV_O_SNAPSHOT
;
1352 flags
|= BDRV_O_NOCACHE
;
1355 add_user_command(optarg
);
1358 flags
|= BDRV_O_CREAT
;
1370 printf("%s version %s\n", progname
, VERSION
);
1381 if ((argc
- optind
) > 1) {
1388 /* initialize commands */
1391 add_command(&open_cmd
);
1392 add_command(&close_cmd
);
1393 add_command(&read_cmd
);
1394 add_command(&readv_cmd
);
1395 add_command(&write_cmd
);
1396 add_command(&writev_cmd
);
1397 add_command(&aio_read_cmd
);
1398 add_command(&aio_write_cmd
);
1399 add_command(&aio_flush_cmd
);
1400 add_command(&flush_cmd
);
1401 add_command(&truncate_cmd
);
1402 add_command(&length_cmd
);
1403 add_command(&info_cmd
);
1404 add_command(&alloc_cmd
);
1406 add_args_command(init_args_command
);
1407 add_check_command(init_check_command
);
1409 /* open the device */
1411 flags
|= BDRV_O_RDONLY
;
1413 flags
|= BDRV_O_RDWR
;
1415 if ((argc
- optind
) == 1)
1416 openfile(argv
[optind
], flags
, growable
);
1420 * Make sure all outstanding requests get flushed the program exits.