diff --git a/platform/mellanox/mlnx-fw-upgrade.j2 b/platform/mellanox/mlnx-fw-upgrade.j2 index eb288624982c..e4c567c1b76e 100755 --- a/platform/mellanox/mlnx-fw-upgrade.j2 +++ b/platform/mellanox/mlnx-fw-upgrade.j2 @@ -20,6 +20,8 @@ declare -r SCRIPT_NAME="$(basename "$0")" declare -r SCRIPT_PATH="$(readlink -f "$0")" declare -r SCRIPT_DIR="$(dirname "$SCRIPT_PATH")" +declare -r LOCKFILE="/tmp/mlxfwmanager-lock" + declare -r YES_PARAM="yes" declare -r NO_PARAM="no" @@ -163,6 +165,20 @@ function ExitSuccess() { exit "${EXIT_SUCCESS}" } +function LockStateChange() { + LogInfo "Locking ${LOCKFILE} from ${SCRIPT_NAME}" + + exec {LOCKFD}>${LOCKFILE} + /usr/bin/flock -x ${LOCKFD} + + LogInfo "Locked ${LOCKFILE} (${LOCKFD}) from ${SCRIPT_NAME}" +} + +function UnlockStateChange() { + LogInfo "Unlocking ${LOCKFILE} (${LOCKFD}) from ${SCRIPT_NAME}" + /usr/bin/flock -u ${LOCKFD} +} + function WaitForDevice() { local -i QUERY_RETRY_COUNT_MAX="10" local -i QUERY_RETRY_COUNT="0" @@ -372,10 +388,20 @@ function ExitIfQEMU() { fi } +function Cleanup() { + if [[ -n "${LOCKFD}" ]]; then + UnlockStateChange + fi +} + +trap Cleanup EXIT + ParseArguments "$@" ExitIfQEMU +LockStateChange + WaitForDevice if [ "${IMAGE_UPGRADE}" != "${YES_PARAM}" ]; then