xref: /freebsd/tools/boot/ci-qemu-test.sh (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1#!/bin/sh
2
3# Install pkgbase packages for loader, kernel, and enough of userland to boot
4# in QEMU and echo "Hello world." from init, as a very quick smoke test for CI.
5# Uses QEMU's virtual FAT filesystem to avoid the need to create a disk image.
6# While designed for CI automated testing, this script can also be run by hand
7# as a quick smoke-test as long as pkgbase packages have been built.  The
8# rootgen.sh and related scripts generate much more extensive tests for many
9# combinations of boot env (ufs, zfs, geli, etc).
10#
11
12set -e
13
14die()
15{
16	echo "$*" 1>&2
17	exit 1
18}
19
20tempdir_cleanup()
21{
22	trap - EXIT SIGINT SIGHUP SIGTERM SIGQUIT
23	rm -rf ${WORKDIR}
24}
25
26tempdir_setup()
27{
28	# Create minimal directory structure and populate it.
29
30	for dir in dev bin efi/boot etc lib libexec sbin usr/lib usr/libexec; do
31		mkdir -p ${ROOTDIR}/${dir}
32	done
33
34	# Install kernel, loader and minimal userland.
35	cat<<EOF >${ROOTDIR}/pkg.conf
36REPOS_DIR=[]
37repositories={local {url = file://$(dirname $OBJTOP)/repo/\${ABI}/latest}}
38EOF
39	ASSUME_ALWAYS_YES=true INSTALL_AS_USER=true pkg \
40	    -o ABI_FILE=$OBJTOP/bin/sh/sh \
41	    -C ${ROOTDIR}/pkg.conf -r ${ROOTDIR} install \
42	    FreeBSD-kernel-generic FreeBSD-bootloader \
43	    FreeBSD-clibs FreeBSD-runtime
44
45	# Put loader in standard EFI location.
46	mv ${ROOTDIR}/boot/loader.efi ${ROOTDIR}/efi/boot/$EFIBOOT
47
48	# Configuration files.
49	cat > ${ROOTDIR}/boot/loader.conf <<EOF
50vfs.root.mountfrom="msdosfs:/dev/$ROOTDEV"
51autoboot_delay=-1
52boot_verbose=YES
53EOF
54	cat > ${ROOTDIR}/etc/rc <<EOF
55#!/bin/sh
56
57echo "Hello world."
58/sbin/sysctl vm.stats.vm.v_wire_count
59/sbin/shutdown -p now
60EOF
61
62	# Entropy needed to boot, see r346250 and followup commits/discussion.
63	dd if=/dev/random of=${ROOTDIR}/boot/entropy bs=4k count=1
64
65	# Remove unnecessary files to keep FAT filesystem size down.
66	rm -rf ${ROOTDIR}/METALOG ${ROOTDIR}/usr/lib
67}
68
69# Locate the top of the source tree, to run make install from.
70: ${SRCTOP:=$(make -V SRCTOP)}
71if [ -z "${SRCTOP}" ]; then
72	die "Cannot locate top of source tree"
73fi
74: ${OBJTOP:=$(make -V OBJTOP)}
75if [ -z "${OBJTOP}" ]; then
76	die "Cannot locate top of object tree"
77fi
78
79: ${TARGET:=$(uname -m)}
80case $TARGET in
81amd64)
82	# Locate the uefi firmware file used by qemu.
83	: ${OVMF:=/usr/local/share/qemu/edk2-x86_64-code.fd}
84	if [ ! -r "${OVMF}" ]; then
85		die "Cannot read UEFI firmware file ${OVMF}"
86	fi
87	QEMU="qemu-system-x86_64 -drive if=pflash,format=raw,readonly=on,file=${OVMF}"
88	EFIBOOT=BOOTx64.EFI
89	ROOTDEV=ada0s1
90	;;
91arm64)
92	QEMU="qemu-system-aarch64 -cpu cortex-a57 -M virt -bios edk2-aarch64-code.fd"
93	EFIBOOT=BOOTAA64.EFI
94	ROOTDEV=vtbd0s1
95	;;
96*)
97	die "Unknown TARGET:TARGET_ARCH $TARGET:$TARGET_ARCH"
98esac
99
100# Create a temp dir to hold the boot image.
101WORKDIR=$(mktemp -d -t ci-qemu-test-fat-root)
102ROOTDIR=${WORKDIR}/stage-root
103trap tempdir_cleanup EXIT SIGINT SIGHUP SIGTERM SIGQUIT
104
105# Populate the boot image in a temp dir.
106( cd ${SRCTOP} && tempdir_setup )
107
108# Using QEMU's virtual FAT support is much faster than creating a disk image,
109# but only supports about 500MB.  Fall back to creating a disk image if the
110# staged root is too large.
111hda="fat:${ROOTDIR}"
112rootsize=$(du -skA ${ROOTDIR} | sed 's/[[:space:]].*$//')
113if [ $rootsize -gt 512000 ]; then
114	echo "Root size ${rootsize}K too large for QEMU virtual FAT" >&2
115	makefs -t msdos -s 1g $WORKDIR/image.fat $ROOTDIR
116	mkimg -s mbr -p efi:=$WORKDIR/image.fat -o $WORKDIR/image.mbr
117	hda="$WORKDIR/image.mbr"
118fi
119
120# And, boot in QEMU.
121: ${BOOTLOG:=${TMPDIR:-/tmp}/ci-qemu-test-boot.log}
122timeout 300 \
123    $QEMU -m 256M -nodefaults \
124        -serial stdio -vga none -nographic -monitor none \
125        -snapshot -hda $hda 2>&1 | tee ${BOOTLOG}
126
127# Check whether we succesfully booted...
128if grep -q 'Hello world.' ${BOOTLOG}; then
129	echo "OK"
130else
131	die "Did not boot successfully, see ${BOOTLOG}"
132fi
133