1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-21 14:50:08 +03:00

Merge pull request #347 from vholer/bug-5170

B #5170: LVM exclusive locking
This commit is contained in:
Javi Fontan 2017-06-27 19:23:35 +02:00 committed by GitHub
commit a414e4d536
4 changed files with 61 additions and 3 deletions

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

View File

@ -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 <<EOF
set -e -o pipefail
$SUDO $SYNC
$SUDO $LVSCAN
$SUDO $LVCREATE --wipesignatures n -L${SIZE}M -n $LV_NAME $VG_NAME
EOF
)
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
$SUDO $LVCREATE --wipesignatures n -L${SIZE}M -n $LV_NAME $VG_NAME
$QEMU_IMG convert -O raw "$SRC_PATH" "$DEV"
rm -f "$DST_PATH"
ln -s "$DEV" "$DST_PATH"

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,5 +62,11 @@ DELETE_CMD=$(cat <<EOF
EOF
)
ssh_exec_and_log "$DST_HOST" "$DELETE_CMD" "Error deleting $DST_PATH"
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

View File

@ -54,6 +54,10 @@ DEV="/dev/${VG_NAME}/${LV_NAME}"
DUMP_CMD=$(cat <<EOF
$DD if=$DEV of=$DST_PATH bs=64k
EOF
)
DELETE_CMD=$(cat <<EOF
$SUDO $LVREMOVE -f $DEV
EOF
)
@ -66,4 +70,8 @@ log "Dumping $SRC to $DST"
ssh_exec_and_log "$SRC_HOST" "$DUMP_CMD" \
"Error dumping $SRC to $DST"
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