mk-vmimage.sh revision 272234
1272234Sgjb#!/bin/sh
2272234Sgjb#-
3272234Sgjb# Copyright (c) 2014 The FreeBSD Foundation
4272234Sgjb# All rights reserved.
5272234Sgjb#
6272234Sgjb# This software was developed by Glen Barber under sponsorship
7272234Sgjb# from the FreeBSD Foundation.
8272234Sgjb#
9272234Sgjb# Redistribution and use in source and binary forms, with or without
10272234Sgjb# modification, are permitted provided that the following conditions
11272234Sgjb# are met:
12272234Sgjb# 1. Redistributions of source code must retain the above copyright
13272234Sgjb#    notice, this list of conditions and the following disclaimer.
14272234Sgjb# 2. Redistributions in binary form must reproduce the above copyright
15272234Sgjb#    notice, this list of conditions and the following disclaimer in the
16272234Sgjb#    documentation and/or other materials provided with the distribution.
17272234Sgjb#
18272234Sgjb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19272234Sgjb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20272234Sgjb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21272234Sgjb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22272234Sgjb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23272234Sgjb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24272234Sgjb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25272234Sgjb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26272234Sgjb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27272234Sgjb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28272234Sgjb# SUCH DAMAGE.
29272234Sgjb#
30272234Sgjb# mk-vmimage.sh: Create virtual machine disk images in various formats.
31272234Sgjb#
32272234Sgjb# $FreeBSD: projects/release-vmimage/release/scripts/mk-vmimage.sh 272234 2014-09-28 01:45:20Z gjb $
33272234Sgjb#
34272234Sgjb
35272234SgjbPATH="/bin:/usr/bin:/sbin:/usr/sbin"
36272234Sgjbexport PATH
37272234Sgjb
38272234Sgjbusage() {
39272234Sgjb	echo "$(basename ${0}) <command> <target> [...]"
40272234Sgjb	if [ -z "${MAKEFLAGS}" ]; then
41272234Sgjb		echo "It is probably not safe to run this by hand yet..."
42272234Sgjb	fi
43272234Sgjb	exit 1
44272234Sgjb}
45272234Sgjb
46272234Sgjbpanic() {
47272234Sgjb	rc="${1}"
48272234Sgjb	shift 1
49272234Sgjb	msg="${@}"
50272234Sgjb	printf "${msg}\n"
51272234Sgjb	if [ ! -z "${mddev}" ]; then
52272234Sgjb		mdconfig -d -u ${mddev}
53272234Sgjb	fi
54272234Sgjb	# Do not allow one failure case to chain through any remaining image
55272234Sgjb	# builds.
56272234Sgjb	exit 0
57272234Sgjb}
58272234Sgjb
59272234Sgjbvm_create_baseimage() {
60272234Sgjb	# Creates the UFS root filesystem for the virtual machine disk,
61272234Sgjb	# written to the formatted disk image with mkimg(1).
62272234Sgjb	i=0
63272234Sgjb	mkdir -p ${DESTDIR}
64272234Sgjb	truncate -s ${VMSIZE} ${VMBASE}
65272234Sgjb	mddev=$(mdconfig -f ${VMBASE})
66272234Sgjb	newfs -L root -j ${mddev}
67272234Sgjb	mount ${mddev} ${DESTDIR}
68272234Sgjb	cd ${WORLDDIR} && \
69272234Sgjb		${IMAKE} DESTDIR=${DESTDIR} \
70272234Sgjb		installworld installkernel distribution || \
71272234Sgjb		panic 1 "\n\nCannot install the base system to ${DESTDIR}."
72272234Sgjb	chroot ${DESTDIR} /usr/bin/newaliases
73272234Sgjb	echo '# Custom /etc/fstab for FreeBSD VM images' \
74272234Sgjb		# > ${DESTDIR}/etc/fstab
75272234Sgjb	echo '/dev/gpt/rootfs	/	ufs	rw	2	2' \
76272234Sgjb		# >> ${DESTDIR}/etc/fstab
77272234Sgjb	echo '/dev/gpt/swapfs	none	swap	sw	0	0' \
78272234Sgjb		# >> ${DESTDIR}/etc/fstab
79272234Sgjb	sync
80272234Sgjb	while ! umount ${DESTDIR}; do
81272234Sgjb		i=$(( $i + 1 ))
82272234Sgjb		if [ $i -ge 10 ]; then
83272234Sgjb			# This should never happen.  But, it has happened.
84272234Sgjb			msg="Cannot umount(8) ${DESTDIR}\n"
85272234Sgjb			msg="${msg}Something has gone horribly wrong."
86272234Sgjb			panic 1 "${msg}"
87272234Sgjb		fi
88272234Sgjb		sleep 1
89272234Sgjb	done
90272234Sgjb
91272234Sgjb	return 0
92272234Sgjb}
93272234Sgjb
94272234Sgjbvm_create_vmdisk() {
95272234Sgjb	mkimg_version=$(mkimg --version 2>/dev/null | awk '{print $2}')
96272234Sgjb
97272234Sgjb	# We need mkimg(1) '--version' output, at minimum, to be able to
98272234Sgjb	# tell what virtual machine disk image formats are available.
99272234Sgjb	# Bail if mkimg(1) reports an empty '--version' value.
100272234Sgjb	if [ -z "${mkimg_version}" ]; then
101272234Sgjb		msg="Cannot determine mkimg(1) version.\n"
102272234Sgjb		msg="${msg}Cannot continue without a known mkimg(1) version."
103272234Sgjb		panic 0 "${msg}"
104272234Sgjb	fi
105272234Sgjb
106272234Sgjb	if ! mkimg --formats 2>/dev/null | grep -q ${FORMAT}; then
107272234Sgjb		panic 0 "Format ${FORMAT} is not supported with this mkimg(1)\n"
108272234Sgjb	fi
109272234Sgjb
110272234Sgjb	case ${FORMAT} in
111272234Sgjb		vhd)
112272234Sgjb			mkimg_format=vhdf
113272234Sgjb			;;
114272234Sgjb		*)
115272234Sgjb			mkimg_format=${FORMAT}
116272234Sgjb			;;
117272234Sgjb	esac
118272234Sgjb
119272234Sgjb	set -x
120272234Sgjb	mkimg -f ${mkimg_format} -s gpt \
121272234Sgjb		-b /boot/pmbr -p freebsd-boot/bootfs:=/boot/gptboot \
122272234Sgjb		-p freebsd-swap/swapfs::1G \
123272234Sgjb		-p freebsd-ufs/rootfs:=${VMBASE} \
124272234Sgjb		-o ${VMIMAGE}
125272234Sgjb
126272234Sgjb	return 0
127272234Sgjb}
128272234Sgjb
129272234Sgjbmain() {
130272234Sgjb	cmd="${1}"
131272234Sgjb
132272234Sgjb	case ${TARGET}/${TARGET_ARCH} in
133272234Sgjb		amd64/amd64|i386/i386)
134272234Sgjb			# FALLTHROUGH
135272234Sgjb			;;
136272234Sgjb		*)
137272234Sgjb			# EX_CANTCREAT
138272234Sgjb			return 0
139272234Sgjb			;;
140272234Sgjb	esac
141272234Sgjb
142272234Sgjb	case ${cmd} in
143272234Sgjb		vm-base)
144272234Sgjb			eval vm_create_baseimage "$@" || return 0
145272234Sgjb			;;
146272234Sgjb		vm-image)
147272234Sgjb			eval vm_create_vmdisk "$@" || return 0
148272234Sgjb			;;
149272234Sgjb		*|\?)
150272234Sgjb			usage
151272234Sgjb			;;
152272234Sgjb	esac
153272234Sgjb
154272234Sgjb	return 0
155272234Sgjb}
156272234Sgjb
157272234Sgjbmain "$@"
158