+
+## LVM ####################################
+
+# returns 0 on success, 1 on error, 2 if not tried
+# outputs error message if error, reason if not tried
+function doLvmBackup () {
+ local lvmdir="$1"
+ if [ ! -d "$lvmdir" ]; then
+ if ! mkdir "$lvmdir"; then
+ echo "could not create $lvmdir"
+ return 2
+ else
+ info "successfully created $lvmdir"
+ fi
+ fi
+ if [ ! -w "$lvmdir" ]; then
+ echo "can not write to directory $lvmdir"
+ return 2
+ fi
+ debug "Let's try to gather the list of LVM volume groups"
+ debug "$VGS --options vg_name --noheadings | @SED@ 's/^[ ]*//' | @SED@ 's/[ ]*$//' | tr '\n' ' '"
+ vgs=`$VGS --options vg_name --noheadings | @SED@ 's/^[ ]*//' | @SED@ 's/[ ]*$//' | tr '\n' ' '`
+ debug "Let's try to backup LVM metadata for detected volume groups: $vgs"
+ debug "$VGCFGBACKUP --file \"${lvmdir}\"/\'%s\' $vgs"
+ output=`$VGCFGBACKUP --file "${lvmdir}"/'%s' $vgs`
+ exit_code=$?
+ debug $output
+ case $exit_code in
+ 0)
+ info "LVM metadata was saved to $lvmdir for volume groups: $vgs"
+ return 0
+ ;;
+ *)
+ echo "LVM metadata could not be saved for at least one of these volume groups: $vgs"
+ return 1
+ ;;
+ esac
+}
+
+if [ "$lvm" == "yes" ]; then
+ output=`doLvmBackup "${parentdir}/lvm"`
+ exit_code=$?
+ case $exit_code in
+ 0) # success. info message has already been displayed
+ true
+ ;;
+ 1) # error
+ fatal "$output"
+ ;;
+ 2) # could not even try
+ fatal "LVM metadata backup was not tried: $output"
+ ;;
+ *) # should never happen
+ fatal "Unhandled error ($exit_code) while trying to backup LVM metadata, please report a bug"
+ ;;
+ esac
+fi