#!/bin/bash
# Script to check ubuntu-vg and prepare a drive for Ceph OSD
# Run on R630-01

set -e

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

echo "=========================================="
echo "Checking ubuntu-vg and Preparing for Ceph OSD"
echo "=========================================="
echo ""

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

echo -e "${BLUE}=== Step 1: Current ubuntu-vg Status ===${NC}"
echo "-----------------------------------"
echo "Volume Group Information:"
vgs ubuntu-vg 2>/dev/null || echo -e "${YELLOW}ubuntu-vg not found${NC}"
echo ""

echo "Logical Volumes in ubuntu-vg:"
lvs ubuntu-vg 2>/dev/null || echo -e "${YELLOW}No logical volumes found${NC}"
echo ""

echo "Physical Volumes in ubuntu-vg:"
pvs | grep ubuntu-vg || echo -e "${YELLOW}No physical volumes found${NC}"
echo ""

echo -e "${BLUE}=== Step 2: All Block Devices ===${NC}"
echo "-----------------------------------"
lsblk -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE,MODEL | grep -E "NAME|sd"
echo ""

echo -e "${BLUE}=== Step 3: Identifying 250GB Drives ===${NC}"
echo "-----------------------------------"
echo "Drives that are ~250GB (247-248 GB):"
for disk in /dev/sd[a-z]; do
    if [ -b "$disk" ]; then
        size_info=$(fdisk -l "$disk" 2>/dev/null | grep "^Disk $disk" | awk '{print $3, $4}')
        if [ ! -z "$size_info" ]; then
            size_num=$(echo "$size_info" | grep -oE '[0-9]+' | head -1)
            if [ "$size_num" -ge 247 ] && [ "$size_num" -le 248 ]; then
                echo -e "${GREEN}  $disk: ${size_info}${NC}"
                # Check if it's in ubuntu-vg
                in_vg=$(pvs 2>/dev/null | grep "$disk" | grep ubuntu-vg | wc -l)
                if [ "$in_vg" -gt 0 ]; then
                    echo -e "    ${YELLOW}  → In ubuntu-vg volume group${NC}"
                fi
            fi
        fi
    fi
done
echo ""

echo -e "${BLUE}=== Step 4: Checking What's Using ubuntu-vg ===${NC}"
echo "-----------------------------------"
if vgs ubuntu-vg &>/dev/null; then
    echo "Logical volumes in ubuntu-vg:"
    lvs ubuntu-vg -o lv_name,lv_size,lv_attr,mountpoint 2>/dev/null
    echo ""
    echo "Checking if LVs are mounted:"
    for lv in $(lvs ubuntu-vg -o lv_path --no-headings 2>/dev/null | awk '{print $1}'); do
        if [ ! -z "$lv" ]; then
            mount_point=$(mount | grep "$lv" | awk '{print $3}')
            if [ ! -z "$mount_point" ]; then
                echo -e "  ${YELLOW}$lv is mounted at $mount_point${NC}"
            else
                echo -e "  ${GREEN}$lv is not mounted${NC}"
            fi
        fi
    done
else
    echo -e "${YELLOW}ubuntu-vg not found${NC}"
fi
echo ""

echo -e "${BLUE}=== Step 5: Current Ceph OSD Status ===${NC}"
echo "-----------------------------------"
ceph osd tree 2>/dev/null || echo -e "${YELLOW}Ceph not accessible${NC}"
echo ""

echo "=========================================="
echo -e "${GREEN}Summary and Recommendations${NC}"
echo "=========================================="
echo ""
echo "If ubuntu-vg is not needed:"
echo "1. Unmount any mounted logical volumes"
echo "2. Remove logical volumes from ubuntu-vg"
echo "3. Remove physical volumes from ubuntu-vg"
echo "4. Remove ubuntu-vg volume group"
echo "5. Wipe the drive(s) to prepare for Ceph OSD"
echo ""
echo "If you need to free up a drive for Ceph OSD:"
echo "1. Identify which drive to use (e.g., /dev/sdc)"
echo "2. Remove it from ubuntu-vg (if safe to do so)"
echo "3. Wipe the drive: wipefs -a /dev/sdX"
echo "4. Create Ceph OSD: ceph-volume lvm create --data /dev/sdX"
echo ""
echo -e "${YELLOW}WARNING: Removing drives from ubuntu-vg will destroy data!${NC}"
echo "Make sure ubuntu-vg is not needed before proceeding."
echo ""

