#!/bin/bash
# Check VM 100 configuration before starting
# Run on Proxmox node: root@ml110-01

VMID=100

echo "=== Pre-Start Check for VM $VMID ==="
echo ""

# 1. VM Status
echo "1. VM Status:"
qm status $VMID 2>&1
echo ""

# 2. Boot Configuration
echo "2. Boot Configuration:"
BOOT_CONFIG=$(qm config $VMID 2>&1 | grep -E "^boot:|^scsi0:|^ide2:")
if [ -z "$BOOT_CONFIG" ]; then
    echo "   ⚠️  No boot configuration found"
else
    echo "$BOOT_CONFIG" | while read line; do
        echo "   $line"
    done
fi
echo ""

# 3. Disk Configuration
echo "3. Disk Configuration:"
DISK_CONFIG=$(qm config $VMID 2>&1 | grep -E "^scsi0:|^scsi1:")
if [ -z "$DISK_CONFIG" ]; then
    echo "   ⚠️  No disk configuration found"
else
    echo "$DISK_CONFIG" | while read line; do
        echo "   $line"
        # Check if disk exists
        if echo "$line" | grep -q "local-lvm:vm-$VMID-disk"; then
            DISK_NAME=$(echo "$line" | sed -n 's/.*local-lvm:\(vm-[0-9]*-disk-[0-9]*\).*/\1/p')
            if [ -n "$DISK_NAME" ]; then
                if lvs | grep -q "$DISK_NAME"; then
                    DISK_SIZE=$(lvs | grep "$DISK_NAME" | awk '{print $4}')
                    echo "      ✅ Disk exists: $DISK_NAME ($DISK_SIZE)"
                else
                    echo "      ⚠️  Disk not found: $DISK_NAME"
                fi
            fi
        fi
    done
fi
echo ""

# 4. Check for Image Import
echo "4. Checking for imported image:"
IMPORTED_DISKS=$(lvs | grep "vm-$VMID-disk" | awk '{print $1}')
if [ -z "$IMPORTED_DISKS" ]; then
    echo "   ⚠️  No imported disks found for VM $VMID"
    echo "   ⚠️  VM may have blank disk - will not boot!"
else
    echo "   ✅ Found disks:"
    echo "$IMPORTED_DISKS" | while read disk; do
        SIZE=$(lvs | grep "$disk" | awk '{print $4}')
        echo "      - $disk ($SIZE)"
    done
fi
echo ""

# 5. Cloud-init Configuration
echo "5. Cloud-init Configuration:"
CLOUDINIT_CONFIG=$(qm config $VMID 2>&1 | grep -E "^ide2:|^ciuser:|^ipconfig0:")
if [ -z "$CLOUDINIT_CONFIG" ]; then
    echo "   ⚠️  No cloud-init configuration found"
else
    echo "$CLOUDINIT_CONFIG" | while read line; do
        echo "   $line"
    done
fi
echo ""

# 6. Network Configuration
echo "6. Network Configuration:"
NETWORK_CONFIG=$(qm config $VMID 2>&1 | grep -E "^net0:")
if [ -z "$NETWORK_CONFIG" ]; then
    echo "   ⚠️  No network configuration found"
else
    echo "   $NETWORK_CONFIG"
fi
echo ""

# 7. Guest Agent
echo "7. Guest Agent Configuration:"
AGENT_CONFIG=$(qm config $VMID 2>&1 | grep -E "^agent:")
if [ -z "$AGENT_CONFIG" ]; then
    echo "   ⚠️  Guest agent not configured"
else
    echo "   $AGENT_CONFIG"
fi
echo ""

# 8. Summary and Recommendations
echo "=== Summary ==="
echo ""

# Check critical issues
ISSUES=0

# Check if boot order is set
if ! qm config $VMID 2>&1 | grep -q "^boot:"; then
    echo "⚠️  ISSUE: Boot order not set"
    ISSUES=$((ISSUES + 1))
fi

# Check if scsi0 disk exists and has data
SCSI0_DISK=$(qm config $VMID 2>&1 | grep "^scsi0:" | sed -n 's/.*local-lvm:\(vm-[0-9]*-disk-[0-9]*\).*/\1/p')
if [ -n "$SCSI0_DISK" ]; then
    if ! lvs | grep -q "$SCSI0_DISK"; then
        echo "⚠️  ISSUE: scsi0 disk not found: $SCSI0_DISK"
        ISSUES=$((ISSUES + 1))
    else
        # Check disk size (should be > 0)
        DISK_SIZE_RAW=$(lvs --units g | grep "$SCSI0_DISK" | awk '{print $4}' | sed 's/g//')
        if [ -n "$DISK_SIZE_RAW" ] && [ "$(echo "$DISK_SIZE_RAW < 1" | bc 2>/dev/null || echo 0)" = "1" ]; then
            echo "⚠️  ISSUE: scsi0 disk appears empty (< 1GB)"
            ISSUES=$((ISSUES + 1))
        fi
    fi
else
    echo "⚠️  ISSUE: scsi0 disk not configured"
    ISSUES=$((ISSUES + 1))
fi

# Check if image was imported
if [ -z "$IMPORTED_DISKS" ]; then
    echo "⚠️  ISSUE: No image imported - VM will not boot"
    ISSUES=$((ISSUES + 1))
fi

if [ $ISSUES -eq 0 ]; then
    echo "✅ No critical issues found"
    echo ""
    echo "VM appears ready to start. Run:"
    echo "  qm start $VMID"
else
    echo ""
    echo "⚠️  Found $ISSUES critical issue(s)"
    echo ""
    echo "Do NOT start the VM until these are resolved!"
    echo ""
    echo "Possible fixes:"
    echo "1. If image not imported, check if image exists:"
    echo "   find /var/lib/vz/template/iso -name 'ubuntu-22.04-cloud.img'"
    echo ""
    echo "2. If boot order not set:"
    echo "   qm set $VMID --boot order=scsi0"
    echo ""
    echo "3. If disk not attached:"
    echo "   qm set $VMID --scsi0 local-lvm:vm-$VMID-disk-X,format=qcow2"
fi

echo ""

