Add files via upload
This commit is contained in:
		
							
								
								
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								LICENSE
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					MIT License
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Copyright (c) 2020 whiskerz007
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
				
			||||||
 | 
					of this software and associated documentation files (the "Software"), to deal
 | 
				
			||||||
 | 
					in the Software without restriction, including without limitation the rights
 | 
				
			||||||
 | 
					to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
				
			||||||
 | 
					copies of the Software, and to permit persons to whom the Software is
 | 
				
			||||||
 | 
					furnished to do so, subject to the following conditions:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					The above copyright notice and this permission notice shall be included in all
 | 
				
			||||||
 | 
					copies or substantial portions of the Software.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
				
			||||||
 | 
					IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
				
			||||||
 | 
					FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
				
			||||||
 | 
					AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
				
			||||||
 | 
					LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
				
			||||||
 | 
					OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | 
				
			||||||
 | 
					SOFTWARE.
 | 
				
			||||||
@@ -1 +1,5 @@
 | 
				
			|||||||
# proxmox_debian_lxc
 | 
					# Proxmox with Docker Template
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					clear; bash -c "$(wget -qLO - https://github.com/Sthopeless/proxmox_debian_lxc/raw/main/create_container.sh)"
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										175
									
								
								create_container.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										175
									
								
								create_container.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,175 @@
 | 
				
			|||||||
 | 
					#!/usr/bin/env bash
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Setup script environment
 | 
				
			||||||
 | 
					set -o errexit  #Exit immediately if a pipeline returns a non-zero status
 | 
				
			||||||
 | 
					set -o errtrace #Trap ERR from shell functions, command substitutions, and commands from subshell
 | 
				
			||||||
 | 
					set -o nounset  #Treat unset variables as an error
 | 
				
			||||||
 | 
					set -o pipefail #Pipe will exit with last non-zero status if applicable
 | 
				
			||||||
 | 
					shopt -s expand_aliases
 | 
				
			||||||
 | 
					alias die='EXIT=$? LINE=$LINENO error_exit'
 | 
				
			||||||
 | 
					trap die ERR
 | 
				
			||||||
 | 
					trap cleanup EXIT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function error_exit() {
 | 
				
			||||||
 | 
					  trap - ERR
 | 
				
			||||||
 | 
					  local DEFAULT='Unknown failure occured.'
 | 
				
			||||||
 | 
					  local REASON="\e[97m${1:-$DEFAULT}\e[39m"
 | 
				
			||||||
 | 
					  local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
 | 
				
			||||||
 | 
					  msg "$FLAG $REASON"
 | 
				
			||||||
 | 
					  [ ! -z ${CTID-} ] && cleanup_ctid
 | 
				
			||||||
 | 
					  exit $EXIT
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					function warn() {
 | 
				
			||||||
 | 
					  local REASON="\e[97m$1\e[39m"
 | 
				
			||||||
 | 
					  local FLAG="\e[93m[WARNING]\e[39m"
 | 
				
			||||||
 | 
					  msg "$FLAG $REASON"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					function info() {
 | 
				
			||||||
 | 
					  local REASON="$1"
 | 
				
			||||||
 | 
					  local FLAG="\e[36m[INFO]\e[39m"
 | 
				
			||||||
 | 
					  msg "$FLAG $REASON"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					function msg() {
 | 
				
			||||||
 | 
					  local TEXT="$1"
 | 
				
			||||||
 | 
					  echo -e "$TEXT"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					function cleanup_ctid() {
 | 
				
			||||||
 | 
					  if [ ! -z ${MOUNT+x} ]; then
 | 
				
			||||||
 | 
					    pct unmount $CTID
 | 
				
			||||||
 | 
					  fi
 | 
				
			||||||
 | 
					  if $(pct status $CTID &>/dev/null); then
 | 
				
			||||||
 | 
					    if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
 | 
				
			||||||
 | 
					      pct stop $CTID
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					    pct destroy $CTID
 | 
				
			||||||
 | 
					  elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
 | 
				
			||||||
 | 
					    pvesm free $ROOTFS
 | 
				
			||||||
 | 
					  fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					function cleanup() {
 | 
				
			||||||
 | 
					  popd >/dev/null
 | 
				
			||||||
 | 
					  rm -rf $TEMP_DIR
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					function load_module() {
 | 
				
			||||||
 | 
					  if ! $(lsmod | grep -Fq $1); then
 | 
				
			||||||
 | 
					    modprobe $1 &>/dev/null || \
 | 
				
			||||||
 | 
					      die "Failed to load '$1' module."
 | 
				
			||||||
 | 
					  fi
 | 
				
			||||||
 | 
					  MODULES_PATH=/etc/modules
 | 
				
			||||||
 | 
					  if ! $(grep -Fxq "$1" $MODULES_PATH); then
 | 
				
			||||||
 | 
					    echo "$1" >> $MODULES_PATH || \
 | 
				
			||||||
 | 
					      die "Failed to add '$1' module to load at boot."
 | 
				
			||||||
 | 
					  fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					TEMP_DIR=$(mktemp -d)
 | 
				
			||||||
 | 
					pushd $TEMP_DIR >/dev/null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Download setup script
 | 
				
			||||||
 | 
					wget -qL https://github.com/Sthopeless/proxmox_debian_lxc/raw/main/setup.sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Detect modules and automatically load at boot
 | 
				
			||||||
 | 
					load_module aufs
 | 
				
			||||||
 | 
					load_module overlay
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Select storage location
 | 
				
			||||||
 | 
					while read -r line; do
 | 
				
			||||||
 | 
					  TAG=$(echo $line | awk '{print $1}')
 | 
				
			||||||
 | 
					  TYPE=$(echo $line | awk '{printf "%-10s", $2}')
 | 
				
			||||||
 | 
					  FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
 | 
				
			||||||
 | 
					  ITEM="  Type: $TYPE Free: $FREE "
 | 
				
			||||||
 | 
					  OFFSET=2
 | 
				
			||||||
 | 
					  if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
 | 
				
			||||||
 | 
					    MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
 | 
				
			||||||
 | 
					  fi
 | 
				
			||||||
 | 
					  STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
 | 
				
			||||||
 | 
					done < <(pvesm status -content rootdir | awk 'NR>1')
 | 
				
			||||||
 | 
					if [ $((${#STORAGE_MENU[@]}/3)) -eq 0 ]; then
 | 
				
			||||||
 | 
					  warn "'Container' needs to be selected for at least one storage location."
 | 
				
			||||||
 | 
					  die "Unable to detect valid storage location."
 | 
				
			||||||
 | 
					elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
 | 
				
			||||||
 | 
					  STORAGE=${STORAGE_MENU[0]}
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
					  while [ -z "${STORAGE:+x}" ]; do
 | 
				
			||||||
 | 
					    STORAGE=$(whiptail --title "Storage Pools" --radiolist \
 | 
				
			||||||
 | 
					    "Which storage pool you would like to use for the container?\n\n" \
 | 
				
			||||||
 | 
					    16 $(($MSG_MAX_LENGTH + 23)) 6 \
 | 
				
			||||||
 | 
					    "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
 | 
				
			||||||
 | 
					  done
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					info "Using '$STORAGE' for storage location."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Get the next guest VM/LXC ID
 | 
				
			||||||
 | 
					CTID=$(pvesh get /cluster/nextid)
 | 
				
			||||||
 | 
					info "Container ID is $CTID."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Download latest Debian 10 LXC template
 | 
				
			||||||
 | 
					msg "Updating LXC template list..."
 | 
				
			||||||
 | 
					pveam update >/dev/null
 | 
				
			||||||
 | 
					msg "Downloading LXC template..."
 | 
				
			||||||
 | 
					OSTYPE=debian
 | 
				
			||||||
 | 
					OSVERSION=${OSTYPE}-10
 | 
				
			||||||
 | 
					mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($OSVERSION.*\)/\1/p" | sort -t - -k 2 -V)
 | 
				
			||||||
 | 
					TEMPLATE="${TEMPLATES[-1]}"
 | 
				
			||||||
 | 
					pveam download local $TEMPLATE >/dev/null ||
 | 
				
			||||||
 | 
					  die "A problem occured while downloading the LXC template."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Create variables for container disk
 | 
				
			||||||
 | 
					STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
 | 
				
			||||||
 | 
					case $STORAGE_TYPE in
 | 
				
			||||||
 | 
					  dir|nfs)
 | 
				
			||||||
 | 
					    DISK_EXT=".raw"
 | 
				
			||||||
 | 
					    DISK_REF="$CTID/"
 | 
				
			||||||
 | 
					    ;;
 | 
				
			||||||
 | 
					  zfspool)
 | 
				
			||||||
 | 
					    DISK_PREFIX="subvol"
 | 
				
			||||||
 | 
					    DISK_FORMAT="subvol"
 | 
				
			||||||
 | 
					    ;;
 | 
				
			||||||
 | 
					esac
 | 
				
			||||||
 | 
					DISK=${DISK_PREFIX:-vm}-${CTID}-disk-0${DISK_EXT-}
 | 
				
			||||||
 | 
					ROOTFS=${STORAGE}:${DISK_REF-}${DISK}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Create LXC
 | 
				
			||||||
 | 
					msg "Creating LXC container..."
 | 
				
			||||||
 | 
					DISK_SIZE=4G
 | 
				
			||||||
 | 
					pvesm alloc $STORAGE $CTID $DISK $DISK_SIZE --format ${DISK_FORMAT:-raw} >/dev/null
 | 
				
			||||||
 | 
					if [ "$STORAGE_TYPE" == "zfspool" ]; then
 | 
				
			||||||
 | 
					  warn "Some containers may not work properly due to ZFS not supporting 'fallocate'."
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
					  mkfs.ext4 $(pvesm path $ROOTFS) &>/dev/null
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					ARCH=$(dpkg --print-architecture)
 | 
				
			||||||
 | 
					HOSTNAME=template
 | 
				
			||||||
 | 
					TEMPLATE_STRING="local:vztmpl/${TEMPLATE}"
 | 
				
			||||||
 | 
					pct create $CTID $TEMPLATE_STRING -arch $ARCH -features nesting=1 \
 | 
				
			||||||
 | 
					  -hostname $HOSTNAME -net0 name=eth0,bridge=vmbr0,ip=dhcp -onboot 1 \
 | 
				
			||||||
 | 
					  -ostype $OSTYPE -rootfs $ROOTFS,size=$DISK_SIZE -storage $STORAGE >/dev/null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Modify LXC permissions to support Docker
 | 
				
			||||||
 | 
					LXC_CONFIG=/etc/pve/lxc/${CTID}.conf
 | 
				
			||||||
 | 
					cat <<EOF >> $LXC_CONFIG
 | 
				
			||||||
 | 
					lxc.cgroup.devices.allow: a
 | 
				
			||||||
 | 
					lxc.cap.drop:
 | 
				
			||||||
 | 
					EOF
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Set container description
 | 
				
			||||||
 | 
					pct set $CTID -description "Debian with Docker"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Set container timezone to match host
 | 
				
			||||||
 | 
					MOUNT=$(pct mount $CTID | cut -d"'" -f 2)
 | 
				
			||||||
 | 
					ln -fs $(readlink /etc/localtime) ${MOUNT}/etc/localtime
 | 
				
			||||||
 | 
					pct unmount $CTID && unset MOUNT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Setup container
 | 
				
			||||||
 | 
					msg "Starting LXC container..."
 | 
				
			||||||
 | 
					pct start $CTID
 | 
				
			||||||
 | 
					pct push $CTID setup.sh /setup.sh -perms 755
 | 
				
			||||||
 | 
					pct exec $CTID /setup.sh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Get network details and show completion message
 | 
				
			||||||
 | 
					IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
 | 
				
			||||||
 | 
					info "Successfully created LXC to $CTID."
 | 
				
			||||||
 | 
					msg "
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Enjoy!
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					"
 | 
				
			||||||
							
								
								
									
										60
									
								
								setup.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								setup.sh
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
				
			|||||||
 | 
					#!/usr/bin/env bash
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Setup script environment
 | 
				
			||||||
 | 
					set -o errexit  #Exit immediately if a pipeline returns a non-zero status
 | 
				
			||||||
 | 
					set -o errtrace #Trap ERR from shell functions, command substitutions, and commands from subshell
 | 
				
			||||||
 | 
					set -o nounset  #Treat unset variables as an error
 | 
				
			||||||
 | 
					set -o pipefail #Pipe will exit with last non-zero status if applicable
 | 
				
			||||||
 | 
					shopt -s expand_aliases
 | 
				
			||||||
 | 
					alias die='EXIT=$? LINE=$LINENO error_exit'
 | 
				
			||||||
 | 
					trap die ERR
 | 
				
			||||||
 | 
					trap 'die "Script interrupted."' INT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function error_exit() {
 | 
				
			||||||
 | 
					  trap - ERR
 | 
				
			||||||
 | 
					  local DEFAULT='Unknown failure occured.'
 | 
				
			||||||
 | 
					  local REASON="\e[97m${1:-$DEFAULT}\e[39m"
 | 
				
			||||||
 | 
					  local FLAG="\e[91m[ERROR:LXC] \e[93m$EXIT@$LINE"
 | 
				
			||||||
 | 
					  msg "$FLAG $REASON"
 | 
				
			||||||
 | 
					  exit $EXIT
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					function msg() {
 | 
				
			||||||
 | 
					  local TEXT="$1"
 | 
				
			||||||
 | 
					  echo -e "$TEXT"
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Prepare container OS
 | 
				
			||||||
 | 
					msg "Setting up container OS..."
 | 
				
			||||||
 | 
					sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
 | 
				
			||||||
 | 
					locale-gen >/dev/null
 | 
				
			||||||
 | 
					apt-get -y purge openssh-{client,server} >/dev/null
 | 
				
			||||||
 | 
					apt-get autoremove >/dev/null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Update container OS
 | 
				
			||||||
 | 
					msg "Updating container OS..."
 | 
				
			||||||
 | 
					apt-get update >/dev/null
 | 
				
			||||||
 | 
					apt-get -qqy upgrade &>/dev/null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Install prerequisites
 | 
				
			||||||
 | 
					msg "Installing prerequisites..."
 | 
				
			||||||
 | 
					apt-get -qqy install \
 | 
				
			||||||
 | 
					    curl &>/dev/null
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Customize container
 | 
				
			||||||
 | 
					msg "Customizing container..."
 | 
				
			||||||
 | 
					rm /etc/motd # Remove message of the day after login
 | 
				
			||||||
 | 
					rm /etc/update-motd.d/10-uname # Remove kernel information after login
 | 
				
			||||||
 | 
					touch ~/.hushlogin # Remove 'Last login: ' and mail notification after login
 | 
				
			||||||
 | 
					GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
 | 
				
			||||||
 | 
					mkdir -p $(dirname $GETTY_OVERRIDE)
 | 
				
			||||||
 | 
					cat << EOF > $GETTY_OVERRIDE
 | 
				
			||||||
 | 
					[Service]
 | 
				
			||||||
 | 
					ExecStart=
 | 
				
			||||||
 | 
					ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
 | 
				
			||||||
 | 
					EOF
 | 
				
			||||||
 | 
					systemctl daemon-reload
 | 
				
			||||||
 | 
					systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Cleanup container
 | 
				
			||||||
 | 
					msg "Cleanup..."
 | 
				
			||||||
 | 
					rm -rf /setup.sh /var/{cache,log}/* /var/lib/apt/lists/*
 | 
				
			||||||
		Reference in New Issue
	
	Block a user