mk-vmimage.sh revision 272436
1#!/bin/sh
2#-
3# Copyright (c) 2014 The FreeBSD Foundation
4# All rights reserved.
5#
6# This software was developed by Glen Barber under sponsorship
7# from the FreeBSD Foundation.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29#
30# mk-vmimage.sh: Create virtual machine disk images in various formats.
31#
32# $FreeBSD: projects/release-vmimage/release/amd64/mk-vmimage.sh 272436 2014-10-02 18:20:01Z gjb $
33#
34
35PATH="/bin:/usr/bin:/sbin:/usr/sbin"
36export PATH
37
38usage_vm_base() {
39	echo -n "$(basename ${0}) vm-base <base image> <source tree>"
40	echo	" <dest dir> <disk image size>"
41	return 0
42}
43
44usage_vm_image() {
45	echo -n "$(basename ${0}) vm-image <base image> <image format>"
46	echo	" <output image>"
47	return 0
48}
49
50usage() {
51	echo "Usage:"
52	echo "$(basename ${0}) [vm-base|vm-image] [...]"
53	echo
54	usage_vm_base
55	echo
56	usage_vm_image
57	exit 1
58}
59
60panic() {
61	msg="${@}"
62	printf "${msg}\n"
63	if [ ! -z "${mddev}" ]; then
64		mdconfig -d -u ${mddev}
65	fi
66	# Do not allow one failure case to chain through any remaining image
67	# builds.
68	return 1
69}
70
71vm_create_baseimage() {
72	# Creates the UFS root filesystem for the virtual machine disk,
73	# written to the formatted disk image with mkimg(1).
74	#
75	# Arguments:
76	# vm-base <base image> <source tree> <dest dir> <disk image size>
77
78	VMBASE="${1}"
79	WORLDDIR="${2}"
80	DESTDIR="${3}"
81	VMSIZE="${4}"
82
83	if [ -z "${VMBASE}" -o -z "${WORLDDIR}" -o -z "${DESTDIR}" \
84		-o -z "${VMSIZE}" ]; then
85			usage
86	fi
87
88	i=0
89	mkdir -p ${DESTDIR}
90	truncate -s ${VMSIZE} ${VMBASE}
91	mddev=$(mdconfig -f ${VMBASE})
92	newfs -j /dev/${mddev}
93	mount /dev/${mddev} ${DESTDIR}
94	cd ${WORLDDIR} && \
95		make DESTDIR=${DESTDIR} \
96		installworld installkernel distribution || \
97		panic "\n\nCannot install the base system to ${DESTDIR}."
98	chroot ${DESTDIR} /usr/bin/newaliases
99	echo '# Custom /etc/fstab for FreeBSD VM images' \
100		> ${DESTDIR}/etc/fstab
101	echo '/dev/gpt/rootfs	/	ufs	rw	2	2' \
102		>> ${DESTDIR}/etc/fstab
103	echo '/dev/gpt/swapfs	none	swap	sw	0	0' \
104		>> ${DESTDIR}/etc/fstab
105	sync
106	while ! umount ${DESTDIR}; do
107		i=$(( $i + 1 ))
108		if [ $i -ge 10 ]; then
109			# This should never happen.  But, it has happened.
110			msg="Cannot umount(8) ${DESTDIR}\n"
111			msg="${msg}Something has gone horribly wrong."
112			panic "${msg}"
113		fi
114		sleep 1
115	done
116
117	return 0
118}
119
120vm_create_vmdisk() {
121	# Creates the virtual machine disk image from the raw disk image.
122	#
123	# Arguments:
124	# vm-image <base image> <image format> <output image>"
125
126	VMBASE="${1}"
127	FORMAT="${2}"
128	VMIMAGE="${3}"
129
130	if [ -z "${VMBASE}" -o -z "${FORMAT}" -o -z "${VMIMAGE}" ]; then
131		usage
132	fi
133
134	mkimg_version=$(mkimg --version 2>/dev/null | awk '{print $2}')
135
136	# We need mkimg(1) '--version' output, at minimum, to be able to
137	# tell what virtual machine disk image formats are available.
138	# Bail if mkimg(1) reports an empty '--version' value.
139	if [ -z "${mkimg_version}" ]; then
140		msg="Cannot determine mkimg(1) version.\n"
141		msg="${msg}Cannot continue without a known mkimg(1) version."
142		panic "${msg}"
143	fi
144
145	if ! mkimg --formats 2>/dev/null | grep -q ${FORMAT}; then
146		panic "'${FORMAT}' is not supported by this mkimg(1).\n"
147	fi
148
149	case ${FORMAT} in
150		vhd)
151			mkimg_format=vhdf
152			;;
153		*)
154			mkimg_format=${FORMAT}
155			;;
156	esac
157
158	mkimg -f ${mkimg_format} -s gpt \
159		-b /boot/pmbr -p freebsd-boot/bootfs:=/boot/gptboot \
160		-p freebsd-swap/swapfs::1G \
161		-p freebsd-ufs/rootfs:=${VMBASE} \
162		-o ${VMIMAGE}
163
164	return 0
165}
166
167main() {
168	cmd="${1}"
169	shift 1
170
171	case ${cmd} in
172		vm-base)
173			eval vm_create_baseimage "$@" || return 0
174			;;
175		vm-image)
176			eval vm_create_vmdisk "$@" || return 0
177			;;
178		*|\?)
179			usage
180			;;
181	esac
182
183	return 0
184}
185
186main "$@"
187