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