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 28name="freebsdci" 29desc="Run FreeBSD CI" 30rcvar=freebsdci_enable 31start_cmd="firstboot_ci_run" 32stop_cmd=":" 33os_arch=$(uname -p) 34parallelism=$(nproc) 35tardev=/dev/vtbd1 36metadir=/meta 37istar=$(file -s ${tardev} | grep "POSIX tar archive" | wc -l) 38 39load_rc_config $name 40: ${freebsdci_enable:="NO"} 41: ${freebsdci_type:="full"} 42: ${freebsdci_test_filters:=""} 43PATH="${PATH}:/usr/local/sbin:/usr/local/bin" 44 45auto_shutdown() 46{ 47 # NOTE: Currently RISC-V kernels lack the ability to 48 # make qemu exit on shutdown. Reboot instead; 49 # it makes qemu exit too. 50 case "$os_arch" in 51 riscv64) 52 shutdown -r now 53 ;; 54 *) 55 shutdown -p now 56 ;; 57 esac 58} 59 60set_environment() 61{ 62 if [ "${istar}" -eq 1 ]; then 63 rm -fr ${metadir} 64 mkdir -p ${metadir} 65 tar xvf ${tardev} -C ${metadir} 66 ci_env_file=$(set -- "${metadir}"/ci-*.env; \ 67 [ "$#" -eq 1 ] && printf '%s\n' "$1") 68 if [ -z "${ci_env_file}" ]; then 69 echo "ci: No CI environment file found in ${metadir}" 70 else 71 env >> "${ci_env_file}" 72 fi 73 else 74 echo "ERROR: no device with POSIX tar archive format found." 75 # Don't shutdown because this is not run in unattended mode 76 exit 1 77 fi 78 79} 80 81smoke_tests() 82{ 83 echo 84 echo "--------------------------------------------------------------" 85 echo "BOOT sequence COMPLETED" 86 echo "INITIATING system SHUTDOWN" 87 echo "--------------------------------------------------------------" 88} 89 90full_tests() 91{ 92 echo 93 echo "--------------------------------------------------------------" 94 echo "BOOT sequence COMPLETED" 95 echo "TEST sequence STARTED" 96 cd /usr/tests 97 set +e 98 kyua \ 99 -v parallelism=${parallelism} \ 100 test ${freebsdci_test_filters} 101 rc=$? 102 set -e 103 if [ ${rc} -ne 0 ] && [ ${rc} -ne 1 ]; then 104 exit ${rc} 105 fi 106 kyua report --verbose --results-filter passed,skipped,xfail,broken,failed --output test-report.txt 107 kyua report-junit --output=test-report.xml 108 mv test-report.* /${metadir} 109 echo "TEST sequence COMPLETED" 110 echo "INITIATING system SHUTDOWN" 111 echo "--------------------------------------------------------------" 112} 113 114firstboot_ci_run() 115{ 116 set_environment 117 if [ "$freebsdci_type" = "smoke" ]; then 118 smoke_tests 119 elif [ "$freebsdci_type" = "full" ]; then 120 full_tests 121 fi 122 tar cvf ${tardev} -C ${metadir} . 123 auto_shutdown 124} 125 126run_rc_command "$1" 127