1#!/bin/sh 2# 3# SPDX-License-Identifier: BSD-2-Clause 4# 5# Copyright (c) 2024 The FreeBSD Foundation 6# 7# This software was developed by Cybermancer Infosec <bofh@FreeBSD.org> 8# under sponsorship from the FreeBSD Foundation. 9# 10# PROVIDE: freebsdci 11# REQUIRE: LOGIN FILESYSTEMS 12# KEYWORD: firstboot 13 14# This script is used to run the firstboot CI tests on the first boot of a 15# FreeBSD image. It is automatically disabled after the first boot. 16# 17# The script will run the firstboot CI tests and then shut down the system. 18# The tests are run in the foreground so that the system can shut down 19# immediately after the tests are finished. 20# 21# Default test types are full and smoke. To run only the smoke tests, set 22# freebsdci_type="smoke" in /etc/rc.conf.local or /etc/rc.conf. 23# To run only the full tests, set freebsdci_type="full" in 24# /etc/rc.conf.local or /etc/rc.conf. 25 26. /etc/rc.subr 27 28: ${freebsdci_enable:="NO"} 29: ${freebsdci_type:="full"} 30 31name="freebsdci" 32desc="Run FreeBSD CI" 33rcvar=freebsdci_enable 34start_cmd="firstboot_ci_run" 35stop_cmd=":" 36os_arch=$(uname -p) 37parallelism=$(nproc) 38tardev=/dev/vtbd1 39metadir=/meta 40istar=$(file -s ${tardev} | grep "POSIX tar archive" | wc -l) 41 42auto_shutdown() 43{ 44 # NOTE: Currently RISC-V kernels lack the ability to 45 # make qemu exit on shutdown. Reboot instead; 46 # it makes qemu exit too. 47 case "$os_arch" in 48 riscv64) 49 shutdown -r now 50 ;; 51 *) 52 shutdown -p now 53 ;; 54 esac 55} 56 57smoke_tests() 58{ 59 echo 60 echo "--------------------------------------------------------------" 61 echo "BOOT sequence COMPLETED" 62 echo "INITIATING system SHUTDOWN" 63 echo "--------------------------------------------------------------" 64} 65 66full_tests() 67{ 68 echo 69 echo "--------------------------------------------------------------" 70 echo "BOOT sequence COMPLETED" 71 echo "TEST sequence STARTED" 72 if [ "${istar}" -eq 1 ]; then 73 rm -fr ${metadir} 74 mkdir -p ${metadir} 75 tar xvf ${tardev} -C ${metadir} 76 cd /usr/tests 77 set +e 78 kyua -v parallelism=${parallelism} test 79 rc=$? 80 set -e 81 if [ ${rc} -ne 0 ] && [ ${rc} -ne 1 ]; then 82 exit ${rc} 83 fi 84 kyua report --verbose --results-filter passed,skipped,xfail,broken,failed --output test-report.txt 85 kyua report-junit --output=test-report.xml 86 mv test-report.* /${metadir} 87 tar cvf ${tardev} -C ${metadir} . 88 else 89 echo "ERROR: no device with POSIX tar archive format found." 90 # Don't shutdown because this is not run in unattended mode 91 exit 1 92 fi 93 echo "TEST sequence COMPLETED" 94 echo "INITIATING system SHUTDOWN" 95 echo "--------------------------------------------------------------" 96} 97 98firstboot_ci_run() 99{ 100 if [ "$freebsdci_type" = "smoke" ]; then 101 smoke_tests 102 elif [ "$freebsdci_type" = "full" ]; then 103 full_tests 104 fi 105 auto_shutdown 106} 107 108load_rc_config $name 109run_rc_command "$1" 110