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