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-azure.sh: Create virtual machine disk images for Microsoft Azure
31#
32# $FreeBSD$
33#
34
35export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin"
36
37usage() {
38	echo "Usage:"
39	echo -n "$(basename ${0}) vm-azure <base image>"
40	echo " <source tree> <dest dir> <disk image size> <vm image name>"
41	exit 1
42}
43
44panic() {
45	msg="${@}"
46	printf "${msg}\n"
47	if [ ! -z "${mddev}" ]; then
48		mdconfig -d -u ${mddev}
49	fi
50	# Do not allow one failure case to chain through any remaining image
51	# builds.
52	exit 0
53}
54
55vm_create_azure() {
56	# Arguments:
57	# vm-azure <base image> <source tree> <dest dir> <disk image size> <vm image name>
58
59	VMBASE="${1}"
60	WORLDDIR="${2}"
61	DESTDIR="${3}"
62	VMSIZE="${4}"
63	VMIMAGE="${5}"
64
65	if [ -z "${VMBASE}" -o -z "${WORLDDIR}" -o -z "${DESTDIR}" \
66		-o -z "${VMSIZE}" -o -z "${VMIMAGE}" ]; then
67			usage
68	fi
69
70	trap "umount ${DESTDIR}/dev ${DESTDIR}" INT QUIT TRAP ABRT TERM
71
72	i=0
73	mkdir -p ${DESTDIR}
74	truncate -s ${VMSIZE} ${VMBASE}
75	mddev=$(mdconfig -f ${VMBASE})
76	newfs -j /dev/${mddev}
77	mkdir -p ${DESTDIR}
78	mount /dev/${mddev} ${DESTDIR}
79	make -C ${WORLDDIR} DESTDIR=$(realpath ${DESTDIR}) \
80		installworld installkernel distribution || \
81		panic 1 "\n\nCannot install the base system to ${DESTDIR}."
82	mount -t devfs devfs ${DESTDIR}/dev
83	chroot ${DESTDIR} /usr/bin/newaliases
84	echo '# Custom /etc/fstab for FreeBSD VM images' \
85		> ${DESTDIR}/etc/fstab
86	echo '/dev/gpt/rootfs	/	ufs	rw	2	2' \
87		>> ${DESTDIR}/etc/fstab
88	# Although a swap partition is created, it is not used in Azure.
89	echo '#/dev/gpt/swapfs	none	swap	sw	0	0' \
90		>> ${DESTDIR}/etc/fstab
91
92	chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
93	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg bootstrap -y
94	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg install -y \
95	        python python2 python27 py27-asn1 sudo bash
96	if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then
97	        chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes /usr/sbin/pkg install -y \
98	                ${VM_EXTRA_PACKAGES}
99	fi
100
101	fetch -o ${DESTDIR}/usr/sbin/waagent \
102		http://people.freebsd.org/~gjb/waagent
103	chmod +x ${DESTDIR}/usr/sbin/waagent
104	rm -f ${DESTDIR}/etc/resolv.conf
105	chroot ${DESTDIR} /usr/sbin/waagent -verbose -install
106	yes | chroot ${DESTDIR} /usr/sbin/waagent -deprovision
107	echo 'sshd_enable="YES"' > ${DESTDIR}/etc/rc.conf
108	echo 'ifconfig_hn0="SYNCDHCP"' >> ${DESTDIR}/etc/rc.conf
109	echo 'waagent_enable="YES"' >> ${DESTDIR}/etc/rc.conf
110
111	echo 'console="comconsole vidconsole"' >> ${DESTDIR}/boot/loader.conf
112	echo 'comconsole_speed="115200"' >> ${DESTDIR}/boot/loader.conf
113
114	if [ ! -z "${VM_RC_LIST}" ]; then
115		for _rcvar in ${VM_RC_LIST}; do
116			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
117		done
118	fi
119
120	sync
121
122	while ! umount ${DESTDIR}/dev ${DESTDIR}; do
123		i=$(( $i + 1 ))
124		if [ $i -ge 10 ]; then
125			# This should never happen.  But, it has happened.
126			msg="Cannot umount(8) ${DESTDIR}\n"
127			msg="${msg}Something has gone horribly wrong."
128			panic 1 "${msg}"
129		fi
130		sleep 1
131	done
132
133	echo "Creating image...  Please wait."
134
135	mkimg -f vhdf -s gpt \
136		-b /boot/pmbr -p freebsd-boot/bootfs:=/boot/gptboot \
137		-p freebsd-swap/swapfs::1G \
138		-p freebsd-ufs/rootfs:=${VMBASE} \
139		-o ${VMIMAGE}.raw
140
141	if [ ! -x "/usr/local/bin/qemu-img" ]; then
142		env ASSUME_ALWAYS_YES=yes pkg install -y emulators/qemu-devel
143	fi
144
145	size=$(qemu-img info -f raw --output json ${VMIMAGE}.raw | awk '/virtual-size/ {print $2}' | tr -d ',')
146	size=$(( ( ${size} / ( 1024 * 1024 ) + 1 ) * ( 1024 * 1024 ) ))
147	qemu-img resize ${VMIMAGE}.raw ${size}
148	qemu-img convert -f raw -o subformat=fixed -O vpc ${VMIMAGE}.raw ${VMIMAGE}
149
150	return 0
151}
152
153main() {
154	cmd="${1}"
155	shift 1
156
157	if [ -e "${AZURECONF}" -a ! -c "${AZURECONF}" ]; then
158		. ${AZURECONF}
159	fi
160
161	case ${cmd} in
162		vm-azure)
163			eval vm_create_azure "$@" || return 0
164			;;
165		*|\?)
166			usage
167			;;
168	esac
169
170	return 0
171}
172
173main "$@"
174