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