Blame SOURCES/lvm2-2_03_14-vdo-more-lvm_import_vdo-fixes.patch

ffcdab
From 6621116b61f4c9ee53166a994be2ef7d80a3c346 Mon Sep 17 00:00:00 2001
ffcdab
From: Zdenek Kabelac <zkabelac@redhat.com>
ffcdab
Date: Mon, 6 Sep 2021 15:06:32 +0200
ffcdab
Subject: [PATCH 4/5] vdo: more lvm_import_vdo fixes
ffcdab
ffcdab
Do not call 'dmsetup info' for non-dm devices.
ffcdab
ffcdab
Better handling for VGNAME and LVNAME - so when convering LV as
ffcdab
backend device automatically recognize it and reuse LV name for VDOLV.
ffcdab
ffcdab
Add prompt for final confirmation before actual conversion is started
ffcdab
(once confirmed, lvm2 commands no longer prompts to avoid leaving
ffcdab
conversion in the middle of its process.)
ffcdab
ffcdab
(cherry picked from commit 89595a366554191c3df1a18e1f82b79c450a21ad)
ffcdab
---
ffcdab
 scripts/lvm_import_vdo.sh | 48 ++++++++++++++++++++++++++++++++++------
ffcdab
 test/shell/vdo-convert.sh | 56 +++++++++++++++++++++++++++++++++++++++++++++++
ffcdab
 2 files changed, 97 insertions(+), 7 deletions(-)
ffcdab
ffcdab
diff --git a/scripts/lvm_import_vdo.sh b/scripts/lvm_import_vdo.sh
ffcdab
index dec32bc..70904f7 100755
ffcdab
--- a/scripts/lvm_import_vdo.sh
ffcdab
+++ b/scripts/lvm_import_vdo.sh
ffcdab
@@ -50,6 +50,7 @@ DM_DEV_DIR="${DM_DEV_DIR:-/dev}"
ffcdab
 DEVICENAME=""
ffcdab
 DEVMAJOR=0
ffcdab
 DEVMINOR=0
ffcdab
+PROMPTING=""
ffcdab
 
ffcdab
 DRY=0
ffcdab
 VERB=""
ffcdab
@@ -57,7 +58,8 @@ FORCE=""
ffcdab
 YES=""
ffcdab
 
ffcdab
 # default name for converted VG and its VDO LV
ffcdab
-NAME="vdovg/vdolvol"
ffcdab
+DEFAULT_NAME="vdovg/vdolvol"
ffcdab
+NAME=""
ffcdab
 
ffcdab
 # help message
ffcdab
 tool_usage() {
ffcdab
@@ -100,6 +102,7 @@ dry() {
ffcdab
 cleanup() {
ffcdab
 	trap '' 2
ffcdab
 
ffcdab
+	test -z "$PROMPTING" || echo "No"
ffcdab
 	rm -rf "$TEMPDIR"
ffcdab
 	# error exit status for break
ffcdab
 	exit "${1:-1}"
ffcdab
@@ -175,7 +178,9 @@ detect_lv_() {
ffcdab
 		;;
ffcdab
 	esac
ffcdab
 
ffcdab
-	DEV="$("$DMSETUP" info -c -j "$DEVMAJOR" -m "$DEVMINOR" -o uuid,name --noheadings --nameprefixes --separator ' ' 2>/dev/null)"
ffcdab
+	test "$DEVMAJOR" != "$(grep device-mapper /proc/devices | cut -f1 -d' ')" && return
ffcdab
+
ffcdab
+	DEV="$("$DMSETUP" info -c -j "$DEVMAJOR" -m "$DEVMINOR" -o uuid,name --noheadings --nameprefixes --separator ' ')"
ffcdab
 	case "$DEV" in
ffcdab
 	Device*)  ;; # no devices
ffcdab
 	*)	eval "$DEV" ;;
ffcdab
@@ -244,15 +249,31 @@ convert2lvm_() {
ffcdab
 	detect_lv_ "$DEVICE"
ffcdab
 	case "$DM_UUID" in
ffcdab
 		LVM-*)	eval "$("$DMSETUP" splitname --nameprefixes --noheadings --separator ' ' "$DM_NAME")"
ffcdab
-			if [ -z "$VGNAME" ] || [ "$VGNAME" = "$LVNAME" ]  ; then
ffcdab
+			if [ -z "$VGNAME" ] || [ "$VGNAME" = "$LVNAME" ] ; then
ffcdab
 				VGNAME=$DM_VG_NAME
ffcdab
-				LVNAME=$DM_LV_NAME
ffcdab
+				verbose "Using existing volume group name $VGNAME."
ffcdab
+				test -n "$LVNAME" || LVNAME=$DM_LV_NAME
ffcdab
 			elif test "$VGNAME" != "$DM_VG_NAME" ; then
ffcdab
-				error "Volume group name \"$VGNAME\" does not match name \"$DM_VG_NAME\" for device \"$DEVICE\"."
ffcdab
+				error "Volume group name \"$VGNAME\" does not match name \"$DM_VG_NAME\" for VDO device \"$DEVICE\"."
ffcdab
 			fi
ffcdab
 			;;
ffcdab
-		*) IS_LV=0
ffcdab
-			# Check $VGNANE does not already exists
ffcdab
+		*)	IS_LV=0
ffcdab
+			# Check if we need to generate unused $VGNANE
ffcdab
+			if [ -z "$VGNAME" ] || [ "$VGNAME" = "$LVNAME" ] ; then
ffcdab
+				VGNAME=${DEFAULT_NAME%/*}
ffcdab
+				# Find largest matching VG name to our 'default' vgname
ffcdab
+				LASTVGNAME=$(LC_ALL=C "$LVM" vgs -oname -O-name --noheadings -S name=~${VGNAME} | grep -E "$VGNAME[0-9]? ?" | head -1)
ffcdab
+				if test -n "$LASTVGNAME" ; then
ffcdab
+					LASTVGNAME=${LASTVGNAME#*${VGNAME}}
ffcdab
+					# If the number is becoming too high, try some random number
ffcdab
+					test "$LASTVGNAME" -gt 99999999 2>/dev/null && LASTVGNAME=$RANDOM
ffcdab
+					# Generate new unused VG name
ffcdab
+					VGNAME="${VGNAME}$(( ${LASTVGNAME} + 1 ))"
ffcdab
+					verbose "Selected unused volume group name $VGNAME."
ffcdab
+				fi
ffcdab
+			fi
ffcdab
+			# New VG is created, LV name should be always unused.
ffcdab
+			test -n "$LVNAME" || LVNAME=${DEFAULT_NAME#*/}
ffcdab
 			"$LVM" vgs "$VGNAME" >/dev/null 2>&1 && error "Cannot use already existing volume group name \"$VGNAME\"."
ffcdab
 			;;
ffcdab
 	esac
ffcdab
@@ -328,6 +349,19 @@ EOF
ffcdab
 )
ffcdab
 	verbose "VDO conversion paramaters: $PARAMS"
ffcdab
 
ffcdab
+	# If user has not provided '--yes', prompt before conversion
ffcdab
+	if test -z "$YES" ; then
ffcdab
+		PROMPTING=yes
ffcdab
+		echo -n "Convert VDO device \"$DEVICE\" to VDO LV \"$VGNAME/$LVNAME\"? [y|N]: "
ffcdab
+		read -n 1 -s ANSWER
ffcdab
+		case "${ANSWER:0:1}" in
ffcdab
+			y|Y )  echo "Yes" ;;
ffcdab
+			* )    echo "No" ; PROMPTING=""; exit ;;
ffcdab
+		esac
ffcdab
+		PROMPTING=""
ffcdab
+		YES="-y" # From now, now prompting
ffcdab
+	fi
ffcdab
+
ffcdab
 	verbose "Stopping VDO volume."
ffcdab
 	dry "$VDO" stop $VDOCONF --name "$VDONAME"
ffcdab
 
ffcdab
diff --git a/test/shell/vdo-convert.sh b/test/shell/vdo-convert.sh
ffcdab
index 493f415..632f86a 100644
ffcdab
--- a/test/shell/vdo-convert.sh
ffcdab
+++ b/test/shell/vdo-convert.sh
ffcdab
@@ -122,3 +122,59 @@ fsck -n "$DM_DEV_DIR/$vg1/$lv2"
ffcdab
 
ffcdab
 vgremove -f $vg1
ffcdab
 
ffcdab
+aux teardown_devs
ffcdab
+
ffcdab
+
ffcdab
+# Check with some real non-DM device from system
ffcdab
+# this needs to dropping DM_DEV_DIR
ffcdab
+
ffcdab
+aux prepare_loop 60000 || skip
ffcdab
+
ffcdab
+test -f LOOP
ffcdab
+LOOP=$(< LOOP)
ffcdab
+
ffcdab
+aux extend_filter "a|$LOOP|"
ffcdab
+aux extend_devices "$LOOP"
ffcdab
+
ffcdab
+#
ffcdab
+# Unfortunatelly generates this in syslog:
ffcdab
+#
ffcdab
+# vdo-start-by-dev@loop0.service: Main process exited, code=exited, status=1/FAILURE
ffcdab
+# vdo-start-by-dev@loop0.service: Failed with result 'exit-code'.
ffcdab
+# Failed to start Start VDO volume backed by loop0.
ffcdab
+#
ffcdab
+# TODO:  Could be handled by:
ffcdab
+#
ffcdab
+# systemctl mask vdo-start-by-dev@
ffcdab
+# systemctl unmask vdo-start-by-dev@
ffcdab
+#
ffcdab
+# automate...
ffcdab
+#
ffcdab
+vdo create $VDOCONF --name "$VDONAME" --device="$LOOP" --vdoLogicalSize=23G \
ffcdab
+	--blockMapCacheSize 192 \
ffcdab
+	--blockMapPeriod 2048 \
ffcdab
+	--emulate512 disabled \
ffcdab
+	--indexMem 0.5 \
ffcdab
+	--maxDiscardSize 10 \
ffcdab
+	--sparseIndex disabled \
ffcdab
+	--vdoAckThreads 2 \
ffcdab
+	--vdoBioRotationInterval 8 \
ffcdab
+	--vdoBioThreads 2 \
ffcdab
+	--vdoCpuThreads 5 \
ffcdab
+	--vdoHashZoneThreads 3 \
ffcdab
+	--vdoLogicalThreads 3 \
ffcdab
+	--writePolicy async-unsafe
ffcdab
+
ffcdab
+# Get VDO table line
ffcdab
+dmsetup table "$VDONAME" | tr " " "\n" | sed -e '5,6d' -e '12d' | tee vdo-orig
ffcdab
+
ffcdab
+DM_DEV_DIR= lvm_import_vdo -y --name $vg/$lv "$LOOP"
ffcdab
+lvs -a $vg
ffcdab
+
ffcdab
+dmsetup table "$vg-${lv}_vpool-vpool" | tr " " "\n" | sed -e '5,6d' -e '12d' | tee new-vdo-lv
ffcdab
+
ffcdab
+# Check there is a match between VDO and LV managed volume
ffcdab
+# (when differentiating parameters are deleted first)
ffcdab
+diff -u vdo-orig new-vdo-lv || die "Found mismatching VDO table lines!"
ffcdab
+
ffcdab
+check lv_field $vg/$lv size "23.00g"
ffcdab
-- 
ffcdab
1.8.3.1
ffcdab