+
+if [ "$luksheaders" == "yes" ]; then
+ devices=`LC_ALL=C $SFDISK -l 2>/dev/null | grep "^Disk /dev" | @AWK@ '{print $2}' | cut -d: -f1`
+ [ -n "$devices" ] || warning "No block device found"
+ targetdevices=""
+ for dev in $devices; do
+ [ -b $dev ] || continue
+ debug "$CRYPTSETUP isLuks $dev"
+ $CRYPTSETUP isLuks $dev
+ [ $? -eq 0 ] && targetdevices="$targetdevices $dev"
+ done
+ for dev in $targetdevices; do
+ label=${dev#/dev/}
+ label=${label//\//-}
+ outputfile=${luksheadersfile//__star__/$label}
+ # the following sizes are expressed in terms of 512-byte sectors
+ debug "Let us find out the Luks header size for $dev"
+ debug "$CRYPTSETUP luksDump \"$dev\" | grep '^Payload offset:' | @AWK@ '{print $3}'"
+ headersize=`$CRYPTSETUP luksDump "$dev" | grep '^Payload offset:' | @AWK@ '{print $3}'`
+ if [ $? -ne 0 ]; then
+ warning "Could not compute the size of Luks header, skipping device $dev"
+ continue
+ elif [ -z "$headersize" -o -n "`echo \"$headersize\" | sed 's/[0-9]*//g'`" ]; then
+ warning "The computed size of Luks header is not an integer, skipping device $dev"
+ continue
+ fi
+ debug "Let us backup the Luks header of device $dev"
+ debug "$DD if=\"${dev}\" of=\"${outputfile}\" bs=512 count=\"${headersize}\""
+ output=`$DD if="${dev}" of="${outputfile}" bs=512 count="${headersize}" 2>&1`
+ exit_code=$?
+ if [ $exit_code -eq 0 ]; then
+ debug $output
+ info "The Luks header of $dev was saved to $outputfile."
+ else
+ debug $output
+ fatal "The Luks header of $dev could not be saved."
+ fi
+ done
+fi
+
+## 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