unos-repository

APK repository for unos
git clone git://git.finwo.net/misc/unos-repository
Log | Files | Refs | README

unos-firstboot (2014B)


      1 #!/bin/sh
      2 #
      3 # unos-firstboot -- one-time platform provisioning
      4 #
      5 # A UNOS image is identical on every machine. Hardware support is added
      6 # afterwards, by package. This runs once on first boot, works out what the
      7 # machine actually is, and pulls in whatever it needs.
      8 #
      9 # Invoked from runit stage 1. Safe to re-run: it is a no-op once the marker
     10 # exists, and `--force` re-runs the detection.
     11 
     12 set -e
     13 
     14 STATEDIR=/var/lib/unos
     15 MARKER="${STATEDIR}/.provisioned"
     16 FORCE=0
     17 
     18 log() { echo "unos-firstboot: $*"; }
     19 
     20 while [ $# -gt 0 ]; do
     21   case "$1" in
     22     --force) FORCE=1 ;;
     23     *) log "unknown argument: $1"; exit 1 ;;
     24   esac
     25   shift
     26 done
     27 
     28 if [ -e "${MARKER}" ] && [ "${FORCE}" -eq 0 ]; then
     29   exit 0
     30 fi
     31 
     32 # Broadcom XGS switch ASICs sit on PCIe under vendor 0x14e4 with device IDs in
     33 # the 0xb??? range (BCM56xxx / BCM78xxx). Broadcom NICs share the vendor ID but
     34 # use different device ranges, so the device prefix is what distinguishes them.
     35 #
     36 # Read straight out of sysfs rather than shelling out to lspci, which busybox
     37 # does not provide.
     38 #
     39 # NOTE: verify the device ID range against real hardware before trusting this
     40 # on a platform we have not seen.
     41 detect_bcm_switch() {
     42   for dev in /sys/bus/pci/devices/*; do
     43     [ -r "${dev}/vendor" ] || continue
     44     [ -r "${dev}/device" ] || continue
     45 
     46     read -r vendor < "${dev}/vendor"
     47     [ "${vendor}" = "0x14e4" ] || continue
     48 
     49     read -r device < "${dev}/device"
     50     case "${device}" in
     51       0xb*)
     52         log "found Broadcom switch ASIC at $(basename "${dev}") (${vendor}:${device})"
     53         return 0
     54         ;;
     55     esac
     56   done
     57   return 1
     58 }
     59 
     60 install_pkg() {
     61   log "installing $1"
     62   if ! apk add "$1"; then
     63     log "failed to install $1"
     64     return 1
     65   fi
     66 }
     67 
     68 mkdir -p "${STATEDIR}"
     69 
     70 if detect_bcm_switch; then
     71   log "platform: broadcom xgs switch"
     72   install_pkg openbcm || exit 1
     73 else
     74   log "platform: generic, no switching ASIC detected"
     75   log "linkd will use the built-in kernel dataplane"
     76 fi
     77 
     78 date -u +%Y-%m-%dT%H:%M:%SZ > "${MARKER}"
     79 log "provisioning complete"