Add create_k3s_ready_lxc.sh

This commit is contained in:
Karolis 2025-12-26 16:14:48 +00:00
commit 859daee4ef

106
create_k3s_ready_lxc.sh Normal file
View file

@ -0,0 +1,106 @@
#!/usr/bin/env bash
set -euo pipefail
echo "=== Proxmox LXC (K3s-ready) Creator ==="
echo
### --- Container basics ---
read -rp "Container ID (e.g. 110): " CT_ID
read -rp "Hostname: " HOSTNAME
read -rp "Rootfs size (GB, e.g. 16): " ROOTFS_SIZE
read -rp "Memory (MB, e.g. 4096): " MEMORY
read -rp "CPU cores (e.g. 2): " CORES
read -rsp "Root password: " PASSWORD
echo
### --- Select LXC template ---
echo
echo "Fetching available LXC templates..."
mapfile -t TEMPLATES < <(pveam list local | awk '/vztmpl/ {print $1}')
if [[ ${#TEMPLATES[@]} -eq 0 ]]; then
echo "❌ No templates found in local storage."
echo "Run: pveam update && pveam available && pveam download local <template>"
exit 1
fi
echo
echo "Select Ubuntu (or other) template:"
select TEMPLATE in "${TEMPLATES[@]}"; do
[[ -n "${TEMPLATE:-}" ]] && break
echo "Invalid selection."
done
### --- Select network bridge ---
echo
echo "Detecting network bridges..."
mapfile -t BRIDGES < <(awk '/iface vmbr/ {print $2}' /etc/network/interfaces)
if [[ ${#BRIDGES[@]} -eq 0 ]]; then
echo "❌ No vmbr bridges detected."
exit 1
fi
echo
echo "Select network bridge:"
select BRIDGE in "${BRIDGES[@]}"; do
[[ -n "${BRIDGE:-}" ]] && break
echo "Invalid selection."
done
echo
echo "Creating container $CT_ID ($HOSTNAME)..."
pct create "$CT_ID" "$TEMPLATE" \
--hostname "$HOSTNAME" \
--net0 "name=eth0,bridge=${BRIDGE},ip=dhcp" \
--memory "$MEMORY" \
--cores "$CORES" \
--rootfs "local-lvm:${ROOTFS_SIZE}" \
--swap 0 \
--unprivileged 0 \
--features nesting=1
echo "Stopping container to patch config..."
pct stop "$CT_ID"
echo "Applying K3s-required LXC configuration..."
cat <<EOF >> /etc/pve/lxc/${CT_ID}.conf
# --- K3s / Kubernetes requirements ---
lxc.apparmor.profile: unconfined
lxc.cgroup.devices.allow: a
lxc.cap.drop:
lxc.mount.auto: "proc:rw sys:rw"
EOF
echo "Starting container..."
pct start "$CT_ID"
sleep 5
echo "Setting root password..."
pct exec "$CT_ID" -- bash -c "echo 'root:${PASSWORD}' | chpasswd"
echo "Installing minimal dependencies..."
pct exec "$CT_ID" -- bash -c "
apt update &&
apt install -y curl ca-certificates
"
echo "Creating /etc/rc.local inside container..."
pct exec "$CT_ID" -- bash -c "
cat <<'EOF' > /etc/rc.local
#!/bin/sh -e
if [ ! -e /dev/kmsg ]; then
ln -s /dev/console /dev/kmsg
fi
mount --make-rshared /
EOF
chmod +x /etc/rc.local
/etc/rc.local
"
echo
echo "✅ Container $CT_ID is ready for K3s installation."
echo " Networking: DHCP on ${BRIDGE}"
echo " Template: ${TEMPLATE}"