xref: /freebsd/release/tools/vmimage.subr (revision 89585511cc052643a774f64f6450d18e7dd51d4a)
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
9db8b5613SRebecca Cranscriptdir=$(dirname $(realpath $0))
10db8b5613SRebecca Cran. ${scriptdir}/../../tools/boot/install-boot.sh
11db8b5613SRebecca Cran
121e7c1f17SGlen Barberexport PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
131e7c1f17SGlen Barbertrap "cleanup" INT QUIT TRAP ABRT TERM
141e7c1f17SGlen Barber
151ca8842fSNathan Whitehorn# Platform-specific large-scale setup
161ca8842fSNathan Whitehorn# Most platforms use GPT, so put that as default, then special cases
171ca8842fSNathan WhitehornPARTSCHEME=gpt
181ca8842fSNathan WhitehornROOTLABEL="gpt"
19efeb11a7SGlen Barbercase "${TARGET}:${TARGET_ARCH}" in
20efeb11a7SGlen Barber	powerpc:powerpc*)
211ca8842fSNathan Whitehorn		PARTSCHEME=mbr
221ca8842fSNathan Whitehorn		ROOTLABEL="ufs"
231ca8842fSNathan Whitehorn		NOSWAP=yes # Can't label swap partition with MBR, so no swap
24efeb11a7SGlen Barber	;;
25efeb11a7SGlen Barberesac
26efeb11a7SGlen Barber
271e7c1f17SGlen Barbererr() {
281e7c1f17SGlen Barber	printf "${@}\n"
291e7c1f17SGlen Barber	cleanup
301e7c1f17SGlen Barber	return 1
311e7c1f17SGlen Barber}
321e7c1f17SGlen Barber
331e7c1f17SGlen Barbercleanup() {
34dcdebabdSColin Percival	if [ -c "${DESTDIR}/dev/null" ]; then
3596341228SColin Percival		umount_loop ${DESTDIR}/dev 2>/dev/null
3696341228SColin Percival	fi
371e7c1f17SGlen Barber
381e7c1f17SGlen Barber	return 0
391e7c1f17SGlen Barber}
401e7c1f17SGlen Barber
411e7c1f17SGlen Barbervm_create_base() {
421e7c1f17SGlen Barber
431e7c1f17SGlen Barber	mkdir -p ${DESTDIR}
441e7c1f17SGlen Barber
451e7c1f17SGlen Barber	return 0
461e7c1f17SGlen Barber}
471e7c1f17SGlen Barber
4825c11557SColin Percivalvm_copy_base() {
491ca8842fSNathan Whitehorn	# Defunct
5025c11557SColin Percival}
5125c11557SColin Percival
521e7c1f17SGlen Barbervm_install_base() {
531e7c1f17SGlen Barber	# Installs the FreeBSD userland/kernel to the virtual machine disk.
541e7c1f17SGlen Barber
551e7c1f17SGlen Barber	cd ${WORLDDIR} && \
561e7c1f17SGlen Barber		make DESTDIR=${DESTDIR} \
571e7c1f17SGlen Barber		installworld installkernel distribution || \
581e7c1f17SGlen Barber		err "\n\nCannot install the base system to ${DESTDIR}."
591e7c1f17SGlen Barber
60c52962c6SGlen Barber	# Bootstrap etcupdate(8) and mergemaster(8) databases.
61c52962c6SGlen Barber	mkdir -p ${DESTDIR}/var/db/etcupdate
62c52962c6SGlen Barber	etcupdate extract -B \
63c52962c6SGlen Barber		-M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
64c52962c6SGlen Barber		-s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
65c52962c6SGlen Barber	sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
66c52962c6SGlen Barber		-F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
67c52962c6SGlen Barber		-D ${DESTDIR}
68c52962c6SGlen Barber
691e7c1f17SGlen Barber	echo '# Custom /etc/fstab for FreeBSD VM images' \
701e7c1f17SGlen Barber		> ${DESTDIR}/etc/fstab
71*89585511SMark Johnston	if [ "${VMFS}" != zfs ]; then
72*89585511SMark Johnston		echo "/dev/${ROOTLABEL}/rootfs   /       ${VMFS}   rw      1       1" \
731e7c1f17SGlen Barber			>> ${DESTDIR}/etc/fstab
74*89585511SMark Johnston	fi
754e7bc9f0SColin Percival	if [ -z "${NOSWAP}" ]; then
761e7c1f17SGlen Barber		echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
771e7c1f17SGlen Barber			>> ${DESTDIR}/etc/fstab
784e7bc9f0SColin Percival	fi
791e7c1f17SGlen Barber
802170fff8SGlen Barber	local hostname
812170fff8SGlen Barber	hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
822170fff8SGlen Barber	echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
83*89585511SMark Johnston	if [ "${VMFS}" = zfs ]; then
84*89585511SMark Johnston		echo "zfs_enable=\"YES\"" >> ${DESTDIR}/etc/rc.conf
85*89585511SMark Johnston		echo "zpool_reguid=\"zroot\"" >> ${DESTDIR}/etc/rc.conf
86*89585511SMark Johnston	fi
872170fff8SGlen Barber
881ecaa3f7SColin Percival	if ! [ -z "${QEMUSTATIC}" ]; then
891ecaa3f7SColin Percival		export EMULATOR=/qemu
901ecaa3f7SColin Percival		cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
911ecaa3f7SColin Percival	fi
921ecaa3f7SColin Percival
93e6f3489cSGlen Barber	mkdir -p ${DESTDIR}/dev
94e6f3489cSGlen Barber	mount -t devfs devfs ${DESTDIR}/dev
951ecaa3f7SColin Percival	chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
961ecaa3f7SColin Percival	chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
9796341228SColin Percival	umount_loop ${DESTDIR}/dev
981e7c1f17SGlen Barber
9930a20c89SGlen Barber	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
10030a20c89SGlen Barber
101*89585511SMark Johnston        if [ "${VMFS}" = zfs ]; then
102*89585511SMark Johnston		echo "kern.geom.label.disk_ident.enable=0" >> ${DESTDIR}/boot/loader.conf
103*89585511SMark Johnston		echo "zfs_load=YES" >> ${DESTDIR}/boot/loader.conf
104*89585511SMark Johnston        fi
105*89585511SMark Johnston
1061e7c1f17SGlen Barber	return 0
1071e7c1f17SGlen Barber}
1081e7c1f17SGlen Barber
1091e7c1f17SGlen Barbervm_extra_install_base() {
1101e7c1f17SGlen Barber	# Prototype.  When overridden, runs extra post-installworld commands
1111e7c1f17SGlen Barber	# as needed, based on the target virtual machine image or cloud
1121e7c1f17SGlen Barber	# provider image target.
1131e7c1f17SGlen Barber
1141e7c1f17SGlen Barber	return 0
1151e7c1f17SGlen Barber}
1161e7c1f17SGlen Barber
1171e7c1f17SGlen Barbervm_extra_enable_services() {
1181e7c1f17SGlen Barber	if [ ! -z "${VM_RC_LIST}" ]; then
1191e7c1f17SGlen Barber		for _rcvar in ${VM_RC_LIST}; do
1201e7c1f17SGlen Barber			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
1211e7c1f17SGlen Barber		done
1221e7c1f17SGlen Barber	fi
1231e7c1f17SGlen Barber
124f8e26d73SGlen Barber	if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
125f8e26d73SGlen Barber		echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
126f8e26d73SGlen Barber			${DESTDIR}/etc/rc.conf
127cbc52907SGlen Barber		# Expand the filesystem to fill the disk.
128cbc52907SGlen Barber		echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf
129cbc52907SGlen Barber		touch ${DESTDIR}/firstboot
130f8e26d73SGlen Barber	fi
131f8e26d73SGlen Barber
1321e7c1f17SGlen Barber	return 0
1331e7c1f17SGlen Barber}
1341e7c1f17SGlen Barber
1351e7c1f17SGlen Barbervm_extra_install_packages() {
136a4900ff5SGlen Barber	if [ -z "${VM_EXTRA_PACKAGES}" ]; then
137a4900ff5SGlen Barber		return 0
138a4900ff5SGlen Barber	fi
13930a20c89SGlen Barber	mkdir -p ${DESTDIR}/dev
14030a20c89SGlen Barber	mount -t devfs devfs ${DESTDIR}/dev
1411ecaa3f7SColin Percival	chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
14230a20c89SGlen Barber		/usr/sbin/pkg bootstrap -y
143be2bc82fSBrad Davis	for p in ${VM_EXTRA_PACKAGES}; do
1441ecaa3f7SColin Percival		chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
145fd17ea8cSBrad Davis			/usr/sbin/pkg install -y ${p}
146fd17ea8cSBrad Davis	done
14796341228SColin Percival	umount_loop ${DESTDIR}/dev
1481e7c1f17SGlen Barber
1491e7c1f17SGlen Barber	return 0
1501e7c1f17SGlen Barber}
1511e7c1f17SGlen Barber
1521e7c1f17SGlen Barbervm_extra_install_ports() {
1531e7c1f17SGlen Barber	# Prototype.  When overridden, installs additional ports within the
1541e7c1f17SGlen Barber	# virtual machine environment.
1551e7c1f17SGlen Barber
1561e7c1f17SGlen Barber	return 0
1571e7c1f17SGlen Barber}
1581e7c1f17SGlen Barber
159a02bfc83SGlen Barbervm_extra_pre_umount() {
16026e4122aSGlen Barber	# Prototype.  When overridden, performs additional tasks within the
16126e4122aSGlen Barber	# virtual machine environment prior to unmounting the filesystem.
16226e4122aSGlen Barber	# Note: When overriding this function, removing resolv.conf in the
16326e4122aSGlen Barber	# disk image must be included.
164a02bfc83SGlen Barber
1651ecaa3f7SColin Percival	if ! [ -z "${QEMUSTATIC}" ]; then
1661ecaa3f7SColin Percival		rm -f ${DESTDIR}/${EMULATOR}
1671ecaa3f7SColin Percival	fi
1682d4ff62fSGlen Barber	rm -f ${DESTDIR}/etc/resolv.conf
1692d4ff62fSGlen Barber	return 0
1702d4ff62fSGlen Barber}
1712d4ff62fSGlen Barber
1722d4ff62fSGlen Barbervm_extra_pkg_rmcache() {
173a4900ff5SGlen Barber	if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
1741ecaa3f7SColin Percival		chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
175a4900ff5SGlen Barber			/usr/local/sbin/pkg clean -y -a
176a4900ff5SGlen Barber	fi
17730a20c89SGlen Barber
178a02bfc83SGlen Barber	return 0
179a02bfc83SGlen Barber}
180a02bfc83SGlen Barber
181*89585511SMark Johnstonbuildfs() {
182*89585511SMark Johnston	local md tmppool
183*89585511SMark Johnston
184*89585511SMark Johnston	case "${VMFS}" in
185*89585511SMark Johnston	ufs)
186*89585511SMark Johnston		makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \
187*89585511SMark Johnston			${VMBASE} ${DESTDIR}
188*89585511SMark Johnston		;;
189*89585511SMark Johnston	zfs)
190*89585511SMark Johnston		makefs -t zfs ${MAKEFSARGS} \
191*89585511SMark Johnston			-o poolname=zroot -o bootfs=zroot/ROOT/default -o rootpath=/ \
192*89585511SMark Johnston			-o fs=zroot\;mountpoint=none \
193*89585511SMark Johnston			-o fs=zroot/ROOT\;mountpoint=none \
194*89585511SMark Johnston			-o fs=zroot/ROOT/default\;mountpoint=/ \
195*89585511SMark Johnston			-o fs=zroot/tmp\;mountpoint=/tmp\;exec=on\;setuid=off \
196*89585511SMark Johnston			-o fs=zroot/usr\;mountpoint=/usr\;canmount=off \
197*89585511SMark Johnston			-o fs=zroot/usr/home \
198*89585511SMark Johnston			-o fs=zroot/usr/ports\;setuid=off \
199*89585511SMark Johnston			-o fs=zroot/usr/src \
200*89585511SMark Johnston			-o fs=zroot/usr/obj \
201*89585511SMark Johnston			-o fs=zroot/var\;mountpoint=/var\;canmount=off \
202*89585511SMark Johnston			-o fs=zroot/var/audit\;setuid=off\;exec=off \
203*89585511SMark Johnston			-o fs=zroot/var/log\;setuid=off\;exec=off \
204*89585511SMark Johnston			-o fs=zroot/var/mail\;atime=on \
205*89585511SMark Johnston			-o fs=zroot/var/tmp\;setuid=off \
206*89585511SMark Johnston			${VMBASE} ${DESTDIR}
207*89585511SMark Johnston		;;
208*89585511SMark Johnston	*)
209*89585511SMark Johnston		echo "Unexpected VMFS value '${VMFS}'"
210*89585511SMark Johnston		exit 1
211*89585511SMark Johnston		;;
212*89585511SMark Johnston	esac
213*89585511SMark Johnston}
214*89585511SMark Johnston
21596341228SColin Percivalumount_loop() {
21696341228SColin Percival	DIR=$1
2171e7c1f17SGlen Barber	i=0
2181e7c1f17SGlen Barber	sync
21996341228SColin Percival	while ! umount ${DIR}; do
2201e7c1f17SGlen Barber		i=$(( $i + 1 ))
2211e7c1f17SGlen Barber		if [ $i -ge 10 ]; then
2221e7c1f17SGlen Barber			# This should never happen.  But, it has happened.
22396341228SColin Percival			echo "Cannot umount(8) ${DIR}"
22496341228SColin Percival			echo "Something has gone horribly wrong."
22596341228SColin Percival			return 1
2261e7c1f17SGlen Barber		fi
2271e7c1f17SGlen Barber		sleep 1
2281e7c1f17SGlen Barber	done
2291e7c1f17SGlen Barber
2301e7c1f17SGlen Barber	return 0
2311e7c1f17SGlen Barber}
2321e7c1f17SGlen Barber
2331e7c1f17SGlen Barbervm_create_disk() {
234*89585511SMark Johnston	local BOOTFILES BOOTPARTSOFFSET FSPARTTYPE X86GPTBOOTFILE
2351e7c1f17SGlen Barber
2361ca8842fSNathan Whitehorn	if [ -z "${NOSWAP}" ]; then
2371ca8842fSNathan Whitehorn		SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
2381ca8842fSNathan Whitehorn	fi
2391ca8842fSNathan Whitehorn
240fbece760SLi-Wen Hsu	if [ -n "${VM_BOOTPARTSOFFSET}" ]; then
241fbece760SLi-Wen Hsu		BOOTPARTSOFFSET=":${VM_BOOTPARTSOFFSET}"
242fbece760SLi-Wen Hsu	fi
243fbece760SLi-Wen Hsu
244*89585511SMark Johnston	case "${VMFS}" in
245*89585511SMark Johnston	ufs)
246*89585511SMark Johnston		FSPARTTYPE=freebsd-ufs
247*89585511SMark Johnston		X86GPTBOOTFILE=i386/gptboot/gptboot
248*89585511SMark Johnston		;;
249*89585511SMark Johnston	zfs)
250*89585511SMark Johnston		FSPARTTYPE=freebsd-zfs
251*89585511SMark Johnston		X86GPTBOOTFILE=i386/gptzfsboot/gptzfsboot
252*89585511SMark Johnston		;;
253*89585511SMark Johnston	*)
254*89585511SMark Johnston		echo "Unexpected VMFS value '${VMFS}'"
255*89585511SMark Johnston		return 1
256*89585511SMark Johnston		;;
257*89585511SMark Johnston	esac
258*89585511SMark Johnston
259*89585511SMark Johnston	echo "Creating image...  Please wait."
260*89585511SMark Johnston	echo
261fbece760SLi-Wen Hsu
2621ca8842fSNathan Whitehorn	BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
2631ca8842fSNathan Whitehorn		WITH_UNIFIED_OBJDIR=yes \
2641ca8842fSNathan Whitehorn		make -C ${WORLDDIR}/stand -V .OBJDIR)"
2651ca8842fSNathan Whitehorn	BOOTFILES="$(realpath ${BOOTFILES})"
266*89585511SMark Johnston	MAKEFSARGS="-s ${VMSIZE}"
2671ca8842fSNathan Whitehorn
2681ca8842fSNathan Whitehorn	case "${TARGET}:${TARGET_ARCH}" in
2691ca8842fSNathan Whitehorn		amd64:amd64 | i386:i386)
2701ca8842fSNathan Whitehorn			ESP=yes
2711ca8842fSNathan Whitehorn			BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \
272*89585511SMark Johnston				   -p freebsd-boot/bootfs:=${BOOTFILES}/${X86GPTBOOTFILE}${BOOTPARTSOFFSET}"
273*89585511SMark Johnston			ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}"
274*89585511SMark Johnston			MAKEFSARGS="$MAKEFSARGS -B little"
2751ca8842fSNathan Whitehorn			;;
2761ca8842fSNathan Whitehorn		arm64:aarch64 | riscv:riscv64*)
2771ca8842fSNathan Whitehorn			ESP=yes
2781ca8842fSNathan Whitehorn			BOOTPARTS=
279*89585511SMark Johnston			ROOTFSPART="-p ${FSPARTTYPE}/rootfs:=${VMBASE}"
280*89585511SMark Johnston			MAKEFSARGS="$MAKEFSARGS -B little"
2811ca8842fSNathan Whitehorn			;;
2821ca8842fSNathan Whitehorn		powerpc:powerpc*)
2831ca8842fSNathan Whitehorn			ESP=no
2841ca8842fSNathan Whitehorn			BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1"
2851ca8842fSNathan Whitehorn			ROOTFSPART="-p freebsd:=${VMBASE}"
2861ca8842fSNathan Whitehorn			if [ ${TARGET_ARCH} = powerpc64le ]; then
287*89585511SMark Johnston				MAKEFSARGS="$MAKEFSARGS -B little"
2881ca8842fSNathan Whitehorn			else
289*89585511SMark Johnston				MAKEFSARGS="$MAKEFSARGS -B big"
2901ca8842fSNathan Whitehorn			fi
2911ca8842fSNathan Whitehorn			;;
2921ca8842fSNathan Whitehorn		*)
2931ca8842fSNathan Whitehorn			echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2
2941ca8842fSNathan Whitehorn			exit 1
2951ca8842fSNathan Whitehorn			;;
2961ca8842fSNathan Whitehorn	esac
2971ca8842fSNathan Whitehorn
2981ca8842fSNathan Whitehorn	if [ ${ESP} = "yes" ]; then
2991ca8842fSNathan Whitehorn		# Create an ESP
3001ca8842fSNathan Whitehorn		espfilename=$(mktemp /tmp/efiboot.XXXXXX)
3011ca8842fSNathan Whitehorn		make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
3021ca8842fSNathan Whitehorn		BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}"
3031ca8842fSNathan Whitehorn
3041ca8842fSNathan Whitehorn		# Add this to fstab
3051ca8842fSNathan Whitehorn		mkdir -p ${DESTDIR}/boot/efi
3061ca8842fSNathan Whitehorn		echo "/dev/${ROOTLABEL}/efiesp	/boot/efi       msdosfs     rw      2       2" \
3071ca8842fSNathan Whitehorn			>> ${DESTDIR}/etc/fstab
3081ca8842fSNathan Whitehorn	fi
3091ca8842fSNathan Whitehorn
3101ca8842fSNathan Whitehorn	echo "Building filesystem...  Please wait."
311*89585511SMark Johnston	buildfs
3121ca8842fSNathan Whitehorn
3131ca8842fSNathan Whitehorn	echo "Building final disk image...  Please wait."
3141ca8842fSNathan Whitehorn	mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \
3151ca8842fSNathan Whitehorn		${BOOTPARTS} \
3161ca8842fSNathan Whitehorn		${SWAPOPT} \
3171ca8842fSNathan Whitehorn		${ROOTFSPART} \
3181ca8842fSNathan Whitehorn		-o ${VMIMAGE}
3191ca8842fSNathan Whitehorn
3201ca8842fSNathan Whitehorn	if [ ${ESP} = "yes" ]; then
3211ca8842fSNathan Whitehorn		rm ${espfilename}
3221ca8842fSNathan Whitehorn	fi
3231e7c1f17SGlen Barber
3241e7c1f17SGlen Barber	return 0
3251e7c1f17SGlen Barber}
3261e7c1f17SGlen Barber
3271e7c1f17SGlen Barbervm_extra_create_disk() {
3281e7c1f17SGlen Barber
3291e7c1f17SGlen Barber	return 0
3301e7c1f17SGlen Barber}
3311e7c1f17SGlen Barber
332