#!/bin/bash
# Efficient script to create all OSDs on available drives
set -e

DRIVES=("sdc" "sdd" "sde" "sdf" "sdg" "sdh")

echo "=== Checking cluster connectivity ==="
if ! ceph quorum_status &>/dev/null; then
    echo "WARNING: Cluster quorum may not be established. Continuing anyway..."
fi

echo "=== Creating OSDs on all drives ==="
for disk in "${DRIVES[@]}"; do
    echo "Processing /dev/$disk..."
    
    # Check if already an OSD
    if ceph-volume lvm list /dev/$disk 2>/dev/null | grep -q "osd\."; then
        echo "  /dev/$disk already has an OSD, skipping"
        continue
    fi
    
    # Create OSD
    echo "  Creating OSD on /dev/$disk..."
    if ceph-volume lvm create --data /dev/$disk --no-systemd 2>&1 | tee /tmp/osd-create-$disk.log; then
        # Get OSD ID
        OSD_ID=$(grep -oP "osd\.\K\d+" /tmp/osd-create-$disk.log | head -1)
        if [ -n "$OSD_ID" ]; then
            echo "  OSD $OSD_ID created on /dev/$disk"
            # Enable and start service
            systemctl enable ceph-osd@$OSD_ID
            systemctl start ceph-osd@$OSD_ID
            echo "  OSD $OSD_ID service started"
        fi
    else
        echo "  WARNING: Failed to create OSD on /dev/$disk"
    fi
    echo ""
done

echo "=== Final OSD status ==="
ceph osd tree 2>&1 || echo "Could not get OSD tree"

echo "=== All OSD services ==="
systemctl list-units | grep ceph-osd | grep active || echo "No active OSD services found"

