|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -eu |
| 3 | +set -o pipefail |
| 4 | + |
| 5 | +ARTIFACT_PATH="${1}" |
| 6 | + |
| 7 | +if [[ -z ${ARTIFACT_PATH} ]]; then |
| 8 | + echo "Path to a kernel or initrd in S3 is required as an argument" |
| 9 | + # shellcheck disable=SC2154 # this is just an example command |
| 10 | + echo "Try 'cray bss bootparameters list --hosts ${xname} | awk '/kernel = /'" |
| 11 | + echo "and 'cray bss bootparameters list --hosts ${xname} | awk '/initrd = /'" |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +# Stuff to cleanup on exit |
| 16 | +REBOOT_NEEDED="false" |
| 17 | + |
| 18 | +pit_check() { |
| 19 | + if [[ $HOSTNAME == *pit* ]]; then |
| 20 | + return 0 |
| 21 | + else |
| 22 | + return 1 |
| 23 | + fi |
| 24 | +} |
| 25 | + |
| 26 | +if ! eval pit_check; then |
| 27 | + # get the mount directory as it can vary between vintages: CSM 0.9.X uses /metal/boot, CSM 1.0 uses /metal/recovery |
| 28 | + BOOTRAID=$(awk '/LABEL=BOOTRAID/ {print $2}' /etc/fstab.metal) |
| 29 | +else |
| 30 | + BOOTRAID="" |
| 31 | +fi |
| 32 | + |
| 33 | +cleanup() { |
| 34 | + if ! eval pit_check; then |
| 35 | + if eval mount | grep "${BOOTRAID}" >/dev/null; then |
| 36 | + umount $BOOTRAID |
| 37 | + fi |
| 38 | + fi |
| 39 | + |
| 40 | + if [[ "$REBOOT_NEEDED" == "true" ]]; then |
| 41 | + echo "The new on-disk boot artifacts will not take effect until a restart of the machine." |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +trap 'echo Cancelling $(basename "${0%.*}") ; cleanup ; exit 1' SIGINT |
| 46 | + |
| 47 | +mount_bootraid() { |
| 48 | + if ! eval pit_check; then |
| 49 | + if ! eval mount | grep "${BOOTRAID}" >/dev/null; then |
| 50 | + # Mount the BOOTRAID partition |
| 51 | + mount -L BOOTRAID -T /etc/fstab.metal |
| 52 | + fi |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +expected_initrd_name() { |
| 57 | + # find the name the grub config expects |
| 58 | + expected_name=$(grep initrdefi "${BOOTRAID}"/boot/grub2/grub.cfg | awk '{print $2}' | awk -F'/' '{print $NF}') |
| 59 | + echo "${expected_name}" |
| 60 | +} |
| 61 | + |
| 62 | +get_artifact_from_s3() { |
| 63 | + local artifact="${1}" |
| 64 | + local path="${2}" |
| 65 | + |
| 66 | + mount_bootraid |
| 67 | + |
| 68 | + echo "Getting $(basename ${artifact}) from s3 (${path})..." |
| 69 | + curl -s -o ${artifact} http://rgw-vip.nmn/"${path/s3:\/\/}" |
| 70 | + |
| 71 | + REBOOT_NEEDED="true" |
| 72 | +} |
| 73 | + |
| 74 | +check_boot_artifact() { |
| 75 | + local artifact="$1" |
| 76 | + # check if the filesize is bigger than 217 (that is the size of an XML error message from s3) |
| 77 | + # or check if this is a filetype we expect for a kernel or initrd |
| 78 | + if ! [[ "$(stat --format=%s $f)" -gt 217 ]] || ! [[ "$(file --brief --mime-type $f)" == application/octet-stream ]]; then |
| 79 | + echo -e "\n${f} size is too small or not an expected file type ($(stat --format=%s $f):$(file --brief --mime-type $f))" |
| 80 | + return 1 |
| 81 | + else |
| 82 | + return 0 |
| 83 | + fi |
| 84 | +} |
| 85 | + |
| 86 | +# mount the raid |
| 87 | +mount_bootraid |
| 88 | + |
| 89 | +if [[ "${ARTIFACT_PATH}" == *kernel* ]]; then |
| 90 | + printf "Examining kernel..." |
| 91 | + f="${BOOTRAID}/boot/kernel" |
| 92 | +elif [[ "${ARTIFACT_PATH}" == *initrd* ]]; then |
| 93 | + printf "Examining initrd..." |
| 94 | + f="${BOOTRAID}/boot/initrd.img.xz" |
| 95 | +else |
| 96 | + printf "Unknown artifact for this script." |
| 97 | + exit 1 |
| 98 | +fi |
| 99 | + |
| 100 | +# Check first if the file exists on the BOOTRAID |
| 101 | +# Sometimes it is not even there |
| 102 | +if [[ -f "${f}" ]]; then |
| 103 | + |
| 104 | + # if the file is bad |
| 105 | + if ! eval check_boot_artifact "${f}"; then |
| 106 | + |
| 107 | + # remove the problematic file |
| 108 | + rm -f "${f}" |
| 109 | + |
| 110 | + # then get a known-good artifact from the path discovered in s3 |
| 111 | + get_artifact_from_s3 "${f}" "${ARTIFACT_PATH}" |
| 112 | + |
| 113 | + if [[ $(basename ${f}) == *initrd* ]]; then |
| 114 | + # Modify the filename if grub is expecting something else |
| 115 | + # Sometimes it was named initrd, other times it has been initrd.img.xz |
| 116 | + if [[ $(basename ${f}) != $(expected_initrd_name) ]]; then |
| 117 | + echo mv "${f}" "$(dirname ${f})/$(expected_initrd_name)" |
| 118 | + fi |
| 119 | + |
| 120 | + fi |
| 121 | + |
| 122 | + else |
| 123 | + |
| 124 | + printf "$(basename ${f}) is OK.\n" |
| 125 | + |
| 126 | + fi |
| 127 | + |
| 128 | +else |
| 129 | + |
| 130 | + echo "$(basename ${f}) not found in BOOTRAID." |
| 131 | + |
| 132 | + get_artifact_from_s3 "${f}" "${ARTIFACT_PATH}" |
| 133 | + |
| 134 | +fi |
| 135 | + |
| 136 | +cleanup |
0 commit comments