xref: /freebsd/release/tools/vmimage.subr (revision dcdebabdead399b7e37d9ff8e5efb18a3de4283c)
11e7c1f17SGlen Barber#!/bin/sh
21e7c1f17SGlen Barber#
31e7c1f17SGlen Barber# $FreeBSD$
41e7c1f17SGlen Barber#
51e7c1f17SGlen Barber#
61e7c1f17SGlen Barber# Common functions for virtual machine image build scripts.
71e7c1f17SGlen Barber#
81e7c1f17SGlen Barber
91e7c1f17SGlen Barberexport PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
101e7c1f17SGlen Barbertrap "cleanup" INT QUIT TRAP ABRT TERM
111e7c1f17SGlen Barber
12efeb11a7SGlen Barberwrite_partition_layout() {
134e7bc9f0SColin Percival	if [ -z "${NOSWAP}" ]; then
144e7bc9f0SColin Percival		SWAPOPT="-p freebsd-swap/swapfs::1G"
154e7bc9f0SColin Percival	fi
16efeb11a7SGlen Barber
17efeb11a7SGlen Barber	case "${TARGET}:${TARGET_ARCH}" in
18efeb11a7SGlen Barber		amd64:amd64 | i386:i386)
197d565d60SGlen Barber			mkimg -s gpt -b /boot/pmbr \
20efeb11a7SGlen Barber				-p freebsd-boot/bootfs:=/boot/gptboot \
214e7bc9f0SColin Percival				${SWAPOPT} \
2238d59ac7SGlen Barber				-p freebsd-ufs/rootfs:=${VMBASE} \
23efeb11a7SGlen Barber				-o ${VMIMAGE}
24efeb11a7SGlen Barber			;;
25efeb11a7SGlen Barber		powerpc:powerpc*)
267d565d60SGlen Barber			mkimg -s apm \
279d3df3bdSGlen Barber				-p apple-boot/bootfs:=/boot/boot1.hfs \
284e7bc9f0SColin Percival				${SWAPOPT} \
2938d59ac7SGlen Barber				-p freebsd-ufs/rootfs:=${VMBASE} \
30efeb11a7SGlen Barber				-o ${VMIMAGE}
31efeb11a7SGlen Barber			;;
32efeb11a7SGlen Barber		*)
33efeb11a7SGlen Barber			# ENOTSUPP
34efeb11a7SGlen Barber			return 1
35efeb11a7SGlen Barber			;;
36efeb11a7SGlen Barber	esac
37efeb11a7SGlen Barber
38efeb11a7SGlen Barber	return 0
39efeb11a7SGlen Barber}
401e7c1f17SGlen Barber
411e7c1f17SGlen Barbererr() {
421e7c1f17SGlen Barber	printf "${@}\n"
431e7c1f17SGlen Barber	cleanup
441e7c1f17SGlen Barber	return 1
451e7c1f17SGlen Barber}
461e7c1f17SGlen Barber
471e7c1f17SGlen Barbercleanup() {
48*dcdebabdSColin Percival	if [ -c "${DESTDIR}/dev/null" ]; then
4996341228SColin Percival		umount_loop ${DESTDIR}/dev 2>/dev/null
5096341228SColin Percival	fi
5196341228SColin Percival	umount_loop ${DESTDIR}
521e7c1f17SGlen Barber	if [ ! -z "${mddev}" ]; then
531e7c1f17SGlen Barber		mdconfig -d -u ${mddev}
541e7c1f17SGlen Barber	fi
551e7c1f17SGlen Barber
561e7c1f17SGlen Barber	return 0
571e7c1f17SGlen Barber}
581e7c1f17SGlen Barber
591e7c1f17SGlen Barbervm_create_base() {
601e7c1f17SGlen Barber	# Creates the UFS root filesystem for the virtual machine disk,
611e7c1f17SGlen Barber	# written to the formatted disk image with mkimg(1).
621e7c1f17SGlen Barber
631e7c1f17SGlen Barber	mkdir -p ${DESTDIR}
641e7c1f17SGlen Barber	truncate -s ${VMSIZE} ${VMBASE}
651e7c1f17SGlen Barber	mddev=$(mdconfig -f ${VMBASE})
661e7c1f17SGlen Barber	newfs -j /dev/${mddev}
671e7c1f17SGlen Barber	mount /dev/${mddev} ${DESTDIR}
681e7c1f17SGlen Barber
691e7c1f17SGlen Barber	return 0
701e7c1f17SGlen Barber}
711e7c1f17SGlen Barber
7225c11557SColin Percivalvm_copy_base() {
7325c11557SColin Percival	# Creates a new UFS root filesystem and copies the contents of the
7425c11557SColin Percival	# current root filesystem into it.  This produces a "clean" disk
7525c11557SColin Percival	# image without any remnants of files which were created temporarily
7625c11557SColin Percival	# during image-creation and have since been deleted (e.g., downloaded
7725c11557SColin Percival	# package archives).
7825c11557SColin Percival
7925c11557SColin Percival	mkdir -p ${DESTDIR}/old
8025c11557SColin Percival	mdold=$(mdconfig -f ${VMBASE})
8125c11557SColin Percival	mount /dev/${mdold} ${DESTDIR}/old
8225c11557SColin Percival
8325c11557SColin Percival	truncate -s ${VMSIZE} ${VMBASE}.tmp
8425c11557SColin Percival	mkdir -p ${DESTDIR}/new
8525c11557SColin Percival	mdnew=$(mdconfig -f ${VMBASE}.tmp)
8625c11557SColin Percival	newfs -j /dev/${mdnew}
8725c11557SColin Percival	mount /dev/${mdnew} ${DESTDIR}/new
8825c11557SColin Percival
8925c11557SColin Percival	tar -cf- -C ${DESTDIR}/old . | tar -xf- -C ${DESTDIR}/new
9025c11557SColin Percival
9196341228SColin Percival	umount_loop /dev/${mdold}
9225c11557SColin Percival	rmdir ${DESTDIR}/old
9325c11557SColin Percival	mdconfig -d -u ${mdold}
9425c11557SColin Percival
9596341228SColin Percival	umount_loop /dev/${mdnew}
9625c11557SColin Percival	rmdir ${DESTDIR}/new
9725c11557SColin Percival	mdconfig -d -u ${mdnew}
9825c11557SColin Percival	mv ${VMBASE}.tmp ${VMBASE}
9925c11557SColin Percival}
10025c11557SColin Percival
1011e7c1f17SGlen Barbervm_install_base() {
1021e7c1f17SGlen Barber	# Installs the FreeBSD userland/kernel to the virtual machine disk.
1031e7c1f17SGlen Barber
1041e7c1f17SGlen Barber	cd ${WORLDDIR} && \
1051e7c1f17SGlen Barber		make DESTDIR=${DESTDIR} \
1061e7c1f17SGlen Barber		installworld installkernel distribution || \
1071e7c1f17SGlen Barber		err "\n\nCannot install the base system to ${DESTDIR}."
1081e7c1f17SGlen Barber
1091e7c1f17SGlen Barber	echo '# Custom /etc/fstab for FreeBSD VM images' \
1101e7c1f17SGlen Barber		> ${DESTDIR}/etc/fstab
1111e7c1f17SGlen Barber	echo '/dev/gpt/rootfs   /       ufs     rw      1       1' \
1121e7c1f17SGlen Barber		>> ${DESTDIR}/etc/fstab
1134e7bc9f0SColin Percival	if [ -z "${NOSWAP}" ]; then
1141e7c1f17SGlen Barber		echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
1151e7c1f17SGlen Barber			>> ${DESTDIR}/etc/fstab
1164e7bc9f0SColin Percival	fi
1171e7c1f17SGlen Barber
118e6f3489cSGlen Barber	mkdir -p ${DESTDIR}/dev
119e6f3489cSGlen Barber	mount -t devfs devfs ${DESTDIR}/dev
1201e7c1f17SGlen Barber	chroot ${DESTDIR} /usr/bin/newaliases
1211e7c1f17SGlen Barber	chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
12296341228SColin Percival	umount_loop ${DESTDIR}/dev
1231e7c1f17SGlen Barber
12430a20c89SGlen Barber	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
12530a20c89SGlen Barber
1261e7c1f17SGlen Barber	return 0
1271e7c1f17SGlen Barber}
1281e7c1f17SGlen Barber
1291e7c1f17SGlen Barbervm_extra_install_base() {
1301e7c1f17SGlen Barber	# Prototype.  When overridden, runs extra post-installworld commands
1311e7c1f17SGlen Barber	# as needed, based on the target virtual machine image or cloud
1321e7c1f17SGlen Barber	# provider image target.
1331e7c1f17SGlen Barber
1341e7c1f17SGlen Barber	return 0
1351e7c1f17SGlen Barber}
1361e7c1f17SGlen Barber
1371e7c1f17SGlen Barbervm_extra_enable_services() {
1381e7c1f17SGlen Barber	if [ ! -z "${VM_RC_LIST}" ]; then
1391e7c1f17SGlen Barber		for _rcvar in ${VM_RC_LIST}; do
1401e7c1f17SGlen Barber			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
1411e7c1f17SGlen Barber		done
1421e7c1f17SGlen Barber	fi
1431e7c1f17SGlen Barber
1441e7c1f17SGlen Barber	return 0
1451e7c1f17SGlen Barber}
1461e7c1f17SGlen Barber
1471e7c1f17SGlen Barbervm_extra_install_packages() {
148a4900ff5SGlen Barber	if [ -z "${VM_EXTRA_PACKAGES}" ]; then
149a4900ff5SGlen Barber		return 0
150a4900ff5SGlen Barber	fi
15130a20c89SGlen Barber	mkdir -p ${DESTDIR}/dev
15230a20c89SGlen Barber	mount -t devfs devfs ${DESTDIR}/dev
15330a20c89SGlen Barber	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
15430a20c89SGlen Barber		/usr/sbin/pkg bootstrap -y
15530a20c89SGlen Barber	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
15630a20c89SGlen Barber		/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
15796341228SColin Percival	umount_loop ${DESTDIR}/dev
1581e7c1f17SGlen Barber
1591e7c1f17SGlen Barber	return 0
1601e7c1f17SGlen Barber}
1611e7c1f17SGlen Barber
1621e7c1f17SGlen Barbervm_extra_install_ports() {
1631e7c1f17SGlen Barber	# Prototype.  When overridden, installs additional ports within the
1641e7c1f17SGlen Barber	# virtual machine environment.
1651e7c1f17SGlen Barber
1661e7c1f17SGlen Barber	return 0
1671e7c1f17SGlen Barber}
1681e7c1f17SGlen Barber
169a02bfc83SGlen Barbervm_extra_pre_umount() {
170a02bfc83SGlen Barber	# Prototype.  When overridden, installs additional ports within the
171a02bfc83SGlen Barber	# virtual machine environment.
172a02bfc83SGlen Barber
1732d4ff62fSGlen Barber	rm -f ${DESTDIR}/etc/resolv.conf
1742d4ff62fSGlen Barber	return 0
1752d4ff62fSGlen Barber}
1762d4ff62fSGlen Barber
1772d4ff62fSGlen Barbervm_extra_pkg_rmcache() {
178a4900ff5SGlen Barber	if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
179a4900ff5SGlen Barber		chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
180a4900ff5SGlen Barber			/usr/local/sbin/pkg clean -y -a
181a4900ff5SGlen Barber	fi
18230a20c89SGlen Barber
183a02bfc83SGlen Barber	return 0
184a02bfc83SGlen Barber}
185a02bfc83SGlen Barber
18696341228SColin Percivalumount_loop() {
18796341228SColin Percival	DIR=$1
1881e7c1f17SGlen Barber	i=0
1891e7c1f17SGlen Barber	sync
19096341228SColin Percival	while ! umount ${DIR}; do
1911e7c1f17SGlen Barber		i=$(( $i + 1 ))
1921e7c1f17SGlen Barber		if [ $i -ge 10 ]; then
1931e7c1f17SGlen Barber			# This should never happen.  But, it has happened.
19496341228SColin Percival			echo "Cannot umount(8) ${DIR}"
19596341228SColin Percival			echo "Something has gone horribly wrong."
19696341228SColin Percival			return 1
1971e7c1f17SGlen Barber		fi
1981e7c1f17SGlen Barber		sleep 1
1991e7c1f17SGlen Barber	done
2001e7c1f17SGlen Barber
2011e7c1f17SGlen Barber	return 0
2021e7c1f17SGlen Barber}
2031e7c1f17SGlen Barber
2041e7c1f17SGlen Barbervm_create_disk() {
2051e7c1f17SGlen Barber	echo "Creating image...  Please wait."
2061e7c1f17SGlen Barber	echo
2071e7c1f17SGlen Barber
208efeb11a7SGlen Barber	write_partition_layout || return 1
2091e7c1f17SGlen Barber
2101e7c1f17SGlen Barber	return 0
2111e7c1f17SGlen Barber}
2121e7c1f17SGlen Barber
2131e7c1f17SGlen Barbervm_extra_create_disk() {
2141e7c1f17SGlen Barber
2151e7c1f17SGlen Barber	return 0
2161e7c1f17SGlen Barber}
2171e7c1f17SGlen Barber
218