xref: /freebsd/usr.bin/mkimg/vmdk.c (revision 445ed7b40948c160f2f7d363d2d0ae1ffac4aabd)
1 /*-
2  * Copyright (c) 2014 Juniper Networks, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/endian.h>
32 #include <sys/errno.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #include "image.h"
40 #include "format.h"
41 #include "mkimg.h"
42 
43 #define	VMDK_IMAGE_ROUND	1048576
44 #define	VMDK_MIN_GRAIN_SIZE	8192
45 #define	VMDK_SECTOR_SIZE	512
46 
47 struct vmdk_header {
48 	uint32_t	magic;
49 #define	VMDK_MAGIC		0x564d444b
50 	uint32_t	version;
51 #define	VMDK_VERSION		1
52 	uint32_t	flags;
53 #define	VMDK_FLAGS_NL_TEST	(1 << 0)
54 #define	VMDK_FLAGS_RGT_USED	(1 << 1)
55 #define	VMDK_FLAGS_COMPRESSED	(1 << 16)
56 #define	VMDK_FLAGS_MARKERS	(1 << 17)
57 	uint64_t	capacity;
58 	uint64_t	grain_size;
59 	uint64_t	desc_offset;
60 	uint64_t	desc_size;
61 	uint32_t	ngtes;
62 #define	VMDK_NGTES		512
63 	uint64_t	rgd_offset;
64 	uint64_t	gd_offset;
65 	uint64_t	overhead;
66 	uint8_t		unclean;
67 	uint32_t	nl_test;
68 #define	VMDK_NL_TEST		0x0a200d0a
69 	uint16_t	compress;
70 #define	VMDK_COMPRESS_NONE	0
71 #define	VMDK_COMPRESS_DEFLATE	1
72 	char		padding[433];
73 } __attribute__((__packed__));
74 
75 static const char desc_fmt[] =
76     "# Disk DescriptorFile\n"
77     "version=%d\n"
78     "CID=%08x\n"
79     "parentCID=ffffffff\n"
80     "createType=\"monolithicSparse\"\n"
81     "# Extent description\n"
82     "RW %ju SPARSE \"%s\"\n"
83     "# The Disk Data Base\n"
84     "#DDB\n"
85     "ddb.adapterType = \"ide\"\n"
86     "ddb.geometry.cylinders = \"%u\"\n"
87     "ddb.geometry.heads = \"%u\"\n"
88     "ddb.geometry.sectors = \"%u\"\n";
89 
90 static uint64_t grainsz;
91 
92 static int
93 vmdk_resize(lba_t imgsz)
94 {
95 	uint64_t imagesz;
96 
97 	imagesz = imgsz * secsz;
98 	imagesz = (imagesz + VMDK_IMAGE_ROUND - 1) & ~(VMDK_IMAGE_ROUND - 1);
99 	grainsz = (blksz < VMDK_MIN_GRAIN_SIZE) ? VMDK_MIN_GRAIN_SIZE : blksz;
100 
101 	if (verbose)
102 		fprintf(stderr, "VMDK: image size = %ju, grain size = %ju\n",
103 		    (uintmax_t)imagesz, (uintmax_t)grainsz);
104 
105 	grainsz /= VMDK_SECTOR_SIZE;
106 	return (image_set_size(imagesz / secsz));
107 }
108 
109 static int
110 vmdk_write(int fd)
111 {
112 	struct vmdk_header hdr;
113 	uint32_t *gt, *gd, *rgd;
114 	char *buf, *desc;
115 	off_t cur, lim;
116 	uint64_t imagesz;
117 	lba_t blkofs, blkcnt;
118 	size_t gdsz, gtsz;
119 	uint32_t sec, cursec;
120 	int error, desc_len, n, ngrains, ngts;
121 
122 	imagesz = (image_get_size() * secsz) / VMDK_SECTOR_SIZE;
123 
124 	memset(&hdr, 0, sizeof(hdr));
125 	le32enc(&hdr.magic, VMDK_MAGIC);
126 	le32enc(&hdr.version, VMDK_VERSION);
127 	le32enc(&hdr.flags, VMDK_FLAGS_NL_TEST | VMDK_FLAGS_RGT_USED);
128 	le64enc(&hdr.capacity, imagesz);
129 	le64enc(&hdr.grain_size, grainsz);
130 
131 	n = asprintf(&desc, desc_fmt, 1 /*version*/, 0 /*CID*/,
132 	    (uintmax_t)imagesz /*size*/, "" /*name*/,
133 	    ncyls /*cylinders*/, nheads /*heads*/, nsecs /*sectors*/);
134 	if (n == -1)
135 		return (ENOMEM);
136 
137 	desc_len = (n + VMDK_SECTOR_SIZE - 1) & ~(VMDK_SECTOR_SIZE - 1);
138 	desc = realloc(desc, desc_len);
139 	memset(desc + n, 0, desc_len - n);
140 
141 	le64enc(&hdr.desc_offset, 1);
142 	le64enc(&hdr.desc_size, desc_len / VMDK_SECTOR_SIZE);
143 	le32enc(&hdr.ngtes, VMDK_NGTES);
144 
145 	sec = desc_len / VMDK_SECTOR_SIZE + 1;
146 
147 	ngrains = imagesz / grainsz;
148 	ngts = (ngrains + VMDK_NGTES - 1) / VMDK_NGTES;
149 	gdsz = (ngts * sizeof(uint32_t) + VMDK_SECTOR_SIZE - 1) &
150 	    ~(VMDK_SECTOR_SIZE - 1);
151 
152 	gd = calloc(gdsz, 1);
153 	if (gd == NULL) {
154 		free(desc);
155 		return (ENOMEM);
156 	}
157 	le64enc(&hdr.gd_offset, sec);
158 	sec += gdsz / VMDK_SECTOR_SIZE;
159 	for (n = 0; n < ngts; n++) {
160 		le32enc(gd + n, sec);
161 		sec += VMDK_NGTES * sizeof(uint32_t) / VMDK_SECTOR_SIZE;
162 	}
163 
164 	rgd = calloc(gdsz, 1);
165 	if (rgd == NULL) {
166 		free(gd);
167 		free(desc);
168 		return (ENOMEM);
169 	}
170 	le64enc(&hdr.rgd_offset, sec);
171 	sec += gdsz / VMDK_SECTOR_SIZE;
172 	for (n = 0; n < ngts; n++) {
173 		le32enc(rgd + n, sec);
174 		sec += VMDK_NGTES * sizeof(uint32_t) / VMDK_SECTOR_SIZE;
175 	}
176 
177 	sec = (sec + grainsz - 1) & ~(grainsz - 1);
178 
179 	if (verbose)
180 		fprintf(stderr, "VMDK: overhead = %ju\n",
181 		    (uintmax_t)(sec * VMDK_SECTOR_SIZE));
182 
183 	le64enc(&hdr.overhead, sec);
184 	be32enc(&hdr.nl_test, VMDK_NL_TEST);
185 
186 	gtsz = ngts * VMDK_NGTES * sizeof(uint32_t);
187 	gt = calloc(gtsz, 1);
188 	if (gt == NULL) {
189 		free(rgd);
190 		free(gd);
191 		free(desc);
192 		return (ENOMEM);
193 	}
194 
195 	cursec = sec;
196 	blkcnt = (grainsz * VMDK_SECTOR_SIZE) / secsz;
197 	for (n = 0; n < ngrains; n++) {
198 		blkofs = n * blkcnt;
199 		if (image_data(blkofs, blkcnt)) {
200 			le32enc(gt + n, cursec);
201 			cursec += grainsz;
202 		}
203 	}
204 
205 	error = 0;
206 	if (!error && sparse_write(fd, &hdr, VMDK_SECTOR_SIZE) < 0)
207 		error = errno;
208 	if (!error && sparse_write(fd, desc, desc_len) < 0)
209 		error = errno;
210 	if (!error && sparse_write(fd, gd, gdsz) < 0)
211 		error = errno;
212 	if (!error && sparse_write(fd, gt, gtsz) < 0)
213 		error = errno;
214 	if (!error && sparse_write(fd, rgd, gdsz) < 0)
215 		error = errno;
216 	if (!error && sparse_write(fd, gt, gtsz) < 0)
217 		error = errno;
218 	free(gt);
219 	free(rgd);
220 	free(gd);
221 	free(desc);
222 	if (error)
223 		return (error);
224 
225 	cur = VMDK_SECTOR_SIZE + desc_len + (gdsz + gtsz) * 2;
226 	lim = sec * VMDK_SECTOR_SIZE;
227 	if (cur < lim) {
228 		buf = calloc(VMDK_SECTOR_SIZE, 1);
229 		if (buf == NULL)
230 			error = ENOMEM;
231 		while (!error && cur < lim) {
232 			if (sparse_write(fd, buf, VMDK_SECTOR_SIZE) < 0)
233 				error = errno;
234 			cur += VMDK_SECTOR_SIZE;
235 		}
236 		if (buf != NULL)
237 			free(buf);
238 	}
239 	if (error)
240 		return (error);
241 
242 	blkcnt = (grainsz * VMDK_SECTOR_SIZE) / secsz;
243 	for (n = 0; n < ngrains; n++) {
244 		blkofs = n * blkcnt;
245 		if (image_data(blkofs, blkcnt)) {
246 			error = image_copyout_region(fd, blkofs, blkcnt);
247 			if (error)
248 				return (error);
249 		}
250 	}
251 	return (image_copyout_done(fd));
252 }
253 
254 static struct mkimg_format vmdk_format = {
255 	.name = "vmdk",
256 	.description = "Virtual Machine Disk",
257 	.resize = vmdk_resize,
258 	.write = vmdk_write,
259 };
260 
261 FORMAT_DEFINE(vmdk_format);
262