Blame SOURCES/s390-no-clobber-disks.patch

17042a
diff --git a/usr/share/rear/conf/default.conf b/usr/share/rear/conf/default.conf
17042a
index 23a83b71..0d13b487 100644
17042a
--- a/usr/share/rear/conf/default.conf
17042a
+++ b/usr/share/rear/conf/default.conf
17042a
@@ -416,6 +416,18 @@ test "$RECOVERY_UPDATE_URL" || RECOVERY_UPDATE_URL=""
17042a
 #   export MIGRATION_MODE='true'
17042a
 # directly before he calls "rear recover":
17042a
 test "$MIGRATION_MODE" || MIGRATION_MODE=''
17042a
+####
17042a
+
17042a
+####
17042a
+# Formatting DASDs (S/390 specific)
17042a
+# DASD (Direct Access Storage Device) denotes a disk drive on the S/390 architecture.
17042a
+# DASDs need to be formatted before use (even before creating a partition table on them).
17042a
+# By default ReaR will format the DASDs that are going to be used to recreate the system
17042a
+# (are referenced in disklayout.conf) before recreating the disk layout.
17042a
+# This can be suppressed by setting FORMAT_DASDS="false". It can be useful when one intends
17042a
+# to use already formatted DASDs as recovery target.
17042a
+FORMAT_DASDS=""
17042a
+####
17042a
 
17042a
 ##
17042a
 # Resizing partitions in MIGRATION_MODE during "rear recover"
17042a
diff --git a/usr/share/rear/layout/prep-for-mount/Linux-s390/205_s390_enable_disk.sh b/usr/share/rear/layout/prep-for-mount/Linux-s390/205_s390_enable_disk.sh
17042a
new file mode 120000
17042a
index 00000000..5f7a2ac0
17042a
--- /dev/null
17042a
+++ b/usr/share/rear/layout/prep-for-mount/Linux-s390/205_s390_enable_disk.sh
17042a
@@ -0,0 +1 @@
17042a
+../../prepare/Linux-s390/205_s390_enable_disk.sh
17042a
\ No newline at end of file
17042a
diff --git a/usr/share/rear/layout/prepare/GNU/Linux/100_include_partition_code.sh b/usr/share/rear/layout/prepare/GNU/Linux/100_include_partition_code.sh
17042a
index 13c69ce8..2a2bc33f 100644
17042a
--- a/usr/share/rear/layout/prepare/GNU/Linux/100_include_partition_code.sh
17042a
+++ b/usr/share/rear/layout/prepare/GNU/Linux/100_include_partition_code.sh
17042a
@@ -24,6 +24,7 @@ fi
17042a
 ### Prepare a disk for partitioning/general usage.
17042a
 create_disk() {
17042a
     local component disk size label junk
17042a
+    local blocksize layout dasdtype dasdcyls junk2
17042a
     read component disk size label junk < <(grep "^disk $1 " "$LAYOUT_FILE")
17042a
 
17042a
     ### Disks should be block devices.
17042a
@@ -67,7 +68,8 @@ sync
17042a
 
17042a
 EOF
17042a
 
17042a
-    create_partitions "$disk" "$label"
17042a
+    # $junk can contain useful DASD-specific fields
17042a
+    create_partitions "$disk" "$label" "$junk"
17042a
 
17042a
     cat >> "$LAYOUT_CODE" <
17042a
 # Make sure device nodes are visible (eg. in RHEL4)
17042a
@@ -93,6 +95,11 @@ create_partitions() {
17042a
     ### List partition types/names to detect disk label type.
17042a
     local -a names=()
17042a
     local part disk size pstart name junk
17042a
+    local orig_block_size layout dasdtype dasdcyls junk2
17042a
+    if [ "$label" == dasd ]; then
17042a
+        # dasd has more fields - junk is not junk anymore
17042a
+        read orig_block_size layout dasdtype dasdcyls junk2 <<<$3
17042a
+    fi
17042a
     while read part disk size pstart name junk ; do
17042a
         names+=( $name )
17042a
         case $name in
17042a
@@ -227,10 +234,12 @@ EOF
17042a
             if [[ "$end" ]] ; then
17042a
                 end=$( mathlib_calculate "$end - 1" )
17042a
             fi
17042a
-            if [[ "$ARCH" == "Linux-s390" ]] ; then
17042a
-                # if dasd disk is LDL formated, then do not partition it, because it is partitioned and can take only partition
17042a
-                if [[ ! "${listDasdLdl[@]}" =~ "$device" ]] ; then
17042a
-                    echo "not LDL dasd formated disk, create a partition"
17042a
+            if [[ "$ARCH" == "Linux-s390" && "$label" == dasd ]] ; then
17042a
+                # LDL formatted disks are already partitioned and should not be partitioned with parted or fdasd , it will fail
17042a
+                if [ "$layout" == LDL ]; then
17042a
+                    Debug "$device: LDL formatted DASD, do not create a partition"
17042a
+                else
17042a
+                    Debug "$device: ${layout} formatted DASD, create a partition"
17042a
                     cat >> "$LAYOUT_CODE" <
17042a
 create_disk_partition "$device" "$name" $number $start $end
17042a
 EOF
17042a
diff --git a/usr/share/rear/layout/prepare/Linux-s390/090_include_dasd_code.sh b/usr/share/rear/layout/prepare/Linux-s390/090_include_dasd_code.sh
17042a
new file mode 100644
17042a
index 00000000..fc5be463
17042a
--- /dev/null
17042a
+++ b/usr/share/rear/layout/prepare/Linux-s390/090_include_dasd_code.sh
17042a
@@ -0,0 +1,17 @@
17042a
+# Generate code for low-level formatting of a DASD
17042a
+
17042a
+dasd_format_code() {
17042a
+    local device size blocksize layout dasdtype dasdcyls
17042a
+
17042a
+    device="$1"
17042a
+    size="$2"
17042a
+    blocksize="$3"
17042a
+    layout="$4"
17042a
+    dasdtype="$5"
17042a
+    dasdcyls="$6"
17042a
+
17042a
+    has_binary dasdfmt || Error "Cannot find 'dasdfmt' command"
17042a
+
17042a
+    LogPrint 'dasdfmt:' $device ', blocksize:' $blocksize ', layout:' $layout
17042a
+    echo "dasdfmt -b $blocksize -d $layout -y $device"
17042a
+}
17042a
diff --git a/usr/share/rear/layout/prepare/Linux-s390/205_s390_enable_disk.sh b/usr/share/rear/layout/prepare/Linux-s390/205_s390_enable_disk.sh
17042a
index c4037e02..0f6946a9 100644
17042a
--- a/usr/share/rear/layout/prepare/Linux-s390/205_s390_enable_disk.sh
17042a
+++ b/usr/share/rear/layout/prepare/Linux-s390/205_s390_enable_disk.sh
17042a
@@ -2,45 +2,36 @@
17042a
 # Before we can compare or map DASD devices we must enable them.
17042a
 # This operation is only needed during "rear recover".
17042a
 
17042a
-format_s390_disk() {
17042a
-    LogPrint "run dasdfmt"
17042a
-    while read line ; do
17042a
-        LogPrint 'dasdfmt:' "$line"
17042a
-        # example format command: dasdfmt -b 4096 -d cdl -y /dev/dasda
17042a
-        # where
17042a
-        #  b is the block size
17042a
-        #  d is the layout: 
17042a
-        #   cdl - compatible disk layout (can be shared with zos and zvm apps)
17042a
-        #   ldl - linux disk layout
17042a
-        #  y - answer yes
17042a
-        device=$( echo $line | awk '{ print $7 }' )
17042a
-        blocksize=$( echo $line | awk '{ print $3 }' )
17042a
-        layout=$( echo $line | awk '{ print tolower($5) }' )
17042a
-        if [[ "$layout" == "ldl" ]] ; then
17042a
-            # listDasdLdl contains devices such as /dev/dasdb that are formatted as LDL
17042a
-            # LDL formatted disks are already partitioned and should not be partitioned with parted or fdasd , it will fail
17042a
-            # this var, listDasdLdl, is used by 100_include_partition_code.sh to exclude writing partition code to diskrestore.sh for LDL disks
17042a
-            listDasdLdl+=( $device )
17042a
-            LogPrint "LDL disk added to listDasdLdl:" ${listDasdLdl[@]}
17042a
-        fi
17042a
-        LogPrint 'dasdfmt:' $device ', blocksize:' $blocksize ', layout:' $layout
17042a
-        # dasd format
17042a
-        dasdfmt -b $blocksize -d $layout -y $device
17042a
-    done < <( grep "^dasdfmt " "$LAYOUT_FILE" )
17042a
-}
17042a
-
17042a
+DISK_MAPPING_HINTS=()
17042a
 
17042a
 enable_s390_disk() {
17042a
+    local keyword device bus len newname
17042a
+
17042a
     LogPrint "run chccwdev"
17042a
-    while read line ; do
17042a
-        LogPrint 'dasd channel:' "$line"
17042a
-        device=$( echo $line | awk '{ print $4 }' )
17042a
-        bus=$( echo $line | awk '{ print $2 }' )
17042a
-        channel=$( echo $line | awk '{ print $5 }' )
17042a
-        LogPrint 'chccwdev:' $device ', bus:' $bus ', channel:' $channel
17042a
-        # dasd channel enable
17042a
-        chccwdev -e $bus
17042a
-    done < <( grep "^dasd_channel " "$LAYOUT_FILE" )
17042a
+    while read len device bus ; do
17042a
+        # this while loop must be outside the pipeline so that variables propagate outside
17042a
+        # (pipelines run in subshells)
17042a
+        LogPrint "Enabling DASD $device with virtual device number $bus"
17042a
+        if chccwdev -e $bus ; then
17042a
+            newname=$(lsdasd $bus | awk "/$bus/ { print \$3}" )
17042a
+            if ! test $newname ; then
17042a
+                LogPrintError "New device with virtual device number $bus not found among online DASDs"
17042a
+                continue
17042a
+            fi
17042a
+            if [ "$newname" != "$device" ]; then
17042a
+                LogPrint "original DASD '$device' changed name to '$newname'"
17042a
+                test "$MIGRATION_MODE" || MIGRATION_MODE='true'
17042a
+            fi
17042a
+            DISK_MAPPING_HINTS+=( "/dev/$device /dev/$newname" )
17042a
+        else
17042a
+            LogPrintError "Failed to enable $bus"
17042a
+        fi
17042a
+    done < <( grep "^dasd_channel " "$LAYOUT_FILE" | while read keyword bus device; do
17042a
+                  # add device name length, so that "dasdb" sorts properly before "dasdaa"
17042a
+                  # we need to create devices in the same order as the kernel orders them (by minor number)
17042a
+                  # - this increases the chance that they will get identical names
17042a
+                  echo ${#device} $device $bus
17042a
+                  done | sort -k1n -k2 )
17042a
 }
17042a
 
17042a
 # May need to look at $OS_VENDOR also as DASD disk layout is distro specific:
17042a
@@ -49,7 +40,6 @@ case $OS_MASTER_VENDOR in
17042a
         # "Fedora" also handles Red Hat
17042a
         # "Debian" also handles Ubuntu
17042a
         enable_s390_disk
17042a
-        format_s390_disk
17042a
         ;;
17042a
     (*)
17042a
         LogPrintError "No code for DASD disk device enablement on $OS_MASTER_VENDOR"
17042a
diff --git a/usr/share/rear/layout/prepare/Linux-s390/360_generate_dasd_format_code.sh b/usr/share/rear/layout/prepare/Linux-s390/360_generate_dasd_format_code.sh
17042a
new file mode 100644
17042a
index 00000000..14bb942d
17042a
--- /dev/null
17042a
+++ b/usr/share/rear/layout/prepare/Linux-s390/360_generate_dasd_format_code.sh
17042a
@@ -0,0 +1,51 @@
17042a
+# DASD_FORMAT_CODE is the script to recreate the dasd formatting (dasdformat.sh).
17042a
+
17042a
+local component disk size label junk
17042a
+local blocksize layout dasdtype dasdcyls junk2
17042a
+
17042a
+
17042a
+save_original_file "$DASD_FORMAT_CODE"
17042a
+
17042a
+# Initialize
17042a
+
17042a
+echo '#!/bin/bash' >"$DASD_FORMAT_CODE"
17042a
+
17042a
+# Show the current output of lsdasd, it can be useful for identifying disks
17042a
+# (in particular it shows the Linux device name <-> virtual device number mapping,
17042a
+# formatted / unformatted status and the number/size of blocks when formatted )
17042a
+echo "# Current output of 'lsdasd':" >>"$DASD_FORMAT_CODE"
17042a
+lsdasd | sed -e 's/^/# /' >>"$DASD_FORMAT_CODE"
17042a
+
17042a
+cat <<EOF >>"$DASD_FORMAT_CODE"
17042a
+
17042a
+LogPrint "Start DASD format restoration."
17042a
+
17042a
+set -e
17042a
+set -x
17042a
+
17042a
+EOF
17042a
+
17042a
+while read component disk size label junk; do
17042a
+    if [ "$label" == dasd ]; then
17042a
+        # Ignore excluded components.
17042a
+        # Normally they are removed in 520_exclude_components.sh,
17042a
+        # but we run before it, so we must skip them here as well.
17042a
+        if IsInArray "$disk" "${EXCLUDE_RECREATE[@]}" ; then
17042a
+            Log "Excluding $disk from DASD reformatting."
17042a
+            continue
17042a
+        fi
17042a
+        # dasd has more fields - junk is not junk anymore
17042a
+        read blocksize layout dasdtype dasdcyls junk2 <<<$junk
17042a
+        dasd_format_code "$disk" "$size" "$blocksize" "$layout" "$dasdtype" "$dasdcyls" >> "$DASD_FORMAT_CODE" || \
17042a
+            LogPrintError "Error producing DASD format code for $disk"
17042a
+    fi
17042a
+done < <(grep "^disk " "$LAYOUT_FILE")
17042a
+
17042a
+cat <<EOF >>"$DASD_FORMAT_CODE"
17042a
+
17042a
+set +x
17042a
+set +e
17042a
+
17042a
+LogPrint "DASD(s) formatted."
17042a
+
17042a
+EOF
17042a
diff --git a/usr/share/rear/layout/prepare/Linux-s390/370_confirm_dasd_format_code.sh b/usr/share/rear/layout/prepare/Linux-s390/370_confirm_dasd_format_code.sh
17042a
new file mode 100644
17042a
index 00000000..5ba4edd5
17042a
--- /dev/null
17042a
+++ b/usr/share/rear/layout/prepare/Linux-s390/370_confirm_dasd_format_code.sh
17042a
@@ -0,0 +1,69 @@
17042a
+# adapted from 100_confirm_layout_code.sh
17042a
+#
17042a
+# Let the user confirm the
17042a
+# DASD format code (dasdformat.sh) script.
17042a
+#
17042a
+
17042a
+is_false "$FORMAT_DASDS" && return 0
17042a
+
17042a
+# Show the user confirmation dialog in any case but when not in migration mode
17042a
+# automatically proceed with less timeout USER_INPUT_INTERRUPT_TIMEOUT (by default 10 seconds)
17042a
+# to avoid longer delays (USER_INPUT_TIMEOUT is by default 300 seconds) in case of unattended recovery:
17042a
+# (taken from 120_confirm_wipedisk_disks.sh)
17042a
+local timeout="$USER_INPUT_TIMEOUT"
17042a
+is_true "$MIGRATION_MODE" || timeout="$USER_INPUT_INTERRUPT_TIMEOUT"
17042a
+
17042a
+rear_workflow="rear $WORKFLOW"
17042a
+original_disk_space_usage_file="$VAR_DIR/layout/config/df.txt"
17042a
+rear_shell_history="$( echo -e "cd $VAR_DIR/layout/\nvi $DASD_FORMAT_CODE\nless $DASD_FORMAT_CODE" )"
17042a
+unset choices
17042a
+choices[0]="Confirm DASD format script and continue '$rear_workflow'"
17042a
+choices[1]="Edit DASD format script ($DASD_FORMAT_CODE)"
17042a
+choices[2]="View DASD format script ($DASD_FORMAT_CODE)"
17042a
+choices[3]="View original disk space usage ($original_disk_space_usage_file)"
17042a
+choices[4]="Confirm what is currently on the DASDs, skip formatting them and continue '$rear_workflow'"
17042a
+choices[5]="Use Relax-and-Recover shell and return back to here"
17042a
+choices[6]="Abort '$rear_workflow'"
17042a
+prompt="Confirm or edit the DASD format script"
17042a
+choice=""
17042a
+wilful_input=""
17042a
+# When USER_INPUT_DASD_FORMAT_CODE_CONFIRMATION has any 'true' value be liberal in what you accept and
17042a
+# assume choices[0] 'Confirm DASD format' was actually meant:
17042a
+is_true "$USER_INPUT_DASD_FORMAT_CODE_CONFIRMATION" && USER_INPUT_DASD_FORMAT_CODE_CONFIRMATION="${choices[0]}"
17042a
+while true ; do
17042a
+    choice="$( UserInput -I DASD_FORMAT_CODE_CONFIRMATION -t "$timeout" -p "$prompt" -D "${choices[0]}" "${choices[@]}" )" && wilful_input="yes" || wilful_input="no"
17042a
+    case "$choice" in
17042a
+        (${choices[0]})
17042a
+            # Confirm DASD format file and continue:
17042a
+            is_true "$wilful_input" && LogPrint "User confirmed DASD format script" || LogPrint "Continuing '$rear_workflow' by default"
17042a
+            break
17042a
+            ;;
17042a
+        (${choices[1]})
17042a
+            # Run 'vi' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user:
17042a
+            vi $DASD_FORMAT_CODE 0<&6 1>&7 2>&8
17042a
+            ;;
17042a
+        (${choices[2]})
17042a
+            # Run 'less' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user:
17042a
+            less $DASD_FORMAT_CODE 0<&6 1>&7 2>&8
17042a
+            ;;
17042a
+        (${choices[3]})
17042a
+            # Run 'less' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user:
17042a
+            less $original_disk_space_usage_file 0<&6 1>&7 2>&8
17042a
+            ;;
17042a
+        (${choices[4]})
17042a
+            # Confirm what is on the disks and continue without formatting
17042a
+            FORMAT_DASDS="false"
17042a
+            ;;
17042a
+        (${choices[5]})
17042a
+            # rear_shell runs 'bash' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user:
17042a
+            rear_shell "" "$rear_shell_history"
17042a
+            ;;
17042a
+        (${choices[6]})
17042a
+            abort_dasd_format
17042a
+            Error "User chose to abort '$rear_workflow' in ${BASH_SOURCE[0]}"
17042a
+            ;;
17042a
+    esac
17042a
+done
17042a
+
17042a
+chmod +x $DASD_FORMAT_CODE
17042a
+
17042a
diff --git a/usr/share/rear/layout/prepare/Linux-s390/400_run_dasd_format_code.sh b/usr/share/rear/layout/prepare/Linux-s390/400_run_dasd_format_code.sh
17042a
new file mode 100644
17042a
index 00000000..16451af6
17042a
--- /dev/null
17042a
+++ b/usr/share/rear/layout/prepare/Linux-s390/400_run_dasd_format_code.sh
17042a
@@ -0,0 +1,185 @@
17042a
+# adapted from 200_run_layout_code.sh
17042a
+#
17042a
+# Run the DASD format code (dasdformat.sh)
17042a
+# again and again until it succeeds or the user aborts.
17042a
+#
17042a
+
17042a
+# Skip DASD formatting when the user has explicitly specified to not format them
17042a
+# or when the user selected "Confirm what is currently on the DASDs, skip formatting them"
17042a
+# in 370_confirm_dasd_format_code.sh
17042a
+
17042a
+is_false "$FORMAT_DASDS" && return 0
17042a
+
17042a
+function lsdasd_output () {
17042a
+    lsdasd 1>> >( tee -a "$RUNTIME_LOGFILE" 1>&7 )
17042a
+}
17042a
+
17042a
+rear_workflow="rear $WORKFLOW"
17042a
+original_disk_space_usage_file="$VAR_DIR/layout/config/df.txt"
17042a
+rear_shell_history="$( echo -e "cd $VAR_DIR/layout/\nvi $DASD_FORMAT_CODE\nless $RUNTIME_LOGFILE" )"
17042a
+wilful_input=""
17042a
+
17042a
+unset choices
17042a
+choices[0]="Rerun DASD format script ($DASD_FORMAT_CODE)"
17042a
+choices[1]="View '$rear_workflow' log file ($RUNTIME_LOGFILE)"
17042a
+choices[2]="Edit DASD format script ($DASD_FORMAT_CODE)"
17042a
+choices[3]="Show what is currently on the disks ('lsdasd' device list)"
17042a
+choices[4]="View original disk space usage ($original_disk_space_usage_file)"
17042a
+choices[5]="Use Relax-and-Recover shell and return back to here"
17042a
+choices[6]="Confirm what is currently on the disks and continue '$rear_workflow'"
17042a
+choices[7]="Abort '$rear_workflow'"
17042a
+prompt="DASD format choices"
17042a
+
17042a
+choice=""
17042a
+# When USER_INPUT_DASD_FORMAT_CODE_RUN has any 'true' value be liberal in what you accept and
17042a
+# assume choices[0] 'Rerun DASD format script' was actually meant
17042a
+# regardless that this likely lets 'rear recover' run an endless loop
17042a
+# of failed DASD format attempts but ReaR must obey what the user specified
17042a
+# (perhaps it is intended to let 'rear recover' loop here until an admin intervenes):
17042a
+is_true "$USER_INPUT_DASD_FORMAT_CODE_RUN" && USER_INPUT_DASD_FORMAT_CODE_RUN="${choices[0]}"
17042a
+
17042a
+unset confirm_choices
17042a
+confirm_choices[0]="Confirm recreated DASD format and continue '$rear_workflow'"
17042a
+confirm_choices[1]="Go back one step to redo DASD format"
17042a
+confirm_choices[2]="Use Relax-and-Recover shell and return back to here"
17042a
+confirm_choices[3]="Abort '$rear_workflow'"
17042a
+confirm_prompt="Confirm the recreated DASD format or go back one step"
17042a
+confirm_choice=""
17042a
+# When USER_INPUT_DASD_FORMAT_MIGRATED_CONFIRMATION has any 'true' value be liberal in what you accept and
17042a
+# assume confirm_choices[0] 'Confirm recreated DASD format and continue' was actually meant:
17042a
+is_true "$USER_INPUT_DASD_FORMAT_MIGRATED_CONFIRMATION" && USER_INPUT_DASD_FORMAT_MIGRATED_CONFIRMATION="${confirm_choices[0]}"
17042a
+
17042a
+# Run the DASD format code (dasdformat.sh)
17042a
+# again and again until it succeeds or the user aborts
17042a
+# or the user confirms to continue with what is currently on the disks
17042a
+# (the user may have setup manually what he needs via the Relax-and-Recover shell):
17042a
+while true ; do
17042a
+    prompt="The DASD format had failed"
17042a
+    # After switching to recreating with DASD format script
17042a
+    # change choices[0] from "Run ..." to "Rerun ...":
17042a
+    choices[0]="Rerun DASD format script ($DASD_FORMAT_CODE)"
17042a
+    # Run DASD_FORMAT_CODE in a sub-shell because it sets 'set -e'
17042a
+    # so that it exits the running shell in case of an error
17042a
+    # but that exit must not exit this running bash here:
17042a
+    ( source $DASD_FORMAT_CODE )
17042a
+    # One must explicitly test whether or not $? is zero in a separated bash command
17042a
+    # because with bash 3.x and bash 4.x code like
17042a
+    #   # ( set -e ; cat qqq ; echo "hello" ) && echo ok || echo failed
17042a
+    #   cat: qqq: No such file or directory
17042a
+    #   hello
17042a
+    #   ok
17042a
+    # does not work as one may expect (cf. what "man bash" describes for 'set -e').
17042a
+    # There is a subtle behavioural difference between bash 3.x and bash 4.x
17042a
+    # when a script that has 'set -e' set gets sourced:
17042a
+    # With bash 3.x the 'set -e' inside the sourced script is effective:
17042a
+    #   # echo 'set -e ; cat qqq ; echo hello' >script.sh
17042a
+    #   # ( source script.sh ) && echo ok || echo failed
17042a
+    #   cat: qqq: No such file or directory
17042a
+    #   failed
17042a
+    # With bash 4.x the 'set -e' inside the sourced script gets noneffective:
17042a
+    #   # echo 'set -e ; cat qqq ; echo hello' >script.sh
17042a
+    #   # ( source script.sh ) && echo ok || echo failed
17042a
+    #   cat: qqq: No such file or directory
17042a
+    #   hello
17042a
+    #   ok
17042a
+    # With bash 3.x and bash 4.x testing $? in a separated bash command
17042a
+    # keeps the 'set -e' inside the sourced script effective:
17042a
+    #   # echo 'set -e ; cat qqq ; echo hello' >script.sh
17042a
+    #   # ( source script.sh ) ; (( $? == 0 )) && echo ok || echo failed
17042a
+    #   cat: qqq: No such file or directory
17042a
+    #   failed
17042a
+    # See also https://github.com/rear/rear/pull/1573#issuecomment-344303590
17042a
+    if (( $? == 0 )) ; then
17042a
+        prompt="DASD format had been successful"
17042a
+        # When DASD_FORMAT_CODE succeeded and when not in migration mode
17042a
+        # break the outer while loop and continue the "rear recover" workflow
17042a
+        # which means continue with restoring the backup:
17042a
+        is_true "$MIGRATION_MODE" || break
17042a
+        # When DASD_FORMAT_CODE succeeded in migration mode
17042a
+        # let the user explicitly confirm the recreated (and usually migrated) format
17042a
+        # before continuing the "rear recover" workflow with restoring the backup.
17042a
+        # Show the recreated DASD format to the user on his terminal (and also in the log file):
17042a
+        LogPrint "Recreated DASD format:"
17042a
+        lsdasd_output
17042a
+        # Run an inner while loop with a user dialog so that the user can inspect the recreated DASD format
17042a
+        # and perhaps even manually fix the recreated DASD format if it is not what the user wants
17042a
+        # (e.g. by using the Relax-and-Recover shell and returning back to this user dialog):
17042a
+        while true ; do
17042a
+            confirm_choice="$( UserInput -I DASD_FORMAT_MIGRATED_CONFIRMATION -p "$confirm_prompt" -D "${confirm_choices[0]}" "${confirm_choices[@]}" )" && wilful_input="yes" || wilful_input="no"
17042a
+            case "$confirm_choice" in
17042a
+                (${confirm_choices[0]})
17042a
+                    # Confirm recreated DASD format and continue:
17042a
+                    is_true "$wilful_input" && LogPrint "User confirmed recreated DASD format" || LogPrint "Continuing with recreated DASD format by default"
17042a
+                    # Break the outer while loop and continue with restoring the backup:
17042a
+                    break 2
17042a
+                    ;;
17042a
+                (${confirm_choices[1]})
17042a
+                    # Go back one step to redo DASD format:
17042a
+                    # Only break the inner while loop (i.e. this user dialog loop)
17042a
+                    # and  continue with the next user dialog below:
17042a
+                    break
17042a
+                    ;;
17042a
+                (${confirm_choices[2]})
17042a
+                    # rear_shell runs 'bash' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user:
17042a
+                    rear_shell "" "$rear_shell_history"
17042a
+                    ;;
17042a
+                (${confirm_choices[3]})
17042a
+                    abort_dasd_format
17042a
+                    Error "User did not confirm the recreated DASD format but aborted '$rear_workflow' in ${BASH_SOURCE[0]}"
17042a
+                    ;;
17042a
+            esac
17042a
+        done
17042a
+    fi
17042a
+    # Run an inner while loop with a user dialog so that the user can fix things
17042a
+    # when DASD_FORMAT_CODE failed.
17042a
+    # Such a fix does not necessarily mean the user must change
17042a
+    # the dasdformat.sh script when DASD_FORMAT_CODE failed.
17042a
+    # The user might also fix things by only using the Relax-and-Recover shell and
17042a
+    # then confirm what is on the disks and continue with restoring the backup
17042a
+    # or abort this "rear recover" run to re-try from scratch.
17042a
+    while true ; do
17042a
+        choice="$( UserInput -I DASD_FORMAT_CODE_RUN -p "$prompt" -D "${choices[0]}" "${choices[@]}" )" && wilful_input="yes" || wilful_input="no"
17042a
+        case "$choice" in
17042a
+            (${choices[0]})
17042a
+                # Rerun or run (after switching to recreating with DASD format script) DASD format script:
17042a
+                is_true "$wilful_input" && LogPrint "User runs DASD format script" || LogPrint "Running DASD format script by default"
17042a
+                # Only break the inner while loop (i.e. the user dialog loop):
17042a
+                break
17042a
+                ;;
17042a
+            (${choices[1]})
17042a
+                # Run 'less' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user:
17042a
+                less $RUNTIME_LOGFILE 0<&6 1>&7 2>&8
17042a
+                ;;
17042a
+            (${choices[2]})
17042a
+                # Run 'vi' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user:
17042a
+                vi $DASD_FORMAT_CODE 0<&6 1>&7 2>&8
17042a
+                ;;
17042a
+            (${choices[3]})
17042a
+                LogPrint "This is the current list of DASDs:"
17042a
+                lsdasd_output
17042a
+                ;;
17042a
+            (${choices[4]})
17042a
+                # Run 'less' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user:
17042a
+                less $original_disk_space_usage_file 0<&6 1>&7 2>&8
17042a
+                ;;
17042a
+            (${choices[5]})
17042a
+                # rear_shell runs 'bash' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user:
17042a
+                rear_shell "" "$rear_shell_history"
17042a
+                ;;
17042a
+            (${choices[6]})
17042a
+                # Confirm what is on the disks and continue:
17042a
+                # Break the outer while loop and continue with restoring the backup:
17042a
+                break 2
17042a
+                ;;
17042a
+            (${choices[7]})
17042a
+                abort_dasd_format
17042a
+                Error "User chose to abort '$rear_workflow' in ${BASH_SOURCE[0]}"
17042a
+                ;;
17042a
+        esac
17042a
+    done
17042a
+# End of the outer while loop:
17042a
+done
17042a
+
17042a
+# Local functions must be 'unset' because bash does not support 'local function ...'
17042a
+# cf. https://unix.stackexchange.com/questions/104755/how-can-i-create-a-local-function-in-my-bashrc
17042a
+unset -f lsdasd_output
17042a
diff --git a/usr/share/rear/layout/prepare/default/010_prepare_files.sh b/usr/share/rear/layout/prepare/default/010_prepare_files.sh
17042a
index 7a980e63..4191be33 100644
17042a
--- a/usr/share/rear/layout/prepare/default/010_prepare_files.sh
17042a
+++ b/usr/share/rear/layout/prepare/default/010_prepare_files.sh
17042a
@@ -7,6 +7,8 @@ LAYOUT_CODE="$VAR_DIR/layout/diskrestore.sh"
17042a
 LAYOUT_XFS_OPT_DIR="$VAR_DIR/layout/xfs"
17042a
 LAYOUT_XFS_OPT_DIR_RESTORE="$LAYOUT_XFS_OPT_DIR/restore"
17042a
 
17042a
+DASD_FORMAT_CODE="$VAR_DIR/layout/dasdformat.sh"
17042a
+
17042a
 FS_UUID_MAP="$VAR_DIR/layout/fs_uuid_mapping"
17042a
 LUN_WWID_MAP="$VAR_DIR/layout/lun_wwid_mapping"
17042a
 
17042a
diff --git a/usr/share/rear/layout/prepare/default/250_compare_disks.sh b/usr/share/rear/layout/prepare/default/250_compare_disks.sh
17042a
index c459b928..751433ba 100644
17042a
--- a/usr/share/rear/layout/prepare/default/250_compare_disks.sh
17042a
+++ b/usr/share/rear/layout/prepare/default/250_compare_disks.sh
17042a
@@ -54,7 +54,9 @@ local more_than_one_same_orig_size=''
17042a
 # Cf. the "Compare disks one by one" code below:
17042a
 while read disk dev size junk ; do
17042a
     if IsInArray "$size" "${original_system_used_disk_sizes[@]}" ; then
17042a
-        more_than_one_same_orig_size='true'
17042a
+        if ! has_mapping_hint "$dev" ; then
17042a
+            more_than_one_same_orig_size='true'
17042a
+        fi
17042a
     else
17042a
         original_system_used_disk_sizes+=( "$size" )
17042a
     fi
17042a
@@ -109,14 +111,17 @@ fi
17042a
 # No further disk comparisons are needed when MIGRATION_MODE is already set true above:
17042a
 if ! is_true "$MIGRATION_MODE" ; then
17042a
     # Compare original disks and their possible target disk one by one:
17042a
-    while read disk dev size junk ; do
17042a
-        dev=$( get_sysfs_name $dev )
17042a
+    while read disk devnode size junk ; do
17042a
+        dev=$( get_sysfs_name $devnode )
17042a
         Log "Comparing $dev"
17042a
         if test -e "/sys/block/$dev" ; then
17042a
             Log "Device /sys/block/$dev exists"
17042a
             newsize=$( get_disk_size $dev )
17042a
             if test "$newsize" -eq "$size" ; then
17042a
                 LogPrint "Device $dev has expected (same) size $size bytes (will be used for '$WORKFLOW')"
17042a
+            elif test "$( get_mapping_hint $devnode )" == "$devnode" ; then
17042a
+                Debug "Found identical mapping hint ${devnode} -> ${devnode}"
17042a
+                LogPrint "Device $dev found according to mapping hints (will be used for '$WORKFLOW')"
17042a
             else
17042a
                 LogPrint "Device $dev has size $newsize bytes but $size bytes is expected (needs manual configuration)"
17042a
                 MIGRATION_MODE='true'
17042a
diff --git a/usr/share/rear/layout/prepare/default/300_map_disks.sh b/usr/share/rear/layout/prepare/default/300_map_disks.sh
17042a
index 2e90768c..468aa35c 100644
17042a
--- a/usr/share/rear/layout/prepare/default/300_map_disks.sh
17042a
+++ b/usr/share/rear/layout/prepare/default/300_map_disks.sh
17042a
@@ -112,7 +112,14 @@ while read keyword orig_device orig_size junk ; do
17042a
     # Continue with next original device when it is already used as source in the mapping file:
17042a
     is_mapping_source "$orig_device" && continue
17042a
     # First, try to find if there is a current disk with same name and same size as the original:
17042a
-    sysfs_device_name="$( get_sysfs_name "$orig_device" )"
17042a
+    # (possibly influenced by mapping hints if known)
17042a
+    if has_mapping_hint "$orig_device" ; then
17042a
+        candidate_target_device_name="$( get_mapping_hint "$orig_device" )"
17042a
+        Debug "Using mapping hint ${candidate_target_device_name} as candidate for $orig_device mapping"
17042a
+    else
17042a
+        candidate_target_device_name="$orig_device"
17042a
+    fi
17042a
+    sysfs_device_name="$( get_sysfs_name "$candidate_target_device_name" )"
17042a
     current_device="/sys/block/$sysfs_device_name"
17042a
     if test -e $current_device ; then
17042a
         current_size=$( get_disk_size $sysfs_device_name )
17042a
@@ -122,11 +129,16 @@ while read keyword orig_device orig_size junk ; do
17042a
         # Continue with next one if the current one is already used as target in the mapping file:
17042a
         is_mapping_target "$preferred_target_device_name" && continue
17042a
         # Use the current one if it is of same size as the old one:
17042a
-        if test "$orig_size" -eq "$current_size" ; then
17042a
+        if has_mapping_hint "$orig_device" || test "$orig_size" -eq "$current_size" ; then
17042a
             # Ensure the determined target device is really a block device:
17042a
             if test -b "$preferred_target_device_name" ; then
17042a
+                if has_mapping_hint "$orig_device" ; then
17042a
+                    mapping_reason="determined by mapping hint"
17042a
+                else
17042a
+                    mapping_reason="same name and same size $current_size"
17042a
+                fi
17042a
                 add_mapping "$orig_device" "$preferred_target_device_name"
17042a
-                LogPrint "Using $preferred_target_device_name (same name and same size) for recreating $orig_device"
17042a
+                LogPrint "Using $preferred_target_device_name ($mapping_reason) for recreating $orig_device"
17042a
                 # Continue with next original device in the LAYOUT_FILE:
17042a
                 continue
17042a
             fi
17042a
diff --git a/usr/share/rear/layout/save/GNU/Linux/200_partition_layout.sh b/usr/share/rear/layout/save/GNU/Linux/200_partition_layout.sh
17042a
index 3ab7357d..da6ce64c 100644
17042a
--- a/usr/share/rear/layout/save/GNU/Linux/200_partition_layout.sh
17042a
+++ b/usr/share/rear/layout/save/GNU/Linux/200_partition_layout.sh
17042a
@@ -362,18 +362,27 @@ Log "Saving disk partitions."
17042a
 
17042a
             if [[ $blockd == dasd* && "$ARCH" == "Linux-s390" ]] ; then
17042a
                 devname=$(get_device_name $disk)
17042a
+                dasdnum=$( lsdasd | awk "\$3 == \"$blockd\" { print \$1}" )
17042a
+                dasdstatus=$( lsdasd | awk "\$3 == \"$blockd\" { print \$2}" )
17042a
+                # ECKD or FBA
17042a
+                dasdtype=$( lsdasd | awk "\$3 == \"$blockd\" { print \$5}" )
17042a
+                if [ "$dasdtype" != ECKD ] && [ "$dasdtype" != FBA ]; then
17042a
+                    LogPrint "Type $dasdtype of DASD $blockd unexpected: neither ECKD nor FBA"
17042a
+                fi
17042a
 
17042a
-                echo "# active dasd bus and channel"
17042a
-                echo "# bus-id <name device> type"
17042a
-                echo "dasd_channel $( lsdasd|grep $blockd|awk '{ print $1 " "  $2 " "  $3 " "  $4}' )"
17042a
-
17042a
-                echo "# dasdfmt - disk layout is either cdl for the compatible disk layout (default) or ldl"
17042a
-                echo "#  example usage: dasdfmt -b 4096 -d cdl -y /dev/dasda"
17042a
-                layout=$(dasdview -x  /dev/$blockd|grep "^format"|awk '{print $7}')
17042a
-                blocksize=$( dasdview -i  /dev/$blockd|grep blocksize|awk '{print $6}' )
17042a
-                echo "# dasdfmt $devname"
17042a
-                echo "# dasdfmt -b <blocksize> -d <layout> -y <devname>"
17042a
-                echo "dasdfmt -b $blocksize -d $layout -y $devname"
17042a
+                echo "# every DASD bus and channel"
17042a
+                echo "# Format: dasd_channel <bus-id> <device name>"
17042a
+                echo "dasd_channel $dasdnum $blockd"
17042a
+
17042a
+                # We need to print the dasd_channel line even for ignored devices,
17042a
+                # otherwise we could have naming gaps and naming would change when
17042a
+                # recreating layout.
17042a
+                # E.g. if dasda is ignored, and dasdb is not, we would create only dasdb
17042a
+                # during recreation, but it would be named dasda.
17042a
+                if [ "$dasdstatus" != active ]; then
17042a
+                    Log "Ignoring $blockd: it is not active (Status is $dasdstatus)"
17042a
+                    continue
17042a
+                fi
17042a
             fi
17042a
 
17042a
             #FIXME: exclude *rpmb (Replay Protected Memory Block) for nvme*, mmcblk* and uas
17042a
@@ -387,11 +396,38 @@ Log "Saving disk partitions."
17042a
                 devname=$(get_device_name $disk)
17042a
                 devsize=$(get_disk_size ${disk#/sys/block/})
17042a
                 disktype=$(parted -s $devname print | grep -E "Partition Table|Disk label" | cut -d ":" -f "2" | tr -d " ")
17042a
-
17042a
-                echo "# Disk $devname"
17042a
-                echo "# Format: disk <devname> <size(bytes)> <partition label type>"
17042a
-                echo "disk $devname $devsize $disktype"
17042a
-
17042a
+                if [ "$disktype" != "dasd" ]; then
17042a
+                    echo "# Disk $devname"
17042a
+                    echo "# Format: disk <devname> <size(bytes)> <partition label type>"
17042a
+                    echo "disk $devname $devsize $disktype"
17042a
+                elif [[ $blockd == dasd* && "$ARCH" == "Linux-s390" ]] ; then
17042a
+                    layout=$(dasdview -x $devname |grep "^format"|awk '{print $7}')
17042a
+                    case "$layout" in
17042a
+                        (NOT)
17042a
+                            # NOT -> dasdview has printed "NOT formatted"
17042a
+                            LogPrintError "Ignoring $blockd: it is not formatted"
17042a
+                            continue
17042a
+                            ;;
17042a
+                        (LDL|CDL)
17042a
+                            ;;
17042a
+                        (*)
17042a
+                            BugError "Invalid 'disk $devname' entry (unknown DASD layout $layout)"
17042a
+                            ;;
17042a
+                    esac
17042a
+                    test $disktype || Error "No partition label type for DASD entry 'disk $devname'"
17042a
+                    blocksize=$( get_block_size "$blockd" )
17042a
+                    if ! test $blocksize ; then
17042a
+                        # fallback - ugly method
17042a
+                        blocksize=$( dasdview -i $devname |grep blocksize|awk '{print $6}' )
17042a
+                        test $blocksize || Error "Unknown block size of DASD $devname"
17042a
+                    fi
17042a
+                    dasdcyls=$( get_dasd_cylinders "$blockd" )
17042a
+                    echo "# Disk $devname"
17042a
+                    echo "# Format: disk <devname> <size(bytes)> <partition label type> <logical block size> <DASD layout> <DASD type> <size(cylinders)>"
17042a
+                    echo "disk $devname $devsize $disktype $blocksize $layout $dasdtype $dasdcyls"
17042a
+                else
17042a
+                    Error "Invalid 'disk $devname' entry (DASD partition label on non-s390 arch $ARCH)"
17042a
+                fi
17042a
                 echo "# Partitions on $devname"
17042a
                 echo "# Format: part <device> <partition size(bytes)> <partition start(bytes)> <partition type|name> <flags> /dev/<partition>"
17042a
                 extract_partitions "$devname"
17042a
diff --git a/usr/share/rear/lib/layout-functions.sh b/usr/share/rear/lib/layout-functions.sh
17042a
index 91c5ff73..4f5b8f6f 100644
17042a
--- a/usr/share/rear/lib/layout-functions.sh
17042a
+++ b/usr/share/rear/lib/layout-functions.sh
17042a
@@ -93,6 +93,12 @@ abort_recreate() {
17042a
     restore_original_file "$LAYOUT_FILE"
17042a
 }
17042a
 
17042a
+abort_dasd_format() {
17042a
+    Log "Error detected during DASD formatting."
17042a
+    Log "Restoring saved original $DASD_FORMAT_FILE"
17042a
+    restore_original_file "$DASD_FORMAT_FILE"
17042a
+}
17042a
+
17042a
 # Test and log if a component $1 (type $2) needs to be recreated.
17042a
 create_component() {
17042a
     local device="$1"
17042a
@@ -722,6 +728,46 @@ get_block_size() {
17042a
     fi
17042a
 }
17042a
 
17042a
+# Get the number of cylinders of a DASD.
17042a
+# The number of cylinders has the advantage of being fixed - size depends on formatting
17042a
+# and number of cylinders is valid even for unformatted DASDs, size is not.
17042a
+get_dasd_cylinders() {
17042a
+    local disk_name="${1##*/}" # /some/path/dasda -> dasda
17042a
+    local dasd_cyls
17042a
+
17042a
+    dasd_cyls=$(dasdview -i /dev/$disk_name | grep cylinders | cut -d ':' -f2 | awk '{print $4}')
17042a
+    ### Make sure we always return a number
17042a
+    echo $(( dasd_cyls ))
17042a
+}
17042a
+
17042a
+# Sometimes we know what the new device for the original device should be in a more reliable way
17042a
+# than by looking at disk sizes. THis information is called "mapping hints". Let's pass them
17042a
+# to the mapping code using the DISK_MAPPING_HINTS array. Each element of the array has the form
17042a
+# "/dev/source /dev/target" (space-separated).
17042a
+
17042a
+# Output the mapping hint for the original device.
17042a
+function get_mapping_hint () {
17042a
+    local device="$1"
17042a
+    local hint mapping_hint_source mapping_hint_target
17042a
+
17042a
+    for hint in "${DISK_MAPPING_HINTS[@]}"; do
17042a
+        mapping_hint_source=${hint%% *}
17042a
+        mapping_hint_target=${hint##* }
17042a
+        if [ "${device}" == "${mapping_hint_source}" ] ; then
17042a
+            echo "$mapping_hint_target"
17042a
+            return 0
17042a
+        fi
17042a
+    done
17042a
+    return 1
17042a
+}
17042a
+
17042a
+# Determine if there is a mapping hint for the original device.
17042a
+function has_mapping_hint () {
17042a
+    local device="$1"
17042a
+
17042a
+    get_mapping_hint "$device" > /dev/null
17042a
+}
17042a
+
17042a
 # Get the UUID of a device.
17042a
 # Device is something like /dev/sda1.
17042a
 blkid_uuid_of_device() {