#!/bin/bash # Colors GREEN="\033[1;32m" RED="\033[1;31m" YELLOW="\033[1;33m" NC="\033[0m" echo -e "${GREEN}Proxmox Apt-Cacher-NG Setup for All LXC/VM${NC}" # Ask for the apt-cacher-ng server IP and port read -rp "Enter your apt-cacher-ng IP address (default: 192.168.1.10): " CACHE_IP CACHE_IP="${CACHE_IP:-192.168.1.10}" read -rp "Enter apt-cacher-ng port (default: 3142): " CACHE_PORT CACHE_PORT="${CACHE_PORT:-3142}" APT_PROXY="Acquire::http::Proxy \"http://${CACHE_IP}:${CACHE_PORT}\";" echo -e "\n${YELLOW}Using APT Proxy: ${APT_PROXY}${NC}\n" # Confirm before proceeding read -rp "Proceed to update all LXC containers and VMs with this proxy? (y/n): " CONFIRM [[ "$CONFIRM" != "y" ]] && echo "Aborted." && exit 1 ### Update LXC containers ### echo -e "\n${GREEN}Updating LXC Containers...${NC}" for CTID in $(pct list | awk 'NR>1 {print $1}'); do echo -e "\n${YELLOW}Updating LXC: $CTID${NC}" pct exec "$CTID" -- bash -c " mkdir -p /etc/apt/apt.conf.d echo '${APT_PROXY}' > /etc/apt/apt.conf.d/01proxy " echo -e "${GREEN}LXC $CTID updated successfully.${NC}" done ### Update VMs via QEMU Guest Agent ### echo -e "\n${GREEN}Updating VMs via QEMU Agent...${NC}" for VMID in $(qm list | awk 'NR>1 {print $1}'); do if qm guest cmd "$VMID" get-osinfo &>/dev/null; then echo -e "\n${YELLOW}Updating VM: $VMID${NC}" qm guest exec "$VMID" -- bash -c " mkdir -p /etc/apt/apt.conf.d echo '${APT_PROXY}' > /etc/apt/apt.conf.d/01proxy " &>/dev/null echo -e "${GREEN}VM $VMID updated successfully.${NC}" else echo -e "${RED}Skipping VM $VMID – QEMU Guest Agent not available.${NC}" fi done echo -e "\n${GREEN}All done. apt-cacher-ng configuration updated on containers and agent-enabled VMs.${NC}"