svn commit: r245073 - in user/hrs/releng/usr.sbin: . makevd

Hiroki Sato hrs at FreeBSD.org
Sat Jan 5 21:52:03 UTC 2013


Author: hrs
Date: Sat Jan  5 21:52:01 2013
New Revision: 245073
URL: http://svnweb.freebsd.org/changeset/base/245073

Log:
  Add makevd(8) to generate a virtual disk image from a UFS image.
  
  Currently it supports VMWare VMDK format.  The generated .vmdk file is a hosted
  sparse extent with an embedded descriptor for VMWare hosted products.

Added:
  user/hrs/releng/usr.sbin/makevd/
  user/hrs/releng/usr.sbin/makevd/Makefile   (contents, props changed)
  user/hrs/releng/usr.sbin/makevd/makevd.8
  user/hrs/releng/usr.sbin/makevd/makevd.c   (contents, props changed)
  user/hrs/releng/usr.sbin/makevd/makevd.h   (contents, props changed)
  user/hrs/releng/usr.sbin/makevd/raw.c   (contents, props changed)
  user/hrs/releng/usr.sbin/makevd/vmdk.c   (contents, props changed)
  user/hrs/releng/usr.sbin/makevd/vmdk.h   (contents, props changed)
Modified:
  user/hrs/releng/usr.sbin/Makefile

Modified: user/hrs/releng/usr.sbin/Makefile
==============================================================================
--- user/hrs/releng/usr.sbin/Makefile	Sat Jan  5 21:42:14 2013	(r245072)
+++ user/hrs/releng/usr.sbin/Makefile	Sat Jan  5 21:52:01 2013	(r245073)
@@ -39,6 +39,7 @@ SUBDIR=	adduser \
 	kldxref \
 	mailwrapper \
 	makefs \
+	makevd \
 	manctl \
 	memcontrol \
 	mergemaster \

Added: user/hrs/releng/usr.sbin/makevd/Makefile
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/hrs/releng/usr.sbin/makevd/Makefile	Sat Jan  5 21:52:01 2013	(r245073)
@@ -0,0 +1,11 @@
+# $FreeBSD$
+
+PROG=	makevd
+MAN=	makevd.8
+SRCS=	makevd.c \
+	raw.c \
+	vmdk.c
+WARNS?=	6
+CFLAGS+= -I${.CURDIR}
+
+.include <bsd.prog.mk>

Added: user/hrs/releng/usr.sbin/makevd/makevd.8
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/hrs/releng/usr.sbin/makevd/makevd.8	Sat Jan  5 21:52:01 2013	(r245073)
@@ -0,0 +1,111 @@
+.\"
+.\" Copyright (c) 2011
+.\"	Hiroki Sato <hrs at FreeBSD.org> All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND ANY
+.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC BE
+.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd January 6, 2013
+.Dt MAKEVD 8
+.Os
+.Sh NAME
+.Nm makevd
+.Nd create a virtual disk image from a file system image
+.Sh SYNOPSIS
+.Nm
+.Op Fl B Ar byte-order
+.Op Fl o Ar image-options
+.Op Fl t Ar image-type
+.Ar input-image-file
+.Sh DESCRIPTION
+The utility
+.Nm
+creates a virtual disk image from the file system image specified in
+.Ar input-image-file .
+The output file name can be specified by using
+.Op Fl o Ar imagename
+option.
+If not specified, it will be determined by the
+.Ar input-image-file .
+The options are as follows:
+.Bl -tag -width flag
+.It Fl B Ar byte-order
+Set the byte order of the image to
+.Ar byte-order .
+Valid byte orders are
+.Ql 4321 ,
+.Ql big ,
+or
+.Ql be
+for big endian, and
+.Ql 1234 ,
+.Ql little ,
+or
+.Ql le
+for little endian.
+Some file systems may have a fixed byte order; in those cases this
+argument will be ignored.
+.Ar image-options
+is a comma separated list of options.
+Valid image specific options are detailed below.
+.It Fl t Ar image-type
+Create an
+.Ar image-type
+virtual disk image.
+The following image types are supported:
+.Bl -tag -width cd9660 -offset indent
+.It Sy vmdk
+VMWare Virtual Machine DisK (VMDK) Format.
+.It Sy raw
+No conversion (default).
+.El
+.El
+.\"
+.\"
+.Ss VMDK-specific options
+.Sy vmdk
+images have VMDK-specific parameters that may be provided.
+Some are optional, and some are mandatory.
+Each of the options consists of a keyword, an equal sign
+.Pq Ql = ,
+and a value.
+The following keywords are supported:
+.Pp
+.Bl -tag -width optimization -offset indent -compact
+.It Fl o Sy uuid
+UUID for the image.  Mandatory.
+.El
+.Sh SEE ALSO
+.Xr uuidgen 1
+.Sh HISTORY
+The
+.Nm
+utility first appeared in
+.Fx 10.0
+.Sh AUTHORS
+.An Hiroki Sato
+.Aq hrs at FreeBSD.org
+.Sh BUGS
+The
+.Fl B Ar byte-order
+option does not work yet.

Added: user/hrs/releng/usr.sbin/makevd/makevd.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/hrs/releng/usr.sbin/makevd/makevd.c	Sat Jan  5 21:52:01 2013	(r245073)
@@ -0,0 +1,202 @@
+/*-
+ * Copyright (c) 2011
+ *	Hiroki Sato <hrs at FreeBSD.org>  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ *
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sysexits.h>
+
+#include "makevd.h"
+#include "vmdk.h"
+
+static LIST_HEAD(optlisthead_t, optlist) oplhead;
+
+/* list of supported disk image type and dispatch functions */
+static struct imtype {
+	const char	*imt_type;
+	int		(*imt_makeim)(struct iminfo *);
+} imtypes[] = {
+	{ "vmdk", vmdk_makeim },
+	{ "none", raw_makeim },
+	{ "raw", raw_makeim },
+	{ .imt_type = NULL },
+};
+
+static struct imtype *get_imtype(const char *);
+static void usage(void);
+
+int
+main(int argc, char *argv[])
+{
+	struct imtype *imt;
+	struct iminfo imi;
+	struct optlist *opl;
+	struct stat sb;
+	int ch;
+	int ifd;
+	int opl_new;
+	char *val;
+
+	setprogname(argv[0]);
+
+	if ((imt = get_imtype(DEFAULT_IMTYPE)) == NULL)
+		errx(1, "Unknown default image type `%s'.", DEFAULT_IMTYPE);
+
+	memset(&imi, 0, sizeof(imi));
+	memset(&sb, 0, sizeof(sb));
+	LIST_INIT(&oplhead);
+	imi.imi_fd = -1;
+
+	while ((ch = getopt(argc, argv, "B:t:o:")) != -1) {
+		switch (ch) {
+		case 'B':
+			if (strcmp(optarg, "be") == 0 ||
+			    strcmp(optarg, "4321") == 0 ||
+			    strcmp(optarg, "big") == 0) {
+#if BYTE_ORDER == LITTLE_ENDIAN
+				imi.imi_needswap = 1;
+#endif
+			} else if (strcmp(optarg, "le") == 0 ||
+			    strcmp(optarg, "1234") == 0 ||
+			    strcmp(optarg, "little") == 0) {
+#if BYTE_ORDER == BIG_ENDIAN
+				imi.imi_needswap = 1;
+#endif
+			} else {
+				warnx("Invalid endian `%s'.", optarg);
+				usage();
+			}
+			break;
+
+		case 't':
+			if ((imt = get_imtype(optarg)) == NULL)
+				errx(1, "Unknown image type `%s'.", optarg);
+			break;
+
+		case 'o':
+			opl_new = 0;
+			val = strchr(optarg, '=');
+			if (val == '\0')
+				val = NULL;
+			else {
+				*val = '\0';
+				val += 1;
+			}
+			/* Check duplicate entry. */
+			LIST_FOREACH(opl, &oplhead, opl_next)
+			    if (strcmp(opl->opl_name, optarg) == 0)
+				    break;
+			if (opl == NULL) {
+				opl = calloc(1, sizeof(*opl));
+				if (opl == NULL)
+					err(EX_OSERR, "%s", optarg);
+				opl_new = 1;
+			} else {
+				free(opl->opl_name);
+				opl->opl_name = NULL;
+				if (opl->opl_val) {
+					free(opl->opl_val);
+					opl->opl_val = NULL;
+				}
+			}
+			if (val)
+				opl->opl_val = strdup(val);
+			opl->opl_name = strdup(optarg);
+			if (opl_new)
+				LIST_INSERT_HEAD(&oplhead, opl, opl_next);
+			break;
+		case '?':
+		case 'h':
+		default:
+			usage();
+			/* NOTREACHED */
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	if (argc != 1)
+		usage();
+
+	ifd = open(argv[0], O_RDONLY);
+	if (ifd < 0)
+		err(EX_NOINPUT, "%s", argv[0]);
+	if (fstat(ifd, &sb) < 0)
+		err(EX_IOERR, "%s", argv[0]);
+
+	imi.imi_fd = ifd;
+	imi.imi_size = sb.st_size;
+	imi.imi_oplhead = &oplhead;
+	LIST_FOREACH(opl, &oplhead, opl_next) {
+#if 0
+		printf("options: %s = %s\n", opl->opl_name, opl->opl_val);
+#endif
+		if (strcmp(opl->opl_name, "uuid") == 0)
+			imi.imi_uuid = opl->opl_val;
+		else if (strcmp(opl->opl_name, "imagename") == 0)
+			imi.imi_imagename = opl->opl_val;
+	}
+	if (imi.imi_imagename == NULL)
+		imi.imi_imagename = strdup(argv[0]);
+
+	imt->imt_makeim(&imi);
+
+	return (0);
+}
+
+static struct imtype *
+get_imtype(const char *type)
+{
+	int i;
+
+	for (i = 0; imtypes[i].imt_type != NULL; i++)
+		if (strcmp(imtypes[i].imt_type, type) == 0)
+			return (&imtypes[i]);
+	return (NULL);
+}
+
+static void
+usage(void)
+{
+	const char *prog;
+
+	prog = getprogname();
+	fprintf(stderr, "usage: %s [-t image-type] [-o image-options] "
+	    "input-file\n",prog);
+
+	exit(1);
+}

Added: user/hrs/releng/usr.sbin/makevd/makevd.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/hrs/releng/usr.sbin/makevd/makevd.h	Sat Jan  5 21:52:01 2013	(r245073)
@@ -0,0 +1,59 @@
+/*-
+ * Copyright (c) 2011
+ *	Hiroki Sato <hrs at FreeBSD.org>  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ *
+ */
+
+#ifndef	_MAKEVD_H
+#define	_MAKEVD_H
+
+#include <sys/queue.h>
+
+struct iminfo {
+	int	imi_fd;
+	off_t	imi_size;
+	int	imi_needswap;
+
+	char	*imi_imagename;	/* vmdk specific */
+	char	*imi_uuid;	/* vmdk specific */
+
+	struct optlisthead_t	*imi_oplhead;
+};
+
+struct optlist {
+	LIST_ENTRY(optlist) opl_next;
+	char *opl_name;
+	char *opl_val;
+};
+
+int	vmdk_makeim(struct iminfo *);
+int	raw_makeim(struct iminfo *);
+
+#ifndef	DEFAULT_IMTYPE
+#define	DEFAULT_IMTYPE	"raw"
+#endif
+
+#endif	/* _MAKEVD_H */

Added: user/hrs/releng/usr.sbin/makevd/raw.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/hrs/releng/usr.sbin/makevd/raw.c	Sat Jan  5 21:52:01 2013	(r245073)
@@ -0,0 +1,83 @@
+/*-
+ * Copyright (c) 2011
+ *	Hiroki Sato <hrs at FreeBSD.org>  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ *
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sysexits.h>
+
+#include "makevd.h"
+
+int
+raw_makeim(struct iminfo *imi)
+{
+	char buf[BUFSIZ], rawfile[PATH_MAX + 10];
+	ssize_t len0, len = 0;
+	int ifd, ofd;
+
+	ifd = imi->imi_fd;
+
+	if (strcmp(imi->imi_imagename, "-") == 0)
+		ofd = STDOUT_FILENO;
+	else {
+		snprintf(rawfile, sizeof(rawfile), "%s.raw",
+		    imi->imi_imagename);
+		ofd = open(rawfile, O_WRONLY|O_CREAT|O_TRUNC,
+		    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+		if (ofd < 0)
+			err(EX_CANTCREAT, "%s", rawfile);
+	}
+
+	for (;;) {
+		len0 = read(ifd, buf, sizeof(buf));
+		if (len0 == 0)
+			break;
+		if (len0 < 0) {
+			warn("read error");
+			return (1);
+		}
+		len = write(ofd, buf, len0);
+		if (len < 0) {
+			warn("write error");
+			return (1);
+		}
+	}
+
+	return (0);
+}

Added: user/hrs/releng/usr.sbin/makevd/vmdk.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/hrs/releng/usr.sbin/makevd/vmdk.c	Sat Jan  5 21:52:01 2013	(r245073)
@@ -0,0 +1,156 @@
+/*-
+ * Copyright (c) 2011
+ *	Hiroki Sato <hrs at FreeBSD.org>  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ *
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/uio.h>
+
+#include <err.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+#include "makevd.h"
+#include "vmdk.h"
+
+int
+vmdk_makeim(struct iminfo *imi)
+{
+	struct SparseExtentHeader SEH, *imh;
+	uint64_t sectors, heads, cylinders, imagesize;
+	char vmdkfile[PATH_MAX + 10], *vmdkfilebase;
+	char buf[BUFSIZ];
+	char desc[1024];
+	ssize_t len0, len = 0;
+	int ifd, ofd;
+
+	imh = &SEH;
+	ifd = imi->imi_fd;
+	imagesize = imi->imi_size;
+
+	memset(imh, 0, sizeof(*imh));
+	memset(desc, 0, sizeof(desc));
+
+	if (imi->imi_uuid == NULL)
+		errx(EX_USAGE, "-o uuid option must be specified.");
+
+	snprintf(vmdkfile, sizeof(vmdkfile), "%s.vmdk", imi->imi_imagename);
+	ofd = open(vmdkfile, O_WRONLY|O_CREAT|O_TRUNC,
+	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
+	if (ofd < 0)
+		err(EX_CANTCREAT, "%s", vmdkfile);
+
+	vmdkfilebase = strrchr(vmdkfile, '/');
+	if (vmdkfilebase == '\0')
+		vmdkfilebase = vmdkfile;
+
+	imh->magicNumber = SPARSE_MAGICNUMBER;
+	imh->version = SPARSE_VERSION_DEFAULT;
+	imh->flags = 1;
+	imh->capacity = 0;
+	imh->grainSize = 16;
+        imh->descriptorOffset = (sizeof(*imh) + 511) / 512;
+        imh->descriptorSize = (sizeof(desc) + 511) / 512;
+        imh->numGTEsPerGT = 512;
+        imh->rgdOffset = 0;
+        imh->gdOffset = 0;
+        imh->overHead = imh->descriptorOffset + imh->descriptorSize;
+        imh->uncleanShutdown = 0;
+        imh->singleEndLineChar = '\n';
+        imh->nonEndLineChar = ' ';
+        imh->doubleEndLineChar1 = '\r';
+        imh->doubleEndLineChar2 = '\n';
+
+	sectors = 63;
+	heads = 16;
+	cylinders = imagesize / (sectors * heads * 512);
+	while (cylinders > 1024) {
+		cylinders >>= 1;
+		heads <<= 1;
+	}
+
+	snprintf(desc, sizeof(desc),
+	    "# Disk Descriptor File\n"
+	    "version=1\n"
+	    "CID=fffffffe\n"
+	    "parentCID=ffffffff\n"
+	    "createType=\"monolithicFlat\"\n"
+	    "# Extent Description\n"
+	    "RW %" PRIu64 " FLAT \"%s\" %" PRIu64 "\n"
+	    "# Disk Data Base\n"
+	    "ddb.toolsVersion = \"0\"\n"
+	    "ddb.virtualHWVersion = \"3\"\n"
+	    "ddb.geometry.sectors = \"%" PRIu64 "\"\n"
+	    "ddb.adapterType = \"ide\"\n"
+	    "ddb.geometry.heads = \"%" PRIu64 "\"\n"
+	    "ddb.geometry.cylinders = \"%" PRIu64 "\"\n"
+	    "ddb.uuid.image=\"%s\"\n",
+	    cylinders * sectors * heads,
+	    vmdkfilebase,
+	    imh->overHead,
+	    sectors,
+	    heads,
+	    cylinders,
+	    imi->imi_uuid);
+
+	len0 = write(ofd, imh, sizeof(*imh));
+	if (len0 != sizeof(*imh)) {
+		warn("write error");
+		return (1);
+	}
+	len0 = write(ofd, desc, sizeof(desc));
+	if (len0 != sizeof(desc)) {
+		warn("write error");
+		return (1);
+	}
+	for (;;) {
+		len0 = read(ifd, buf, sizeof(buf));
+		if (len0 == 0)
+			break;
+		if (len0 < 0) {
+			warn("read error");
+			return (1);
+		}
+		len = write(ofd, buf, len0);
+		if (len < 0) {
+			warn("write error");
+			return (1);
+		}
+	}
+
+	return (0);
+}

Added: user/hrs/releng/usr.sbin/makevd/vmdk.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ user/hrs/releng/usr.sbin/makevd/vmdk.h	Sat Jan  5 21:52:01 2013	(r245073)
@@ -0,0 +1,70 @@
+/*-
+ * Copyright (c) 2011
+ *	Hiroki Sato <hrs at FreeBSD.org>  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ *
+ */
+#ifndef _VMDK_H
+#define _VMDK_H
+
+#include <sys/endian.h>
+#include <stdint.h>
+
+typedef uint64_t SectorType;
+typedef uint32_t uint32;
+typedef uint16_t uint16;
+typedef uint8_t uint8;
+typedef uint8_t Bool;
+
+struct SparseExtentHeader {
+	uint32	     magicNumber;
+	uint32	     version;
+	uint32	     flags;
+	SectorType   capacity;
+	SectorType   grainSize;
+	SectorType   descriptorOffset;
+	SectorType   descriptorSize;
+	uint32	     numGTEsPerGT;
+	SectorType   rgdOffset;
+	SectorType   gdOffset;
+	SectorType   overHead;
+	Bool	     uncleanShutdown;
+	char	     singleEndLineChar;
+	char	     nonEndLineChar;
+	char	     doubleEndLineChar1;
+	char	     doubleEndLineChar2;
+	uint16	     compressAlgorithm;
+	uint8	     pad[433];
+} __attribute__((__packed__));
+
+#if _BYTE_ORDER == _BIG_ENDIAN
+#define	SPARSE_MAGICNUMBER		(0x4b444d56)	/* 'K' 'D' 'M' 'V' */
+#else
+#define	SPARSE_MAGICNUMBER		(0x564d444b)	/* 'V' 'M' 'D' 'K' */
+#endif
+
+#define	SPARSE_VERSION_DEFAULT		2
+
+#endif	/* _VMDK_H */


More information about the svn-src-user mailing list