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 2f5ebe7719..a891b11a4b 100755 --- a/src/tm_mad/fs_lvm/clone +++ b/src/tm_mad/fs_lvm/clone @@ -90,11 +90,23 @@ LV_NAME="lv-one-$VM_ID-$DISK_ID" VG_NAME="vg-one-$DS_SYS_ID" DEV="/dev/${VG_NAME}/${LV_NAME}" +# Execute lvcreate with a lock in the frontend +CREATE_CMD=$(cat <