xref: /freebsd/release/tools/vmimage.subr (revision be2bc82f18fb544c5a62b02f6faf2e320e0e881e)
1#!/bin/sh
2#
3# $FreeBSD$
4#
5#
6# Common functions for virtual machine image build scripts.
7#
8
9scriptdir=$(dirname $(realpath $0))
10. ${scriptdir}/../../tools/boot/install-boot.sh
11
12export PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
13trap "cleanup" INT QUIT TRAP ABRT TERM
14
15# Platform-specific large-scale setup
16# Most platforms use GPT, so put that as default, then special cases
17PARTSCHEME=gpt
18ROOTLABEL="gpt"
19case "${TARGET}:${TARGET_ARCH}" in
20	powerpc:powerpc*)
21		PARTSCHEME=mbr
22		ROOTLABEL="ufs"
23		NOSWAP=yes # Can't label swap partition with MBR, so no swap
24	;;
25esac
26
27err() {
28	printf "${@}\n"
29	cleanup
30	return 1
31}
32
33cleanup() {
34	if [ -c "${DESTDIR}/dev/null" ]; then
35		umount_loop ${DESTDIR}/dev 2>/dev/null
36	fi
37
38	return 0
39}
40
41vm_create_base() {
42	# Creates the UFS root filesystem for the virtual machine disk,
43	# written to the formatted disk image with mkimg(1).
44
45	mkdir -p ${DESTDIR}
46
47	return 0
48}
49
50vm_copy_base() {
51	# Defunct
52}
53
54vm_install_base() {
55	# Installs the FreeBSD userland/kernel to the virtual machine disk.
56
57	cd ${WORLDDIR} && \
58		make DESTDIR=${DESTDIR} \
59		installworld installkernel distribution || \
60		err "\n\nCannot install the base system to ${DESTDIR}."
61
62	# Bootstrap etcupdate(8) and mergemaster(8) databases.
63	mkdir -p ${DESTDIR}/var/db/etcupdate
64	etcupdate extract -B \
65		-M "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
66		-s ${WORLDDIR} -d ${DESTDIR}/var/db/etcupdate
67	sh ${WORLDDIR}/release/scripts/mm-mtree.sh -m ${WORLDDIR} \
68		-F "TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}" \
69		-D ${DESTDIR}
70
71	echo '# Custom /etc/fstab for FreeBSD VM images' \
72		> ${DESTDIR}/etc/fstab
73	echo "/dev/${ROOTLABEL}/rootfs   /       ufs     rw      1       1" \
74		>> ${DESTDIR}/etc/fstab
75	if [ -z "${NOSWAP}" ]; then
76		echo '/dev/gpt/swapfs  none    swap    sw      0       0' \
77			>> ${DESTDIR}/etc/fstab
78	fi
79
80	local hostname
81	hostname="$(echo $(uname -o) | tr '[:upper:]' '[:lower:]')"
82	echo "hostname=\"${hostname}\"" >> ${DESTDIR}/etc/rc.conf
83
84	if ! [ -z "${QEMUSTATIC}" ]; then
85		export EMULATOR=/qemu
86		cp ${QEMUSTATIC} ${DESTDIR}/${EMULATOR}
87	fi
88
89	mkdir -p ${DESTDIR}/dev
90	mount -t devfs devfs ${DESTDIR}/dev
91	chroot ${DESTDIR} ${EMULATOR} /usr/bin/newaliases
92	chroot ${DESTDIR} ${EMULATOR} /bin/sh /etc/rc.d/ldconfig forcestart
93	umount_loop ${DESTDIR}/dev
94
95	cp /etc/resolv.conf ${DESTDIR}/etc/resolv.conf
96
97	return 0
98}
99
100vm_extra_install_base() {
101	# Prototype.  When overridden, runs extra post-installworld commands
102	# as needed, based on the target virtual machine image or cloud
103	# provider image target.
104
105	return 0
106}
107
108vm_extra_enable_services() {
109	if [ ! -z "${VM_RC_LIST}" ]; then
110		for _rcvar in ${VM_RC_LIST}; do
111			echo ${_rcvar}_enable="YES" >> ${DESTDIR}/etc/rc.conf
112		done
113	fi
114
115	if [ -z "${VMCONFIG}" -o -c "${VMCONFIG}" ]; then
116		echo 'ifconfig_DEFAULT="DHCP inet6 accept_rtadv"' >> \
117			${DESTDIR}/etc/rc.conf
118		# Expand the filesystem to fill the disk.
119		echo 'growfs_enable="YES"' >> ${DESTDIR}/etc/rc.conf
120		touch ${DESTDIR}/firstboot
121	fi
122
123	return 0
124}
125
126vm_extra_install_packages() {
127	if [ -z "${VM_EXTRA_PACKAGES}" ]; then
128		return 0
129	fi
130	mkdir -p ${DESTDIR}/dev
131	mount -t devfs devfs ${DESTDIR}/dev
132	chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
133		/usr/sbin/pkg bootstrap -y
134	for p in ${VM_EXTRA_PACKAGES}; do
135		chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
136			/usr/sbin/pkg install -y ${p}
137	done
138	umount_loop ${DESTDIR}/dev
139
140	return 0
141}
142
143vm_extra_install_ports() {
144	# Prototype.  When overridden, installs additional ports within the
145	# virtual machine environment.
146
147	return 0
148}
149
150vm_extra_pre_umount() {
151	# Prototype.  When overridden, performs additional tasks within the
152	# virtual machine environment prior to unmounting the filesystem.
153	# Note: When overriding this function, removing resolv.conf in the
154	# disk image must be included.
155
156	if ! [ -z "${QEMUSTATIC}" ]; then
157		rm -f ${DESTDIR}/${EMULATOR}
158	fi
159	rm -f ${DESTDIR}/etc/resolv.conf
160	return 0
161}
162
163vm_extra_pkg_rmcache() {
164	if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
165		chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
166			/usr/local/sbin/pkg clean -y -a
167	fi
168
169	return 0
170}
171
172umount_loop() {
173	DIR=$1
174	i=0
175	sync
176	while ! umount ${DIR}; do
177		i=$(( $i + 1 ))
178		if [ $i -ge 10 ]; then
179			# This should never happen.  But, it has happened.
180			echo "Cannot umount(8) ${DIR}"
181			echo "Something has gone horribly wrong."
182			return 1
183		fi
184		sleep 1
185	done
186
187	return 0
188}
189
190vm_create_disk() {
191	echo "Creating image...  Please wait."
192	echo
193
194	if [ -z "${NOSWAP}" ]; then
195		SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
196	fi
197
198	BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
199		WITH_UNIFIED_OBJDIR=yes \
200		make -C ${WORLDDIR}/stand -V .OBJDIR)"
201	BOOTFILES="$(realpath ${BOOTFILES})"
202
203	case "${TARGET}:${TARGET_ARCH}" in
204		amd64:amd64 | i386:i386)
205			ESP=yes
206			BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \
207				   -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot"
208			ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
209			MAKEFSARGS="-B little"
210			;;
211		arm64:aarch64 | riscv:riscv64*)
212			ESP=yes
213			BOOTPARTS=
214			ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
215			MAKEFSARGS="-B little"
216			;;
217		powerpc:powerpc*)
218			ESP=no
219			BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1"
220			ROOTFSPART="-p freebsd:=${VMBASE}"
221			if [ ${TARGET_ARCH} = powerpc64le ]; then
222				MAKEFSARGS="-B little"
223			else
224				MAKEFSARGS="-B big"
225			fi
226			;;
227		*)
228			echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2
229			exit 1
230			;;
231	esac
232
233	if [ ${ESP} = "yes" ]; then
234		# Create an ESP
235		espfilename=$(mktemp /tmp/efiboot.XXXXXX)
236		make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
237		BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}"
238
239		# Add this to fstab
240		mkdir -p ${DESTDIR}/boot/efi
241		echo "/dev/${ROOTLABEL}/efiesp	/boot/efi       msdosfs     rw      2       2" \
242			>> ${DESTDIR}/etc/fstab
243	fi
244
245	echo "Building filesystem...  Please wait."
246	makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \
247	    -s ${VMSIZE} ${VMBASE} ${DESTDIR}
248
249	echo "Building final disk image...  Please wait."
250	mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \
251		${BOOTPARTS} \
252		${SWAPOPT} \
253		${ROOTFSPART} \
254		-o ${VMIMAGE}
255
256	if [ ${ESP} = "yes" ]; then
257		rm ${espfilename}
258	fi
259
260	return 0
261}
262
263vm_extra_create_disk() {
264
265	return 0
266}
267
268