~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/blktap/drivers/qcow-create.c

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

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