ia64/xen-unstable

view tools/blktap/drivers/qcow-create.c @ 13044:a05fefbeb19f

Add sparseness flag to qcow-create.

For environments where space must be guaranteed in advance
use the -p flag to remove sparseness from the qcow file.

Signed-off-by: Julian Chesterfield <julian@xensource.com>
author Julian Chesterfield <julian@xensource.com>
date Thu Dec 14 20:23:07 2006 +0000 (2006-12-14)
parents 2937703f0ed0
children 08fa19392d75
line source
1 /* qcow-create.c
2 *
3 * Generates a qcow format disk.
4 *
5 * (c) 2006 Andrew Warfield and Julian Chesterfield
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation; or, when distributed
10 * separately from the Linux kernel or incorporated into other
11 * software packages, subject to the following license:
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 * IN THE SOFTWARE.
30 */
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/statvfs.h>
38 #include <sys/stat.h>
39 #include <sys/ioctl.h>
40 #include <linux/fs.h>
41 #include <string.h>
42 #include "tapdisk.h"
44 #if 1
45 #define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a )
46 #else
47 #define DFPRINTF(_f, _a...) ((void)0)
48 #endif
50 #define QCOW_NONSPARSE_FILE 0x00
51 #define QCOW_SPARSE_FILE 0x02
52 #define MAX_NAME_LEN 1000
54 void help(void)
55 {
56 fprintf(stderr, "Qcow-utils: v1.0.0\n");
57 fprintf(stderr,
58 "usage: qcow-create [-h help] [-p reserve] <SIZE(MB)> <FILENAME> "
59 "[<BACKING_FILENAME>]\n");
60 exit(-1);
61 }
63 int main(int argc, char *argv[])
64 {
65 int ret = -1, c, backed = 0;
66 int flags = QCOW_SPARSE_FILE;
67 uint64_t size;
68 char filename[MAX_NAME_LEN], bfilename[MAX_NAME_LEN];
70 for(;;) {
71 c = getopt(argc, argv, "hp");
72 if (c == -1)
73 break;
74 switch(c) {
75 case 'h':
76 help();
77 exit(0);
78 break;
79 case 'p':
80 flags = QCOW_NONSPARSE_FILE;
81 break;
82 }
83 }
85 printf("Optind %d, argc %d\n", optind, argc);
86 if ( !(optind == (argc - 2) || optind == (argc - 3)) )
87 help();
89 size = atoi(argv[optind++]);
90 size = size << 20;
92 if (snprintf(filename, MAX_NAME_LEN, "%s",argv[optind++]) >=
93 MAX_NAME_LEN) {
94 fprintf(stderr,"Device name too long\n");
95 exit(-1);
96 }
98 if (optind != argc) {
99 backed = 1;
100 if (snprintf(bfilename, MAX_NAME_LEN, "%s",argv[optind++]) >=
101 MAX_NAME_LEN) {
102 fprintf(stderr,"Device name too long\n");
103 exit(-1);
104 }
105 }
107 DFPRINTF("Creating file size %llu, name %s\n",(long long unsigned)size, filename);
108 if (!backed)
109 ret = qcow_create(filename,size,NULL,flags);
110 else
111 ret = qcow_create(filename,size,bfilename,flags);
113 if (ret < 0) DPRINTF("Unable to create QCOW file\n");
114 else DPRINTF("QCOW file successfully created\n");
116 return 0;
117 }