mk-vmimage.sh revision 272376
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 272376 2014-10-01 17:05:40Z gjb $
33#
34
35PATH="/bin:/usr/bin:/sbin:/usr/sbin"
36export PATH
37
38usage() {
39	echo "$(basename ${0}) <command> <target> [...]"
40	exit 1
41}
42
43panic() {
44	rc="${1}"
45	shift 1
46	msg="${@}"
47	printf "${msg}\n"
48	if [ ! -z "${mddev}" ]; then
49		mdconfig -d -u ${mddev}
50	fi
51	# Do not allow one failure case to chain through any remaining image
52	# builds.
53	exit 0
54}
55
56vm_create_baseimage() {
57	# Creates the UFS root filesystem for the virtual machine disk,
58	# written to the formatted disk image with mkimg(1).
59	i=0
60	mkdir -p ${DESTDIR}
61	truncate -s ${VMSIZE} ${VMBASE}
62	mddev=$(mdconfig -f ${VMBASE})
63	newfs -j /dev/${mddev}
64	mount /dev/${mddev} ${DESTDIR}
65	cd ${WORLDDIR} && \
66		${IMAKE} DESTDIR=${DESTDIR} \
67		installworld installkernel distribution || \
68		panic 1 "\n\nCannot install the base system to ${DESTDIR}."
69	chroot ${DESTDIR} /usr/bin/newaliases
70	echo '# Custom /etc/fstab for FreeBSD VM images' \
71		> ${DESTDIR}/etc/fstab
72	echo '/dev/gpt/rootfs	/	ufs	rw	2	2' \
73		>> ${DESTDIR}/etc/fstab
74	echo '/dev/gpt/swapfs	none	swap	sw	0	0' \
75		>> ${DESTDIR}/etc/fstab
76	sync
77	while ! umount ${DESTDIR}; do
78		i=$(( $i + 1 ))
79		if [ $i -ge 10 ]; then
80			# This should never happen.  But, it has happened.
81			msg="Cannot umount(8) ${DESTDIR}\n"
82			msg="${msg}Something has gone horribly wrong."
83			panic 1 "${msg}"
84		fi
85		sleep 1
86	done
87
88	return 0
89}
90
91vm_create_vmdisk() {
92	mkimg_version=$(mkimg --version 2>/dev/null | awk '{print $2}')
93
94	# We need mkimg(1) '--version' output, at minimum, to be able to
95	# tell what virtual machine disk image formats are available.
96	# Bail if mkimg(1) reports an empty '--version' value.
97	if [ -z "${mkimg_version}" ]; then
98		msg="Cannot determine mkimg(1) version.\n"
99		msg="${msg}Cannot continue without a known mkimg(1) version."
100		panic 0 "${msg}"
101	fi
102
103	if ! mkimg --formats 2>/dev/null | grep -q ${FORMAT}; then
104		panic 0 "'${FORMAT}' is not supported by this mkimg(1).\n"
105	fi
106
107	case ${FORMAT} in
108		vhd)
109			mkimg_format=vhdf
110			;;
111		*)
112			mkimg_format=${FORMAT}
113			;;
114	esac
115
116	mkimg -f ${mkimg_format} -s gpt \
117		-b /boot/pmbr -p freebsd-boot/bootfs:=/boot/gptboot \
118		-p freebsd-swap/swapfs::1G \
119		-p freebsd-ufs/rootfs:=${VMBASE} \
120		-o ${VMIMAGE}
121
122	return 0
123}
124
125main() {
126	cmd="${1}"
127
128	if [ -z "${MAKEFLAGS}" ]; then
129		echo "It is probably not safe to run this by hand yet..."
130	fi
131
132	case ${cmd} in
133		vm-base)
134			eval vm_create_baseimage "$@" || return 0
135			;;
136		vm-image)
137			eval vm_create_vmdisk "$@" || return 0
138			;;
139		*|\?)
140			usage
141			;;
142	esac
143
144	return 0
145}
146
147main "$@"
148