root / qemu-mkcow.c @ 2edcdce3
History | View | Annotate | Download (4.3 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 <sys/mman.h> |
32 | 33e3963e | bellard | #include <fcntl.h> |
33 | 33e3963e | bellard | #include <signal.h> |
34 | 33e3963e | bellard | #include <time.h> |
35 | 33e3963e | bellard | #include <sys/time.h> |
36 | 33e3963e | bellard | #include <malloc.h> |
37 | 33e3963e | bellard | #include <termios.h> |
38 | 33e3963e | bellard | #include <sys/poll.h> |
39 | 33e3963e | bellard | #include <errno.h> |
40 | 33e3963e | bellard | #include <sys/wait.h> |
41 | abcd5da7 | bellard | #include <sys/stat.h> |
42 | 33e3963e | bellard | #include <netinet/in.h> |
43 | 33e3963e | bellard | |
44 | 33e3963e | bellard | #include "vl.h" |
45 | 33e3963e | bellard | |
46 | abcd5da7 | bellard | #include "bswap.h" |
47 | 33e3963e | bellard | |
48 | 33e3963e | bellard | int cow_create(int cow_fd, const char *image_filename, |
49 | 33e3963e | bellard | int64_t image_sectors) |
50 | 33e3963e | bellard | { |
51 | 33e3963e | bellard | struct cow_header_v2 cow_header;
|
52 | 33e3963e | bellard | int fd;
|
53 | 33e3963e | bellard | struct stat st;
|
54 | 33e3963e | bellard | |
55 | 33e3963e | bellard | memset(&cow_header, 0, sizeof(cow_header)); |
56 | 33e3963e | bellard | cow_header.magic = htonl(COW_MAGIC); |
57 | 33e3963e | bellard | cow_header.version = htonl(COW_VERSION); |
58 | 33e3963e | bellard | if (image_filename) {
|
59 | 33e3963e | bellard | fd = open(image_filename, O_RDONLY); |
60 | 33e3963e | bellard | if (fd < 0) { |
61 | 33e3963e | bellard | perror(image_filename); |
62 | 33e3963e | bellard | exit(1);
|
63 | 33e3963e | bellard | } |
64 | 33e3963e | bellard | image_sectors = lseek64(fd, 0, SEEK_END);
|
65 | 33e3963e | bellard | if (fstat(fd, &st) != 0) { |
66 | 33e3963e | bellard | close(fd); |
67 | 33e3963e | bellard | return -1; |
68 | 33e3963e | bellard | } |
69 | 33e3963e | bellard | close(fd); |
70 | 33e3963e | bellard | image_sectors /= 512;
|
71 | 33e3963e | bellard | cow_header.mtime = htonl(st.st_mtime); |
72 | 33e3963e | bellard | realpath(image_filename, cow_header.backing_file); |
73 | 33e3963e | bellard | } |
74 | 33e3963e | bellard | cow_header.sectorsize = htonl(512);
|
75 | 33e3963e | bellard | cow_header.size = image_sectors * 512;
|
76 | 33e3963e | bellard | #ifndef WORDS_BIGENDIAN
|
77 | 33e3963e | bellard | cow_header.size = bswap64(cow_header.size); |
78 | 33e3963e | bellard | #endif
|
79 | 33e3963e | bellard | write(cow_fd, &cow_header, sizeof(cow_header));
|
80 | 33e3963e | bellard | /* resize to include at least all the bitmap */
|
81 | 33e3963e | bellard | ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3)); |
82 | 33e3963e | bellard | lseek(cow_fd, 0, SEEK_SET);
|
83 | 33e3963e | bellard | return 0; |
84 | 33e3963e | bellard | } |
85 | 33e3963e | bellard | |
86 | 33e3963e | bellard | void help(void) |
87 | 33e3963e | bellard | { |
88 | 4690764b | bellard | printf("vlmkcow version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
89 | 4690764b | bellard | "usage: vlmkcow [-h] [-f disk_image] cow_image [cow_size]\n"
|
90 | 33e3963e | bellard | "Create a Copy On Write disk image from an optional raw disk image\n"
|
91 | 33e3963e | bellard | "\n"
|
92 | 33e3963e | bellard | "-f disk_image set the raw disk image name\n"
|
93 | 33e3963e | bellard | "cow_image the created cow_image\n"
|
94 | 33e3963e | bellard | "cow_size the create cow_image size in MB if no raw disk image is used\n"
|
95 | 33e3963e | bellard | "\n"
|
96 | 33e3963e | bellard | "Once the cow_image is created from a raw disk image, you must not modify the original raw disk image\n"
|
97 | 33e3963e | bellard | ); |
98 | 33e3963e | bellard | exit(1);
|
99 | 33e3963e | bellard | } |
100 | 33e3963e | bellard | |
101 | 33e3963e | bellard | int main(int argc, char **argv) |
102 | 33e3963e | bellard | { |
103 | 33e3963e | bellard | const char *image_filename, *cow_filename; |
104 | 33e3963e | bellard | int cow_fd, c, nb_args;
|
105 | 33e3963e | bellard | int64_t image_size; |
106 | 33e3963e | bellard | |
107 | 33e3963e | bellard | image_filename = NULL;
|
108 | 33e3963e | bellard | image_size = 0;
|
109 | 33e3963e | bellard | for(;;) {
|
110 | 33e3963e | bellard | c = getopt(argc, argv, "hf:");
|
111 | 33e3963e | bellard | if (c == -1) |
112 | 33e3963e | bellard | break;
|
113 | 33e3963e | bellard | switch(c) {
|
114 | 33e3963e | bellard | case 'h': |
115 | 33e3963e | bellard | help(); |
116 | 33e3963e | bellard | break;
|
117 | 33e3963e | bellard | case 'f': |
118 | 33e3963e | bellard | image_filename = optarg; |
119 | 33e3963e | bellard | break;
|
120 | 33e3963e | bellard | } |
121 | 33e3963e | bellard | } |
122 | 33e3963e | bellard | if (!image_filename)
|
123 | 33e3963e | bellard | nb_args = 2;
|
124 | 33e3963e | bellard | else
|
125 | 33e3963e | bellard | nb_args = 1;
|
126 | 33e3963e | bellard | if (optind + nb_args != argc)
|
127 | 33e3963e | bellard | help(); |
128 | 33e3963e | bellard | |
129 | 33e3963e | bellard | cow_filename = argv[optind]; |
130 | 33e3963e | bellard | if (nb_args == 2) { |
131 | 33e3963e | bellard | image_size = (int64_t)atoi(argv[optind + 1]) * 2 * 1024; |
132 | 33e3963e | bellard | } |
133 | 33e3963e | bellard | |
134 | 33e3963e | bellard | cow_fd = open(cow_filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
|
135 | 33e3963e | bellard | if (!cow_fd < 0) |
136 | 33e3963e | bellard | return -1; |
137 | 33e3963e | bellard | if (cow_create(cow_fd, image_filename, image_size) < 0) { |
138 | 33e3963e | bellard | fprintf(stderr, "%s: error while formating\n", cow_filename);
|
139 | 33e3963e | bellard | exit(1);
|
140 | 33e3963e | bellard | } |
141 | 33e3963e | bellard | close(cow_fd); |
142 | 33e3963e | bellard | return 0; |
143 | 33e3963e | bellard | } |