#!/bin/bash
# License: GPL
# Author: Steven Shiau <steven _at_ clonezilla org>
# Description: Program to restore the MDRAID layout.
# This file contains code generated by Google's Gemini. 

#
DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
. $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
. /etc/drbl/drbl-ocs.conf
. $DRBL_SCRIPT_PATH/sbin/ocs-functions

# Load the config in ocs-live.conf.
# This is specially for Clonezilla live. It will overwrite some settings of /etc/drbl/drbl-ocs.conf, such as $DIA...
[ -e "/etc/ocs/ocs-live.conf" ] && . /etc/ocs/ocs-live.conf

#
USAGE() {
    echo "$ocs - To restore the MDRAID layout from image dir"
    echo "Usage:"
    echo "To run $ocs:"
    echo "$ocs [OPTION] IMAGE_DIR_PATH [TARGET_DEV...]"
    echo
    echo "Options:"
    echo "-b, --batch-mode   Run image checking in batch mode"
    echo
    echo "TARGET_DEV can be target disk(s) or partition(s), e.g., nvme0n1, /dev/sda."
    echo "If TARGET_DEV is specified, only MDRAID arrays whose members are on TARGET_DEV will be recreated,"
    echo "and member devices outside of TARGET_DEV will never be wiped or modified."
    echo
    echo "Ex:"
    echo "To restore the MDRAID device from the image \"my-image\", which is located in $ocsroot, run"
    echo "   $ocs $ocsroot/my-image"
    echo "To restore only arrays associated with target disk nvme0n1, run"
    echo "   $ocs -b $ocsroot/my-image nvme0n1"
    echo
} # end of USAGE

####################
### Main program ###
####################

ocs_file="$0"
ocs=`basename $ocs_file`
#
while [ $# -gt 0 ]; do
 case "$1" in
   -b|--batch) ocs_batch_mode="on"; shift;;
   -*)     echo "${0}: ${1}: invalid option" >&2
           USAGE >& 2
           exit 2 ;;
   *)      break ;;
 esac
done

#
check_if_root
ask_and_load_lang_set

#
if [ "$#" -lt 1 ]; then
    USAGE
    exit 1
fi

IMG_DIR="${1%/}"
shift
TARGET_DEVICES="$(echo "$*" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"

if [ ! -d "$IMG_DIR" ]; then
    echo "Error: Directory '$IMG_DIR' does not exist."
    exit 1
fi

# Helper: check if an array configuration belongs to target devices
array_belongs_to_target() {
    local env_f="$1"
    [ -z "$TARGET_DEVICES" ] && return 0

    local members member member_kname member_disk tdev env_md_name
    env_md_name="$(basename "$env_f" -config.env)"
    for tdev in $TARGET_DEVICES; do
        tdev="${tdev#/dev/}"
        if [ "$env_md_name" = "$tdev" ]; then
            return 0
        fi
    done
    members="$(awk -F= '/^MD_MEMBERS_ORDERED=/ {gsub(/"/, "", $2); print $2}' "$env_f" 2>/dev/null)"
    for member in $members; do
        [ "$member" = "missing" ] && continue
        member_kname="$(basename "$member")"
        member_disk="$(get_diskname "$member_kname")"
        for tdev in $TARGET_DEVICES; do
            tdev="${tdev#/dev/}"
            if [ "$member_disk" = "$tdev" ] || [ "$member_kname" = "$tdev" ]; then
                return 0
            fi
        done
    done
    return 1
}

# Helper: check if a specific member device is on target devices
is_member_on_target() {
    local member="$1"
    [ -z "$TARGET_DEVICES" ] && return 0
    [ "$member" = "missing" ] && return 1

    local member_kname member_disk tdev
    member_kname="$(basename "$member")"
    member_disk="$(get_diskname "$member_kname")"
    for tdev in $TARGET_DEVICES; do
        tdev="${tdev#/dev/}"
        if [ "$member_disk" = "$tdev" ] || [ "$member_kname" = "$tdev" ]; then
            return 0
        fi
    done
    return 1
}

# Helper: sanitize MD v1.x superblock padding (zeroing pad3 and recalculating checksum)
# to maintain compatibility with older kernels (e.g. RHEL/Rocky Linux) where non-zero
# padding triggers -EINVAL in super_1_load.
sanitize_md_superblock() {
    local member_dev="$1"
    [ -b "$member_dev" ] || return 0

    perl -e '
        use strict;
        use warnings;

        my ($dev_path) = @ARGV;
        open my $dev, "+<", $dev_path or exit 0;

        sub calc_csum {
            my ($data) = @_;
            my ($max_dev) = unpack("x220 V", $data);
            my $size = 256 + $max_dev * 2;
            substr($data, 216, 4, "\x00\x00\x00\x00");
            my $newcsum = 0;
            my $off = 0;
            while ($off + 4 <= $size) {
                $newcsum += unpack("V", substr($data, $off, 4));
                $off += 4;
            }
            if ($off + 2 <= $size) {
                $newcsum += unpack("v", substr($data, $off, 2));
            }
            my $csum = ($newcsum & 0xffffffff) + ($newcsum >> 32);
            $csum = ($csum & 0xffffffff) + ($csum >> 32);
            return $csum;
        }

        # Offsets to check: 4096 (v1.2), 0 (v1.1)
        my @offsets = (4096, 0);
        sysseek($dev, 0, 2);
        my $dev_size = sysseek($dev, 0, 1);
        if ($dev_size && $dev_size >= 16384) {
            my $v10_offset = ($dev_size & ~4095) - 8192;
            push @offsets, $v10_offset if $v10_offset > 0;
        }

        my $sanitized = 0;
        for my $sb_offset (@offsets) {
            sysseek($dev, $sb_offset, 0) or next;
            my $buf;
            my $n = sysread($dev, $buf, 4096);
            next unless defined($n) && $n >= 256;

            my $magic = unpack("V", substr($buf, 0, 4));
            next unless $magic == 0xa92b4efc;

            my $major = unpack("V", substr($buf, 4, 4));
            next unless $major == 1;

            my ($max_dev) = unpack("x220 V", $buf);
            my $sb_size = 256 + $max_dev * 2;
            next if $n < $sb_size;

            my $pad3 = substr($buf, 224, 32);
            if ($pad3 ne ("\x00" x 32)) {
                print "  -> Sanitizing MD v1.x superblock padding at offset $sb_offset on $dev_path...\n";
                substr($buf, 224, 32, "\x00" x 32);
                my $new_csum = calc_csum($buf);
                substr($buf, 216, 4, pack("V", $new_csum));
                sysseek($dev, $sb_offset, 0);
                syswrite($dev, substr($buf, 0, $sb_size));
                $sanitized = 1;
            }
        }
        close $dev;
        exit($sanitized ? 0 : 1);
    ' "$member_dev" 2>/dev/null || true
}

echo "------------------------------------------"
echo "1: Recreating the MDRAID Arrays"
echo "------------------------------------------"

# Check if there are any config files
if ! ls "$IMG_DIR"/md*-config.env 1> /dev/null 2>&1; then
    echo "Error: Could not find any MDRAID configuration files (e.g., md127-config.env)."
    exit 1
fi

# If target devices are specified, check if any config files match
if [ -n "$TARGET_DEVICES" ]; then
    has_target_array=0
    for ENV_FILE in "$IMG_DIR"/md*-config.env; do
        [ -e "$ENV_FILE" ] || continue
        if array_belongs_to_target "$ENV_FILE"; then
            has_target_array=1
            break
        fi
    done
    if [ "$has_target_array" -eq 0 ]; then
        echo "No MDRAID configuration files matching target devices ($TARGET_DEVICES) found."
        echo "Skipping MDRAID restoration."
        echo "------------------------------------------"
        exit 0
    fi
fi

# ====================================================================
# The "udev Auto-Assembly Race Condition" Preventer
# We MUST stop all arrays and zero all superblocks across ALL configs 
# simultaneously before recreating, otherwise udev will lock the disks.
# ====================================================================
echo "  -> Stopping background arrays auto-assembled by udev..."
if [ -n "$TARGET_DEVICES" ]; then
    ocs-mdraid-stop $TARGET_DEVICES
else
    ocs-mdraid-stop
fi
udevadm settle
sleep 2

echo "  -> Pre-wiping all MDRAID superblocks to clear stale metadata..."
for ENV_FILE in "$IMG_DIR"/md*-config.env; do
    [ -e "$ENV_FILE" ] || continue
    array_belongs_to_target "$ENV_FILE" || continue
    MD_DEV_NAME=$(basename "$ENV_FILE" -config.env)
    if grep -qE "^${MD_DEV_NAME}[[:space:]]*:" /proc/mdstat 2>/dev/null; then
        echo "  -> Target array /dev/$MD_DEV_NAME is already active. Skipping pre-wiping of superblocks."
        continue
    fi
    # Run in subshell to isolate variables
    (
        source "$ENV_FILE"
        for MEMBER in $MD_MEMBERS_ORDERED; do
            if [ "$MEMBER" != "missing" ] && [ -b "$MEMBER" ]; then
                is_member_on_target "$MEMBER" || continue
                mdadm --zero-superblock "$MEMBER" 2>/dev/null || true
                wipefs -a "$MEMBER" 2>/dev/null || true
            fi
        done
    )
done
udevadm settle
# ====================================================================

# Loop through all saved MDRAID configurations
for ENV_FILE in "$IMG_DIR"/md*-config.env; do
    [ -e "$ENV_FILE" ] || continue
    
    MD_DEV_NAME=$(basename "$ENV_FILE" -config.env)
    MD_DEV_PATH="/dev/$MD_DEV_NAME"
    MD_DUMP="$IMG_DIR/${MD_DEV_NAME}-pt.sf"

    if ! array_belongs_to_target "$ENV_FILE"; then
        echo "  -> Skipping $MD_DEV_PATH: array members do not belong to target devices ($TARGET_DEVICES)."
        continue
    fi

    if [ "$ocs_batch_mode" != "on" ]; then
      echo "WARNING: This will overwrite MDRAID layout on target device $MD_DEV_PATH."
      confirm_continue_or_not_default_quit
    fi

    echo "  -> Target MD array: $MD_DEV_PATH"

    if grep -qE "^${MD_DEV_NAME}[[:space:]]*:" /proc/mdstat 2>/dev/null; then
        echo "  -> Array $MD_DEV_PATH is already active in /proc/mdstat. Skipping array recreation."
    else
        MD_UUID_PARAM=""
        MD_NAME_PARAM=""
        MD_META_PARAM=""
        MD_LEVEL_PARAM=""
        MD_DEVICES_PARAM=""
        MD_CHUNK_PARAM=""
        MD_LAYOUT_PARAM=""
        MD_DATA_OFFSET_PARAM=""
        MD_MEMBERS=""

        # Unset variables to prevent leakage from the previous loop iteration
        unset MD_UUID MD_NAME MD_META MD_LEVEL MD_DEVICES_COUNT MD_CHUNK MD_LAYOUT MD_DATA_OFFSET MD_MEMBERS_ORDERED
        
        # Parse all advanced variables from the env config
        source "$ENV_FILE"

        [ -n "${MD_NAME:-}" ] && MD_NAME_PARAM="--name=$MD_NAME"
        [ -n "${MD_UUID:-}" ] && MD_UUID_PARAM="--uuid=$MD_UUID"
        [ -n "${MD_META:-}" ] && MD_META_PARAM="--metadata=$MD_META"
        [ -n "${MD_LEVEL:-}" ] && MD_LEVEL_PARAM="--level=$MD_LEVEL"
        [ -n "${MD_DEVICES_COUNT:-}" ] && MD_DEVICES_PARAM="--raid-devices=$MD_DEVICES_COUNT"
        [ -n "${MD_CHUNK:-}" ] && MD_CHUNK_PARAM="--chunk=$MD_CHUNK"
        [ -n "${MD_LAYOUT:-}" ] && MD_LAYOUT_PARAM="--layout=$MD_LAYOUT"
        [ -n "${MD_DATA_OFFSET:-}" ] && MD_DATA_OFFSET_PARAM="--data-offset=${MD_DATA_OFFSET}s"
        [ -n "${MD_MEMBERS_ORDERED:-}" ] && MD_MEMBERS="$MD_MEMBERS_ORDERED"

        echo "  -> Array Level: ${MD_LEVEL:-Unknown} | Devices: ${MD_DEVICES_COUNT:-Unknown}"
        echo "  -> Exact Device Order: $MD_MEMBERS"

        # ====================================================================
        # Selective MDRAID Assassination
        # Prevent mdadm --create from failing or grabbing the wrong size if 
        # the kernel still holds the physical partitions in a phantom array.
        # ====================================================================
        for MEMBER in $MD_MEMBERS; do
            if [ "$MEMBER" != "missing" ] && [ -b "$MEMBER" ]; then
                is_member_on_target "$MEMBER" || continue
                MEMBER_KNAME="$(basename "$MEMBER")"
                for md_sys in /sys/class/block/md*; do
                    if [ -d "$md_sys" ] && [ -e "$md_sys/slaves/$MEMBER_KNAME" ]; then
                        stale_md="/dev/$(basename "$md_sys")"
                        echo "  -> Target $MEMBER is held by stale array $stale_md. Stopping $stale_md..."
                        umount "$stale_md" 2>/dev/null || true
                        mdadm --stop "$stale_md" 2>/dev/null || true
                    fi
                done
                # Ensure the superblock is wiped one last time before creation
                mdadm --zero-superblock "$MEMBER" 2>/dev/null || true
            fi
        done
        udevadm settle
        sleep 2
        # ====================================================================

        # ====================================================================
        # Smart Array Name Retry Logic
        # ====================================================================
        BASE_CMD="mdadm --create \"$MD_DEV_PATH\" --force --run $MD_LEVEL_PARAM $MD_DEVICES_PARAM $MD_META_PARAM $MD_DATA_OFFSET_PARAM $MD_UUID_PARAM $MD_CHUNK_PARAM $MD_LAYOUT_PARAM $MD_MEMBERS --assume-clean"

        echo "  -> Attempting to create array with original name: ${MD_NAME:-None}"
        if ! eval "yes | $BASE_CMD $MD_NAME_PARAM" 2>/dev/null; then
            echo "  -> [Warning] Creation failed! Current mdadm likely rejects non-POSIX characters like ':'."
            
            SAFE_MD_NAME="$(echo "$MD_NAME" | sed -r -e 's/[^a-zA-Z0-9.-]/_/g')"
            echo "  -> Retrying with sanitized name: $SAFE_MD_NAME..."
            
            MD_NAME_PARAM="--name=$SAFE_MD_NAME"
            if ! eval "yes | $BASE_CMD $MD_NAME_PARAM"; then
                echo "  -> [Error] Array creation failed completely!"
                exit 1
            fi
        else
            echo "  -> Array created successfully with original name."
        fi

        udevadm settle
        sleep 3
        # ====================================================================

        # Sanitize MD v1.x superblock padding to ensure compatibility with older kernels (e.g. RHEL/Rocky Linux)
        echo "  -> Checking and sanitizing superblock padding for older kernel compatibility..."
        mdadm --stop "$MD_DEV_PATH" 2>/dev/null || true
        udevadm settle
        sleep 1
        for MEMBER in $MD_MEMBERS; do
            [ "$MEMBER" = "missing" ] && continue
            [ -b "$MEMBER" ] || continue
            sanitize_md_superblock "$MEMBER"
        done
        udevadm settle
        sleep 1
        mdadm --assemble --run "$MD_DEV_PATH" $MD_MEMBERS 2>/dev/null || true
        udevadm settle
        sleep 2
    fi

    if [ -f "$MD_DUMP" ]; then
        echo "------------------------------------------"
        echo "Phase 2: Restoring MD Array Internal Partitions for $MD_DEV_PATH"
        echo "------------------------------------------"
        echo "  -> Applying internal partition table to $MD_DEV_PATH..."
        sfdisk --force "$MD_DEV_PATH" < "$MD_DUMP"
        
        # TARGETED partprobe for MD device
        echo "  -> Reloading partition table for $MD_DEV_PATH..."
        partprobe "$MD_DEV_PATH" 2>/dev/null || true
        udevadm settle
        sleep 3
    fi
done

echo "Restoration Complete (Layout & Metadata Only)!"
echo "Advanced RAID array is now structurally identical to the original."
echo $msg_delimiter_star_line
