devmatrix-scripts/proxmox/create-production-vm.sh

138 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# Proxmox Production VM Creator
# Run this on the Proxmox host as root
# Source: https://git.lemonlink.eu/devmatrix/devmatrix-scripts
set -e
# Configuration
VM_ID=101
VM_NAME="DevMatrix-Prod"
VM_IP="192.168.5.211/24"
VM_GW="192.168.5.1"
VM_CPU=4
VM_RAM=8192 # 8GB
VM_DISK=100 # GB
STORAGE="local-lvm" # Change to your storage
BRIDGE="vmbr0"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log() { echo -e "${BLUE}[PROXMOX]${NC} $1"; }
success() { echo -e "${GREEN}[✓]${NC} $1"; }
warning() { echo -e "${YELLOW}[!]${NC} $1"; }
error() { echo -e "${RED}[✗]${NC} $1"; exit 1; }
# Check if running on Proxmox
if [ ! -f "/etc/pve/priv/authkey.pub" ]; then
error "This script must be run on the Proxmox host"
fi
# Check if VM already exists
if qm status $VM_ID > /dev/null 2>&1; then
error "VM $VM_ID already exists!"
fi
log "🚀 Creating DevMatrix Production VM"
log "===================================="
log ""
log "Configuration:"
log " VM ID: $VM_ID"
log " Name: $VM_NAME"
log " CPU: $VM_CPU cores"
log " RAM: $((VM_RAM / 1024))GB"
log " Disk: ${VM_DISK}GB"
log " IP: $VM_IP"
log ""
read -p "Continue? (y/N): " confirm
if [[ $confirm != [yY] ]]; then
error "Aborted"
fi
# Download Ubuntu cloud image if not exists
CLOUD_IMAGE="/var/lib/vz/template/iso/jammy-server-cloudimg-amd64.img"
if [ ! -f "$CLOUD_IMAGE" ]; then
log "Downloading Ubuntu 22.04 cloud image..."
mkdir -p /var/lib/vz/template/iso
wget -q --show-progress \
https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img \
-O "$CLOUD_IMAGE"
success "Downloaded Ubuntu cloud image"
fi
# Create VM
log "Creating VM..."
qm create $VM_ID \
--name "$VM_NAME" \
--memory $VM_RAM \
--balloon 0 \
--cores $VM_CPU \
--cpu cputype=host \
--net0 virtio,bridge=$BRIDGE \
--scsihw virtio-scsi-single \
--ostype l26 \
--agent enabled=1
# Import disk
log "Importing disk..."
qm importdisk $VM_ID "$CLOUD_IMAGE" $STORAGE --format qcow2
# Attach disk
qm set $VM_ID --scsi0 ${STORAGE}:vm-${VM_ID}-disk-0
# Resize disk
log "Resizing disk to ${VM_DISK}GB..."
qm disk resize $VM_ID scsi0 ${VM_DISK}G
# Create CloudInit drive
log "Configuring CloudInit..."
qm set $VM_ID --ide2 ${STORAGE}:cloudinit
# Set boot order
qm set $VM_ID --boot order=scsi0
# Configure serial
qm set $VM_ID --serial0 socket --vga serial0
# Set IP configuration
qm set $VM_ID --ipconfig0 ip=$VM_IP,gw=$VM_GW
# Set user/password (change these!)
qm set $VM_ID --ciuser devmatrix
qm set $VM_ID --cipassword $(openssl rand -base64 16)
# Enable QEMU agent
qm set $VM_ID --agent enabled=1
# Start VM
log "Starting VM..."
qm start $VM_ID
# Wait for VM to boot
log "Waiting for VM to boot (30s)..."
sleep 30
success "✅ Production VM created and started!"
log ""
log "VM Details:"
log " ID: $VM_ID"
log " Name: $VM_NAME"
log " IP: $VM_IP"
log " Console: https://$(hostname -I | awk '{print $1}'):8006/#v1:0:=$VM_ID"
log ""
log "Next steps:"
log "1. Open console and get IP: qm console $VM_ID"
log "2. SSH into VM: ssh devmatrix@192.168.5.211"
log "3. Run setup: curl -fsSL https://git.lemonlink.eu/devmatrix/devmatrix-scripts/raw/branch/main/proxmox/setup-production-vm.sh | sudo bash"
log ""
log "To view VM status:"
log " qm status $VM_ID"
log " qm list"
log ""