Files
loc_az_hci/scripts/troubleshooting/recreate-template-from-cloud-image.sh
defiQUG c39465c2bd
Some checks failed
Test / test (push) Has been cancelled
Initial commit: loc_az_hci (smom-dbis-138 excluded via .gitignore)
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 09:04:46 -08:00

153 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
source ~/.bashrc
# Recreate Template from Cloud Image
# Recreates template VM 9000 from Ubuntu cloud image via SSH
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
# Load environment variables
if [ -f "$PROJECT_ROOT/.env" ]; then
set -a
source <(grep -v '^#' "$PROJECT_ROOT/.env" | grep -v '^$' | sed 's/#.*$//' | grep '=')
set +a
fi
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_step() {
echo -e "\n${BLUE}=== $1 ===${NC}"
}
PROXMOX_HOST="${PROXMOX_ML110_IP:-192.168.1.206}"
CLOUD_IMAGE="/var/lib/vz/template/iso/ubuntu-24.04-server-cloudimg-amd64.img"
VMID=9000
main() {
log_step "Recreating Template from Cloud Image"
log_info "This will recreate template VM 9000 from Ubuntu cloud image"
log_warn "All VMs cloned from this template will need to be recreated"
echo ""
# Check SSH access
log_info "Checking SSH access to Proxmox host ($PROXMOX_HOST)..."
# Try with SSH key first
SSH_KEY="$HOME/.ssh/id_ed25519_proxmox"
if [ -f "$SSH_KEY" ]; then
SSH_OPTS="-i $SSH_KEY"
else
SSH_OPTS=""
fi
if ! ssh $SSH_OPTS -o StrictHostKeyChecking=no -o ConnectTimeout=5 "root@$PROXMOX_HOST" "echo 'SSH OK'" &> /dev/null; then
log_error "SSH access to $PROXMOX_HOST failed"
log_info "Please ensure:"
log_info " 1. SSH is enabled on Proxmox host"
log_info " 2. Root login is allowed"
log_info " 3. SSH key is set up or password authentication is enabled"
exit 1
fi
log_info "✓ SSH access confirmed"
# Check if cloud image exists
log_info "Checking if cloud image exists..."
if ssh $SSH_OPTS "root@$PROXMOX_HOST" "[ -f $CLOUD_IMAGE ]"; then
log_info "✓ Cloud image found: $CLOUD_IMAGE"
else
log_error "Cloud image not found: $CLOUD_IMAGE"
log_info "Please upload Ubuntu 24.04 cloud image to Proxmox storage first"
exit 1
fi
# Stop and delete existing template
log_step "Step 1: Removing Existing Template"
log_info "Stopping VM $VMID (if running)..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm stop $VMID" 2>/dev/null || true
sleep 3
log_info "Deleting VM $VMID..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm destroy $VMID --purge" 2>/dev/null || true
sleep 3
# Create new VM shell
log_step "Step 2: Creating New VM Shell"
log_info "Creating VM $VMID..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm create $VMID \
--name ubuntu-24.04-cloudinit \
--memory 2048 \
--cores 2 \
--net0 virtio,bridge=vmbr0"
# Import cloud image
log_step "Step 3: Importing Cloud Image"
log_info "Importing cloud image (this may take a few minutes)..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm importdisk $VMID $CLOUD_IMAGE local-lvm"
# Attach disk
log_step "Step 4: Attaching Disk"
log_info "Attaching imported disk..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm set $VMID \
--scsihw virtio-scsi-pci \
--scsi0 local-lvm:vm-${VMID}-disk-0"
# Configure boot
log_step "Step 5: Configuring Boot"
log_info "Setting boot order..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm set $VMID --boot order=scsi0"
# Configure UEFI
log_step "Step 6: Configuring UEFI"
log_info "Enabling UEFI/OVMF..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm set $VMID --bios ovmf --efidisk0 local-lvm:1"
# Enable QEMU Guest Agent
log_step "Step 7: Enabling QEMU Guest Agent"
log_info "Enabling agent..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm set $VMID --agent 1"
# Configure cloud-init
log_step "Step 8: Configuring Cloud-Init"
log_info "Setting up cloud-init..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm set $VMID --ide2 local-lvm:cloudinit"
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm set $VMID --serial0 socket --vga serial0"
# Convert to template
log_step "Step 9: Converting to Template"
log_info "Converting VM to template..."
ssh $SSH_OPTS "root@$PROXMOX_HOST" "qm template $VMID"
log_step "Template Recreation Complete!"
log_info "✓ Template VM 9000 recreated from Ubuntu cloud image"
log_info "✓ Cloud-init is pre-installed in the image"
log_info "✓ QEMU Guest Agent enabled"
log_info ""
log_info "Next steps:"
log_info " 1. Recreate VMs: ./scripts/deploy/recreate-vms-smaller-disks.sh --yes"
log_info " 2. Verify VM boot and network connectivity"
}
main "$@"