From e15cdcce836b344800569f21a0f0fd2da7ebc337 Mon Sep 17 00:00:00 2001 From: Vlastimil Holer Date: Thu, 15 Jun 2017 12:28:52 +0200 Subject: [PATCH] B #5170: Common scripts function "exclusive", use in fs_lvm --- src/mad/sh/scripts_common.sh | 30 ++++++++++++++++++++++++++++++ src/tm_mad/fs_lvm/clone | 9 ++++----- src/tm_mad/fs_lvm/delete | 12 +++++++++--- src/tm_mad/fs_lvm/mvds | 6 +++--- 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/mad/sh/scripts_common.sh b/src/mad/sh/scripts_common.sh index 6d917232ab..4c1cc9fa7a 100644 --- a/src/mad/sh/scripts_common.sh +++ b/src/mad/sh/scripts_common.sh @@ -129,6 +129,36 @@ function error_message ) 1>&2 } +# Ensures the code is executed exclusively +function exclusive +{ + LOCK_FILE="/var/lock/one/$1" + TIMEOUT=$2 + shift 2 + + ( umask 0027; touch "${LOCK_FILE}" 2>/dev/null ) + + # open lockfile + exec 2>/dev/null {FD}>"${LOCK_FILE}" + if [ $? -ne 0 ]; then + log_error "Could not create or open lock ${LOCK_FILE}" + exit -2 + fi + + # acquire lock + flock -w "${TIMEOUT}" "${FD}" + if [ $? -ne 0 ]; then + log_error "Could not acquire exclusive lock on ${LOCK_FILE}" + exit -2 + fi + + "$@" + + EXEC_RC=$? + eval "exec ${FD}>&-" + return $EXEC_RC +} + # Executes a command, if it fails returns error message and exits # If a second parameter is present it is used as the error message when # the command fails diff --git a/src/tm_mad/fs_lvm/clone b/src/tm_mad/fs_lvm/clone index 025ea0c367..a891b11a4b 100755 --- a/src/tm_mad/fs_lvm/clone +++ b/src/tm_mad/fs_lvm/clone @@ -91,22 +91,21 @@ VG_NAME="vg-one-$DS_SYS_ID" DEV="/dev/${VG_NAME}/${LV_NAME}" # Execute lvcreate with a lock in the frontend -mkdir -p $DST_DS_PATH - CREATE_CMD=$(cat <