| 1 | # adapted from 200_run_layout_code.sh |
| 2 | # |
| 3 | # Run the DASD format code (dasdformat.sh) |
| 4 | # again and again until it succeeds or the user aborts. |
| 5 | # |
| 6 | |
| 7 | # Skip DASD formatting when the user has explicitly specified to not format them |
| 8 | # or when the user selected "Confirm what is currently on the DASDs, skip formatting them" |
| 9 | # in 370_confirm_dasd_format_code.sh |
| 10 | |
| 11 | is_false "$FORMAT_DASDS" && return 0 |
| 12 | |
| 13 | function lsdasd_output () { |
| 14 | lsdasd 1>> >( tee -a "$RUNTIME_LOGFILE" 1>&7 ) |
| 15 | } |
| 16 | |
| 17 | rear_workflow="rear $WORKFLOW" |
| 18 | original_disk_space_usage_file="$VAR_DIR/layout/config/df.txt" |
| 19 | rear_shell_history="$( echo -e "cd $VAR_DIR/layout/\nvi $DASD_FORMAT_CODE\nless $RUNTIME_LOGFILE" )" |
| 20 | wilful_input="" |
| 21 | |
| 22 | unset choices |
| 23 | choices[0]="Rerun DASD format script ($DASD_FORMAT_CODE)" |
| 24 | choices[1]="View '$rear_workflow' log file ($RUNTIME_LOGFILE)" |
| 25 | choices[2]="Edit DASD format script ($DASD_FORMAT_CODE)" |
| 26 | choices[3]="Show what is currently on the disks ('lsdasd' device list)" |
| 27 | choices[4]="View original disk space usage ($original_disk_space_usage_file)" |
| 28 | choices[5]="Use Relax-and-Recover shell and return back to here" |
| 29 | choices[6]="Confirm what is currently on the disks and continue '$rear_workflow'" |
| 30 | choices[7]="Abort '$rear_workflow'" |
| 31 | prompt="DASD format choices" |
| 32 | |
| 33 | choice="" |
| 34 | # When USER_INPUT_DASD_FORMAT_CODE_RUN has any 'true' value be liberal in what you accept and |
| 35 | # assume choices[0] 'Rerun DASD format script' was actually meant |
| 36 | # regardless that this likely lets 'rear recover' run an endless loop |
| 37 | # of failed DASD format attempts but ReaR must obey what the user specified |
| 38 | # (perhaps it is intended to let 'rear recover' loop here until an admin intervenes): |
| 39 | is_true "$USER_INPUT_DASD_FORMAT_CODE_RUN" && USER_INPUT_DASD_FORMAT_CODE_RUN="${choices[0]}" |
| 40 | |
| 41 | unset confirm_choices |
| 42 | confirm_choices[0]="Confirm recreated DASD format and continue '$rear_workflow'" |
| 43 | confirm_choices[1]="Go back one step to redo DASD format" |
| 44 | confirm_choices[2]="Use Relax-and-Recover shell and return back to here" |
| 45 | confirm_choices[3]="Abort '$rear_workflow'" |
| 46 | confirm_prompt="Confirm the recreated DASD format or go back one step" |
| 47 | confirm_choice="" |
| 48 | # When USER_INPUT_DASD_FORMAT_MIGRATED_CONFIRMATION has any 'true' value be liberal in what you accept and |
| 49 | # assume confirm_choices[0] 'Confirm recreated DASD format and continue' was actually meant: |
| 50 | is_true "$USER_INPUT_DASD_FORMAT_MIGRATED_CONFIRMATION" && USER_INPUT_DASD_FORMAT_MIGRATED_CONFIRMATION="${confirm_choices[0]}" |
| 51 | |
| 52 | # Run the DASD format code (dasdformat.sh) |
| 53 | # again and again until it succeeds or the user aborts |
| 54 | # or the user confirms to continue with what is currently on the disks |
| 55 | # (the user may have setup manually what he needs via the Relax-and-Recover shell): |
| 56 | while true ; do |
| 57 | prompt="The DASD format had failed" |
| 58 | # After switching to recreating with DASD format script |
| 59 | # change choices[0] from "Run ..." to "Rerun ...": |
| 60 | choices[0]="Rerun DASD format script ($DASD_FORMAT_CODE)" |
| 61 | # Run DASD_FORMAT_CODE in a sub-shell because it sets 'set -e' |
| 62 | # so that it exits the running shell in case of an error |
| 63 | # but that exit must not exit this running bash here: |
| 64 | ( source $DASD_FORMAT_CODE ) |
| 65 | # One must explicitly test whether or not $? is zero in a separated bash command |
| 66 | # because with bash 3.x and bash 4.x code like |
| 67 | # # ( set -e ; cat qqq ; echo "hello" ) && echo ok || echo failed |
| 68 | # cat: qqq: No such file or directory |
| 69 | # hello |
| 70 | # ok |
| 71 | # does not work as one may expect (cf. what "man bash" describes for 'set -e'). |
| 72 | # There is a subtle behavioural difference between bash 3.x and bash 4.x |
| 73 | # when a script that has 'set -e' set gets sourced: |
| 74 | # With bash 3.x the 'set -e' inside the sourced script is effective: |
| 75 | # # echo 'set -e ; cat qqq ; echo hello' >script.sh |
| 76 | # # ( source script.sh ) && echo ok || echo failed |
| 77 | # cat: qqq: No such file or directory |
| 78 | # failed |
| 79 | # With bash 4.x the 'set -e' inside the sourced script gets noneffective: |
| 80 | # # echo 'set -e ; cat qqq ; echo hello' >script.sh |
| 81 | # # ( source script.sh ) && echo ok || echo failed |
| 82 | # cat: qqq: No such file or directory |
| 83 | # hello |
| 84 | # ok |
| 85 | # With bash 3.x and bash 4.x testing $? in a separated bash command |
| 86 | # keeps the 'set -e' inside the sourced script effective: |
| 87 | # # echo 'set -e ; cat qqq ; echo hello' >script.sh |
| 88 | # # ( source script.sh ) ; (( $? == 0 )) && echo ok || echo failed |
| 89 | # cat: qqq: No such file or directory |
| 90 | # failed |
| 91 | # See also https://github.com/rear/rear/pull/1573#issuecomment-344303590 |
| 92 | if (( $? == 0 )) ; then |
| 93 | prompt="DASD format had been successful" |
| 94 | # When DASD_FORMAT_CODE succeeded and when not in migration mode |
| 95 | # break the outer while loop and continue the "rear recover" workflow |
| 96 | # which means continue with restoring the backup: |
| 97 | is_true "$MIGRATION_MODE" || break |
| 98 | # When DASD_FORMAT_CODE succeeded in migration mode |
| 99 | # let the user explicitly confirm the recreated (and usually migrated) format |
| 100 | # before continuing the "rear recover" workflow with restoring the backup. |
| 101 | # Show the recreated DASD format to the user on his terminal (and also in the log file): |
| 102 | LogPrint "Recreated DASD format:" |
| 103 | lsdasd_output |
| 104 | # Run an inner while loop with a user dialog so that the user can inspect the recreated DASD format |
| 105 | # and perhaps even manually fix the recreated DASD format if it is not what the user wants |
| 106 | # (e.g. by using the Relax-and-Recover shell and returning back to this user dialog): |
| 107 | while true ; do |
| 108 | confirm_choice="$( UserInput -I DASD_FORMAT_MIGRATED_CONFIRMATION -p "$confirm_prompt" -D "${confirm_choices[0]}" "${confirm_choices[@]}" )" && wilful_input="yes" || wilful_input="no" |
| 109 | case "$confirm_choice" in |
| 110 | (${confirm_choices[0]}) |
| 111 | # Confirm recreated DASD format and continue: |
| 112 | is_true "$wilful_input" && LogPrint "User confirmed recreated DASD format" || LogPrint "Continuing with recreated DASD format by default" |
| 113 | # Break the outer while loop and continue with restoring the backup: |
| 114 | break 2 |
| 115 | ;; |
| 116 | (${confirm_choices[1]}) |
| 117 | # Go back one step to redo DASD format: |
| 118 | # Only break the inner while loop (i.e. this user dialog loop) |
| 119 | # and continue with the next user dialog below: |
| 120 | break |
| 121 | ;; |
| 122 | (${confirm_choices[2]}) |
| 123 | # rear_shell runs 'bash' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user: |
| 124 | rear_shell "" "$rear_shell_history" |
| 125 | ;; |
| 126 | (${confirm_choices[3]}) |
| 127 | abort_dasd_format |
| 128 | Error "User did not confirm the recreated DASD format but aborted '$rear_workflow' in ${BASH_SOURCE[0]}" |
| 129 | ;; |
| 130 | esac |
| 131 | done |
| 132 | fi |
| 133 | # Run an inner while loop with a user dialog so that the user can fix things |
| 134 | # when DASD_FORMAT_CODE failed. |
| 135 | # Such a fix does not necessarily mean the user must change |
| 136 | # the dasdformat.sh script when DASD_FORMAT_CODE failed. |
| 137 | # The user might also fix things by only using the Relax-and-Recover shell and |
| 138 | # then confirm what is on the disks and continue with restoring the backup |
| 139 | # or abort this "rear recover" run to re-try from scratch. |
| 140 | while true ; do |
| 141 | choice="$( UserInput -I DASD_FORMAT_CODE_RUN -p "$prompt" -D "${choices[0]}" "${choices[@]}" )" && wilful_input="yes" || wilful_input="no" |
| 142 | case "$choice" in |
| 143 | (${choices[0]}) |
| 144 | # Rerun or run (after switching to recreating with DASD format script) DASD format script: |
| 145 | is_true "$wilful_input" && LogPrint "User runs DASD format script" || LogPrint "Running DASD format script by default" |
| 146 | # Only break the inner while loop (i.e. the user dialog loop): |
| 147 | break |
| 148 | ;; |
| 149 | (${choices[1]}) |
| 150 | # Run 'less' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user: |
| 151 | less $RUNTIME_LOGFILE 0<&6 1>&7 2>&8 |
| 152 | ;; |
| 153 | (${choices[2]}) |
| 154 | # Run 'vi' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user: |
| 155 | vi $DASD_FORMAT_CODE 0<&6 1>&7 2>&8 |
| 156 | ;; |
| 157 | (${choices[3]}) |
| 158 | LogPrint "This is the current list of DASDs:" |
| 159 | lsdasd_output |
| 160 | ;; |
| 161 | (${choices[4]}) |
| 162 | # Run 'less' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user: |
| 163 | less $original_disk_space_usage_file 0<&6 1>&7 2>&8 |
| 164 | ;; |
| 165 | (${choices[5]}) |
| 166 | # rear_shell runs 'bash' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user: |
| 167 | rear_shell "" "$rear_shell_history" |
| 168 | ;; |
| 169 | (${choices[6]}) |
| 170 | # Confirm what is on the disks and continue: |
| 171 | # Break the outer while loop and continue with restoring the backup: |
| 172 | break 2 |
| 173 | ;; |
| 174 | (${choices[7]}) |
| 175 | abort_dasd_format |
| 176 | Error "User chose to abort '$rear_workflow' in ${BASH_SOURCE[0]}" |
| 177 | ;; |
| 178 | esac |
| 179 | done |
| 180 | # End of the outer while loop: |
| 181 | done |
| 182 | |
| 183 | # Local functions must be 'unset' because bash does not support 'local function ...' |
| 184 | # cf. https://unix.stackexchange.com/questions/104755/how-can-i-create-a-local-function-in-my-bashrc |
| 185 | unset -f lsdasd_output |