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  */
31 
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 <string.h>
41 #include "tapdisk.h"
42 #include "qcow.h"
43 
44 #if 1
45 #define DFPRINTF(_f, _a...) fprintf ( stderr, _f , ## _a )
46 #else
47 #define DFPRINTF(_f, _a...) ((void)0)
48 #endif
49 
50 #define MAX_NAME_LEN 1000
51 
help(void)52 void help(void)
53 {
54 	fprintf(stderr, "Qcow-utils: v1.0.0\n");
55 	fprintf(stderr,
56 		"usage: qcow-create [-h help] [-r reserve] <SIZE(MB)> <FILENAME> "
57 		"[<BACKING_FILENAME>]\n");
58 	exit(-1);
59 }
60 
main(int argc,char * argv[])61 int main(int argc, char *argv[])
62 {
63 	int ret = -1, c, backed = 0;
64 	int sparse =  1;
65 	uint64_t size;
66 	char filename[MAX_NAME_LEN], bfilename[MAX_NAME_LEN];
67 
68         for(;;) {
69                 c = getopt(argc, argv, "hr");
70                 if (c == -1)
71                         break;
72                 switch(c) {
73                 case 'h':
74                         help();
75                         exit(0);
76                         break;
77                 case 'r':
78 			sparse = 0;
79 			break;
80 		default:
81 			fprintf(stderr, "Unknown option\n");
82 			help();
83 		}
84 	}
85 
86 	printf("Optind %d, argc %d\n", optind, argc);
87 	if ( !(optind == (argc - 2) || optind == (argc - 3)) )
88 		help();
89 
90 	size = atoi(argv[optind++]);
91 	size = size << 20;
92 
93 	if (snprintf(filename, MAX_NAME_LEN, "%s",argv[optind++]) >=
94 		MAX_NAME_LEN) {
95 		fprintf(stderr,"Device name too long\n");
96 		exit(-1);
97 	}
98 
99 	if (optind != argc) {
100 		/*Backing file argument*/
101 		backed = 1;
102 		if (snprintf(bfilename, MAX_NAME_LEN, "%s",argv[optind++]) >=
103 			MAX_NAME_LEN) {
104 			fprintf(stderr,"Device name too long\n");
105 			exit(-1);
106 		}
107 	}
108 
109 	DFPRINTF("Creating file size %"PRIu64", name %s\n",(uint64_t)size, filename);
110 	if (!backed)
111 		ret = qcow_create(filename,size,NULL,sparse);
112 	else
113 		ret = qcow_create(filename,size,bfilename,sparse);
114 
115 	if (ret < 0)
116 		DPRINTF("Unable to create QCOW file\n");
117 	else
118 		DPRINTF("QCOW file successfully created\n");
119 
120 	return 0;
121 }
122