|
| 1 | +#!/bin/bash -e |
| 2 | + |
| 3 | +# Exit codes |
| 4 | +ASIC_CONFIG_UNCHANGED=0 |
| 5 | +ASIC_CONFIG_CHANGED=1 |
| 6 | +SRC_ASIC_CONFIG_NOT_FOUND=2 |
| 7 | +DST_ASIC_CONFIG_NOT_FOUND=3 |
| 8 | + |
| 9 | +# Logging utilities |
| 10 | +function error() |
| 11 | +{ |
| 12 | + logger -p user.err "$@" |
| 13 | +} |
| 14 | + |
| 15 | +function debug() |
| 16 | +{ |
| 17 | + logger -p user.info "$@" |
| 18 | +} |
| 19 | + |
| 20 | +# Retrieve the source ASIC config checksum from the source image |
| 21 | +# Exits with error SRC_ASIC_CONFIG_NOT_FOUND if the checksum is not found |
| 22 | +function GetSourceASICConfigChecksum() |
| 23 | +{ |
| 24 | + if [[ ! -f "/etc/sonic/asic_config_checksum" ]]; then |
| 25 | + error "ASIC config not found in src image, can't verify changes" |
| 26 | + exit "${SRC_ASIC_CONFIG_NOT_FOUND}" |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +# Retrieve the destination ASIC config checksum from the destination image |
| 31 | +# Exits with error DST_ASIC_CONFIG_NOT_FOUND if the checksum is not found |
| 32 | +function GetDestinationASICConfigChecksum() |
| 33 | +{ |
| 34 | + DST_IMAGE_PATH="/host/image-${DST_SONIC_IMAGE#SONiC-OS-}" |
| 35 | + FS_PATH="${DST_IMAGE_PATH}/fs.squashfs" |
| 36 | + FS_MOUNTPOINT="/tmp/image-${DST_SONIC_IMAGE#SONiC-OS-}-fs" |
| 37 | + |
| 38 | + # Verify that the destination image exists |
| 39 | + if [[ ! -d ${DST_IMAGE_PATH} ]]; then |
| 40 | + error "ASIC config not found in dst image, can't verify changes" |
| 41 | + exit "${DST_ASIC_CONFIG_NOT_FOUND}" |
| 42 | + fi |
| 43 | + |
| 44 | + mkdir "${FS_MOUNTPOINT}" |
| 45 | + mount -t squashfs "${FS_PATH}" "${FS_MOUNTPOINT}" |
| 46 | + |
| 47 | + if [[ ! -f "${FS_MOUNTPOINT}/etc/sonic/asic_config_checksum" ]]; then |
| 48 | + error "ASIC config not found in dst image, can't verify changes" |
| 49 | + umount "${FS_MOUNTPOINT}" |
| 50 | + rm -rf "${FS_MOUNTPOINT}" |
| 51 | + exit "${DST_ASIC_CONFIG_NOT_FOUND}" |
| 52 | + fi |
| 53 | + |
| 54 | + cp "${FS_MOUNTPOINT}/etc/sonic/asic_config_checksum" /tmp/dst_asic_config_checksum |
| 55 | + umount "${FS_MOUNTPOINT}" |
| 56 | + rm -rf "${FS_MOUNTPOINT}" |
| 57 | +} |
| 58 | + |
| 59 | +# Confirm that the src and dst ASIC config checksums match |
| 60 | +# Exits with ASIC_CONFIG_CHANGED if the checksums differ |
| 61 | +function ConfirmASICConfigChecksumsMatch() |
| 62 | +{ |
| 63 | + SRC_CONFIG_CHECKSUM=$(cat /etc/sonic/asic_config_checksum) |
| 64 | + DST_CONFIG_CHECKSUM=$(cat /tmp/dst_asic_config_checksum) |
| 65 | + if [[ "${SRC_CONFIG_CHECKSUM}" != "${DST_CONFIG_CHECKSUM}" ]]; then |
| 66 | + error "ASIC config may have changed, checksum failed" |
| 67 | + exit "${ASIC_CONFIG_CHANGED}" |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +# Main starts here |
| 72 | +debug "Checking that ASIC configuration has not changed" |
| 73 | + |
| 74 | +SRC_SONIC_IMAGE="$(sonic_installer list | grep "Current: " | cut -f2 -d' ')" |
| 75 | +DST_SONIC_IMAGE="$(sonic_installer list | grep "Next: " | cut -f2 -d' ')" |
| 76 | +if [[ "${SRC_SONIC_IMAGE}" == "${DST_SONIC_IMAGE}" ]]; then |
| 77 | + debug "ASIC config unchanged, src and dst SONiC version are the same" |
| 78 | + exit "${ASIC_CONFIG_UNCHANGED}" |
| 79 | +fi |
| 80 | + |
| 81 | +GetSourceASICConfigChecksum |
| 82 | +GetDestinationASICConfigChecksum |
| 83 | +ConfirmASICConfigChecksumsMatch |
| 84 | + |
| 85 | +debug "ASIC config unchanged, checksum passed" |
| 86 | +exit "${ASIC_CONFIG_UNCHANGED}" |
0 commit comments