xref: /freebsd/release/tools/vmimage.subr (revision 7d565d608f2aed33566ed8e6dc371e42b626686e)
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() {
13efeb11a7SGlen Barber
14efeb11a7SGlen Barber	case "${TARGET}:${TARGET_ARCH}" in
15efeb11a7SGlen Barber		amd64:amd64 | i386:i386)
16*7d565d60SGlen Barber			mkimg -s gpt -b /boot/pmbr \
17efeb11a7SGlen Barber				-p freebsd-boot/bootfs:=/boot/gptboot \
18efeb11a7SGlen Barber				-p freebsd-swap/swapfs::1G \
1938d59ac7SGlen Barber				-p freebsd-ufs/rootfs:=${VMBASE} \
20efeb11a7SGlen Barber				-o ${VMIMAGE}
21efeb11a7SGlen Barber			;;
22efeb11a7SGlen Barber		powerpc:powerpc*)
23*7d565d60SGlen Barber			mkimg -s apm \
24efeb11a7SGlen Barber				-p freebsd-boot/bootfs:=/boot/boot1.hfs \
25efeb11a7SGlen Barber				-p freebsd-swap/swapfs::1G \
2638d59ac7SGlen Barber				-p freebsd-ufs/rootfs:=${VMBASE} \
27efeb11a7SGlen Barber				-o ${VMIMAGE}
28efeb11a7SGlen Barber			;;
29efeb11a7SGlen Barber		*)
30efeb11a7SGlen Barber			# ENOTSUPP
31efeb11a7SGlen Barber			return 1
32efeb11a7SGlen Barber			;;
33efeb11a7SGlen Barber	esac
34efeb11a7SGlen Barber
35efeb11a7SGlen Barber	return 0
36efeb11a7SGlen Barber}
371e7c1f17SGlen Barber
381e7c1f17SGlen Barbererr() {
391e7c1f17SGlen Barber	printf "${@}\n"
401e7c1f17SGlen Barber	cleanup
411e7c1f17SGlen Barber	return 1
421e7c1f17SGlen Barber}
431e7c1f17SGlen Barber
441e7c1f17SGlen Barbercleanup() {
451e7c1f17SGlen Barber	if [ ! -z "${mddev}" ]; then
461e7c1f17SGlen Barber		mdconfig -d -u ${mddev}
471e7c1f17SGlen Barber	fi
481e7c1f17SGlen Barber	umount ${DESTDIR}/dev
491e7c1f17SGlen Barber	umount ${DESTDIR}
501e7c1f17SGlen Barber
511e7c1f17SGlen Barber	return 0
521e7c1f17SGlen Barber}
531e7c1f17SGlen Barber
541e7c1f17SGlen Barbervm_create_base() {
551e7c1f17SGlen Barber	# Creates the UFS root filesystem for the virtual machine disk,
561e7c1f17SGlen Barber	# written to the formatted disk image with mkimg(1).
571e7c1f17SGlen Barber
581e7c1f17SGlen Barber	mkdir -p ${DESTDIR}
591e7c1f17SGlen Barber	truncate -s ${VMSIZE} ${VMBASE}
601e7c1f17SGlen Barber	mddev=$(mdconfig -f ${VMBASE})
611e7c1f17SGlen Barber	newfs -j /dev/${mddev}
621e7c1f17SGlen Barber	mount /dev/${mddev} ${DESTDIR}
631e7c1f17SGlen Barber
641e7c1f17SGlen Barber	return 0
651e7c1f17SGlen Barber}
661e7c1f17SGlen Barber
671e7c1f17SGlen Barbervm_install_base() {
681e7c1f17SGlen Barber	# Installs the FreeBSD userland/kernel to the virtual machine disk.
691e7c1f17SGlen Barber
701e7c1f17SGlen Barber	cd ${WORLDDIR} && \
711e7c1f17SGlen Barber		make DESTDIR=${DESTDIR} \
721e7c1f17SGlen Barber		installworld installkernel distribution || \
731e7c1f17SGlen Barber		err "\n\nCannot install the base system to ${DESTDIR}."
741e7c1f17SGlen Barber
751e7c1f17SGlen Barber	echo '# Custom /etc/fstab for FreeBSD VM images' \
761e7c1f17SGlen Barber		> ${DESTDIR}/etc/fstab
771e7c1f17SGlen Barber	echo '/dev/gpt/rootfs   /       ufs     rw      1       1' \
781e7c1f17SGlen Barber		>> ${DESTDIR}/etc/fstab
791e7c1f17SGlen Barber	echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
801e7c1f17SGlen Barber		>> ${DESTDIR}/etc/fstab
811e7c1f17SGlen Barber
821e7c1f17SGlen Barber	chroot ${DESTDIR} /usr/bin/newaliases
831e7c1f17SGlen Barber	chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
841e7c1f17SGlen Barber
851e7c1f17SGlen Barber	return 0
861e7c1f17SGlen Barber}
871e7c1f17SGlen Barber
881e7c1f17SGlen Barbervm_extra_install_base() {
891e7c1f17SGlen Barber	# Prototype.  When overridden, runs extra post-installworld commands
901e7c1f17SGlen Barber	# as needed, based on the target virtual machine image or cloud
911e7c1f17SGlen Barber	# provider image target.
921e7c1f17SGlen Barber
931e7c1f17SGlen Barber	return 0
941e7c1f17SGlen Barber}
951e7c1f17SGlen Barber
961e7c1f17SGlen Barbervm_extra_enable_services() {
971e7c1f17SGlen Barber	if [ ! -z "${VM_RC_LIST}" ]; then
981e7c1f17SGlen Barber		for _rcvar in ${VM_RC_LIST}; do
991e7c1f17SGlen Barber			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
1001e7c1f17SGlen Barber		done
1011e7c1f17SGlen Barber	fi
1021e7c1f17SGlen Barber
1031e7c1f17SGlen Barber	return 0
1041e7c1f17SGlen Barber}
1051e7c1f17SGlen Barber
1061e7c1f17SGlen Barbervm_extra_install_packages() {
1071e7c1f17SGlen Barber	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
1081e7c1f17SGlen Barber		/usr/sbin/pkg bootstrap -y
1091e7c1f17SGlen Barber	if [ ! -z "${VM_EXTRA_PACKAGES}" ]; then
1101e7c1f17SGlen Barber		chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
1111e7c1f17SGlen Barber			/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
1121e7c1f17SGlen Barber	fi
1131e7c1f17SGlen Barber
1141e7c1f17SGlen Barber	return 0
1151e7c1f17SGlen Barber}
1161e7c1f17SGlen Barber
1171e7c1f17SGlen Barbervm_extra_install_ports() {
1181e7c1f17SGlen Barber	# Prototype.  When overridden, installs additional ports within the
1191e7c1f17SGlen Barber	# virtual machine environment.
1201e7c1f17SGlen Barber
1211e7c1f17SGlen Barber	return 0
1221e7c1f17SGlen Barber}
1231e7c1f17SGlen Barber
124a02bfc83SGlen Barbervm_extra_pre_umount() {
125a02bfc83SGlen Barber	# Prototype.  When overridden, installs additional ports within the
126a02bfc83SGlen Barber	# virtual machine environment.
127a02bfc83SGlen Barber
128a02bfc83SGlen Barber	return 0
129a02bfc83SGlen Barber}
130a02bfc83SGlen Barber
1311e7c1f17SGlen Barbervm_umount_base() {
1321e7c1f17SGlen Barber	i=0
1331e7c1f17SGlen Barber	sync
1341e7c1f17SGlen Barber	while ! umount ${DESTDIR}/dev ${DESTDIR}; do
1351e7c1f17SGlen Barber		i=$(( $i + 1 ))
1361e7c1f17SGlen Barber		if [ $i -ge 10 ]; then
1371e7c1f17SGlen Barber			# This should never happen.  But, it has happened.
1381e7c1f17SGlen Barber			msg="Cannot umount(8) ${DESTDIR}\n"
1391e7c1f17SGlen Barber			msg="${msg}Something has gone horribly wrong."
1401e7c1f17SGlen Barber			err "${msg}"
1411e7c1f17SGlen Barber		fi
1421e7c1f17SGlen Barber		sleep 1
1431e7c1f17SGlen Barber	done
1441e7c1f17SGlen Barber
1451e7c1f17SGlen Barber	return 0
1461e7c1f17SGlen Barber}
1471e7c1f17SGlen Barber
1481e7c1f17SGlen Barbervm_create_disk() {
1491e7c1f17SGlen Barber	echo "Creating image...  Please wait."
1501e7c1f17SGlen Barber	echo
1511e7c1f17SGlen Barber
152efeb11a7SGlen Barber	write_partition_layout || return 1
1531e7c1f17SGlen Barber
1541e7c1f17SGlen Barber	return 0
1551e7c1f17SGlen Barber}
1561e7c1f17SGlen Barber
1571e7c1f17SGlen Barbervm_extra_create_disk() {
1581e7c1f17SGlen Barber
1591e7c1f17SGlen Barber	return 0
1601e7c1f17SGlen Barber}
1611e7c1f17SGlen Barber
162