xref: /freebsd/release/tools/vmimage.subr (revision dfa099890c456aed9b7924373d4dc2f013856c04)
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
14eb46c987SNavdeep Parhar		SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
154e7bc9f0SColin Percival	fi
16efeb11a7SGlen Barber
17*dfa09989SBryan Drewery	BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
18*dfa09989SBryan Drewery	    WITH_UNIFIED_OBJDIR=yes \
19*dfa09989SBryan Drewery	    make -C ${WORLDDIR}/sys/boot -V .OBJDIR)"
20*dfa09989SBryan Drewery	BOOTFILES="$(realpath ${BOOTFILES})"
214b8175eeSGlen Barber
22efeb11a7SGlen Barber	case "${TARGET}:${TARGET_ARCH}" in
23efeb11a7SGlen Barber		amd64:amd64 | i386:i386)
24ccbf2502SGlen Barber			mkimg -s gpt -f ${VMFORMAT} \
25ccbf2502SGlen Barber				-b ${BOOTFILES}/i386/pmbr/pmbr \
264b8175eeSGlen Barber				-p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \
274e7bc9f0SColin Percival				${SWAPOPT} \
2838d59ac7SGlen Barber				-p freebsd-ufs/rootfs:=${VMBASE} \
29efeb11a7SGlen Barber				-o ${VMIMAGE}
30efeb11a7SGlen Barber			;;
316a3afe69SGlen Barber		arm64:aarch64)
32ccbf2502SGlen Barber			mkimg -s mbr -f ${VMFORMAT} \
3308a75d1fSGlen Barber				-p efi:=${BOOTFILES}/efi/boot1/boot1.efifat \
3408a75d1fSGlen Barber				-p freebsd:=${VMBASE} \
356a3afe69SGlen Barber				-o ${VMIMAGE}
366a3afe69SGlen Barber			;;
37efeb11a7SGlen Barber		powerpc:powerpc*)
38ccbf2502SGlen Barber			mkimg -s apm -f ${VMFORMAT} \
394b8175eeSGlen Barber				-p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
404e7bc9f0SColin Percival				${SWAPOPT} \
4138d59ac7SGlen Barber				-p freebsd-ufs/rootfs:=${VMBASE} \
42efeb11a7SGlen Barber				-o ${VMIMAGE}
43efeb11a7SGlen Barber			;;
44efeb11a7SGlen Barber		*)
45efeb11a7SGlen Barber			# ENOTSUPP
46efeb11a7SGlen Barber			return 1
47efeb11a7SGlen Barber			;;
48efeb11a7SGlen Barber	esac
49efeb11a7SGlen Barber
50efeb11a7SGlen Barber	return 0
51efeb11a7SGlen Barber}
521e7c1f17SGlen Barber
531e7c1f17SGlen Barbererr() {
541e7c1f17SGlen Barber	printf "${@}\n"
551e7c1f17SGlen Barber	cleanup
561e7c1f17SGlen Barber	return 1
571e7c1f17SGlen Barber}
581e7c1f17SGlen Barber
591e7c1f17SGlen Barbercleanup() {
60dcdebabdSColin Percival	if [ -c "${DESTDIR}/dev/null" ]; then
6196341228SColin Percival		umount_loop ${DESTDIR}/dev 2>/dev/null
6296341228SColin Percival	fi
6396341228SColin Percival	umount_loop ${DESTDIR}
641e7c1f17SGlen Barber	if [ ! -z "${mddev}" ]; then
651e7c1f17SGlen Barber		mdconfig -d -u ${mddev}
661e7c1f17SGlen Barber	fi
671e7c1f17SGlen Barber
681e7c1f17SGlen Barber	return 0
691e7c1f17SGlen Barber}
701e7c1f17SGlen Barber
711e7c1f17SGlen Barbervm_create_base() {
721e7c1f17SGlen Barber	# Creates the UFS root filesystem for the virtual machine disk,
731e7c1f17SGlen Barber	# written to the formatted disk image with mkimg(1).
741e7c1f17SGlen Barber
751e7c1f17SGlen Barber	mkdir -p ${DESTDIR}
761e7c1f17SGlen Barber	truncate -s ${VMSIZE} ${VMBASE}
771e7c1f17SGlen Barber	mddev=$(mdconfig -f ${VMBASE})
7808a75d1fSGlen Barber	newfs -L rootfs /dev/${mddev}
791e7c1f17SGlen Barber	mount /dev/${mddev} ${DESTDIR}
801e7c1f17SGlen Barber
811e7c1f17SGlen Barber	return 0
821e7c1f17SGlen Barber}
831e7c1f17SGlen Barber
8425c11557SColin Percivalvm_copy_base() {
8525c11557SColin Percival	# Creates a new UFS root filesystem and copies the contents of the
8625c11557SColin Percival	# current root filesystem into it.  This produces a "clean" disk
8725c11557SColin Percival	# image without any remnants of files which were created temporarily
8825c11557SColin Percival	# during image-creation and have since been deleted (e.g., downloaded
8925c11557SColin Percival	# package archives).
9025c11557SColin Percival
9125c11557SColin Percival	mkdir -p ${DESTDIR}/old
9225c11557SColin Percival	mdold=$(mdconfig -f ${VMBASE})
9325c11557SColin Percival	mount /dev/${mdold} ${DESTDIR}/old
9425c11557SColin Percival
9525c11557SColin Percival	truncate -s ${VMSIZE} ${VMBASE}.tmp
9625c11557SColin Percival	mkdir -p ${DESTDIR}/new
9725c11557SColin Percival	mdnew=$(mdconfig -f ${VMBASE}.tmp)
9808a75d1fSGlen Barber	newfs -L rootfs /dev/${mdnew}
9925c11557SColin Percival	mount /dev/${mdnew} ${DESTDIR}/new
10025c11557SColin Percival
1014b8175eeSGlen Barber	tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
10225c11557SColin Percival
10396341228SColin Percival	umount_loop /dev/${mdold}
10425c11557SColin Percival	rmdir ${DESTDIR}/old
10525c11557SColin Percival	mdconfig -d -u ${mdold}
10625c11557SColin Percival
10796341228SColin Percival	umount_loop /dev/${mdnew}
10825c11557SColin Percival	rmdir ${DESTDIR}/new
10991c42fffSGlen Barber	tunefs -n enable /dev/${mdnew}
11025c11557SColin Percival	mdconfig -d -u ${mdnew}
11125c11557SColin Percival	mv ${VMBASE}.tmp ${VMBASE}
11225c11557SColin Percival}
11325c11557SColin Percival
1141e7c1f17SGlen Barbervm_install_base() {
1151e7c1f17SGlen Barber	# Installs the FreeBSD userland/kernel to the virtual machine disk.
1161e7c1f17SGlen Barber
1171e7c1f17SGlen Barber	cd ${WORLDDIR} && \
1181e7c1f17SGlen Barber		make DESTDIR=${DESTDIR} \
1191e7c1f17SGlen Barber		installworld installkernel distribution || \
1201e7c1f17SGlen Barber		err "\n\nCannot install the base system to ${DESTDIR}."
1211e7c1f17SGlen Barber
122c52962c6SGlen Barber	# Bootstrap etcupdate(8) and mergemaster(8) databases.
123c52962c6SGlen Barber	mkdir -p ${DESTDIR}/var/db/etcupdate
124c52962c6SGlen Barber	etcupdate extract -B \
125c52962c6SGlen Barber		-M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
126c52962c6SGlen Barber		-s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
127c52962c6SGlen Barber	sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
128c52962c6SGlen Barber		-F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
129c52962c6SGlen Barber		-D ${DESTDIR}
130c52962c6SGlen Barber
1311e7c1f17SGlen Barber	echo '# Custom /etc/fstab for FreeBSD VM images' \
1321e7c1f17SGlen Barber		> ${DESTDIR}/etc/fstab
13308a75d1fSGlen Barber	echo "/dev/${ROOTLABEL}/rootfs   /       ufs     rw      1       1" \
1341e7c1f17SGlen Barber		>> ${DESTDIR}/etc/fstab
1354e7bc9f0SColin Percival	if [ -z "${NOSWAP}" ]; then
1361e7c1f17SGlen Barber		echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
1371e7c1f17SGlen Barber			>> ${DESTDIR}/etc/fstab
1384e7bc9f0SColin Percival	fi
1391e7c1f17SGlen Barber
1402170fff8SGlen Barber	local hostname
1412170fff8SGlen Barber	hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
1422170fff8SGlen Barber	echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
1432170fff8SGlen Barber
144e6f3489cSGlen Barber	mkdir -p ${DESTDIR}/dev
145e6f3489cSGlen Barber	mount -t devfs devfs ${DESTDIR}/dev
1461e7c1f17SGlen Barber	chroot ${DESTDIR} /usr/bin/newaliases
1471e7c1f17SGlen Barber	chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
14896341228SColin Percival	umount_loop ${DESTDIR}/dev
1491e7c1f17SGlen Barber
15030a20c89SGlen Barber	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
15130a20c89SGlen Barber
1521e7c1f17SGlen Barber	return 0
1531e7c1f17SGlen Barber}
1541e7c1f17SGlen Barber
1551e7c1f17SGlen Barbervm_extra_install_base() {
1561e7c1f17SGlen Barber	# Prototype.  When overridden, runs extra post-installworld commands
1571e7c1f17SGlen Barber	# as needed, based on the target virtual machine image or cloud
1581e7c1f17SGlen Barber	# provider image target.
1591e7c1f17SGlen Barber
1601e7c1f17SGlen Barber	return 0
1611e7c1f17SGlen Barber}
1621e7c1f17SGlen Barber
1631e7c1f17SGlen Barbervm_extra_enable_services() {
1641e7c1f17SGlen Barber	if [ ! -z "${VM_RC_LIST}" ]; then
1651e7c1f17SGlen Barber		for _rcvar in ${VM_RC_LIST}; do
1661e7c1f17SGlen Barber			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
1671e7c1f17SGlen Barber		done
1681e7c1f17SGlen Barber	fi
1691e7c1f17SGlen Barber
170f8e26d73SGlen Barber	if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
171f8e26d73SGlen Barber		echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
172f8e26d73SGlen Barber			${DESTDIR}/etc/rc.conf
173f8e26d73SGlen Barber	fi
174f8e26d73SGlen Barber
1751e7c1f17SGlen Barber	return 0
1761e7c1f17SGlen Barber}
1771e7c1f17SGlen Barber
1781e7c1f17SGlen Barbervm_extra_install_packages() {
179a4900ff5SGlen Barber	if [ -z "${VM_EXTRA_PACKAGES}" ]; then
180a4900ff5SGlen Barber		return 0
181a4900ff5SGlen Barber	fi
18230a20c89SGlen Barber	mkdir -p ${DESTDIR}/dev
18330a20c89SGlen Barber	mount -t devfs devfs ${DESTDIR}/dev
18430a20c89SGlen Barber	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
18530a20c89SGlen Barber		/usr/sbin/pkg bootstrap -y
18630a20c89SGlen Barber	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
18730a20c89SGlen Barber		/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
18896341228SColin Percival	umount_loop ${DESTDIR}/dev
1891e7c1f17SGlen Barber
1901e7c1f17SGlen Barber	return 0
1911e7c1f17SGlen Barber}
1921e7c1f17SGlen Barber
1931e7c1f17SGlen Barbervm_extra_install_ports() {
1941e7c1f17SGlen Barber	# Prototype.  When overridden, installs additional ports within the
1951e7c1f17SGlen Barber	# virtual machine environment.
1961e7c1f17SGlen Barber
1971e7c1f17SGlen Barber	return 0
1981e7c1f17SGlen Barber}
1991e7c1f17SGlen Barber
200a02bfc83SGlen Barbervm_extra_pre_umount() {
20126e4122aSGlen Barber	# Prototype.  When overridden, performs additional tasks within the
20226e4122aSGlen Barber	# virtual machine environment prior to unmounting the filesystem.
20326e4122aSGlen Barber	# Note: When overriding this function, removing resolv.conf in the
20426e4122aSGlen Barber	# disk image must be included.
205a02bfc83SGlen Barber
2062d4ff62fSGlen Barber	rm -f ${DESTDIR}/etc/resolv.conf
2072d4ff62fSGlen Barber	return 0
2082d4ff62fSGlen Barber}
2092d4ff62fSGlen Barber
2102d4ff62fSGlen Barbervm_extra_pkg_rmcache() {
211a4900ff5SGlen Barber	if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
212a4900ff5SGlen Barber		chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
213a4900ff5SGlen Barber			/usr/local/sbin/pkg clean -y -a
214a4900ff5SGlen Barber	fi
21530a20c89SGlen Barber
216a02bfc83SGlen Barber	return 0
217a02bfc83SGlen Barber}
218a02bfc83SGlen Barber
21996341228SColin Percivalumount_loop() {
22096341228SColin Percival	DIR=$1
2211e7c1f17SGlen Barber	i=0
2221e7c1f17SGlen Barber	sync
22396341228SColin Percival	while ! umount ${DIR}; do
2241e7c1f17SGlen Barber		i=$(( $i + 1 ))
2251e7c1f17SGlen Barber		if [ $i -ge 10 ]; then
2261e7c1f17SGlen Barber			# This should never happen.  But, it has happened.
22796341228SColin Percival			echo "Cannot umount(8) ${DIR}"
22896341228SColin Percival			echo "Something has gone horribly wrong."
22996341228SColin Percival			return 1
2301e7c1f17SGlen Barber		fi
2311e7c1f17SGlen Barber		sleep 1
2321e7c1f17SGlen Barber	done
2331e7c1f17SGlen Barber
2341e7c1f17SGlen Barber	return 0
2351e7c1f17SGlen Barber}
2361e7c1f17SGlen Barber
2371e7c1f17SGlen Barbervm_create_disk() {
2381e7c1f17SGlen Barber	echo "Creating image...  Please wait."
2391e7c1f17SGlen Barber	echo
2401e7c1f17SGlen Barber
241efeb11a7SGlen Barber	write_partition_layout || return 1
2421e7c1f17SGlen Barber
2431e7c1f17SGlen Barber	return 0
2441e7c1f17SGlen Barber}
2451e7c1f17SGlen Barber
2461e7c1f17SGlen Barbervm_extra_create_disk() {
2471e7c1f17SGlen Barber
2481e7c1f17SGlen Barber	return 0
2491e7c1f17SGlen Barber}
2501e7c1f17SGlen Barber
251