| 1 | # Code to recreate TCG Opal 2-compliant self-encrypting disks |
| 2 | |
| 3 | function create_opaldisk_continue_unencrypted() { |
| 4 | local error_message="${1:-?}" |
| 5 | local device="${2:-?}" |
| 6 | # prints message, stops with an error unless user confirms to continue. |
| 7 | |
| 8 | LogPrintError "$error_message" |
| 9 | |
| 10 | prompt="Continue without disk encryption on $device (y/n) ?" |
| 11 | if [[ "$(opal_choice_input "OPAL_OPALDISK_CREATE_UNENCRYPTED" "$prompt" "y" "n")" == "y" ]]; then |
| 12 | echo "LogPrint \"Skipping opaldisk:$device: $error_message\"" >> "$LAYOUT_CODE" |
| 13 | else |
| 14 | Error "Confirmation denied. Aborting." |
| 15 | fi |
| 16 | } |
| 17 | |
| 18 | function create_opaldisk() { |
| 19 | local device=${1#opaldisk:} |
| 20 | # recreates a component opaldisk:<device> |
| 21 | |
| 22 | local opaldisk options |
| 23 | read opaldisk device options < <(grep "^opaldisk $device " "$LAYOUT_FILE") |
| 24 | |
| 25 | local boot="" password="" pba_image_file="" |
| 26 | |
| 27 | local option key value |
| 28 | for option in $options; do |
| 29 | key="${option%=*}" |
| 30 | value="${option#*=}" |
| 31 | |
| 32 | case "$key" in |
| 33 | boot) |
| 34 | boot="$value" |
| 35 | : ${pba_image_file:="$(opal_local_pba_image_file)"} |
| 36 | [[ -n "$pba_image_file" ]] || Error "Could not find a PBA image for self-encrypting Opal 2 boot disk $device." |
| 37 | ;; |
| 38 | password) |
| 39 | password="$value" |
| 40 | ;; |
| 41 | esac |
| 42 | done |
| 43 | |
| 44 | local devices=( $(opal_devices) ) |
| 45 | if ! IsInArray "$device" "${devices[@]}"; then |
| 46 | create_opaldisk_continue_unencrypted "Device $device is not a TCG Opal 2-compliant self-encrypting disk." "$device" |
| 47 | return 0 |
| 48 | fi |
| 49 | if [[ "$(opal_device_attribute "$device" "support")" == "n" ]]; then |
| 50 | create_opaldisk_continue_unencrypted "Device $(opal_device_identification "$device") does not support locking." "$device" |
| 51 | return 0 |
| 52 | fi |
| 53 | |
| 54 | { |
| 55 | echo "# Protect against passwords appearing in the log file" |
| 56 | echo "{ opaldisk_caller_bash_set_options=\"\$-\"; set +x; } 2>/dev/null" # silently turn off '-x' but remember its state |
| 57 | |
| 58 | echo "LogPrint \"Setting up TCG Opal 2 self-encrypting disk $device\"" |
| 59 | |
| 60 | if [[ -n "$password" ]]; then |
| 61 | echo "opaldisk_password='$password'" |
| 62 | else |
| 63 | local prompt="password for self-encrypting disk $device" |
| 64 | echo "# Reuse OPAL_DISK_PASSWORD if multiple self-encrypting disks are present" |
| 65 | echo ": \${OPAL_DISK_PASSWORD:=\"\$(opal_checked_password_input \"OPAL_DISK_PASSWORD\" \"$prompt\")\"}" |
| 66 | echo "opaldisk_password=\"\$OPAL_DISK_PASSWORD\"" |
| 67 | fi |
| 68 | |
| 69 | echo "opal_device_recreate_setup \"$device\" \"\$opaldisk_password\"" |
| 70 | |
| 71 | if [[ "$boot" == "y" ]]; then |
| 72 | echo "opal_device_recreate_boot_support \"$device\" \"\$opaldisk_password\" \"$pba_image_file\"" |
| 73 | fi |
| 74 | |
| 75 | echo "[[ \"\$opaldisk_caller_bash_set_options\" == *x* ]] && set -x" # restore '-x' to previous state |
| 76 | echo "" |
| 77 | } >> "$LAYOUT_CODE" |
| 78 | } |