Update create_k3s_ready_lxc.sh

This commit is contained in:
Karolis 2025-12-26 16:18:36 +00:00
parent 859daee4ef
commit ad4bcaa840

View file

@ -1,59 +1,129 @@
#!/usr/bin/env bash
set -euo pipefail
echo "=== Proxmox LXC (K3s-ready) Creator ==="
echo
### ---------- helpers ----------
die() { echo "$*" >&2; exit 1; }
info() { echo "$*"; }
ok() { echo "$*"; }
choose_from_list() {
local prompt="$1"
shift
local items=("$@")
echo
echo "$prompt"
echo
local i=1
for item in "${items[@]}"; do
printf " %2d) %s\n" "$i" "$item"
((i++))
done
while true; do
echo
read -rp "Enter number or exact value: " choice
# numeric
if [[ "$choice" =~ ^[0-9]+$ ]] && (( choice >= 1 && choice <= ${#items[@]} )); then
echo "${items[choice-1]}"
return
fi
# exact match
for item in "${items[@]}"; do
if [[ "$choice" == "$item" ]]; then
echo "$item"
return
fi
done
echo "❌ Invalid selection. Try again."
done
}
### ---------- banner ----------
cat <<EOF
=================================================
Proxmox LXC Creator — K3s READY (No K3s)
=================================================
EOF
### ---------- basic input ----------
read -rp "Container ID: " CT_ID
[[ "$CT_ID" =~ ^[0-9]+$ ]] || die "Container ID must be numeric"
pct status "$CT_ID" &>/dev/null && die "Container ID $CT_ID already exists"
### --- 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 -rp "Rootfs size (GB) [16]: " ROOTFS_SIZE
read -rp "Memory (MB) [4096]: " MEMORY
read -rp "CPU cores [2]: " CORES
read -rsp "Root password: " PASSWORD
echo
### --- Select LXC template ---
echo
echo "Fetching available LXC templates..."
ROOTFS_SIZE=${ROOTFS_SIZE:-16}
MEMORY=${MEMORY:-4096}
CORES=${CORES:-2}
### ---------- templates ----------
info "Detecting LXC templates..."
mapfile -t TEMPLATES < <(pveam list local | awk '/vztmpl/ {print $1}')
[[ ${#TEMPLATES[@]} -gt 0 ]] || die "No templates found in local storage"
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."
# Prefer Ubuntu if present
DEFAULT_TEMPLATE=""
for t in "${TEMPLATES[@]}"; do
[[ "$t" =~ ubuntu ]] && DEFAULT_TEMPLATE="$t" && break
done
### --- Select network bridge ---
echo
echo "Detecting network bridges..."
if [[ -n "$DEFAULT_TEMPLATE" ]]; then
echo
read -rp "Ubuntu template found. Use it? [Y/n]: " yn
if [[ ! "$yn" =~ ^[Nn]$ ]]; then
TEMPLATE="$DEFAULT_TEMPLATE"
fi
fi
[[ -z "${TEMPLATE:-}" ]] && TEMPLATE=$(choose_from_list "Select LXC template:" "${TEMPLATES[@]}")
### ---------- bridges ----------
info "Detecting network bridges..."
mapfile -t BRIDGES < <(awk '/iface vmbr/ {print $2}' /etc/network/interfaces)
[[ ${#BRIDGES[@]} -gt 0 ]] || die "No vmbr bridges found"
if [[ ${#BRIDGES[@]} -eq 0 ]]; then
echo "❌ No vmbr bridges detected."
exit 1
DEFAULT_BRIDGE="${BRIDGES[0]}"
read -rp "Use default bridge (${DEFAULT_BRIDGE})? [Y/n]: " yn
if [[ "$yn" =~ ^[Nn]$ ]]; then
BRIDGE=$(choose_from_list "Select network bridge:" "${BRIDGES[@]}")
else
BRIDGE="$DEFAULT_BRIDGE"
fi
echo
echo "Select network bridge:"
select BRIDGE in "${BRIDGES[@]}"; do
[[ -n "${BRIDGE:-}" ]] && break
echo "Invalid selection."
done
### ---------- summary ----------
cat <<EOF
echo
echo "Creating container $CT_ID ($HOSTNAME)..."
================== SUMMARY ==================
Container ID : $CT_ID
Hostname : $HOSTNAME
Template : $TEMPLATE
Bridge : $BRIDGE (DHCP)
Disk : ${ROOTFS_SIZE}G
Memory : ${MEMORY} MB
Cores : ${CORES}
============================================
EOF
read -rp "Proceed? [y/N]: " confirm
[[ "$confirm" =~ ^[Yy]$ ]] || die "Aborted by user"
### ---------- create ----------
info "Creating container..."
pct create "$CT_ID" "$TEMPLATE" \
--hostname "$HOSTNAME" \
--net0 "name=eth0,bridge=${BRIDGE},ip=dhcp" \
--net0 "name=eth0,bridge=$BRIDGE,ip=dhcp" \
--memory "$MEMORY" \
--cores "$CORES" \
--rootfs "local-lvm:${ROOTFS_SIZE}" \
@ -61,32 +131,27 @@ pct create "$CT_ID" "$TEMPLATE" \
--unprivileged 0 \
--features nesting=1
echo "Stopping container to patch config..."
pct stop "$CT_ID"
echo "Applying K3s-required LXC configuration..."
info "Applying K3s-compatible 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"
info "Configuring container..."
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
@ -95,12 +160,10 @@ if [ ! -e /dev/kmsg ]; then
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}"
ok "Container $CT_ID is ready for K3s installation."
echo "Next step inside container:"
echo " curl -sfL https://get.k3s.io | sh"