#!/bin/bash set -e CTID="" CONTAINER_NAME="" DISTRO="" ROOTPASS="" while getopts ":c:n:d:p:" flag; do case "${flag}" in c) CTID="${OPTARG}" ;; n) CONTAINER_NAME="${OPTARG}" ;; d) DISTRO="${OPTARG}" ;; p) ROOTPASS="${OPTARG}" ;; \?) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done if [[ -z $CTID || -z $CONTAINER_NAME || -z $DISTRO || -z $ROOTPASS ]]; then echo "CTID, CONTAINER_NAME, DISTRO, ROOTPASS arguments are required." echo "Usage: ./create-lxc.sh -c -n -d -p " exit 1 fi template=$(pveam available | grep "$DISTRO" | sort -rV | head -n 1 | awk '{print $2}') if [[ -z $template ]]; then echo "$DISTRO template not found." exit 1 fi template=$(pveam list local | grep "$template" | awk '{print $1}') if [[ -z $template ]]; then echo "$DISTRO template not found. Downloading..." pveam download local "$template" fi pct create "$CTID" "$template" --hostname "$CONTAINER_NAME" --memory 1024 --storage local-lvm --net0 name=eth0,bridge=vmbr0,firewall=1,ip=dhcp --unprivileged --ostype "$DISTRO" --password="$ROOTPASS" echo -e "lxc.cgroup.devices.allow: c 10:200 rwm\nlxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file" >> /etc/pve/lxc/"$CTID".conf pct start "$CTID" pct status "$CTID" | grep "status: running" || sleep 10 echo lxc now running