| #!/bin/bash |
| |
| # Function to check if eMMC is available |
| # Returns "true" or "false" string to stdout |
| get_emmc_status() { |
| local target="emmc-available.target" |
| local output |
| |
| # Run systemctl status and capture output |
| output=$(systemctl status "$target" 2>&1) |
| |
| # Parse output for "Active: active" |
| if echo "$output" | grep -q "Active: active"; then |
| echo "true" |
| else |
| echo "false" |
| fi |
| } |
| |
| # Function to get related logs for eMMC services |
| get_emmc_logs() { |
| # Run journalctl for both services and combine output |
| { |
| echo "--- xyz.openbmc_project.eStoraged.service ---" |
| journalctl -u xyz.openbmc_project.eStoraged.service -b |
| echo "" |
| echo "--- psdmd ---" |
| journalctl -u psdmd -b |
| echo "" |
| echo "--- bmc_crypto ---" |
| journalctl -u bmc_crypto -b |
| } |
| } |
| |
| # Function to format input text into a JSON array of strings |
| format_log_array() { |
| awk ' |
| BEGIN { printf "[\n" } |
| { |
| # Escape backslashes and double quotes inside the content |
| gsub(/\\/, "\\\\"); |
| gsub(/"/, "\\\""); |
| |
| # If this is not the first line processed, print a comma and newline |
| # to separate the previous element |
| if (NR > 1) { printf ",\n" } |
| |
| # Print the current line wrapped in quotes with 4-space indent |
| printf " \"%s\"", $0 |
| } |
| END { printf "\n ]" } |
| ' |
| } |
| |
| # Function to format input text into a mixed JSON array |
| # Detects if a line is already JSON and inserts it directly (unquoted) |
| # Otherwise escapes it as a string |
| format_json_or_text_array() { |
| awk ' |
| BEGIN { printf "[\n" } |
| { |
| # Create a clean version of the line for checking (trim whitespace) |
| clean = $0 |
| gsub(/^[ \t]+|[ \t]+$/, "", clean) |
| |
| # Separator |
| if (NR > 1) { printf ",\n" } |
| |
| # Heuristic: if line starts with { and ends with }, treat as raw JSON object |
| if (clean ~ /^\{.*\}$/) { |
| printf " %s", $0 |
| } else { |
| # Otherwise treat as string: escape and quote |
| gsub(/\\/, "\\\\", $0); |
| gsub(/"/, "\\\"", $0); |
| printf " \"%s\"", $0 |
| } |
| } |
| END { printf "\n ]" } |
| ' |
| } |
| |
| # --- Main Logic --- |
| |
| # 1. Get eMMC availability |
| emmc_val=$(get_emmc_status) |
| |
| # 2. Get eMMC related logs and format them as a JSON array |
| raw_emmc_logs=$(get_emmc_logs) |
| emmc_json_logs=$(echo "$raw_emmc_logs" | format_log_array) |
| |
| # 3. Get Disk Usage and format as a JSON array |
| raw_disk_usage=$(df -h) |
| disk_usage_json=$(echo "$raw_disk_usage" | format_log_array) |
| |
| # 4. Get LUKS Status and format as a JSON array |
| # We capture stderr (2>&1) as well, in case the device doesn't exist or command fails |
| raw_luks_status=$(cryptsetup status luks-mmcblk0 2>&1) |
| luks_status_json=$(echo "$raw_luks_status" | format_log_array) |
| |
| # 5. Get Install Service Logs and format as a JSON array |
| raw_install_logs=$(journalctl -u install-service -b) |
| install_logs_json=$(echo "$raw_install_logs" | format_log_array) |
| |
| # 6. Get Activation Check Logs and format as a JSON array |
| raw_activation_logs=$(journalctl -u activation-check -b) |
| activation_logs_json=$(echo "$raw_activation_logs" | format_log_array) |
| |
| # 7. Get Installer Internal Logs. Preserve JSON object structure |
| raw_internal_logs=$(cat /run/install/progress_internal.log 2>&1) |
| internal_logs_json=$(echo "$raw_internal_logs" | format_json_or_text_array) |
| |
| # 8. Generate Final JSON and save to file |
| # Note: The json variables already contain brackets [ ] and quotes, so we don't add quotes here |
| |
| # Ensure the directory exists |
| mkdir -p /run/install |
| |
| cat <<EOF > /run/install/debug_log.json.tmp |
| { |
| "EmmcAvaiable": $emmc_val, |
| "EmmcRelatedLogs": $emmc_json_logs, |
| "DiskUsage": $disk_usage_json, |
| "LuksStatus": $luks_status_json, |
| "InstallServiceLogs": $install_logs_json, |
| "ActivationCheckLogs": $activation_logs_json, |
| "InstallerInternalLogs": $internal_logs_json |
| } |
| EOF |
| |
| mv /run/install/debug_log.json.tmp /run/install/debug_log.json |