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