xref: /freebsd/usr.bin/mkimg/vmdk.c (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
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/errno.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include "endian.h"
37 #include "image.h"
38 #include "format.h"
39 #include "mkimg.h"
40 
41 #define	VMDK_IMAGE_ROUND	1048576
42 #define	VMDK_MIN_GRAIN_SIZE	8192
43 #define	VMDK_SECTOR_SIZE	512
44 
45 struct vmdk_header {
46 	uint32_t	magic;
47 #define	VMDK_MAGIC		0x564d444b
48 	uint32_t	version;
49 #define	VMDK_VERSION		1
50 	uint32_t	flags;
51 #define	VMDK_FLAGS_NL_TEST	(1 << 0)
52 #define	VMDK_FLAGS_RGT_USED	(1 << 1)
53 #define	VMDK_FLAGS_COMPRESSED	(1 << 16)
54 #define	VMDK_FLAGS_MARKERS	(1 << 17)
55 	uint64_t	capacity;
56 	uint64_t	grain_size;
57 	uint64_t	desc_offset;
58 	uint64_t	desc_size;
59 	uint32_t	ngtes;
60 #define	VMDK_NGTES		512
61 	uint64_t	rgd_offset;
62 	uint64_t	gd_offset;
63 	uint64_t	overhead;
64 	uint8_t		unclean;
65 	uint32_t	nl_test;
66 #define	VMDK_NL_TEST		0x0a200d0a
67 	uint16_t	compress;
68 #define	VMDK_COMPRESS_NONE	0
69 #define	VMDK_COMPRESS_DEFLATE	1
70 	char		padding[433];
71 } __attribute__((__packed__));
72 
73 static const char desc_fmt[] =
74     "# Disk DescriptorFile\n"
75     "version=%d\n"
76     "CID=%08x\n"
77     "parentCID=ffffffff\n"
78     "createType=\"monolithicSparse\"\n"
79     "# Extent description\n"
80     "RW %ju SPARSE \"%s\"\n"
81     "# The Disk Data Base\n"
82     "#DDB\n"
83     "ddb.adapterType = \"ide\"\n"
84     "ddb.geometry.cylinders = \"%u\"\n"
85     "ddb.geometry.heads = \"%u\"\n"
86     "ddb.geometry.sectors = \"%u\"\n";
87 
88 static uint64_t grainsz;
89 
90 static int
91 vmdk_resize(lba_t imgsz)
92 {
93 	uint64_t imagesz;
94 
95 	imagesz = imgsz * secsz;
96 	imagesz = (imagesz + VMDK_IMAGE_ROUND - 1) & ~(VMDK_IMAGE_ROUND - 1);
97 	grainsz = (blksz < VMDK_MIN_GRAIN_SIZE) ? VMDK_MIN_GRAIN_SIZE : blksz;
98 
99 	if (verbose)
100 		fprintf(stderr, "VMDK: image size = %ju, grain size = %ju\n",
101 		    (uintmax_t)imagesz, (uintmax_t)grainsz);
102 
103 	grainsz /= VMDK_SECTOR_SIZE;
104 	return (image_set_size(imagesz / secsz));
105 }
106 
107 static int
108 vmdk_write(int fd)
109 {
110 	struct vmdk_header hdr;
111 	uint32_t *gt, *gd, *rgd;
112 	char *buf, *desc;
113 	off_t cur, lim;
114 	uint64_t imagesz;
115 	lba_t blkofs, blkcnt;
116 	size_t gdsz, gtsz;
117 	uint32_t sec, cursec;
118 	int error, desc_len, n, ngrains, ngts;
119 
120 	imagesz = (image_get_size() * secsz) / VMDK_SECTOR_SIZE;
121 
122 	memset(&hdr, 0, sizeof(hdr));
123 	le32enc(&hdr.magic, VMDK_MAGIC);
124 	le32enc(&hdr.version, VMDK_VERSION);
125 	le32enc(&hdr.flags, VMDK_FLAGS_NL_TEST | VMDK_FLAGS_RGT_USED);
126 	le64enc(&hdr.capacity, imagesz);
127 	le64enc(&hdr.grain_size, grainsz);
128 
129 	n = asprintf(&desc, desc_fmt, 1 /*version*/, 0 /*CID*/,
130 	    (uintmax_t)imagesz /*size*/, "" /*name*/,
131 	    ncyls /*cylinders*/, nheads /*heads*/, nsecs /*sectors*/);
132 	if (n == -1)
133 		return (ENOMEM);
134 
135 	desc_len = (n + VMDK_SECTOR_SIZE - 1) & ~(VMDK_SECTOR_SIZE - 1);
136 	desc = realloc(desc, desc_len);
137 	memset(desc + n, 0, desc_len - n);
138 
139 	le64enc(&hdr.desc_offset, 1);
140 	le64enc(&hdr.desc_size, desc_len / VMDK_SECTOR_SIZE);
141 	le32enc(&hdr.ngtes, VMDK_NGTES);
142 
143 	sec = desc_len / VMDK_SECTOR_SIZE + 1;
144 
145 	ngrains = imagesz / grainsz;
146 	ngts = (ngrains + VMDK_NGTES - 1) / VMDK_NGTES;
147 	gdsz = (ngts * sizeof(uint32_t) + VMDK_SECTOR_SIZE - 1) &
148 	    ~(VMDK_SECTOR_SIZE - 1);
149 
150 	gd = calloc(1, gdsz);
151 	if (gd == NULL) {
152 		free(desc);
153 		return (ENOMEM);
154 	}
155 	le64enc(&hdr.gd_offset, sec);
156 	sec += gdsz / VMDK_SECTOR_SIZE;
157 	for (n = 0; n < ngts; n++) {
158 		le32enc(gd + n, sec);
159 		sec += VMDK_NGTES * sizeof(uint32_t) / VMDK_SECTOR_SIZE;
160 	}
161 
162 	rgd = calloc(1, gdsz);
163 	if (rgd == NULL) {
164 		free(gd);
165 		free(desc);
166 		return (ENOMEM);
167 	}
168 	le64enc(&hdr.rgd_offset, sec);
169 	sec += gdsz / VMDK_SECTOR_SIZE;
170 	for (n = 0; n < ngts; n++) {
171 		le32enc(rgd + n, sec);
172 		sec += VMDK_NGTES * sizeof(uint32_t) / VMDK_SECTOR_SIZE;
173 	}
174 
175 	sec = (sec + grainsz - 1) & ~(grainsz - 1);
176 
177 	if (verbose)
178 		fprintf(stderr, "VMDK: overhead = %ju\n",
179 		    (uintmax_t)(sec * VMDK_SECTOR_SIZE));
180 
181 	le64enc(&hdr.overhead, sec);
182 	be32enc(&hdr.nl_test, VMDK_NL_TEST);
183 
184 	gt = calloc(ngts, VMDK_NGTES * sizeof(uint32_t));
185 	if (gt == NULL) {
186 		free(rgd);
187 		free(gd);
188 		free(desc);
189 		return (ENOMEM);
190 	}
191 	gtsz = ngts * VMDK_NGTES * sizeof(uint32_t);
192 
193 	cursec = sec;
194 	blkcnt = (grainsz * VMDK_SECTOR_SIZE) / secsz;
195 	for (n = 0; n < ngrains; n++) {
196 		blkofs = n * blkcnt;
197 		if (image_data(blkofs, blkcnt)) {
198 			le32enc(gt + n, cursec);
199 			cursec += grainsz;
200 		}
201 	}
202 
203 	error = 0;
204 	if (!error && sparse_write(fd, &hdr, VMDK_SECTOR_SIZE) < 0)
205 		error = errno;
206 	if (!error && sparse_write(fd, desc, desc_len) < 0)
207 		error = errno;
208 	if (!error && sparse_write(fd, gd, gdsz) < 0)
209 		error = errno;
210 	if (!error && sparse_write(fd, gt, gtsz) < 0)
211 		error = errno;
212 	if (!error && sparse_write(fd, rgd, gdsz) < 0)
213 		error = errno;
214 	if (!error && sparse_write(fd, gt, gtsz) < 0)
215 		error = errno;
216 	free(gt);
217 	free(rgd);
218 	free(gd);
219 	free(desc);
220 	if (error)
221 		return (error);
222 
223 	cur = VMDK_SECTOR_SIZE + desc_len + (gdsz + gtsz) * 2;
224 	lim = sec * VMDK_SECTOR_SIZE;
225 	if (cur < lim) {
226 		buf = calloc(1, VMDK_SECTOR_SIZE);
227 		if (buf == NULL)
228 			error = ENOMEM;
229 		while (!error && cur < lim) {
230 			if (sparse_write(fd, buf, VMDK_SECTOR_SIZE) < 0)
231 				error = errno;
232 			cur += VMDK_SECTOR_SIZE;
233 		}
234 		if (buf != NULL)
235 			free(buf);
236 	}
237 	if (error)
238 		return (error);
239 
240 	blkcnt = (grainsz * VMDK_SECTOR_SIZE) / secsz;
241 	for (n = 0; n < ngrains; n++) {
242 		blkofs = n * blkcnt;
243 		if (image_data(blkofs, blkcnt)) {
244 			error = image_copyout_region(fd, blkofs, blkcnt);
245 			if (error)
246 				return (error);
247 		}
248 	}
249 	return (image_copyout_done(fd));
250 }
251 
252 static struct mkimg_format vmdk_format = {
253 	.name = "vmdk",
254 	.description = "Virtual Machine Disk",
255 	.resize = vmdk_resize,
256 	.write = vmdk_write,
257 };
258 
259 FORMAT_DEFINE(vmdk_format);
260