1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-03-31 14:50:37 +03:00

added more safety checks

fixed error reporting commands
extended with Exa and Peta support
This commit is contained in:
Zdeněk Kabeláč 2008-01-08 16:45:43 +00:00
parent e0f34a9720
commit 95bf59095c

View File

@ -12,7 +12,7 @@
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Author: Zdenek Kabelac <zkabelac@redhat.com>
# Author: Zdenek Kabelac <zkabelac at redhat.com>
#
# Script for resizing devices (usable for LVM resize)
#
@ -39,9 +39,9 @@ RESIZE_XFS=xfs_growfs
MOUNT=mount
UMOUNT=umount
MKDIR=mkdir
RM=rm
BLOCKDEV=echo
BLKID=echo
RMDIR=rmdir
BLOCKDEV=blockdev
BLKID=blkid
GREP=grep
READLINK=readlink
FSCK=fsck
@ -69,7 +69,7 @@ tool_usage() {
echo " ${TOOL} [options] check device"
echo " - Check the filesystem on device using fsck"
echo
echo " ${TOOL} [options] resize device [new_size[BKMGT]]"
echo " ${TOOL} [options] resize device [new_size[BKMGTPE]]"
echo " - Change the size of the filesystem on device to new_size"
echo
echo " Options:"
@ -97,8 +97,11 @@ error() {
}
dry() {
if [ "$DRY" -ne 0 ]; then
verbose "Dry execution $@"
return 0
fi
verbose "Executing $@"
test "$DRY" -ne 0 && return 0
$@
}
@ -115,10 +118,12 @@ cleanup() {
exit $1
}
# convert parameters from Mega/Kilo/Bytes/Blocks
# and print number of bytes
# convert parameter from Exa/Peta/Tera/Giga/Mega/Kilo/Bytes and blocks
# (2^(60/50/40/30/20/10/0))
decode_size() {
case "$1" in
*[eE]) NEWSIZE=$(( ${1%[eE]} * 1152921504606846976 )) ;;
*[pP]) NEWSIZE=$(( ${1%[pP]} * 1125899906842624 )) ;;
*[tT]) NEWSIZE=$(( ${1%[tT]} * 1099511627776 )) ;;
*[gG]) NEWSIZE=$(( ${1%[gG]} * 1073741824 )) ;;
*[mM]) NEWSIZE=$(( ${1%[mM]} * 1048576 )) ;;
@ -133,14 +138,15 @@ decode_size() {
# detect filesystem on the given device
# dereference device name if it is symbolic link
detect_fs() {
VOLUME=$($READLINK -n "$1")
VOLUME=$($READLINK -e -n "$1") || error "Cannot get readlink $1"
# use /dev/null as cache file to be sure about the result
FSTYPE=$($BLKID -c /dev/null -o value -s TYPE "$VOLUME" || error "Cannot get FSTYPE of \"$VOLUME\"")
FSTYPE=$($BLKID -c /dev/null -o value -s TYPE "$VOLUME") || error "Cannot get FSTYPE of \"$VOLUME\""
verbose "\"$FSTYPE\" filesystem found on \"$VOLUME\""
}
# check if the given device is already mounted and where
detect_mounted() {
$MOUNT >/dev/null || error "Cannot detect mounted device $VOLUME"
MOUNTED=$($MOUNT | $GREP "$VOLUME")
MOUNTED=${MOUNTED##* on }
MOUNTED=${MOUNTED% type *} # allow type in the mount name
@ -165,7 +171,9 @@ temp_mount() {
}
temp_umount() {
dry $UMOUNT "$TEMPDIR" && dry $RM -r "${TEMPDIR%%m}" || error "Failed to umount $TEMPDIR"
dry $UMOUNT "$TEMPDIR" || error "Failed to umount $TEMPDIR"
dry $RMDIR "${TEMPDIR}" || error "Failed to remove $TEMPDIR"
dry $RMDIR "${TEMPDIR%%m}" || error "Failed to remove ${TEMPDIR%%m}"
}
yes_no() {
@ -207,7 +215,7 @@ resize_ext() {
decode_size $1 $BLOCKSIZE
FSFORCE=$FORCE
if [ $NEWBLOCKCOUNT -lt $BLOCKCOUNT -o $EXTOFF -eq 1 ]; then
if [ "$NEWBLOCKCOUNT" -lt "$BLOCKCOUNT" -o "$EXTOFF" -eq 1 ]; then
detect_mounted && verbose "$RESIZE_EXT needs unmounted filesystem" && try_umount
REMOUNT=$MOUNTED
# CHECKME: after umount resize2fs requires fsck or -f flag.
@ -288,7 +296,7 @@ resize() {
verbose "Device \"$VOLUME\" has $DEVSIZE bytes"
# if the size parameter is missing use device size
NEWSIZE=$2
test -z $NEWSIZE && NEWSIZE=${DEVSIZE}b
test -z "$NEWSIZE" && NEWSIZE=${DEVSIZE}b
trap cleanup 2
#IFS=$'\n' # don't use bash-ism ??
IFS="$(printf \"\\n\")" # needed for parsing output
@ -316,6 +324,18 @@ check() {
# start point of this script
# - parsing parameters
#############################
# test some prerequisities
test -n "$TUNE_EXT" -a -n "$RESIZE_EXT" -a -n "$TUNE_REISER" -a -n "$RESIZE_REISER" \
-a -n "$TUNE_XFS" -a -n "$RESIZE_XFS" -a -n "$MOUNT" -a -n "$UMOUNT" -a -n "$MKDIR" \
-a -n "$RMDIR" -a -n "$BLOCKDEV" -a -n "$BLKID" -a -n "$GREP" -a -n "$READLINK" \
-a -n "$FSCK" -a -n "$XFS_CHECK" || error "Required command definitions in the script are missing!"
$($READLINK -e -n / >/dev/null 2>&1) || error "$READLINK does not support options -e -n"
TEST64BIT=$(( 1000 * 1000000000000 ))
test $TEST64BIT -eq 1000000000000000 || error "Shell does not handle 64bit arithmetic"
$(echo Y | $GREP Y >/dev/null) || error "Grep does not work properly"
if [ "$1" = "" ] ; then
tool_usage
fi