B #5170: Common scripts function "exclusive", use in fs_lvm

This commit is contained in:
Vlastimil Holer authored and Vlastimil Holer committed 2017-06-15 18:38:50 +02:00
1 parent fa1ed2be55
commit e15cdcce83
4 files changed
+46 -11

No files matched your search

+30
View File
@@ -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
+4 -5
View File
@@ -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 <<EOF
set -e -o pipefail
$SUDO $SYNC
$SUDO $LVSCAN
$SUDO $LVCREATE --wipesignatures n -L${SIZE}M -n $LV_NAME $VG_NAME
EOF
)
LOCK_FILE="$DST_DS_PATH/lock"
flock -w 120 $LOCK_FILE ssh_exec_and_log "$DST_HOST" "$CREATE_CMD" \
LOCK="tm-fs_lvm-${DS_SYS_ID}.lock"
exclusive "${LOCK}" 120 ssh_exec_and_log "$DST_HOST" "$CREATE_CMD" \
"Error creating LV named $LV_NAME"
CLONE_CMD=$(cat <<EOF
set -e -o pipefail
mkdir -p $DST_DIR
$QEMU_IMG convert -O raw "$SRC_PATH" "$DEV"
rm -f "$DST_PATH"
+9 -3
View File
@@ -39,6 +39,8 @@ source $TMCOMMON
DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`
DS_SYS_ID=$(echo $DST_PATH | grep -E '\/disk\.[[:digit:]]+$' | $AWK -F '/' '{print $(NF-2)}')
# Delete the device if it's a clone (LVM snapshot)
DELETE_CMD=$(cat <<EOF
set -x
@@ -60,7 +62,11 @@ DELETE_CMD=$(cat <<EOF
EOF
)
LOCK_FILE="$DST_DS_PATH/lock"
flock -w 120 $LOCK_FILE ssh_exec_and_log "$DST_HOST" "$DELETE_CMD" \
if [ -n "${DS_SYS_ID}" ]; then
LOCK="tm-fs_lvm-${DS_SYS_ID}.lock"
exclusive "${LOCK}" 120 ssh_exec_and_log "$DST_HOST" "$DELETE_CMD" \
"Error deleting $DST_PATH"
else
ssh_exec_and_log "$DST_HOST" "$DELETE_CMD" \
"Error deleting $DST_PATH"
fi
+3 -3
View File
@@ -70,8 +70,8 @@ log "Dumping $SRC to $DST"
ssh_exec_and_log "$SRC_HOST" "$DUMP_CMD" \
"Error dumping $SRC to $DST"
LOCK_FILE="$DST_DS_PATH/lock"
flock -w 120 $LOCK_FILE ssh_exec_and_log "$SRC_HOST" "$REMOVE_CMD" \
"Error deleting LV $LV_NAME"
LOCK="tm-fs_lvm-${DS_SYS_ID}.lock"
exclusive "${LOCK}" 120 ssh_exec_and_log "$SRC_HOST" "$DELETE_CMD" \
"Error dumping $SRC to $DST"
exit 0