xref: /freebsd/release/tools/vmimage.subr (revision db8b56134506840832bec2d1ce07b9e00d4d6d71)
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
9*db8b5613SRebecca Cranscriptdir=$(dirname $(realpath $0))
10*db8b5613SRebecca Cran. ${scriptdir}/../../tools/boot/install-boot.sh
11*db8b5613SRebecca 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
15efeb11a7SGlen Barberwrite_partition_layout() {
164e7bc9f0SColin Percival	if [ -z "${NOSWAP}" ]; then
17eb46c987SNavdeep Parhar		SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
184e7bc9f0SColin Percival	fi
19efeb11a7SGlen Barber
20dfa09989SBryan Drewery	BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
21dfa09989SBryan Drewery		WITH_UNIFIED_OBJDIR=yes \
22ca987d46SWarner Losh		make -C ${WORLDDIR}/stand -V .OBJDIR)"
23dfa09989SBryan Drewery	BOOTFILES="$(realpath ${BOOTFILES})"
244b8175eeSGlen Barber
25efeb11a7SGlen Barber	case "${TARGET}:${TARGET_ARCH}" in
26efeb11a7SGlen Barber		amd64:amd64 | i386:i386)
27ccbf2502SGlen Barber			mkimg -s gpt -f ${VMFORMAT} \
28ccbf2502SGlen Barber				-b ${BOOTFILES}/i386/pmbr/pmbr \
294b8175eeSGlen Barber				-p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot \
304e7bc9f0SColin Percival				${SWAPOPT} \
3138d59ac7SGlen Barber				-p freebsd-ufs/rootfs:=${VMBASE} \
32efeb11a7SGlen Barber				-o ${VMIMAGE}
33efeb11a7SGlen Barber			;;
346a3afe69SGlen Barber		arm64:aarch64)
35*db8b5613SRebecca Cran			# Create an ESP
36*db8b5613SRebecca Cran			espfilename=$(mktemp /tmp/efiboot.XXXXXX)
37*db8b5613SRebecca Cran			make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
38ccbf2502SGlen Barber			mkimg -s mbr -f ${VMFORMAT} \
39*db8b5613SRebecca Cran				-p efi:=${espfilename} \
4008a75d1fSGlen Barber				-p freebsd:=${VMBASE} \
416a3afe69SGlen Barber				-o ${VMIMAGE}
42*db8b5613SRebecca Cran			rm ${espfilename}
436a3afe69SGlen Barber			;;
44efeb11a7SGlen Barber		powerpc:powerpc*)
45ccbf2502SGlen Barber			mkimg -s apm -f ${VMFORMAT} \
464b8175eeSGlen Barber				-p apple-boot/bootfs:=${BOOTFILES}/powerpc/boot1.chrp/boot1.hfs \
474e7bc9f0SColin Percival				${SWAPOPT} \
4838d59ac7SGlen Barber				-p freebsd-ufs/rootfs:=${VMBASE} \
49efeb11a7SGlen Barber				-o ${VMIMAGE}
50efeb11a7SGlen Barber			;;
51efeb11a7SGlen Barber		*)
52efeb11a7SGlen Barber			# ENOTSUPP
53efeb11a7SGlen Barber			return 1
54efeb11a7SGlen Barber			;;
55efeb11a7SGlen Barber	esac
56efeb11a7SGlen Barber
57efeb11a7SGlen Barber	return 0
58efeb11a7SGlen Barber}
591e7c1f17SGlen Barber
601e7c1f17SGlen Barbererr() {
611e7c1f17SGlen Barber	printf "${@}\n"
621e7c1f17SGlen Barber	cleanup
631e7c1f17SGlen Barber	return 1
641e7c1f17SGlen Barber}
651e7c1f17SGlen Barber
661e7c1f17SGlen Barbercleanup() {
67dcdebabdSColin Percival	if [ -c "${DESTDIR}/dev/null" ]; then
6896341228SColin Percival		umount_loop ${DESTDIR}/dev 2>/dev/null
6996341228SColin Percival	fi
7096341228SColin Percival	umount_loop ${DESTDIR}
711e7c1f17SGlen Barber	if [ ! -z "${mddev}" ]; then
721e7c1f17SGlen Barber		mdconfig -d -u ${mddev}
731e7c1f17SGlen Barber	fi
741e7c1f17SGlen Barber
751e7c1f17SGlen Barber	return 0
761e7c1f17SGlen Barber}
771e7c1f17SGlen Barber
781e7c1f17SGlen Barbervm_create_base() {
791e7c1f17SGlen Barber	# Creates the UFS root filesystem for the virtual machine disk,
801e7c1f17SGlen Barber	# written to the formatted disk image with mkimg(1).
811e7c1f17SGlen Barber
821e7c1f17SGlen Barber	mkdir -p ${DESTDIR}
831e7c1f17SGlen Barber	truncate -s ${VMSIZE} ${VMBASE}
841e7c1f17SGlen Barber	mddev=$(mdconfig -f ${VMBASE})
8508a75d1fSGlen Barber	newfs -L rootfs /dev/${mddev}
861e7c1f17SGlen Barber	mount /dev/${mddev} ${DESTDIR}
871e7c1f17SGlen Barber
881e7c1f17SGlen Barber	return 0
891e7c1f17SGlen Barber}
901e7c1f17SGlen Barber
9125c11557SColin Percivalvm_copy_base() {
9225c11557SColin Percival	# Creates a new UFS root filesystem and copies the contents of the
9325c11557SColin Percival	# current root filesystem into it.  This produces a "clean" disk
9425c11557SColin Percival	# image without any remnants of files which were created temporarily
9525c11557SColin Percival	# during image-creation and have since been deleted (e.g., downloaded
9625c11557SColin Percival	# package archives).
9725c11557SColin Percival
9825c11557SColin Percival	mkdir -p ${DESTDIR}/old
9925c11557SColin Percival	mdold=$(mdconfig -f ${VMBASE})
10025c11557SColin Percival	mount /dev/${mdold} ${DESTDIR}/old
10125c11557SColin Percival
10225c11557SColin Percival	truncate -s ${VMSIZE} ${VMBASE}.tmp
10325c11557SColin Percival	mkdir -p ${DESTDIR}/new
10425c11557SColin Percival	mdnew=$(mdconfig -f ${VMBASE}.tmp)
10508a75d1fSGlen Barber	newfs -L rootfs /dev/${mdnew}
10625c11557SColin Percival	mount /dev/${mdnew} ${DESTDIR}/new
10725c11557SColin Percival
1084b8175eeSGlen Barber	tar -cf- -C ${DESTDIR}/old . | tar -xUf- -C ${DESTDIR}/new
10925c11557SColin Percival
11096341228SColin Percival	umount_loop /dev/${mdold}
11125c11557SColin Percival	rmdir ${DESTDIR}/old
11225c11557SColin Percival	mdconfig -d -u ${mdold}
11325c11557SColin Percival
11496341228SColin Percival	umount_loop /dev/${mdnew}
11525c11557SColin Percival	rmdir ${DESTDIR}/new
11691c42fffSGlen Barber	tunefs -n enable /dev/${mdnew}
11725c11557SColin Percival	mdconfig -d -u ${mdnew}
11825c11557SColin Percival	mv ${VMBASE}.tmp ${VMBASE}
11925c11557SColin Percival}
12025c11557SColin Percival
1211e7c1f17SGlen Barbervm_install_base() {
1221e7c1f17SGlen Barber	# Installs the FreeBSD userland/kernel to the virtual machine disk.
1231e7c1f17SGlen Barber
1241e7c1f17SGlen Barber	cd ${WORLDDIR} && \
1251e7c1f17SGlen Barber		make DESTDIR=${DESTDIR} \
1261e7c1f17SGlen Barber		installworld installkernel distribution || \
1271e7c1f17SGlen Barber		err "\n\nCannot install the base system to ${DESTDIR}."
1281e7c1f17SGlen Barber
129c52962c6SGlen Barber	# Bootstrap etcupdate(8) and mergemaster(8) databases.
130c52962c6SGlen Barber	mkdir -p ${DESTDIR}/var/db/etcupdate
131c52962c6SGlen Barber	etcupdate extract -B \
132c52962c6SGlen Barber		-M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
133c52962c6SGlen Barber		-s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
134c52962c6SGlen Barber	sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
135c52962c6SGlen Barber		-F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
136c52962c6SGlen Barber		-D ${DESTDIR}
137c52962c6SGlen Barber
1381e7c1f17SGlen Barber	echo '# Custom /etc/fstab for FreeBSD VM images' \
1391e7c1f17SGlen Barber		> ${DESTDIR}/etc/fstab
14008a75d1fSGlen Barber	echo "/dev/${ROOTLABEL}/rootfs   /       ufs     rw      1       1" \
1411e7c1f17SGlen Barber		>> ${DESTDIR}/etc/fstab
1424e7bc9f0SColin Percival	if [ -z "${NOSWAP}" ]; then
1431e7c1f17SGlen Barber		echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
1441e7c1f17SGlen Barber			>> ${DESTDIR}/etc/fstab
1454e7bc9f0SColin Percival	fi
1461e7c1f17SGlen Barber
1472170fff8SGlen Barber	local hostname
1482170fff8SGlen Barber	hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
1492170fff8SGlen Barber	echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
1502170fff8SGlen Barber
151e6f3489cSGlen Barber	mkdir -p ${DESTDIR}/dev
152e6f3489cSGlen Barber	mount -t devfs devfs ${DESTDIR}/dev
1531e7c1f17SGlen Barber	chroot ${DESTDIR} /usr/bin/newaliases
1541e7c1f17SGlen Barber	chroot ${DESTDIR} /etc/rc.d/ldconfig forcestart
15596341228SColin Percival	umount_loop ${DESTDIR}/dev
1561e7c1f17SGlen Barber
15730a20c89SGlen Barber	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
15830a20c89SGlen Barber
1591e7c1f17SGlen Barber	return 0
1601e7c1f17SGlen Barber}
1611e7c1f17SGlen Barber
1621e7c1f17SGlen Barbervm_extra_install_base() {
1631e7c1f17SGlen Barber	# Prototype.  When overridden, runs extra post-installworld commands
1641e7c1f17SGlen Barber	# as needed, based on the target virtual machine image or cloud
1651e7c1f17SGlen Barber	# provider image target.
1661e7c1f17SGlen Barber
1671e7c1f17SGlen Barber	return 0
1681e7c1f17SGlen Barber}
1691e7c1f17SGlen Barber
1701e7c1f17SGlen Barbervm_extra_enable_services() {
1711e7c1f17SGlen Barber	if [ ! -z "${VM_RC_LIST}" ]; then
1721e7c1f17SGlen Barber		for _rcvar in ${VM_RC_LIST}; do
1731e7c1f17SGlen Barber			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
1741e7c1f17SGlen Barber		done
1751e7c1f17SGlen Barber	fi
1761e7c1f17SGlen Barber
177f8e26d73SGlen Barber	if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
178f8e26d73SGlen Barber		echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
179f8e26d73SGlen Barber			${DESTDIR}/etc/rc.conf
180f8e26d73SGlen Barber	fi
181f8e26d73SGlen Barber
1821e7c1f17SGlen Barber	return 0
1831e7c1f17SGlen Barber}
1841e7c1f17SGlen Barber
1851e7c1f17SGlen Barbervm_extra_install_packages() {
186a4900ff5SGlen Barber	if [ -z "${VM_EXTRA_PACKAGES}" ]; then
187a4900ff5SGlen Barber		return 0
188a4900ff5SGlen Barber	fi
18930a20c89SGlen Barber	mkdir -p ${DESTDIR}/dev
19030a20c89SGlen Barber	mount -t devfs devfs ${DESTDIR}/dev
19130a20c89SGlen Barber	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
19230a20c89SGlen Barber		/usr/sbin/pkg bootstrap -y
19330a20c89SGlen Barber	chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
19430a20c89SGlen Barber		/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
19596341228SColin Percival	umount_loop ${DESTDIR}/dev
1961e7c1f17SGlen Barber
1971e7c1f17SGlen Barber	return 0
1981e7c1f17SGlen Barber}
1991e7c1f17SGlen Barber
2001e7c1f17SGlen Barbervm_extra_install_ports() {
2011e7c1f17SGlen Barber	# Prototype.  When overridden, installs additional ports within the
2021e7c1f17SGlen Barber	# virtual machine environment.
2031e7c1f17SGlen Barber
2041e7c1f17SGlen Barber	return 0
2051e7c1f17SGlen Barber}
2061e7c1f17SGlen Barber
207a02bfc83SGlen Barbervm_extra_pre_umount() {
20826e4122aSGlen Barber	# Prototype.  When overridden, performs additional tasks within the
20926e4122aSGlen Barber	# virtual machine environment prior to unmounting the filesystem.
21026e4122aSGlen Barber	# Note: When overriding this function, removing resolv.conf in the
21126e4122aSGlen Barber	# disk image must be included.
212a02bfc83SGlen Barber
2132d4ff62fSGlen Barber	rm -f ${DESTDIR}/etc/resolv.conf
2142d4ff62fSGlen Barber	return 0
2152d4ff62fSGlen Barber}
2162d4ff62fSGlen Barber
2172d4ff62fSGlen Barbervm_extra_pkg_rmcache() {
218a4900ff5SGlen Barber	if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
219a4900ff5SGlen Barber		chroot ${DESTDIR} env ASSUME_ALWAYS_YES=yes \
220a4900ff5SGlen Barber			/usr/local/sbin/pkg clean -y -a
221a4900ff5SGlen Barber	fi
22230a20c89SGlen Barber
223a02bfc83SGlen Barber	return 0
224a02bfc83SGlen Barber}
225a02bfc83SGlen Barber
22696341228SColin Percivalumount_loop() {
22796341228SColin Percival	DIR=$1
2281e7c1f17SGlen Barber	i=0
2291e7c1f17SGlen Barber	sync
23096341228SColin Percival	while ! umount ${DIR}; do
2311e7c1f17SGlen Barber		i=$(( $i + 1 ))
2321e7c1f17SGlen Barber		if [ $i -ge 10 ]; then
2331e7c1f17SGlen Barber			# This should never happen.  But, it has happened.
23496341228SColin Percival			echo "Cannot umount(8) ${DIR}"
23596341228SColin Percival			echo "Something has gone horribly wrong."
23696341228SColin Percival			return 1
2371e7c1f17SGlen Barber		fi
2381e7c1f17SGlen Barber		sleep 1
2391e7c1f17SGlen Barber	done
2401e7c1f17SGlen Barber
2411e7c1f17SGlen Barber	return 0
2421e7c1f17SGlen Barber}
2431e7c1f17SGlen Barber
2441e7c1f17SGlen Barbervm_create_disk() {
2451e7c1f17SGlen Barber	echo "Creating image...  Please wait."
2461e7c1f17SGlen Barber	echo
2471e7c1f17SGlen Barber
248efeb11a7SGlen Barber	write_partition_layout || return 1
2491e7c1f17SGlen Barber
2501e7c1f17SGlen Barber	return 0
2511e7c1f17SGlen Barber}
2521e7c1f17SGlen Barber
2531e7c1f17SGlen Barbervm_extra_create_disk() {
2541e7c1f17SGlen Barber
2551e7c1f17SGlen Barber	return 0
2561e7c1f17SGlen Barber}
2571e7c1f17SGlen Barber
258