xref: /freebsd/release/tools/vmimage.subr (revision 1ca8842f3ad9725863c9affc044d1974a51818a9)
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	chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
135		/usr/sbin/pkg install -y ${VM_EXTRA_PACKAGES}
136	umount_loop ${DESTDIR}/dev
137
138	return 0
139}
140
141vm_extra_install_ports() {
142	# Prototype.  When overridden, installs additional ports within the
143	# virtual machine environment.
144
145	return 0
146}
147
148vm_extra_pre_umount() {
149	# Prototype.  When overridden, performs additional tasks within the
150	# virtual machine environment prior to unmounting the filesystem.
151	# Note: When overriding this function, removing resolv.conf in the
152	# disk image must be included.
153
154	if ! [ -z "${QEMUSTATIC}" ]; then
155		rm -f ${DESTDIR}/${EMULATOR}
156	fi
157	rm -f ${DESTDIR}/etc/resolv.conf
158	return 0
159}
160
161vm_extra_pkg_rmcache() {
162	if [ -e ${DESTDIR}/usr/local/sbin/pkg ]; then
163		chroot ${DESTDIR} ${EMULATOR} env ASSUME_ALWAYS_YES=yes \
164			/usr/local/sbin/pkg clean -y -a
165	fi
166
167	return 0
168}
169
170umount_loop() {
171	DIR=$1
172	i=0
173	sync
174	while ! umount ${DIR}; do
175		i=$(( $i + 1 ))
176		if [ $i -ge 10 ]; then
177			# This should never happen.  But, it has happened.
178			echo "Cannot umount(8) ${DIR}"
179			echo "Something has gone horribly wrong."
180			return 1
181		fi
182		sleep 1
183	done
184
185	return 0
186}
187
188vm_create_disk() {
189	echo "Creating image...  Please wait."
190	echo
191
192	if [ -z "${NOSWAP}" ]; then
193		SWAPOPT="-p freebsd-swap/swapfs::${SWAPSIZE}"
194	fi
195
196	BOOTFILES="$(env TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
197		WITH_UNIFIED_OBJDIR=yes \
198		make -C ${WORLDDIR}/stand -V .OBJDIR)"
199	BOOTFILES="$(realpath ${BOOTFILES})"
200
201	case "${TARGET}:${TARGET_ARCH}" in
202		amd64:amd64 | i386:i386)
203			ESP=yes
204			BOOTPARTS="-b ${BOOTFILES}/i386/pmbr/pmbr \
205				   -p freebsd-boot/bootfs:=${BOOTFILES}/i386/gptboot/gptboot"
206			ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
207			MAKEFSARGS="-B little"
208			;;
209		arm64:aarch64 | riscv:riscv64*)
210			ESP=yes
211			BOOTPARTS=
212			ROOTFSPART="-p freebsd-ufs/rootfs:=${VMBASE}"
213			MAKEFSARGS="-B little"
214			;;
215		powerpc:powerpc*)
216			ESP=no
217			BOOTPARTS="-p prepboot:=${BOOTFILES}/powerpc/boot1.chrp/boot1.elf -a 1"
218			ROOTFSPART="-p freebsd:=${VMBASE}"
219			if [ ${TARGET_ARCH} = powerpc64le ]; then
220				MAKEFSARGS="-B little"
221			else
222				MAKEFSARGS="-B big"
223			fi
224			;;
225		*)
226			echo "vmimage.subr: unsupported target '${TARGET}:${TARGET_ARCH}'" >&2
227			exit 1
228			;;
229	esac
230
231	if [ ${ESP} = "yes" ]; then
232		# Create an ESP
233		espfilename=$(mktemp /tmp/efiboot.XXXXXX)
234		make_esp_file ${espfilename} ${fat32min} ${BOOTFILES}/efi/loader_lua/loader_lua.efi
235		BOOTPARTS="${BOOTPARTS} -p efi/efiesp:=${espfilename}"
236
237		# Add this to fstab
238		mkdir -p ${DESTDIR}/boot/efi
239		echo "/dev/${ROOTLABEL}/efiesp	/boot/efi       msdosfs     rw      2       2" \
240			>> ${DESTDIR}/etc/fstab
241	fi
242
243	echo "Building filesystem...  Please wait."
244	makefs ${MAKEFSARGS} -o label=rootfs -o version=2 -o softupdates=1 \
245	    -s ${VMSIZE} ${VMBASE} ${DESTDIR}
246
247	echo "Building final disk image...  Please wait."
248	mkimg -s ${PARTSCHEME} -f ${VMFORMAT} \
249		${BOOTPARTS} \
250		${SWAPOPT} \
251		${ROOTFSPART} \
252		-o ${VMIMAGE}
253
254	if [ ${ESP} = "yes" ]; then
255		rm ${espfilename}
256	fi
257
258	return 0
259}
260
261vm_extra_create_disk() {
262
263	return 0
264}
265
266