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