#!/bin/bash
# Comprehensive disk layout analysis for R630-01
# Analyzes the current disk configuration and provides recommendations

# Don't exit on error - we want to continue even if some commands fail
set +e

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

echo "=========================================="
echo -e "${CYAN}R630-01 Disk Layout Analysis${NC}"
echo "=========================================="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo -e "${YELLOW}Warning: Some information may be limited without root access${NC}"
    echo ""
fi

echo -e "${BLUE}=== System Overview ===${NC}"
echo "-----------------------------------"
echo "Hostname: $(hostname 2>/dev/null || echo 'unknown')"
echo "Date: $(date)"
echo ""

echo -e "${BLUE}=== Complete Block Device Layout ===${NC}"
echo "-----------------------------------"
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE,MODEL,SERIAL,UUID
echo ""

echo -e "${BLUE}=== Disk Categorization ===${NC}"
echo "-----------------------------------"

# System disk (sda)
echo -e "${GREEN}System Disk (sda):${NC}"
if [ -b /dev/sda ]; then
    sda_size=$(lsblk -d -n -o SIZE /dev/sda 2>/dev/null || echo "unknown")
    echo "  Size: $sda_size"
    echo "  Partitions:"
    lsblk -n /dev/sda | grep -v "^sda" | sed 's/^/    /'
    echo "  LVM Volumes:"
    if command -v pvs &>/dev/null; then
        pvs 2>/dev/null | grep sda | sed 's/^/    /' || echo "    (No LVM volumes found)"
    fi
    echo "  Status: ${GREEN}Active system disk${NC}"
else
    echo "  ${RED}Not found${NC}"
fi
echo ""

# Secondary disk (sdb)
echo -e "${YELLOW}Secondary Disk (sdb):${NC}"
if [ -b /dev/sdb ]; then
    sdb_size=$(lsblk -d -n -o SIZE /dev/sdb 2>/dev/null || echo "unknown")
    echo "  Size: $sdb_size"
    partitions=$(lsblk -n /dev/sdb 2>/dev/null | grep -c "part" || echo "0")
    if [ "$partitions" -eq 0 ]; then
        echo "  Status: ${YELLOW}Unused - No partitions${NC}"
        echo "  Recommendation: Available for Ceph OSD or Proxmox storage"
    else
        echo "  Partitions:"
        lsblk -n /dev/sdb | grep -v "^sdb" | sed 's/^/    /'
        echo "  Status: ${YELLOW}Has partitions${NC}"
    fi
else
    echo "  ${RED}Not found${NC}"
fi
echo ""

# Ceph candidate disks (sdc-sdh)
echo -e "${CYAN}Ceph Candidate Disks (sdc-sdh):${NC}"
ceph_disks=0
ceph_available=0
ceph_used=0

for disk in sdc sdd sde sdf sdg sdh; do
    if [ -b "/dev/$disk" ]; then
        ceph_disks=$((ceph_disks + 1))
        size=$(lsblk -d -n -o SIZE /dev/$disk 2>/dev/null || echo "unknown")
        partitions=$(lsblk -n /dev/$disk 2>/dev/null | grep -c "part" || echo "0")
        partitions=$(echo "$partitions" | tr -d '\n' | head -1)
        mountpoint=$(lsblk -d -n -o MOUNTPOINT /dev/$disk 2>/dev/null | tr -d '\n' || echo "")
        fstype=$(lsblk -d -n -o FSTYPE /dev/$disk 2>/dev/null | tr -d '\n' || echo "")
        
        echo "  /dev/$disk:"
        echo "    Size: $size"
        
        if [ "${partitions:-0}" -eq 0 ] && [ -z "$mountpoint" ] && [ -z "$fstype" ]; then
            echo -e "    Status: ${GREEN}Available (no partitions, unmounted)${NC}"
            ceph_available=$((ceph_available + 1))
        elif [ "${partitions:-0}" -gt 0 ]; then
            echo "    Partitions: $partitions"
            lsblk -n /dev/$disk | grep -v "^$disk" | sed 's/^/      /'
            echo -e "    Status: ${YELLOW}Has partitions${NC}"
            ceph_used=$((ceph_used + 1))
        else
            echo -e "    Status: ${YELLOW}In use (mounted or has filesystem)${NC}"
            ceph_used=$((ceph_used + 1))
        fi
        
        # Check if in LVM
        if command -v pvs &>/dev/null; then
            in_lvm=$(pvs 2>/dev/null | grep "/dev/$disk" | wc -l)
            if [ "$in_lvm" -gt 0 ]; then
                vg=$(pvs 2>/dev/null | grep "/dev/$disk" | awk '{print $2}')
                echo -e "    LVM: ${YELLOW}In volume group: $vg${NC}"
            fi
        fi
        
        # Check if has Ceph OSD
        if command -v ceph-volume &>/dev/null; then
            has_osd=$(ceph-volume lvm list /dev/$disk 2>/dev/null | grep -c "osd\." || echo "0")
            if [ "$has_osd" -gt 0 ]; then
                osd_id=$(ceph-volume lvm list /dev/$disk 2>/dev/null | grep -oP "osd\.\K\d+" | head -1)
                echo -e "    Ceph: ${GREEN}OSD $osd_id${NC}"
            fi
        fi
    else
        echo "  /dev/$disk: ${RED}Not found${NC}"
    fi
done
echo ""

echo -e "${BLUE}=== Storage Summary ===${NC}"
echo "-----------------------------------"
echo "Total block devices: $(lsblk -d -n | grep -c '^sd' || echo '0')"
echo "System disk (sda): 1"
echo "Secondary disk (sdb): $([ -b /dev/sdb ] && echo '1' || echo '0')"
echo "Ceph candidate disks (sdc-sdh): $ceph_disks"
echo "  - Available: $ceph_available"
echo "  - In use: $ceph_used"
echo ""

echo -e "${BLUE}=== Proxmox Storage Status ===${NC}"
echo "-----------------------------------"
if command -v pvesm &>/dev/null; then
    pvesm status 2>/dev/null || echo -e "${YELLOW}pvesm not accessible${NC}"
else
    echo -e "${YELLOW}pvesm command not found${NC}"
fi
echo ""

echo -e "${BLUE}=== Ceph Status ===${NC}"
echo "-----------------------------------"
if command -v ceph &>/dev/null; then
    echo "Ceph health:"
    ceph health 2>/dev/null || echo -e "${YELLOW}Ceph not accessible${NC}"
    echo ""
    echo "OSD tree:"
    ceph osd tree 2>/dev/null || echo -e "${YELLOW}Ceph not accessible${NC}"
    echo ""
    echo "OSD details:"
    ceph osd df 2>/dev/null || echo -e "${YELLOW}Ceph not accessible${NC}"
else
    echo -e "${YELLOW}Ceph not installed${NC}"
fi
echo ""

echo -e "${BLUE}=== LVM Status ===${NC}"
echo "-----------------------------------"
if command -v vgs &>/dev/null; then
    echo "Volume Groups:"
    vgs 2>/dev/null | sed 's/^/  /' || echo -e "  ${YELLOW}No volume groups found${NC}"
    echo ""
    echo "Physical Volumes:"
    pvs 2>/dev/null | sed 's/^/  /' || echo -e "  ${YELLOW}No physical volumes found${NC}"
    echo ""
    echo "Logical Volumes:"
    lvs 2>/dev/null | sed 's/^/  /' || echo -e "  ${YELLOW}No logical volumes found${NC}"
else
    echo -e "${YELLOW}LVM not available${NC}"
fi
echo ""

echo -e "${BLUE}=== Recommendations ===${NC}"
echo "-----------------------------------"

if [ "$ceph_available" -gt 0 ]; then
    echo -e "${GREEN}Available disks for Ceph OSD:${NC}"
    for disk in sdc sdd sde sdf sdg sdh; do
        if [ -b "/dev/$disk" ]; then
            partitions=$(lsblk -n /dev/$disk 2>/dev/null | grep -c "part" || echo "0")
            mountpoint=$(lsblk -d -n -o MOUNTPOINT /dev/$disk 2>/dev/null || echo "")
            if [ "$partitions" -eq 0 ] && [ -z "$mountpoint" ]; then
                echo "  - /dev/$disk: Ready for Ceph OSD"
                echo "    Command: ceph-volume lvm create --data /dev/$disk"
            fi
        fi
    done
    echo ""
fi

if [ -b /dev/sdb ]; then
    partitions=$(lsblk -n /dev/sdb 2>/dev/null | grep -c "part" || echo "0")
    if [ "$partitions" -eq 0 ]; then
        echo -e "${GREEN}Secondary disk (sdb) available:${NC}"
        echo "  - Can be used for Ceph OSD"
        echo "  - Can be added as Proxmox storage"
        echo "  - Can be used for backup storage"
        echo ""
    fi
fi

if [ "$ceph_used" -gt 0 ]; then
    echo -e "${YELLOW}Disks currently in use:${NC}"
    echo "  - Review partitions and usage before repurposing"
    echo "  - Check if data needs to be migrated"
    echo ""
fi

echo "=========================================="
echo -e "${CYAN}Analysis Complete${NC}"
echo "=========================================="

