On Thu, 31 Jan 2013, Stefan G. Weichinger wrote:

I would also like to see improved scripts, I have the issue at one
client that the LVM-snapshots aren't correctly removed sometimes ...
that leads to non-working backups and dozens of lurking snapshots ...


I had a lot of problems with LVM snapshots that would not go away, so I wrote a script deleteLV that repeatedly waits a little while then tries again. This has worked fine, though i have a suspicion that SuSE fixed the bug I was working round at exactly the same time as I installed my script!

The script deleteLV is appended to this message in case anyone finds it useful.

Bob Vickers

#! /bin/bash

# Delete an LVM logical volume (typically used for a snapshot that won't go
# away). See # https://bugzilla.novell.com/show_bug.cgi?id=642296
# Author: Bob Vickers, Royal Holloway University of london

set -o nounset
export PATH=/sbin:/usr/sbin:/bin:/usr/bin

usage=\
" deleteLV  [-n maxTries] [-t timetowait] LVdevice...
    -n         max number of tries (default 10)
    -t         time to wait after sync (default 10 seconds)"

#
#       Parse the command options
declare -i maxtries=10 delay=10
ERRFLAG=OK
OPTIND=1
while getopts n:t: OPTION
do
    case $OPTION in
    n)  maxtries=$OPTARG;;
    t)  delay=$OPTARG;;
    \?) ERRFLAG=BAD;;
    esac
done
shift `expr $OPTIND - 1`
#
# Exit with a usage message if the syntax is wrong.
#
if [ $ERRFLAG != OK -o $# -eq 0 ]
then
    echo 2>&1 "$0: usage:"
    echo 2>&1 "$usage"
    exit 1
fi

declare -i ntries

for lv in "$@"
do
    if lvs $lv >/dev/null
    then
        # We have a valid LVM logical volume, so repeatedly try to delete it.
        ntries=0
        while (( $ntries < $maxtries ))
        do
            ntries=$(( $ntries + 1 ))
            echo >&2 "$0: Removing $lv attempt $ntries"
            udevadm settle
            sync
            sleep $delay
            lvremove -f $lv && break
        done
        if lvs $lv >/dev/null 2>/dev/null
        then
            echo >&2  "$0: FAILED to remove $lv after $ntries attempts"
        else
            echo >&2  "$0: Successfully removed $lv after $ntries attempt(s)"
        fi
    else
         echo >&2  "$0: $lv is not an LVM logical volume"
    fi
done

Reply via email to