Blame SOURCES/lvm2-2_02_188-fsadm-enhance-error-handling.patch

e1e9d4
From d79c7a28521dc79275ca87737096bcee8c5121c8 Mon Sep 17 00:00:00 2001
e1e9d4
From: Zdenek Kabelac <zkabelac@redhat.com>
e1e9d4
Date: Sat, 24 Oct 2020 01:13:42 +0200
e1e9d4
Subject: [PATCH 2/2] fsadm: enhance error handling
e1e9d4
e1e9d4
Set more secure bash failure mode for pipilines.
e1e9d4
Avoid using unset variables.
e1e9d4
Enhnace error reporting for failing command.
e1e9d4
Avoid using error via 'case..esac || error'.
e1e9d4
e1e9d4
(cherry picked from commit 9a2a59c0ed4320324e908be60693bac7ed507824)
e1e9d4
e1e9d4
Conflicts:
e1e9d4
	scripts/fsadm.sh
e1e9d4
---
e1e9d4
 scripts/fsadm.sh | 51 ++++++++++++++++++++++++++++++---------------------
e1e9d4
 1 file changed, 30 insertions(+), 21 deletions(-)
e1e9d4
e1e9d4
diff --git a/scripts/fsadm.sh b/scripts/fsadm.sh
e1e9d4
index c4c7515..c59d7b6 100755
e1e9d4
--- a/scripts/fsadm.sh
e1e9d4
+++ b/scripts/fsadm.sh
e1e9d4
@@ -29,6 +29,8 @@
e1e9d4
 #   2 break detected
e1e9d4
 #   3 unsupported online filesystem check for given mounted fs
e1e9d4
 
e1e9d4
+set -euE -o pipefail
e1e9d4
+
e1e9d4
 TOOL=fsadm
e1e9d4
 
e1e9d4
 _SAVEPATH=$PATH
e1e9d4
@@ -61,7 +63,7 @@ CRYPTSETUP=cryptsetup
e1e9d4
 # user may override lvm location by setting LVM_BINARY
e1e9d4
 LVM=${LVM_BINARY:-lvm}
e1e9d4
 
e1e9d4
-YES=${_FSADM_YES}
e1e9d4
+YES="${_FSADM_YES-}"
e1e9d4
 DRY=0
e1e9d4
 VERB=
e1e9d4
 FORCE=
e1e9d4
@@ -206,7 +208,7 @@ decode_major_minor() {
e1e9d4
 # detect filesystem on the given device
e1e9d4
 # dereference device name if it is symbolic link
e1e9d4
 detect_fs() {
e1e9d4
-	test -n "$VOLUME_ORIG" || VOLUME_ORIG=$1
e1e9d4
+	test -n "${VOLUME_ORIG-}" || VOLUME_ORIG=$1
e1e9d4
 	VOLUME=${1/#"${DM_DEV_DIR}/"/}
e1e9d4
 	VOLUME=$("$READLINK" $READLINK_E "$DM_DEV_DIR/$VOLUME")
e1e9d4
 	test -n "$VOLUME" || error "Cannot get readlink \"$1\"."
e1e9d4
@@ -257,11 +259,11 @@ check_valid_mounted_device() {
e1e9d4
 	local MOUNTEDMAJORMINOR
e1e9d4
 	local VOL
e1e9d4
 	local CURNAME
e1e9d4
-	local SUGGEST="Possibly device \"$1\" has been renamed to \"$CURNAME\"?"
e1e9d4
 
e1e9d4
 	VOL=$("$READLINK" $READLINK_E "$1")
e1e9d4
 	CURNAME=$(dmsetup info -c -j "$MAJOR" -m "$MINOR" -o name --noheadings)
e1e9d4
 	# more confused, device is not DM....
e1e9d4
+	local SUGGEST="Possibly device \"$1\" has been renamed to \"$CURNAME\"?"
e1e9d4
 	test -n "$CURNAME" || SUGGEST="Mounted volume is not a device mapper device???"
e1e9d4
 
e1e9d4
 	test -n "$VOL" ||
e1e9d4
@@ -270,7 +272,7 @@ check_valid_mounted_device() {
e1e9d4
 		"Filesystem utilities currently do not support renamed devices."
e1e9d4
 
e1e9d4
 	case "$VOL" in
e1e9d4
-	  # hardcoded /dev  since udev does not create these entries elsewhere
e1e9d4
+	  # hardcoded /dev  since kernel does not create these entries elsewhere
e1e9d4
 	  /dev/dm-[0-9]*)
e1e9d4
 		read -r <"/sys/block/${VOL#/dev/}/dev" MOUNTEDMAJORMINOR 2>&1 || error "Cannot get major:minor for \"$VOLUME\"."
e1e9d4
 		;;
e1e9d4
@@ -674,20 +676,23 @@ resize() {
e1e9d4
 	# if the size parameter is missing use device size
e1e9d4
 	#if [ -n "$NEWSIZE" -a $NEWSIZE <
e1e9d4
 	test -z "$NEWSIZE" && NEWSIZE=${DEVSIZE}b
e1e9d4
-	test -n "$NEWSIZE_ORIG" || NEWSIZE_ORIG=$NEWSIZE
e1e9d4
+	NEWSIZE_ORIG=${NEWSIZE_ORIG:-$NEWSIZE}
e1e9d4
 	IFS=$NL
e1e9d4
-	test -z "$DO_CRYPTRESIZE" || detect_crypt_device "$VOLUME_ORIG" "$NEWSIZE_ORIG"
e1e9d4
-	test -z "$CRYPT_GROW" || resize_crypt "$VOLUME_ORIG"
e1e9d4
+	test -z "${DO_CRYPTRESIZE-}" || detect_crypt_device "$VOLUME_ORIG" "$NEWSIZE_ORIG"
e1e9d4
+	test -z "${CRYPT_GROW-}" || resize_crypt "$VOLUME_ORIG"
e1e9d4
+
e1e9d4
 	case "$FSTYPE" in
e1e9d4
-	  "ext3"|"ext2"|"ext4") resize_ext $NEWSIZE ;;
e1e9d4
-	  "reiserfs") resize_reiser $NEWSIZE ;;
e1e9d4
-	  "xfs") resize_xfs $NEWSIZE ;;
e1e9d4
+	  ext[234])	CMD=resize_ext ;;
e1e9d4
+	  "reiserfs")	CMD=resize_reiser ;;
e1e9d4
+	  "xfs")	CMD=resize_xfs ;;
e1e9d4
 	  "crypto_LUKS")
e1e9d4
-		which "$CRYPTSETUP" > /dev/null 2>&1 || error "$CRYPTSETUP utility required to resize LUKS volume"
e1e9d4
-		resize_luks $NEWSIZE ;;
e1e9d4
+		which "$CRYPTSETUP" >"$NULL" 2>&1 || error "$CRYPTSETUP utility required to resize LUKS volume"
e1e9d4
+		CMD=resize_luks ;;
e1e9d4
 	  *) error "Filesystem \"$FSTYPE\" on device \"$VOLUME\" is not supported by this tool." ;;
e1e9d4
-	esac || error "Resize $FSTYPE failed."
e1e9d4
-	test -z "$CRYPT_SHRINK" || resize_crypt "$VOLUME_ORIG"
e1e9d4
+	esac
e1e9d4
+
e1e9d4
+	$CMD $NEWSIZE || error "$FSTYPE resize failed."
e1e9d4
+	test -z "${CRYPT_SHRINK-}" || resize_crypt "$VOLUME_ORIG"
e1e9d4
 }
e1e9d4
 
e1e9d4
 ####################################
e1e9d4
@@ -716,7 +721,7 @@ check() {
e1e9d4
 	fi
e1e9d4
 
e1e9d4
 	case "$FSTYPE" in
e1e9d4
-	  "ext2"|"ext3"|"ext4")
e1e9d4
+	  ext[234])
e1e9d4
 		IFS_CHECK=$IFS
e1e9d4
 		IFS=$NL
e1e9d4
 		for i in $(LC_ALL=C "$TUNE_EXT" -l "$VOLUME"); do
e1e9d4
@@ -740,15 +745,15 @@ check() {
e1e9d4
 
e1e9d4
 	case "$FSTYPE" in
e1e9d4
 	  "xfs") if which "$XFS_CHECK" >"$NULL" 2>&1 ; then
e1e9d4
-			dry "$XFS_CHECK" "$VOLUME"
e1e9d4
+			dry "$XFS_CHECK" "$VOLUME" || error "Xfs check failed."
e1e9d4
 		 else
e1e9d4
 			# Replacement for outdated xfs_check
e1e9d4
 			# FIXME: for small devices we need to force_geometry,
e1e9d4
 			# since we run in '-n' mode, it shouldn't be problem.
e1e9d4
 			# Think about better way....
e1e9d4
-			dry "$XFS_REPAIR" -n -o force_geometry "$VOLUME"
e1e9d4
+			dry "$XFS_REPAIR" -n -o force_geometry "$VOLUME" || error "Xfs repair failed."
e1e9d4
 		 fi ;;
e1e9d4
-	  "ext2"|"ext3"|"ext4"|"reiserfs")
e1e9d4
+	  ext[234]|"reiserfs")
e1e9d4
 	        # check if executed from interactive shell environment
e1e9d4
 		case "$-" in
e1e9d4
 		  *i*) FLAG=$YES ;;
e1e9d4
@@ -757,8 +762,9 @@ check() {
e1e9d4
 		accept_0_1 dry "$FSCK" $FORCE $FLAG "$VOLUME" || error "Fsck $FSTYPE failed."
e1e9d4
 		;;
e1e9d4
 	  "crypto_LUKS")
e1e9d4
-		which "$CRYPTSETUP" > /dev/null 2>&1 || error "$CRYPTSETUP utility required."
e1e9d4
-		check_luks ;;
e1e9d4
+		which "$CRYPTSETUP" >"$NULL" 2>&1 || error "$CRYPTSETUP utility required."
e1e9d4
+		check_luks || error "Crypto luks check failed."
e1e9d4
+		;;
e1e9d4
 	  *)
e1e9d4
 		error "Filesystem \"$FSTYPE\" on device \"$VOLUME\" is not supported by this tool." ;;
e1e9d4
 	esac
e1e9d4
@@ -771,7 +777,7 @@ check() {
e1e9d4
 trap "cleanup 2" 2
e1e9d4
 
e1e9d4
 # test if we are not invoked recursively
e1e9d4
-test -n "$FSADM_RUNNING" && exit 0
e1e9d4
+test -n "${FSADM_RUNNING-}" && exit 0
e1e9d4
 
e1e9d4
 # test some prerequisities
e1e9d4
 for i in "$TUNE_EXT" "$RESIZE_EXT" "$TUNE_REISER" "$RESIZE_REISER" \
e1e9d4
@@ -793,6 +799,9 @@ if [ "$#" -eq 0 ] ; then
e1e9d4
 	tool_usage
e1e9d4
 fi
e1e9d4
 
e1e9d4
+CHECK=""
e1e9d4
+RESIZE=""
e1e9d4
+
e1e9d4
 while [ "$#" -ne 0 ]
e1e9d4
 do
e1e9d4
 	 case "$1" in
e1e9d4
-- 
e1e9d4
1.8.3.1
e1e9d4