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