snapfsck.sh
changeset 8 b917ead1b6ef
parent 7 bd12aa54868e
child 9 a2875a6fd7b2
--- a/snapfsck.sh	Fri May 04 13:10:26 2012 +0200
+++ b/snapfsck.sh	Fri May 04 15:37:10 2012 +0200
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 
 set -e
 
@@ -32,6 +32,17 @@
 
 }
 
+cleanup() {
+
+    # if you have problems removing volumes then you may have the 'udisks'
+    # package installed; see:
+    # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=549691#74
+    # https://bugzilla.redhat.com/show_bug.cgi?id=577798#c5
+    # https://www.redhat.com/archives/linux-lvm/2010-August/msg00029.html
+    lvs "$vg/$lvs" >/dev/null 2>&1 && lvremove -f "$vg/$lvs"
+
+}
+
 while getopts "s:" opt; do
   case $opt in
     s)
@@ -50,21 +61,14 @@
 lv=${p##*/}
 lvs="snap.${lv}"
 
+trap cleanup EXIT
+
 [ -n "${vg}" -a -n "${lv}" -a "${vg}" != "${p}" -a "${lv}" != "${p}" ] || usage
 
 d=$(date +%Y%m%d%H%M%S)
 lvcreate -n"$lvs" -L"$SNAPSIZE" -s "$vg/$lv"
 if e2fsck -fp "/dev/$vg/$lvs"; then
     tune2fs -C 1 -T "$d" "/dev/$vg/$lv"
-    waittime=1 # second
-    tries=60
-    i=1
-    # if you have problems removing volumes then you may have the 'udisks'
-    # package installed; see:
-    # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=549691#74
-    # https://bugzilla.redhat.com/show_bug.cgi?id=577798#c5
-    # https://www.redhat.com/archives/linux-lvm/2010-August/msg00029.html
-    lvremove -f "$vg/$lvs"
 else
-    die fsck returned nonzero: this may indicate filesystem corruption on the origin volume. The snapshot has not been removed.
+    die fsck returned nonzero: this may indicate filesystem corruption on the origin volume.
 fi