Files
proxmox/scripts/remove-containers.sh

108 lines
2.9 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
# Remove and purge Proxmox LXC containers by VMID range
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Check if running on Proxmox host
if ! command -v pct >/dev/null 2>&1; then
log_error "This script must be run on a Proxmox host (pct command not found)"
exit 1
fi
# Check if running as root
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run as root"
exit 1
fi
# Parse arguments
START_VMID="${1:-106}"
END_VMID="${2:-112}"
if [[ ! "$START_VMID" =~ ^[0-9]+$ ]] || [[ ! "$END_VMID" =~ ^[0-9]+$ ]]; then
log_error "Invalid VMID range. Usage: $0 [START_VMID] [END_VMID]"
exit 1
fi
if [[ $START_VMID -gt $END_VMID ]]; then
log_error "START_VMID ($START_VMID) must be less than or equal to END_VMID ($END_VMID)"
exit 1
fi
log_info "========================================="
log_info "Remove and Purge Containers"
log_info "========================================="
log_info "VMID Range: $START_VMID to $END_VMID"
log_info ""
# Confirm deletion
log_warn "WARNING: This will PERMANENTLY DELETE containers $START_VMID through $END_VMID"
log_warn "All data in these containers will be lost!"
echo ""
read -p "$(echo -e ${YELLOW}Continue? [y/N]: ${NC})" -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
log_info "Operation cancelled"
exit 0
fi
echo ""
# Process each VMID
for vmid in $(seq $START_VMID $END_VMID); do
log_info "Processing container $vmid..."
# Check if container exists
if ! pct list | grep -q "^\s*$vmid\s"; then
log_warn "Container $vmid does not exist, skipping"
continue
fi
# Get container status
local status
status=$(pct status "$vmid" 2>/dev/null | awk '{print $2}' || echo "unknown")
log_info "Container $vmid status: $status"
# Stop container if running
if [[ "$status" == "running" ]]; then
log_info "Stopping container $vmid..."
pct stop "$vmid" --timeout 30 || {
log_warn "Failed to stop container $vmid gracefully, forcing stop..."
pct stop "$vmid" --skiplock || true
}
sleep 2
fi
# Remove container
log_info "Removing container $vmid..."
if pct destroy "$vmid" --purge 2>/dev/null; then
log_success "Container $vmid removed and purged"
else
log_error "Failed to remove container $vmid"
fi
done
log_info ""
log_success "========================================="
log_success "Container removal completed!"
log_success "========================================="
log_info ""
log_info "Removed containers: $START_VMID through $END_VMID"
log_info ""