#!/bin/bash
# fix-all-vm-boot.sh
# Fix "Nothing to boot" issue for all VMs by importing OS image

set -euo pipefail

PROXMOX_1_HOST="192.168.11.10"
PROXMOX_2_HOST="192.168.11.11"
PROXMOX_PASS="L@kers2010"

SITE1_VMS="136 139 141 142 145 146 150 151"
SITE2_VMS="101 104 137 138 144 148"

IMAGE_PATH="/var/lib/vz/template/iso/ubuntu-22.04-cloud.img"
STORAGE="local-lvm"

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

fix_vm() {
    local host=$1
    local vmid=$2
    local vmname=$3
    
    echo "Processing VMID $vmid ($vmname)..."
    
    # Stop VM
    status=$(sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
        "qm status $vmid 2>&1 | grep -oP 'status: \\K\\w+' || echo 'stopped'")
    
    if [ "$status" = "running" ]; then
        echo "  Stopping VM..."
        sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
            "qm stop $vmid" 2>&1 | head -1 || true
        sleep 3
    fi
    
    # Unlock if locked
    sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
        "qm unlock $vmid" 2>&1 | head -1 || true
    
    # Check if disk has data
    disk_usage=$(sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
        "lvs | grep \"vm-${vmid}-disk-0\" | awk '{print \$6}'" 2>/dev/null || echo "0.00")
    
    if [ "$disk_usage" != "0.00" ] && [ -n "$disk_usage" ]; then
        echo "  ✅ Disk already has data ($disk_usage used)"
        echo "  Starting VM..."
        sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
            "qm start $vmid" 2>&1 | head -1 || true
        return 0
    fi
    
    # Import image to a temporary disk first
    echo "  Importing OS image..."
    result=$(sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
        "qm importdisk $vmid $IMAGE_PATH $STORAGE --format raw 2>&1" || true)
    
    sleep 5
    
    # Find the imported disk (should be disk-1 or higher)
    imported_disk=$(sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
        "lvs | grep \"vm-${vmid}-disk\" | grep -v 'cloudinit' | grep -v 'disk-0' | tail -1 | awk '{print \$1}'" 2>/dev/null || echo "")
    
    if [ -n "$imported_disk" ]; then
        echo "  Found imported disk: $imported_disk"
        echo "  Copying to main disk..."
        # Copy the imported disk to the main disk
        sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
            "dd if=/dev/pve/${imported_disk} of=/dev/pve/vm-${vmid}-disk-0 bs=4M status=progress 2>&1 | tail -3" || true
        echo "  Copy complete"
    else
        echo "  ⚠️  No imported disk found, trying direct import..."
    fi
    
    # Ensure boot order
    sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
        "qm set $vmid --boot order=scsi0" 2>&1 | head -1 || true
    
    echo "  Starting VM..."
    sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$host \
        "qm start $vmid" 2>&1 | head -1 || true
    
    echo "  ✅ VM started"
}

echo "=========================================="
echo "Fix VM Boot Issue - Import OS Image"
echo "=========================================="
echo ""

echo "Site 1 (ml110-01):"
for vmid in $SITE1_VMS; do
    name=$(sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$PROXMOX_1_HOST \
        "qm config $vmid | grep '^name:' | cut -d' ' -f2" || echo "unknown")
    fix_vm "$PROXMOX_1_HOST" "$vmid" "$name"
    echo ""
done

echo "Site 2 (r630-01):"
for vmid in $SITE2_VMS; do
    name=$(sshpass -p "$PROXMOX_PASS" ssh -o StrictHostKeyChecking=no root@$PROXMOX_2_HOST \
        "qm config $vmid | grep '^name:' | cut -d' ' -f2" || echo "unknown")
    fix_vm "$PROXMOX_2_HOST" "$vmid" "$name"
    echo ""
done

echo "=========================================="
echo "Boot fix complete!"
echo "=========================================="

