#!/bin/bash
# Script to verify and configure 250GB drives on R630-01 for Ceph OSD
# Run this script on R630-01 (192.168.11.11)

set -e

echo "=========================================="
echo "R630-01 Disk Verification and Ceph OSD Setup"
echo "=========================================="
echo ""

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo -e "${RED}Please run as root${NC}"
    exit 1
fi

echo "Step 1: Listing all block devices..."
echo "-----------------------------------"
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE,MODEL,SERIAL | grep -E "NAME|sd"
echo ""

echo "Step 2: Checking all disks..."
echo "-----------------------------------"
fdisk -l 2>/dev/null | grep -E "^Disk /dev/sd" | sort
echo ""

echo "Step 3: Identifying 250GB drives..."
echo "-----------------------------------"
echo "Looking for drives around 250GB size..."
fdisk -l 2>/dev/null | grep -E "^Disk /dev/sd" | grep -iE "232|233|234|235|236|237|238|239|240|241|242|243|244|245|246|247|248|249|250|251|252|253|254|255|256|257|258|259|260" || echo "No 250GB drives found in fdisk output"
echo ""

echo "Step 4: Checking current Ceph OSD status..."
echo "-----------------------------------"
if command -v ceph &> /dev/null; then
    echo "Current OSD tree:"
    ceph osd tree 2>/dev/null || echo "Ceph not accessible or not configured"
    echo ""
    echo "Current OSD details:"
    ceph osd df 2>/dev/null || echo "Ceph not accessible"
    echo ""
    echo "Ceph health:"
    ceph health 2>/dev/null || echo "Ceph not accessible"
else
    echo "Ceph command not found"
fi
echo ""

echo "Step 5: Checking storage pools..."
echo "-----------------------------------"
if command -v pvesm &> /dev/null; then
    pvesm status 2>/dev/null || echo "pvesm not accessible"
else
    echo "pvesm command not found"
fi
echo ""

echo "Step 6: Finding unused disks (potential for OSD)..."
echo "-----------------------------------"
echo "Checking for disks without partitions or filesystems..."
for disk in /dev/sd[a-z]; do
    if [ -b "$disk" ]; then
        # Skip if it's the boot disk or has partitions
        if ! lsblk -n -o MOUNTPOINT "$disk" 2>/dev/null | grep -q "/"; then
            size=$(fdisk -l "$disk" 2>/dev/null | grep "^Disk $disk" | awk '{print $3 $4}')
            echo "  $disk: $size (potentially available)"
        fi
    fi
done
echo ""

echo "=========================================="
echo "Summary and Recommendations"
echo "=========================================="
echo ""
echo "1. Review the output above to identify the 6x 250GB drives"
echo "2. Check if any drives are uninitialized (no partitions)"
echo "3. If a drive is available, you can create a Ceph OSD with:"
echo ""
echo "   # WARNING: This will destroy all data on the drive!"
echo "   ceph-volume lvm create --data /dev/sdX"
echo ""
echo "   Replace sdX with the actual device (e.g., sdc, sdd, etc.)"
echo ""
echo "4. After creating OSD, verify with:"
echo "   ceph osd tree"
echo "   ceph health"
echo ""
echo "5. If Ceph health improves, the third OSD is working!"
echo ""

