xref: /freebsd/tools/boot/ci-qemu-test.sh (revision 6180d0382df2a7537e0ef7e33b18058f30329e8e)
1cc2e55c7SEd Maste#!/bin/sh
2cc2e55c7SEd Maste
308d0b468SEd Maste# Install pkgbase packages for loader, kernel, and enough of userland to boot
408d0b468SEd Maste# in QEMU and echo "Hello world." from init, as a very quick smoke test for CI.
508d0b468SEd Maste# Uses QEMU's virtual FAT filesystem to avoid the need to create a disk image.
608d0b468SEd Maste# While designed for CI automated testing, this script can also be run by hand
708d0b468SEd Maste# as a quick smoke-test as long as pkgbase packages have been built.  The
808d0b468SEd Maste# rootgen.sh and related scripts generate much more extensive tests for many
908d0b468SEd Maste# combinations of boot env (ufs, zfs, geli, etc).
10cc2e55c7SEd Maste#
11cc2e55c7SEd Maste
12cc2e55c7SEd Masteset -e
13cc2e55c7SEd Maste
142614c483SIan Leporedie()
152614c483SIan Lepore{
162614c483SIan Lepore	echo "$*" 1>&2
172614c483SIan Lepore	exit 1
182614c483SIan Lepore}
19cc2e55c7SEd Maste
202614c483SIan Leporetempdir_cleanup()
212614c483SIan Lepore{
222614c483SIan Lepore	trap - EXIT SIGINT SIGHUP SIGTERM SIGQUIT
237b0593fdSEd Maste	rm -rf ${WORKDIR}
242614c483SIan Lepore}
252614c483SIan Lepore
262614c483SIan Leporetempdir_setup()
272614c483SIan Lepore{
282614c483SIan Lepore	# Create minimal directory structure and populate it.
292614c483SIan Lepore
302614c483SIan Lepore	for dir in dev bin efi/boot etc lib libexec sbin usr/lib usr/libexec; do
312614c483SIan Lepore		mkdir -p ${ROOTDIR}/${dir}
32cc2e55c7SEd Maste	done
33cc2e55c7SEd Maste
34cc2e55c7SEd Maste	# Install kernel, loader and minimal userland.
3508d0b468SEd Maste	cat<<EOF >${ROOTDIR}/pkg.conf
3608d0b468SEd MasteREPOS_DIR=[]
3708d0b468SEd Masterepositories={local {url = file://$(dirname $OBJTOP)/repo/\${ABI}/latest}}
3808d0b468SEd MasteEOF
3908d0b468SEd Maste	ASSUME_ALWAYS_YES=true INSTALL_AS_USER=true pkg \
4008d0b468SEd Maste	    -o ABI_FILE=$OBJTOP/bin/sh/sh \
4108d0b468SEd Maste	    -C ${ROOTDIR}/pkg.conf -r ${ROOTDIR} install \
4208d0b468SEd Maste	    FreeBSD-kernel-generic FreeBSD-bootloader \
4308d0b468SEd Maste	    FreeBSD-clibs FreeBSD-runtime
44cc2e55c7SEd Maste
45cc2e55c7SEd Maste	# Put loader in standard EFI location.
46bba96bb1SEd Maste	mv ${ROOTDIR}/boot/loader.efi ${ROOTDIR}/efi/boot/$EFIBOOT
47cc2e55c7SEd Maste
48cc2e55c7SEd Maste	# Configuration files.
492614c483SIan Lepore	cat > ${ROOTDIR}/boot/loader.conf <<EOF
50bba96bb1SEd Mastevfs.root.mountfrom="msdosfs:/dev/$ROOTDEV"
51cc2e55c7SEd Masteautoboot_delay=-1
52cc2e55c7SEd Masteboot_verbose=YES
53cc2e55c7SEd MasteEOF
542614c483SIan Lepore	cat > ${ROOTDIR}/etc/rc <<EOF
55cc2e55c7SEd Maste#!/bin/sh
56cc2e55c7SEd Maste
57cc2e55c7SEd Masteecho "Hello world."
58f6307004SEd Maste/sbin/sysctl vm.stats.vm.v_wire_count
59cc2e55c7SEd Maste/sbin/shutdown -p now
60cc2e55c7SEd MasteEOF
61cc2e55c7SEd Maste
62297ce4ceSEd Maste	# Entropy needed to boot, see r346250 and followup commits/discussion.
63297ce4ceSEd Maste	dd if=/dev/random of=${ROOTDIR}/boot/entropy bs=4k count=1
64297ce4ceSEd Maste
65cc2e55c7SEd Maste	# Remove unnecessary files to keep FAT filesystem size down.
662614c483SIan Lepore	rm -rf ${ROOTDIR}/METALOG ${ROOTDIR}/usr/lib
672614c483SIan Lepore}
682614c483SIan Lepore
692614c483SIan Lepore# Locate the top of the source tree, to run make install from.
702614c483SIan Lepore: ${SRCTOP:=$(make -V SRCTOP)}
712614c483SIan Leporeif [ -z "${SRCTOP}" ]; then
722614c483SIan Lepore	die "Cannot locate top of source tree"
732614c483SIan Leporefi
7408d0b468SEd Maste: ${OBJTOP:=$(make -V OBJTOP)}
7508d0b468SEd Masteif [ -z "${OBJTOP}" ]; then
7608d0b468SEd Maste	die "Cannot locate top of object tree"
7708d0b468SEd Mastefi
782614c483SIan Lepore
79bba96bb1SEd Maste: ${TARGET:=$(uname -m)}
80bba96bb1SEd Mastecase $TARGET in
81bba96bb1SEd Masteamd64)
82c1ffbf2dSRebecca Cran	# Locate the uefi firmware file used by qemu.
837d9794b3SEd Maste	: ${OVMF:=/usr/local/share/qemu/edk2-x86_64-code.fd}
842614c483SIan Lepore	if [ ! -r "${OVMF}" ]; then
85c1ffbf2dSRebecca Cran		die "Cannot read UEFI firmware file ${OVMF}"
862614c483SIan Lepore	fi
87a2de35e1SEd Maste	QEMU="qemu-system-x86_64 -drive if=pflash,format=raw,readonly=on,file=${OVMF}"
88bba96bb1SEd Maste	EFIBOOT=BOOTx64.EFI
89bba96bb1SEd Maste	ROOTDEV=ada0s1
90bba96bb1SEd Maste	;;
91bba96bb1SEd Mastearm64)
92bba96bb1SEd Maste	QEMU="qemu-system-aarch64 -cpu cortex-a57 -M virt -bios edk2-aarch64-code.fd"
93bba96bb1SEd Maste	EFIBOOT=BOOTAA64.EFI
94bba96bb1SEd Maste	ROOTDEV=vtbd0s1
95bba96bb1SEd Maste	;;
96bba96bb1SEd Maste*)
97bba96bb1SEd Maste	die "Unknown TARGET:TARGET_ARCH $TARGET:$TARGET_ARCH"
98bba96bb1SEd Masteesac
992614c483SIan Lepore
1002614c483SIan Lepore# Create a temp dir to hold the boot image.
1017b0593fdSEd MasteWORKDIR=$(mktemp -d -t ci-qemu-test-fat-root)
1027b0593fdSEd MasteROOTDIR=${WORKDIR}/stage-root
1032614c483SIan Leporetrap tempdir_cleanup EXIT SIGINT SIGHUP SIGTERM SIGQUIT
1042614c483SIan Lepore
1052614c483SIan Lepore# Populate the boot image in a temp dir.
1062614c483SIan Lepore( cd ${SRCTOP} && tempdir_setup )
107cc2e55c7SEd Maste
1087b0593fdSEd Maste# Using QEMU's virtual FAT support is much faster than creating a disk image,
1097b0593fdSEd Maste# but only supports about 500MB.  Fall back to creating a disk image if the
1107b0593fdSEd Maste# staged root is too large.
1117b0593fdSEd Mastehda="fat:${ROOTDIR}"
1127b0593fdSEd Masterootsize=$(du -skA ${ROOTDIR} | sed 's/[[:space:]].*$//')
1137b0593fdSEd Masteif [ $rootsize -gt 512000 ]; then
1147b0593fdSEd Maste	echo "Root size ${rootsize}K too large for QEMU virtual FAT" >&2
1157b0593fdSEd Maste	makefs -t msdos -s 1g $WORKDIR/image.fat $ROOTDIR
1167b0593fdSEd Maste	mkimg -s mbr -p efi:=$WORKDIR/image.fat -o $WORKDIR/image.mbr
1177b0593fdSEd Maste	hda="$WORKDIR/image.mbr"
1187b0593fdSEd Mastefi
1197b0593fdSEd Maste
120cc2e55c7SEd Maste# And, boot in QEMU.
12179b02e70SEd Maste: ${BOOTLOG:=${TMPDIR:-/tmp}/ci-qemu-test-boot.log}
122cc2e55c7SEd Mastetimeout 300 \
123bba96bb1SEd Maste    $QEMU -m 256M -nodefaults \
124cc2e55c7SEd Maste        -serial stdio -vga none -nographic -monitor none \
1257b0593fdSEd Maste        -snapshot -hda $hda 2>&1 | tee ${BOOTLOG}
1262614c483SIan Lepore
1272614c483SIan Lepore# Check whether we succesfully booted...
128*6180d038SEd Masteif grep -q 'Hello world.' ${BOOTLOG} && egrep -q '^Uptime: ' ${BOOTLOG}; then
129*6180d038SEd Maste	echo "Boot successful"
1302614c483SIan Leporeelse
1312614c483SIan Lepore	die "Did not boot successfully, see ${BOOTLOG}"
1322614c483SIan Leporefi
133