#!/bin/bash # cleanup.sh — post-build janitor for BTC.sh # # Removes the volatile cleanroom, unmounts the ramfs, and re-stages a fresh # BTC.sh copy under /opt/BTC/BTC-/ ready to be invoked. # # Usage: # ./cleanup.sh # uses BTC_VERSION from BTC.sh auto-detect # ./cleanup.sh # e.g. ./cleanup.sh haswell # ./cleanup.sh # e.g. ./cleanup.sh haswell 0.4.2 # # Defaults: # target_id = haswell (matches the original cleanup.sh behavior) # version = read from BTC.sh (falls back to 0.4.2) # # NOTE: this script must be run on the target machine AFTER the BTC.sh build # host has produced the golden image. It does NOT rebuild anything — it # just clears the working state and re-stages the next BTC.sh copy. set -euo pipefail BTC_ARCHIVE="/opt/BTC" SOURCES_ACTIVE="/usr/src" # --- Resolve target_id argument ----------------------------------------- # The original cleanup.sh hardcoded DCOSNET-HASWELL-AVX2-LTO. We default # to the same target so behavior is unchanged for users who run the script # with no arguments, but expose the full SYS_LABEL derivation so any of the # 22 registered targets can be cleaned up by passing its target_id. TARGET_ID="${1:-haswell}" # --- Resolve BTC_VERSION ------------------------------------------------- # Prefer an explicit second arg, otherwise parse it out of the BTC.sh # sitting next to this script so the cleanup tracks the deployed version # automatically. Falls back to 0.4.2 if neither is available. BTC_VERSION="${2:-}" if [[ -z "${BTC_VERSION}" ]]; then SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BTC_SH="${SCRIPT_DIR}/BTC.sh" if [[ -f "${BTC_SH}" ]]; then BTC_VERSION="$(grep -m1 -E '^readonly BTC_VERSION=' "${BTC_SH}" \ | sed -E 's/^readonly BTC_VERSION="([^"]+)".*$/\1/' || true)" fi fi BTC_VERSION="${BTC_VERSION:-0.4.2}" # --- Derive SYS_LABEL the same way BTC.sh does --------------------------- # For native x86_64 builds BTC.sh produces SYS_LABEL=DCOSNET---LTO. # For cross builds it produces SYS_LABEL=DCOSNET----CROSS. # The cleanup needs to match the build's actual label, so we use the same # uppercase + family/ISA tags. ISA is read from BTC_TARGETS[] if BTC.sh # is reachable; otherwise we conservatively probe by family. FAMILY="" case "${TARGET_ID}" in # AVX512 Intel HEDT/Server (must come before the AVX2 catch-all) skylake-x|skylake-server) FAMILY="intel"; ISA_TAG="AVX512" ;; # AVX2 Intel HEDT/Server haswell|haswell-ep|broadwell|broadwell-ep|skylake) FAMILY="intel"; ISA_TAG="AVX2" ;; # AVX512 AMD znver4) FAMILY="amd"; ISA_TAG="AVX512" ;; # AVX2 AMD znver1|znver2|znver3) FAMILY="amd"; ISA_TAG="AVX2" ;; apu-zn1|apu-zn2|apu-zn3|apu-zn4) FAMILY="amd-apu"; ISA_TAG="AVX2" ;; # SSE4_2 Intel Atom atom-silvermont|atom-goldmont|atom-tremont|atom-sierraforest) FAMILY="atom"; ISA_TAG="SSE4_2" ;; # Embedded mipselr2) FAMILY="mips"; ISA_TAG="MIPS32" ;; armv7) FAMILY="arm"; ISA_TAG="NEON" ;; tilegx) FAMILY="tile"; ISA_TAG="TILE" ;; *) echo ">> [ERROR] Unknown target_id: ${TARGET_ID}" echo ">> Run 'BTC.sh --list' to see registered targets." exit 1 ;; esac # Mirror BTC.sh's native-vs-cross SYS_LABEL rule: # native -> DCOSNET---LTO # cross -> DCOSNET----CROSS # x86_64 targets are typically run native; everything else is cross. if [[ "${FAMILY}" == "intel" || "${FAMILY}" == "amd" || "${FAMILY}" == "amd-apu" || "${FAMILY}" == "atom" ]]; then SYS_LABEL="DCOSNET-${TARGET_ID^^}-${ISA_TAG}-LTO" else SYS_LABEL="DCOSNET-${FAMILY^^}-${TARGET_ID^^}-${ISA_TAG}-CROSS" fi echo ">> [CLEANUP] target_id=${TARGET_ID} version=${BTC_VERSION} sys_label=${SYS_LABEL}" # --- Tear down the previous build state ---------------------------------- cd "${BTC_ARCHIVE}" rm -rf "BTC-${BTC_VERSION}" # Lazy-unmount the ramfs cleanroom (in case a previous run crashed mid-build). umount -l "${SOURCES_ACTIVE}" 2>/dev/null || true # Remove the target-specific cleanroom directory and its logs. rm -rf "${SOURCES_ACTIVE}/${SYS_LABEL}-cleanroom" rm -rf "${BTC_ARCHIVE}/logs/${SYS_LABEL}" # --- Re-stage the next BTC.sh copy --------------------------------------- # Upload the fixed BTC.sh from download/, then: mkdir -p "BTC-${BTC_VERSION}" if [[ -f "${BTC_ARCHIVE}/BTC.sh" ]]; then cp "${BTC_ARCHIVE}/BTC.sh" "BTC-${BTC_VERSION}/" elif [[ -f "./BTC.sh" ]]; then cp "./BTC.sh" "BTC-${BTC_VERSION}/" else echo ">> [WARN] No BTC.sh found to stage under BTC-${BTC_VERSION}/." echo ">> Drop BTC.sh into ${BTC_ARCHIVE}/ or ${PWD}/ and re-run." fi cd "BTC-${BTC_VERSION}" && chmod +x BTC.sh echo ">> [READY] btc staged at ${BTC_ARCHIVE}/BTC-${BTC_VERSION}/BTC.sh" echo ">> Target label cleaned: ${SYS_LABEL}" echo ">> Invoke with: ./BTC.sh ${TARGET_ID}"