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 #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
echo "=== Proxmox LXC (K3s-ready) Creator ===" ### ---------- helpers ----------
die() { echo "$*" >&2; exit 1; }
info() { echo "$*"; }
ok() { echo "$*"; }
choose_from_list() {
local prompt="$1"
shift
local items=("$@")
echo
echo "$prompt"
echo echo
### --- Container basics --- local i=1
read -rp "Container ID (e.g. 110): " CT_ID 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"
read -rp "Hostname: " HOSTNAME read -rp "Hostname: " HOSTNAME
read -rp "Rootfs size (GB, e.g. 16): " ROOTFS_SIZE read -rp "Rootfs size (GB) [16]: " ROOTFS_SIZE
read -rp "Memory (MB, e.g. 4096): " MEMORY read -rp "Memory (MB) [4096]: " MEMORY
read -rp "CPU cores (e.g. 2): " CORES read -rp "CPU cores [2]: " CORES
read -rsp "Root password: " PASSWORD read -rsp "Root password: " PASSWORD
echo echo
### --- Select LXC template --- ROOTFS_SIZE=${ROOTFS_SIZE:-16}
echo MEMORY=${MEMORY:-4096}
echo "Fetching available LXC templates..." CORES=${CORES:-2}
### ---------- templates ----------
info "Detecting LXC templates..."
mapfile -t TEMPLATES < <(pveam list local | awk '/vztmpl/ {print $1}') 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 # Prefer Ubuntu if present
echo "❌ No templates found in local storage." DEFAULT_TEMPLATE=""
echo "Run: pveam update && pveam available && pveam download local <template>" for t in "${TEMPLATES[@]}"; do
exit 1 [[ "$t" =~ ubuntu ]] && DEFAULT_TEMPLATE="$t" && break
fi
echo
echo "Select Ubuntu (or other) template:"
select TEMPLATE in "${TEMPLATES[@]}"; do
[[ -n "${TEMPLATE:-}" ]] && break
echo "Invalid selection."
done done
### --- Select network bridge --- if [[ -n "$DEFAULT_TEMPLATE" ]]; then
echo echo
echo "Detecting network bridges..." 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) mapfile -t BRIDGES < <(awk '/iface vmbr/ {print $2}' /etc/network/interfaces)
[[ ${#BRIDGES[@]} -gt 0 ]] || die "No vmbr bridges found"
if [[ ${#BRIDGES[@]} -eq 0 ]]; then DEFAULT_BRIDGE="${BRIDGES[0]}"
echo "❌ No vmbr bridges detected." read -rp "Use default bridge (${DEFAULT_BRIDGE})? [Y/n]: " yn
exit 1 if [[ "$yn" =~ ^[Nn]$ ]]; then
BRIDGE=$(choose_from_list "Select network bridge:" "${BRIDGES[@]}")
else
BRIDGE="$DEFAULT_BRIDGE"
fi fi
echo ### ---------- summary ----------
echo "Select network bridge:" cat <<EOF
select BRIDGE in "${BRIDGES[@]}"; do
[[ -n "${BRIDGE:-}" ]] && break
echo "Invalid selection."
done
echo ================== SUMMARY ==================
echo "Creating container $CT_ID ($HOSTNAME)..." 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" \ pct create "$CT_ID" "$TEMPLATE" \
--hostname "$HOSTNAME" \ --hostname "$HOSTNAME" \
--net0 "name=eth0,bridge=${BRIDGE},ip=dhcp" \ --net0 "name=eth0,bridge=$BRIDGE,ip=dhcp" \
--memory "$MEMORY" \ --memory "$MEMORY" \
--cores "$CORES" \ --cores "$CORES" \
--rootfs "local-lvm:${ROOTFS_SIZE}" \ --rootfs "local-lvm:${ROOTFS_SIZE}" \
@ -61,32 +131,27 @@ pct create "$CT_ID" "$TEMPLATE" \
--unprivileged 0 \ --unprivileged 0 \
--features nesting=1 --features nesting=1
echo "Stopping container to patch config..."
pct stop "$CT_ID" pct stop "$CT_ID"
echo "Applying K3s-required LXC configuration..." info "Applying K3s-compatible LXC configuration..."
cat <<EOF >> /etc/pve/lxc/${CT_ID}.conf cat <<EOF >> /etc/pve/lxc/${CT_ID}.conf
# --- K3s / Kubernetes requirements ---
lxc.apparmor.profile: unconfined lxc.apparmor.profile: unconfined
lxc.cgroup.devices.allow: a lxc.cgroup.devices.allow: a
lxc.cap.drop: lxc.cap.drop:
lxc.mount.auto: "proc:rw sys:rw" lxc.mount.auto: "proc:rw sys:rw"
EOF EOF
echo "Starting container..."
pct start "$CT_ID" pct start "$CT_ID"
sleep 5 sleep 5
echo "Setting root password..." info "Configuring container..."
pct exec "$CT_ID" -- bash -c "echo 'root:${PASSWORD}' | chpasswd" pct exec "$CT_ID" -- bash -c "echo 'root:$PASSWORD' | chpasswd"
echo "Installing minimal dependencies..."
pct exec "$CT_ID" -- bash -c " pct exec "$CT_ID" -- bash -c "
apt update && apt update &&
apt install -y curl ca-certificates apt install -y curl ca-certificates
" "
echo "Creating /etc/rc.local inside container..."
pct exec "$CT_ID" -- bash -c " pct exec "$CT_ID" -- bash -c "
cat <<'EOF' > /etc/rc.local cat <<'EOF' > /etc/rc.local
#!/bin/sh -e #!/bin/sh -e
@ -95,12 +160,10 @@ if [ ! -e /dev/kmsg ]; then
fi fi
mount --make-rshared / mount --make-rshared /
EOF EOF
chmod +x /etc/rc.local chmod +x /etc/rc.local
/etc/rc.local /etc/rc.local
" "
echo ok "Container $CT_ID is ready for K3s installation."
echo "✅ Container $CT_ID is ready for K3s installation." echo "Next step inside container:"
echo " Networking: DHCP on ${BRIDGE}" echo " curl -sfL https://get.k3s.io | sh"
echo " Template: ${TEMPLATE}"