#!/bin/bash
# Install qemu-guest-agent in VM 100 via Proxmox console
# This script provides commands to run on the Proxmox node

set -euo pipefail

VMID=100
PROXMOX_NODE="192.168.11.10"

echo "=========================================="
echo "Install Guest Agent in VM 100"
echo "=========================================="
echo ""
echo "Since qm guest exec doesn't work (guest agent not installed),"
echo "we need to access the VM via console or SSH."
echo ""
echo "Option 1: Via Proxmox Web Console"
echo "--------------------------------------"
echo "1. Open Proxmox web UI: https://$PROXMOX_NODE:8006"
echo "2. Navigate to: VM 100 -> Console"
echo "3. Login to the VM"
echo "4. Run these commands:"
echo ""
echo "   sudo apt-get update"
echo "   sudo apt-get install -y qemu-guest-agent"
echo "   sudo systemctl enable qemu-guest-agent"
echo "   sudo systemctl start qemu-guest-agent"
echo "   sudo systemctl status qemu-guest-agent"
echo ""
echo "Option 2: Via SSH (if VM has network access)"
echo "--------------------------------------"
echo "1. Get VM IP address (if available):"
echo "   ssh root@$PROXMOX_NODE 'qm config $VMID | grep net0'"
echo ""
echo "2. Try to find IP via ARP:"
echo "   ssh root@$PROXMOX_NODE 'qm config $VMID | grep -oP \"mac=\\\\K[^,]+\" | xargs -I {} arp -a | grep {}'"
echo ""
echo "3. If IP found, SSH to VM:"
echo "   ssh admin@<VM_IP>"
echo ""
echo "4. Then run the installation commands above"
echo ""
echo "Option 3: Via Proxmox Shell (qm terminal)"
echo "--------------------------------------"
echo "Note: qm terminal requires guest agent, so this won't work"
echo ""
echo "Option 4: Force Restart VM (if cloud-init should install it)"
echo "--------------------------------------"
echo "If VM was created with cloud-init that includes qemu-guest-agent,"
echo "a restart might trigger cloud-init to install it:"
echo ""
echo "   ssh root@$PROXMOX_NODE 'qm shutdown $VMID'"
echo "   # Wait for shutdown, or force stop:"
echo "   ssh root@$PROXMOX_NODE 'qm stop $VMID'"
echo "   ssh root@$PROXMOX_NODE 'qm start $VMID'"
echo ""
echo "=========================================="
echo "Verification"
echo "=========================================="
echo ""
echo "After installation, verify:"
echo ""
echo "   ssh root@$PROXMOX_NODE 'qm guest exec $VMID -- systemctl status qemu-guest-agent'"
echo ""
echo "Or run the full check script:"
echo ""
echo "   ssh root@$PROXMOX_NODE '/usr/local/bin/complete-vm-100-guest-agent-check.sh'"
echo ""

