17ec681f3Smrg#!/bin/bash
27ec681f3Smrg
37ec681f3Smrg# Boot script for devices attached to a PoE switch, using NFS for the root
47ec681f3Smrg# filesystem.
57ec681f3Smrg
67ec681f3Smrg# We're run from the root of the repo, make a helper var for our paths
77ec681f3SmrgBM=$CI_PROJECT_DIR/install/bare-metal
87ec681f3SmrgCI_COMMON=$CI_PROJECT_DIR/install/common
97ec681f3Smrg
107ec681f3Smrg# Runner config checks
117ec681f3Smrgif [ -z "$BM_SERIAL" ]; then
127ec681f3Smrg  echo "Must set BM_SERIAL in your gitlab-runner config.toml [[runners]] environment"
137ec681f3Smrg  echo "This is the serial port to listen the device."
147ec681f3Smrg  exit 1
157ec681f3Smrgfi
167ec681f3Smrg
177ec681f3Smrgif [ -z "$BM_POE_ADDRESS" ]; then
187ec681f3Smrg  echo "Must set BM_POE_ADDRESS in your gitlab-runner config.toml [[runners]] environment"
197ec681f3Smrg  echo "This is the PoE switch address to connect for powering up/down devices."
207ec681f3Smrg  exit 1
217ec681f3Smrgfi
227ec681f3Smrg
237ec681f3Smrgif [ -z "$BM_POE_USERNAME" ]; then
247ec681f3Smrg  echo "Must set BM_POE_USERNAME in your gitlab-runner config.toml [[runners]] environment"
257ec681f3Smrg  echo "This is the PoE switch username."
267ec681f3Smrg  exit 1
277ec681f3Smrgfi
287ec681f3Smrg
297ec681f3Smrgif [ -z "$BM_POE_PASSWORD" ]; then
307ec681f3Smrg  echo "Must set BM_POE_PASSWORD in your gitlab-runner config.toml [[runners]] environment"
317ec681f3Smrg  echo "This is the PoE switch password."
327ec681f3Smrg  exit 1
337ec681f3Smrgfi
347ec681f3Smrg
357ec681f3Smrgif [ -z "$BM_POE_INTERFACE" ]; then
367ec681f3Smrg  echo "Must set BM_POE_INTERFACE in your gitlab-runner config.toml [[runners]] environment"
377ec681f3Smrg  echo "This is the PoE switch interface where the device is connected."
387ec681f3Smrg  exit 1
397ec681f3Smrgfi
407ec681f3Smrg
417ec681f3Smrgif [ -z "$BM_POWERUP" ]; then
427ec681f3Smrg  echo "Must set BM_POWERUP in your gitlab-runner config.toml [[runners]] environment"
437ec681f3Smrg  echo "This is a shell script that should power up the device and begin its boot sequence."
447ec681f3Smrg  exit 1
457ec681f3Smrgfi
467ec681f3Smrg
477ec681f3Smrgif [ -z "$BM_POWERDOWN" ]; then
487ec681f3Smrg  echo "Must set BM_POWERDOWN in your gitlab-runner config.toml [[runners]] environment"
497ec681f3Smrg  echo "This is a shell script that should power off the device."
507ec681f3Smrg  exit 1
517ec681f3Smrgfi
527ec681f3Smrg
537ec681f3Smrgif [ ! -d /nfs ]; then
547ec681f3Smrg  echo "NFS rootfs directory needs to be mounted at /nfs by the gitlab runner"
557ec681f3Smrg  exit 1
567ec681f3Smrgfi
577ec681f3Smrg
587ec681f3Smrgif [ ! -d /tftp ]; then
597ec681f3Smrg  echo "TFTP directory for this board needs to be mounted at /tftp by the gitlab runner"
607ec681f3Smrg  exit 1
617ec681f3Smrgfi
627ec681f3Smrg
637ec681f3Smrg# job config checks
647ec681f3Smrgif [ -z "$BM_ROOTFS" ]; then
657ec681f3Smrg  echo "Must set BM_ROOTFS to your board's rootfs directory in the job's variables"
667ec681f3Smrg  exit 1
677ec681f3Smrgfi
687ec681f3Smrg
697ec681f3Smrgif [ -z "$BM_BOOTFS" ]; then
707ec681f3Smrg  echo "Must set /boot files for the TFTP boot in the job's variables"
717ec681f3Smrg  exit 1
727ec681f3Smrgfi
737ec681f3Smrg
747ec681f3Smrgif [ -z "$BM_CMDLINE" ]; then
757ec681f3Smrg  echo "Must set BM_CMDLINE to your board's kernel command line arguments"
767ec681f3Smrg  exit 1
777ec681f3Smrgfi
787ec681f3Smrg
797ec681f3Smrgif [ -z "$BM_BOOTCONFIG" ]; then
807ec681f3Smrg  echo "Must set BM_BOOTCONFIG to your board's required boot configuration arguments"
817ec681f3Smrg  exit 1
827ec681f3Smrgfi
837ec681f3Smrg
847ec681f3Smrgset -ex
857ec681f3Smrg
867ec681f3Smrg# Clear out any previous run's artifacts.
877ec681f3Smrgrm -rf results/
887ec681f3Smrgmkdir -p results
897ec681f3Smrg
907ec681f3Smrg# Create the rootfs in the NFS directory.  rm to make sure it's in a pristine
917ec681f3Smrg# state, since it's volume-mounted on the host.
927ec681f3Smrgrsync -a --delete $BM_ROOTFS/ /nfs/
937ec681f3Smrg
947ec681f3Smrg# If BM_BOOTFS is an URL, download it
957ec681f3Smrgif echo $BM_BOOTFS | grep -q http; then
967ec681f3Smrg  apt install -y wget
977ec681f3Smrg  wget ${FDO_HTTP_CACHE_URI:-}$BM_BOOTFS -O /tmp/bootfs.tar
987ec681f3Smrg  BM_BOOTFS=/tmp/bootfs.tar
997ec681f3Smrgfi
1007ec681f3Smrg
1017ec681f3Smrg# If BM_BOOTFS is a file, assume it is a tarball and uncompress it
1027ec681f3Smrgif [ -f $BM_BOOTFS ]; then
1037ec681f3Smrg  mkdir -p /tmp/bootfs
1047ec681f3Smrg  tar xf $BM_BOOTFS -C /tmp/bootfs
1057ec681f3Smrg  BM_BOOTFS=/tmp/bootfs
1067ec681f3Smrgfi
1077ec681f3Smrg
1087ec681f3Smrg# Install kernel modules (it could be either in /lib/modules or
1097ec681f3Smrg# /usr/lib/modules, but we want to install in the latter)
1107ec681f3Smrg[ -d $BM_BOOTFS/usr/lib/modules ] && rsync -a --delete $BM_BOOTFS/usr/lib/modules/ /nfs/usr/lib/modules/
1117ec681f3Smrg[ -d $BM_BOOTFS/lib/modules ] && rsync -a --delete $BM_BOOTFS/lib/modules/ /nfs/usr/lib/modules/
1127ec681f3Smrg
1137ec681f3Smrg# Install kernel image + bootloader files
1147ec681f3Smrgrsync -a --delete $BM_BOOTFS/boot/ /tftp/
1157ec681f3Smrg
1167ec681f3Smrg# Create the rootfs in the NFS directory
1177ec681f3Smrgmkdir -p /nfs/results
1187ec681f3Smrg. $BM/rootfs-setup.sh /nfs
1197ec681f3Smrg
1207ec681f3Smrgecho "$BM_CMDLINE" > /tftp/cmdline.txt
1217ec681f3Smrg
1227ec681f3Smrg# Add some required options in config.txt
1237ec681f3Smrgprintf "$BM_BOOTCONFIG" >> /tftp/config.txt
1247ec681f3Smrg
1257ec681f3Smrgset +e
1267ec681f3SmrgATTEMPTS=2
1277ec681f3Smrgwhile [ $((ATTEMPTS--)) -gt 0 ]; do
1287ec681f3Smrg  python3 $BM/poe_run.py \
1297ec681f3Smrg          --dev="$BM_SERIAL" \
1307ec681f3Smrg          --powerup="$BM_POWERUP" \
1317ec681f3Smrg          --powerdown="$BM_POWERDOWN" \
1327ec681f3Smrg          --timeout="${BM_POE_TIMEOUT:-60}"
1337ec681f3Smrg  ret=$?
1347ec681f3Smrg
1357ec681f3Smrg  if [ $ret -eq 2 ]; then
1367ec681f3Smrg    echo "Did not detect boot sequence, retrying..."
1377ec681f3Smrg  else
1387ec681f3Smrg    ATTEMPTS=0
1397ec681f3Smrg  fi
1407ec681f3Smrgdone
1417ec681f3Smrgset -e
1427ec681f3Smrg
1437ec681f3Smrg# Bring artifacts back from the NFS dir to the build dir where gitlab-runner
1447ec681f3Smrg# will look for them.
1457ec681f3Smrgcp -Rp /nfs/results/. results/
1467ec681f3Smrg
1477ec681f3Smrgexit $ret
148