1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-02 01:18:26 +03:00

fsadm: enhance error handling

Set more secure bash failure mode for pipilines.
Avoid using unset variables.
Enhnace error reporting for failing command.
Avoid using error via 'case..esac || error'.
This commit is contained in:
Zdenek Kabelac 2020-10-24 01:13:42 +02:00
parent 51a532719c
commit 8c2779ba34

View File

@ -29,6 +29,8 @@
# 2 break detected # 2 break detected
# 3 unsupported online filesystem check for given mounted fs # 3 unsupported online filesystem check for given mounted fs
set -euE -o pipefail
TOOL=fsadm TOOL=fsadm
_SAVEPATH=$PATH _SAVEPATH=$PATH
@ -61,7 +63,7 @@ CRYPTSETUP=cryptsetup
# user may override lvm location by setting LVM_BINARY # user may override lvm location by setting LVM_BINARY
LVM=${LVM_BINARY:-lvm} LVM=${LVM_BINARY:-lvm}
YES=${_FSADM_YES} YES="${_FSADM_YES-}"
DRY=0 DRY=0
VERB= VERB=
FORCE= FORCE=
@ -206,7 +208,7 @@ decode_major_minor() {
# detect filesystem on the given device # detect filesystem on the given device
# dereference device name if it is symbolic link # dereference device name if it is symbolic link
detect_fs() { detect_fs() {
test -n "$VOLUME_ORIG" || VOLUME_ORIG=$1 test -n "${VOLUME_ORIG-}" || VOLUME_ORIG=$1
VOLUME=${1/#"${DM_DEV_DIR}/"/} VOLUME=${1/#"${DM_DEV_DIR}/"/}
VOLUME=$("$READLINK" $READLINK_E "$DM_DEV_DIR/$VOLUME") VOLUME=$("$READLINK" $READLINK_E "$DM_DEV_DIR/$VOLUME")
test -n "$VOLUME" || error "Cannot get readlink \"$1\"." test -n "$VOLUME" || error "Cannot get readlink \"$1\"."
@ -257,11 +259,11 @@ check_valid_mounted_device() {
local MOUNTEDMAJORMINOR local MOUNTEDMAJORMINOR
local VOL local VOL
local CURNAME local CURNAME
local SUGGEST="Possibly device \"$1\" has been renamed to \"$CURNAME\"?"
VOL=$("$READLINK" $READLINK_E "$1") VOL=$("$READLINK" $READLINK_E "$1")
CURNAME=$(dmsetup info -c -j "$MAJOR" -m "$MINOR" -o name --noheadings) CURNAME=$(dmsetup info -c -j "$MAJOR" -m "$MINOR" -o name --noheadings)
# more confused, device is not DM.... # more confused, device is not DM....
local SUGGEST="Possibly device \"$1\" has been renamed to \"$CURNAME\"?"
test -n "$CURNAME" || SUGGEST="Mounted volume is not a device mapper device???" test -n "$CURNAME" || SUGGEST="Mounted volume is not a device mapper device???"
test -n "$VOL" || test -n "$VOL" ||
@ -270,7 +272,7 @@ check_valid_mounted_device() {
"Filesystem utilities currently do not support renamed devices." "Filesystem utilities currently do not support renamed devices."
case "$VOL" in case "$VOL" in
# hardcoded /dev since udev does not create these entries elsewhere # hardcoded /dev since kernel does not create these entries elsewhere
/dev/dm-[0-9]*) /dev/dm-[0-9]*)
read -r <"/sys/block/${VOL#/dev/}/dev" MOUNTEDMAJORMINOR 2>&1 || error "Cannot get major:minor for \"$VOLUME\"." read -r <"/sys/block/${VOL#/dev/}/dev" MOUNTEDMAJORMINOR 2>&1 || error "Cannot get major:minor for \"$VOLUME\"."
;; ;;
@ -674,20 +676,23 @@ resize() {
# if the size parameter is missing use device size # if the size parameter is missing use device size
#if [ -n "$NEWSIZE" -a $NEWSIZE < #if [ -n "$NEWSIZE" -a $NEWSIZE <
test -z "$NEWSIZE" && NEWSIZE=${DEVSIZE}b test -z "$NEWSIZE" && NEWSIZE=${DEVSIZE}b
test -n "$NEWSIZE_ORIG" || NEWSIZE_ORIG=$NEWSIZE NEWSIZE_ORIG=${NEWSIZE_ORIG:-$NEWSIZE}
IFS=$NL IFS=$NL
test -z "$DO_CRYPTRESIZE" || detect_crypt_device "$VOLUME_ORIG" "$NEWSIZE_ORIG" test -z "${DO_CRYPTRESIZE-}" || detect_crypt_device "$VOLUME_ORIG" "$NEWSIZE_ORIG"
test -z "$CRYPT_GROW" || resize_crypt "$VOLUME_ORIG" test -z "${CRYPT_GROW-}" || resize_crypt "$VOLUME_ORIG"
case "$FSTYPE" in case "$FSTYPE" in
"ext3"|"ext2"|"ext4") resize_ext $NEWSIZE ;; ext[234]) CMD=resize_ext ;;
"reiserfs") resize_reiser $NEWSIZE ;; "reiserfs") CMD=resize_reiser ;;
"xfs") resize_xfs $NEWSIZE ;; "xfs") CMD=resize_xfs ;;
"crypto_LUKS") "crypto_LUKS")
which "$CRYPTSETUP" >"$NULL" 2>&1 || error "$CRYPTSETUP utility required to resize LUKS volume" which "$CRYPTSETUP" >"$NULL" 2>&1 || error "$CRYPTSETUP utility required to resize LUKS volume"
resize_luks $NEWSIZE ;; CMD=resize_luks ;;
*) error "Filesystem \"$FSTYPE\" on device \"$VOLUME\" is not supported by this tool." ;; *) error "Filesystem \"$FSTYPE\" on device \"$VOLUME\" is not supported by this tool." ;;
esac || error "Resize $FSTYPE failed." esac
test -z "$CRYPT_SHRINK" || resize_crypt "$VOLUME_ORIG"
$CMD $NEWSIZE || error "$FSTYPE resize failed."
test -z "${CRYPT_SHRINK-}" || resize_crypt "$VOLUME_ORIG"
} }
#################################### ####################################
@ -716,7 +721,7 @@ check() {
fi fi
case "$FSTYPE" in case "$FSTYPE" in
"ext2"|"ext3"|"ext4") ext[234])
IFS_CHECK=$IFS IFS_CHECK=$IFS
IFS=$NL IFS=$NL
for i in $(LC_ALL=C "$TUNE_EXT" -l "$VOLUME"); do for i in $(LC_ALL=C "$TUNE_EXT" -l "$VOLUME"); do
@ -740,15 +745,15 @@ check() {
case "$FSTYPE" in case "$FSTYPE" in
"xfs") if which "$XFS_CHECK" >"$NULL" 2>&1 ; then "xfs") if which "$XFS_CHECK" >"$NULL" 2>&1 ; then
dry "$XFS_CHECK" "$VOLUME" dry "$XFS_CHECK" "$VOLUME" || error "Xfs check failed."
else else
# Replacement for outdated xfs_check # Replacement for outdated xfs_check
# FIXME: for small devices we need to force_geometry, # FIXME: for small devices we need to force_geometry,
# since we run in '-n' mode, it shouldn't be problem. # since we run in '-n' mode, it shouldn't be problem.
# Think about better way.... # Think about better way....
dry "$XFS_REPAIR" -n -o force_geometry "$VOLUME" dry "$XFS_REPAIR" -n -o force_geometry "$VOLUME" || error "Xfs repair failed."
fi ;; fi ;;
"ext2"|"ext3"|"ext4"|"reiserfs") ext[234]|"reiserfs")
# check if executed from interactive shell environment # check if executed from interactive shell environment
case "$-" in case "$-" in
*i*) FLAG=$YES ;; *i*) FLAG=$YES ;;
@ -758,7 +763,8 @@ check() {
;; ;;
"crypto_LUKS") "crypto_LUKS")
which "$CRYPTSETUP" >"$NULL" 2>&1 || error "$CRYPTSETUP utility required." which "$CRYPTSETUP" >"$NULL" 2>&1 || error "$CRYPTSETUP utility required."
check_luks ;; check_luks || error "Crypto luks check failed."
;;
*) *)
error "Filesystem \"$FSTYPE\" on device \"$VOLUME\" is not supported by this tool." ;; error "Filesystem \"$FSTYPE\" on device \"$VOLUME\" is not supported by this tool." ;;
esac esac
@ -771,7 +777,7 @@ check() {
trap "cleanup 2" 2 trap "cleanup 2" 2
# test if we are not invoked recursively # test if we are not invoked recursively
test -n "$FSADM_RUNNING" && exit 0 test -n "${FSADM_RUNNING-}" && exit 0
# test some prerequisities # test some prerequisities
for i in "$TUNE_EXT" "$RESIZE_EXT" "$TUNE_REISER" "$RESIZE_REISER" \ for i in "$TUNE_EXT" "$RESIZE_EXT" "$TUNE_REISER" "$RESIZE_REISER" \
@ -793,6 +799,9 @@ if [ "$#" -eq 0 ] ; then
tool_usage tool_usage
fi fi
CHECK=""
RESIZE=""
while [ "$#" -ne 0 ] while [ "$#" -ne 0 ]
do do
case "$1" in case "$1" in