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