1#!/bin/bash 2 3# Boot script for devices attached to a PoE switch, using NFS for the root 4# filesystem. 5 6# We're run from the root of the repo, make a helper var for our paths 7BM=$CI_PROJECT_DIR/install/bare-metal 8CI_COMMON=$CI_PROJECT_DIR/install/common 9 10# Runner config checks 11if [ -z "$BM_SERIAL" ]; then 12 echo "Must set BM_SERIAL in your gitlab-runner config.toml [[runners]] environment" 13 echo "This is the serial port to listen the device." 14 exit 1 15fi 16 17if [ -z "$BM_POE_ADDRESS" ]; then 18 echo "Must set BM_POE_ADDRESS in your gitlab-runner config.toml [[runners]] environment" 19 echo "This is the PoE switch address to connect for powering up/down devices." 20 exit 1 21fi 22 23if [ -z "$BM_POE_USERNAME" ]; then 24 echo "Must set BM_POE_USERNAME in your gitlab-runner config.toml [[runners]] environment" 25 echo "This is the PoE switch username." 26 exit 1 27fi 28 29if [ -z "$BM_POE_PASSWORD" ]; then 30 echo "Must set BM_POE_PASSWORD in your gitlab-runner config.toml [[runners]] environment" 31 echo "This is the PoE switch password." 32 exit 1 33fi 34 35if [ -z "$BM_POE_INTERFACE" ]; then 36 echo "Must set BM_POE_INTERFACE in your gitlab-runner config.toml [[runners]] environment" 37 echo "This is the PoE switch interface where the device is connected." 38 exit 1 39fi 40 41if [ -z "$BM_POWERUP" ]; then 42 echo "Must set BM_POWERUP in your gitlab-runner config.toml [[runners]] environment" 43 echo "This is a shell script that should power up the device and begin its boot sequence." 44 exit 1 45fi 46 47if [ -z "$BM_POWERDOWN" ]; then 48 echo "Must set BM_POWERDOWN in your gitlab-runner config.toml [[runners]] environment" 49 echo "This is a shell script that should power off the device." 50 exit 1 51fi 52 53if [ ! -d /nfs ]; then 54 echo "NFS rootfs directory needs to be mounted at /nfs by the gitlab runner" 55 exit 1 56fi 57 58if [ ! -d /tftp ]; then 59 echo "TFTP directory for this board needs to be mounted at /tftp by the gitlab runner" 60 exit 1 61fi 62 63# job config checks 64if [ -z "$BM_ROOTFS" ]; then 65 echo "Must set BM_ROOTFS to your board's rootfs directory in the job's variables" 66 exit 1 67fi 68 69if [ -z "$BM_BOOTFS" ]; then 70 echo "Must set /boot files for the TFTP boot in the job's variables" 71 exit 1 72fi 73 74if [ -z "$BM_CMDLINE" ]; then 75 echo "Must set BM_CMDLINE to your board's kernel command line arguments" 76 exit 1 77fi 78 79if [ -z "$BM_BOOTCONFIG" ]; then 80 echo "Must set BM_BOOTCONFIG to your board's required boot configuration arguments" 81 exit 1 82fi 83 84set -ex 85 86# Clear out any previous run's artifacts. 87rm -rf results/ 88mkdir -p results 89 90# Create the rootfs in the NFS directory. rm to make sure it's in a pristine 91# state, since it's volume-mounted on the host. 92rsync -a --delete $BM_ROOTFS/ /nfs/ 93 94# If BM_BOOTFS is an URL, download it 95if echo $BM_BOOTFS | grep -q http; then 96 apt install -y wget 97 wget ${FDO_HTTP_CACHE_URI:-}$BM_BOOTFS -O /tmp/bootfs.tar 98 BM_BOOTFS=/tmp/bootfs.tar 99fi 100 101# If BM_BOOTFS is a file, assume it is a tarball and uncompress it 102if [ -f $BM_BOOTFS ]; then 103 mkdir -p /tmp/bootfs 104 tar xf $BM_BOOTFS -C /tmp/bootfs 105 BM_BOOTFS=/tmp/bootfs 106fi 107 108# Install kernel modules (it could be either in /lib/modules or 109# /usr/lib/modules, but we want to install in the latter) 110[ -d $BM_BOOTFS/usr/lib/modules ] && rsync -a --delete $BM_BOOTFS/usr/lib/modules/ /nfs/usr/lib/modules/ 111[ -d $BM_BOOTFS/lib/modules ] && rsync -a --delete $BM_BOOTFS/lib/modules/ /nfs/usr/lib/modules/ 112 113# Install kernel image + bootloader files 114rsync -a --delete $BM_BOOTFS/boot/ /tftp/ 115 116# Create the rootfs in the NFS directory 117mkdir -p /nfs/results 118. $BM/rootfs-setup.sh /nfs 119 120echo "$BM_CMDLINE" > /tftp/cmdline.txt 121 122# Add some required options in config.txt 123printf "$BM_BOOTCONFIG" >> /tftp/config.txt 124 125set +e 126ATTEMPTS=2 127while [ $((ATTEMPTS--)) -gt 0 ]; do 128 python3 $BM/poe_run.py \ 129 --dev="$BM_SERIAL" \ 130 --powerup="$BM_POWERUP" \ 131 --powerdown="$BM_POWERDOWN" \ 132 --timeout="${BM_POE_TIMEOUT:-60}" 133 ret=$? 134 135 if [ $ret -eq 2 ]; then 136 echo "Did not detect boot sequence, retrying..." 137 else 138 ATTEMPTS=0 139 fi 140done 141set -e 142 143# Bring artifacts back from the NFS dir to the build dir where gitlab-runner 144# will look for them. 145cp -Rp /nfs/results/. results/ 146 147exit $ret 148