Statistics
| Branch: | Revision:

root / qemu-mkcow.c @ 63ce9e0a

History | View | Annotate | Download (4.4 kB)

1 33e3963e bellard
/*
2 33e3963e bellard
 * create a COW disk image
3 33e3963e bellard
 * 
4 33e3963e bellard
 * Copyright (c) 2003 Fabrice Bellard
5 33e3963e bellard
 * 
6 33e3963e bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 33e3963e bellard
 * of this software and associated documentation files (the "Software"), to deal
8 33e3963e bellard
 * in the Software without restriction, including without limitation the rights
9 33e3963e bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 33e3963e bellard
 * copies of the Software, and to permit persons to whom the Software is
11 33e3963e bellard
 * furnished to do so, subject to the following conditions:
12 33e3963e bellard
 *
13 33e3963e bellard
 * The above copyright notice and this permission notice shall be included in
14 33e3963e bellard
 * all copies or substantial portions of the Software.
15 33e3963e bellard
 *
16 33e3963e bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 33e3963e bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 33e3963e bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 33e3963e bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 33e3963e bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 33e3963e bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 33e3963e bellard
 * THE SOFTWARE.
23 33e3963e bellard
 */
24 33e3963e bellard
#include <stdlib.h>
25 33e3963e bellard
#include <stdio.h>
26 33e3963e bellard
#include <stdarg.h>
27 33e3963e bellard
#include <string.h>
28 33e3963e bellard
#include <getopt.h>
29 33e3963e bellard
#include <inttypes.h>
30 33e3963e bellard
#include <unistd.h>
31 33e3963e bellard
#include <fcntl.h>
32 33e3963e bellard
#include <signal.h>
33 33e3963e bellard
#include <time.h>
34 33e3963e bellard
#include <sys/time.h>
35 33e3963e bellard
#include <errno.h>
36 abcd5da7 bellard
#include <sys/stat.h>
37 33e3963e bellard
#include <netinet/in.h>
38 33e3963e bellard
39 00af2b26 bellard
#include "cow.h"
40 33e3963e bellard
41 abcd5da7 bellard
#include "bswap.h"
42 33e3963e bellard
43 33e3963e bellard
int cow_create(int cow_fd, const char *image_filename, 
44 33e3963e bellard
               int64_t image_sectors)
45 33e3963e bellard
{
46 33e3963e bellard
    struct cow_header_v2 cow_header;
47 33e3963e bellard
    int fd;
48 33e3963e bellard
    struct stat st;
49 33e3963e bellard
50 33e3963e bellard
    memset(&cow_header, 0, sizeof(cow_header));
51 33e3963e bellard
    cow_header.magic = htonl(COW_MAGIC);
52 33e3963e bellard
    cow_header.version = htonl(COW_VERSION);
53 33e3963e bellard
    if (image_filename) {
54 33e3963e bellard
        fd = open(image_filename, O_RDONLY);
55 33e3963e bellard
        if (fd < 0) {
56 33e3963e bellard
            perror(image_filename);
57 33e3963e bellard
            exit(1);
58 33e3963e bellard
        }
59 33e3963e bellard
        image_sectors = lseek64(fd, 0, SEEK_END);
60 33e3963e bellard
        if (fstat(fd, &st) != 0) {
61 33e3963e bellard
            close(fd);
62 33e3963e bellard
            return -1;
63 33e3963e bellard
        }
64 33e3963e bellard
        close(fd);
65 33e3963e bellard
        image_sectors /= 512;
66 33e3963e bellard
        cow_header.mtime = htonl(st.st_mtime);
67 33e3963e bellard
        realpath(image_filename, cow_header.backing_file);
68 33e3963e bellard
    }
69 33e3963e bellard
    cow_header.sectorsize = htonl(512);
70 33e3963e bellard
    cow_header.size = image_sectors * 512;
71 33e3963e bellard
#ifndef WORDS_BIGENDIAN
72 33e3963e bellard
    cow_header.size = bswap64(cow_header.size);
73 33e3963e bellard
#endif    
74 33e3963e bellard
    write(cow_fd, &cow_header, sizeof(cow_header));
75 33e3963e bellard
    /* resize to include at least all the bitmap */
76 33e3963e bellard
    ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3));
77 33e3963e bellard
    lseek(cow_fd, 0, SEEK_SET);
78 33e3963e bellard
    return 0;
79 33e3963e bellard
}
80 33e3963e bellard
81 33e3963e bellard
void help(void)
82 33e3963e bellard
{
83 bee32909 bellard
    printf("qemu-mkcow version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n"
84 bee32909 bellard
           "usage: qemu-mkcow [-h] [-f disk_image] cow_image [cow_size]\n"
85 33e3963e bellard
           "Create a Copy On Write disk image from an optional raw disk image\n"
86 33e3963e bellard
           "\n"
87 33e3963e bellard
           "-f disk_image   set the raw disk image name\n"
88 33e3963e bellard
           "cow_image       the created cow_image\n"
89 33e3963e bellard
           "cow_size        the create cow_image size in MB if no raw disk image is used\n"
90 33e3963e bellard
           "\n"
91 33e3963e bellard
           "Once the cow_image is created from a raw disk image, you must not modify the original raw disk image\n"
92 33e3963e bellard
           );
93 33e3963e bellard
    exit(1);
94 33e3963e bellard
}
95 33e3963e bellard
96 33e3963e bellard
int main(int argc, char **argv)
97 33e3963e bellard
{
98 33e3963e bellard
    const char *image_filename, *cow_filename;
99 00af2b26 bellard
    int cow_fd, c, nb_args, simple_image;
100 33e3963e bellard
    int64_t image_size;
101 33e3963e bellard
    
102 33e3963e bellard
    image_filename = NULL;
103 33e3963e bellard
    image_size = 0;
104 00af2b26 bellard
    simple_image = 0;
105 33e3963e bellard
    for(;;) {
106 00af2b26 bellard
        c = getopt(argc, argv, "hf:s");
107 33e3963e bellard
        if (c == -1)
108 33e3963e bellard
            break;
109 33e3963e bellard
        switch(c) {
110 33e3963e bellard
        case 'h':
111 33e3963e bellard
            help();
112 33e3963e bellard
            break;
113 33e3963e bellard
        case 'f':
114 33e3963e bellard
            image_filename = optarg;
115 33e3963e bellard
            break;
116 00af2b26 bellard
        case 's':
117 00af2b26 bellard
            simple_image = 1;
118 00af2b26 bellard
            break;
119 33e3963e bellard
        }
120 33e3963e bellard
    }
121 33e3963e bellard
    if (!image_filename)
122 33e3963e bellard
        nb_args = 2;
123 33e3963e bellard
    else
124 33e3963e bellard
        nb_args = 1;
125 33e3963e bellard
    if (optind + nb_args != argc)
126 33e3963e bellard
        help();
127 33e3963e bellard
128 33e3963e bellard
    cow_filename = argv[optind];
129 33e3963e bellard
    if (nb_args == 2) {
130 33e3963e bellard
        image_size = (int64_t)atoi(argv[optind + 1]) * 2 * 1024;
131 33e3963e bellard
    }
132 33e3963e bellard
133 00af2b26 bellard
    cow_fd = open(cow_filename, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, 0644);
134 33e3963e bellard
    if (!cow_fd < 0)
135 33e3963e bellard
        return -1;
136 00af2b26 bellard
    if (simple_image) {
137 00af2b26 bellard
        ftruncate64(cow_fd, image_size * 512);
138 00af2b26 bellard
    } else {
139 00af2b26 bellard
        if (cow_create(cow_fd, image_filename, image_size) < 0) {
140 00af2b26 bellard
            fprintf(stderr, "%s: error while formating\n", cow_filename);
141 00af2b26 bellard
            exit(1);
142 00af2b26 bellard
        }
143 33e3963e bellard
    }
144 33e3963e bellard
    close(cow_fd);
145 33e3963e bellard
    return 0;
146 33e3963e bellard
}