vmdk.c revision 266132
1176417Sthompsa/*- 2176417Sthompsa * Copyright (c) 2014 Juniper Networks, Inc. 3176417Sthompsa * All rights reserved. 4176417Sthompsa * 5176417Sthompsa * Redistribution and use in source and binary forms, with or without 6176417Sthompsa * modification, are permitted provided that the following conditions 7176417Sthompsa * are met: 8176417Sthompsa * 1. Redistributions of source code must retain the above copyright 9176417Sthompsa * notice, this list of conditions and the following disclaimer. 10176417Sthompsa * 2. Redistributions in binary form must reproduce the above copyright 11176417Sthompsa * notice, this list of conditions and the following disclaimer in the 12176417Sthompsa * documentation and/or other materials provided with the distribution. 13176417Sthompsa * 14176417Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15176417Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16176417Sthompsa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17176417Sthompsa * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18176417Sthompsa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19176417Sthompsa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20176417Sthompsa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21176417Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22176417Sthompsa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23176417Sthompsa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24176417Sthompsa * SUCH DAMAGE. 25176417Sthompsa */ 26176417Sthompsa 27176417Sthompsa#include <sys/cdefs.h> 28266013Smarius__FBSDID("$FreeBSD: user/marcel/mkimg/vmdk.c 266132 2014-05-15 14:48:25Z marcel $"); 29206622Suqs 30176417Sthompsa#include <sys/types.h> 31176417Sthompsa#include <sys/apm.h> 32176417Sthompsa#include <sys/endian.h> 33176417Sthompsa#include <sys/errno.h> 34176417Sthompsa#include <stdint.h> 35176417Sthompsa#include <stdio.h> 36176417Sthompsa#include <stdlib.h> 37176417Sthompsa#include <string.h> 38176417Sthompsa#include <unistd.h> 39176417Sthompsa 40176417Sthompsa#include "image.h" 41176417Sthompsa#include "format.h" 42176417Sthompsa#include "mkimg.h" 43176417Sthompsa 44176417Sthompsa#define VMDK_IMAGE_ROUND 1048576 45176417Sthompsa#define VMDK_MIN_GRAIN_SIZE 8192 46176417Sthompsa#define VMDK_SECTOR_SIZE 512 47176417Sthompsa 48176417Sthompsastruct vmdk_header { 49176417Sthompsa uint32_t magic; 50176417Sthompsa#define VMDK_MAGIC 0x564d444b 51176417Sthompsa uint32_t version; 52176417Sthompsa#define VMDK_VERSION 1 53176417Sthompsa uint32_t flags; 54176417Sthompsa#define VMDK_FLAGS_NL_TEST (1 << 0) 55176417Sthompsa#define VMDK_FLAGS_RGT_USED (1 << 1) 56176417Sthompsa#define VMDK_FLAGS_COMPRESSED (1 << 16) 57176417Sthompsa#define VMDK_FLAGS_MARKERS (1 << 17) 58176417Sthompsa uint64_t capacity; 59176417Sthompsa uint64_t grain_size; 60176417Sthompsa uint64_t desc_offset; 61176417Sthompsa uint64_t desc_size; 62176417Sthompsa uint32_t ngtes; 63176417Sthompsa#define VMDK_NGTES 512 64176417Sthompsa uint64_t rgd_offset; 65176417Sthompsa uint64_t gd_offset; 66176417Sthompsa uint64_t overhead; 67176417Sthompsa uint8_t unclean; 68176417Sthompsa uint32_t nl_test; 69176417Sthompsa#define VMDK_NL_TEST 0x0a200d0a 70176417Sthompsa uint16_t compress; 71176417Sthompsa#define VMDK_COMPRESS_NONE 0 72176417Sthompsa#define VMDK_COMPRESS_DEFLATE 1 73176417Sthompsa char padding[433]; 74176417Sthompsa} __attribute__((__packed__)); 75266013Smarius 76176417Sthompsastatic const char desc_fmt[] = 77176417Sthompsa "# Disk DescriptorFile\n" 78176417Sthompsa "version=%d\n" 79176417Sthompsa "CID=%08x\n" 80176417Sthompsa "parentCID=ffffffff\n" 81176417Sthompsa "createType=\"monolithicSparse\"\n" 82176417Sthompsa "# Extent description\n" 83176417Sthompsa "RW %ju SPARSE \"%s\"\n" 84176417Sthompsa "# The Disk Data Base\n" 85176417Sthompsa "#DDB\n" 86176417Sthompsa "ddb.adapterType = \"ide\"\n" 87176417Sthompsa "ddb.geometry.cylinders = \"%u\"\n" 88176417Sthompsa "ddb.geometry.heads = \"%u\"\n" 89 "ddb.geometry.sectors = \"%u\"\n"; 90 91static uint64_t grainsz; 92 93static int 94vmdk_resize(lba_t imgsz) 95{ 96 uint64_t imagesz; 97 98 imagesz = imgsz * secsz; 99 imagesz = (imagesz + VMDK_IMAGE_ROUND - 1) & ~(VMDK_IMAGE_ROUND - 1); 100 grainsz = (blksz < VMDK_MIN_GRAIN_SIZE) ? VMDK_MIN_GRAIN_SIZE : blksz; 101 102 if (verbose) 103 fprintf(stderr, "VMDK: image size = %ju, grain size = %ju\n", 104 (uintmax_t)imagesz, (uintmax_t)grainsz); 105 106 grainsz /= VMDK_SECTOR_SIZE; 107 return (image_set_size(imagesz / secsz)); 108} 109 110static int 111vmdk_write(int fd) 112{ 113 struct vmdk_header hdr; 114 uint32_t *gt, *gd; 115 char *buf, *desc; 116 off_t cur, lim; 117 uint64_t imagesz; 118 size_t gdsz, gtsz; 119 uint32_t sec; 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 le64enc(&hdr.rgd_offset, sec); 147 le64enc(&hdr.gd_offset, sec); 148 149 ngrains = imagesz / grainsz; 150 ngts = (ngrains + VMDK_NGTES - 1) / VMDK_NGTES; 151 gdsz = (ngts * sizeof(uint32_t) + VMDK_SECTOR_SIZE - 1) & 152 ~(VMDK_SECTOR_SIZE - 1); 153 gd = calloc(gdsz, 1); 154 if (gd == NULL) { 155 free(desc); 156 return (ENOMEM); 157 } 158 159 sec += gdsz / VMDK_SECTOR_SIZE; 160 for (n = 0; n < ngts; n++) { 161 le32enc(gd + n, sec); 162 sec += VMDK_NGTES * sizeof(uint32_t) / VMDK_SECTOR_SIZE; 163 } 164 165 sec = (sec + grainsz - 1) & ~(grainsz - 1); 166 167 if (verbose) 168 fprintf(stderr, "VMDK: overhead = %ju\n", 169 (uintmax_t)(sec * VMDK_SECTOR_SIZE)); 170 171 le64enc(&hdr.overhead, sec); 172 be32enc(&hdr.nl_test, VMDK_NL_TEST); 173 174 gtsz = ngts * VMDK_NGTES * sizeof(uint32_t); 175 gt = calloc(gtsz, 1); 176 if (gt == NULL) { 177 free(gd); 178 free(desc); 179 return (ENOMEM); 180 } 181 182 for (n = 0; n < ngrains; n++) 183 le32enc(gt + n, sec + n * grainsz); 184 185 error = 0; 186 if (!error && sparse_write(fd, &hdr, VMDK_SECTOR_SIZE) < 0) 187 error = errno; 188 if (!error && sparse_write(fd, desc, desc_len) < 0) 189 error = errno; 190 if (!error && sparse_write(fd, gd, gdsz) < 0) 191 error = errno; 192 if (!error && sparse_write(fd, gt, gtsz) < 0) 193 error = errno; 194 free(gt); 195 free(gd); 196 free(desc); 197 if (error) 198 return (error); 199 200 cur = VMDK_SECTOR_SIZE + desc_len + gdsz + gtsz; 201 lim = sec * VMDK_SECTOR_SIZE; 202 if (cur < lim) { 203 buf = calloc(VMDK_SECTOR_SIZE, 1); 204 if (buf == NULL) 205 error = ENOMEM; 206 while (!error && cur < lim) { 207 if (sparse_write(fd, buf, VMDK_SECTOR_SIZE) < 0) 208 error = errno; 209 cur += VMDK_SECTOR_SIZE; 210 } 211 if (buf != NULL) 212 free(buf); 213 } 214 if (!error) 215 error = image_copyout(fd); 216 return (error); 217} 218 219static struct mkimg_format vmdk_format = { 220 .name = "vmdk", 221 .description = "Virtual Machine Disk", 222 .resize = vmdk_resize, 223 .write = vmdk_write, 224}; 225 226FORMAT_DEFINE(vmdk_format); 227