xref: /freebsd/release/tools/vmimage.subr (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
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	if [ -n "${VM_BOOTPARTSOFFSET}" ]; then
199		BOOTPARTSOFFSET=":${VM_BOOTPARTSOFFSET}"
200	fi
201
202
203	BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
204		WITH_UNIFIED_OBJDIR=yes \
205		make -C ${WORLDDIR}/stand -V .OBJDIR)"
206	BOOTFILES="$(realpath ${BOOTFILES})"
207
208	case "${TARGET}:${TARGET_ARCH}" in
209		amd64:amd64 | i386:i386)
210			ESP=yes
211			BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \
212				   -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot${BOOTPARTSOFFSET}"
213			ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
214			MAKEFSARGS="-B little"
215			;;
216		arm64:aarch64 | riscv:riscv64*)
217			ESP=yes
218			BOOTPARTS=
219			ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
220			MAKEFSARGS="-B little"
221			;;
222		powerpc:powerpc*)
223			ESP=no
224			BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1"
225			ROOTFSPART="-p freebsd:=${VMBASE}"
226			if [ ${TARGET_ARCH} = powerpc64le ]; then
227				MAKEFSARGS="-B little"
228			else
229				MAKEFSARGS="-B big"
230			fi
231			;;
232		*)
233			echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2
234			exit 1
235			;;
236	esac
237
238	if [ ${ESP} = "yes" ]; then
239		# Create an ESP
240		espfilename=$(mktemp /tmp/efiboot.XXXXXX)
241		make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
242		BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}"
243
244		# Add this to fstab
245		mkdir -p ${DESTDIR}/boot/efi
246		echo "/dev/${ROOTLABEL}/efiesp	/boot/efi       msdosfs     rw      2       2" \
247			>> ${DESTDIR}/etc/fstab
248	fi
249
250	echo "Building filesystem...  Please wait."
251	makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \
252	    -s ${VMSIZE} ${VMBASE} ${DESTDIR}
253
254	echo "Building final disk image...  Please wait."
255	mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \
256		${BOOTPARTS} \
257		${SWAPOPT} \
258		${ROOTFSPART} \
259		-o ${VMIMAGE}
260
261	if [ ${ESP} = "yes" ]; then
262		rm ${espfilename}
263	fi
264
265	return 0
266}
267
268vm_extra_create_disk() {
269
270	return 0
271}
272
273