mariadb_container.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env bash
  2. # Setup script environment
  3. set -o errexit #Exit immediately if a pipeline returns a non-zero status
  4. set -o errtrace #Trap ERR from shell functions, command substitutions, and commands from subshell
  5. set -o nounset #Treat unset variables as an error
  6. set -o pipefail #Pipe will exit with last non-zero status if applicable
  7. shopt -s expand_aliases
  8. alias die='EXIT=$? LINE=$LINENO error_exit'
  9. trap die ERR
  10. trap cleanup EXIT
  11. function error_exit() {
  12. trap - ERR
  13. local DEFAULT='Unknown failure occured.'
  14. local REASON="\e[97m${1:-$DEFAULT}\e[39m"
  15. local FLAG="\e[91m[ERROR] \e[93m$EXIT@$LINE"
  16. msg "$FLAG $REASON"
  17. [ ! -z ${CTID-} ] && cleanup_ctid
  18. exit $EXIT
  19. }
  20. function warn() {
  21. local REASON="\e[97m$1\e[39m"
  22. local FLAG="\e[93m[WARNING]\e[39m"
  23. msg "$FLAG $REASON"
  24. }
  25. function info() {
  26. local REASON="$1"
  27. local FLAG="\e[36m[INFO]\e[39m"
  28. msg "$FLAG $REASON"
  29. }
  30. function msg() {
  31. local TEXT="$1"
  32. echo -e "$TEXT"
  33. }
  34. function cleanup_ctid() {
  35. if [ ! -z ${MOUNT+x} ]; then
  36. pct unmount $CTID
  37. fi
  38. if $(pct status $CTID &>/dev/null); then
  39. if [ "$(pct status $CTID | awk '{print $2}')" == "running" ]; then
  40. pct stop $CTID
  41. fi
  42. pct destroy $CTID
  43. elif [ "$(pvesm list $STORAGE --vmid $CTID)" != "" ]; then
  44. pvesm free $ROOTFS
  45. fi
  46. }
  47. function cleanup() {
  48. popd >/dev/null
  49. rm -rf $TEMP_DIR
  50. }
  51. function load_module() {
  52. if ! $(lsmod | grep -Fq $1); then
  53. modprobe $1 &>/dev/null || \
  54. die "Failed to load '$1' module."
  55. fi
  56. MODULES_PATH=/etc/modules
  57. if ! $(grep -Fxq "$1" $MODULES_PATH); then
  58. echo "$1" >> $MODULES_PATH || \
  59. die "Failed to add '$1' module to load at boot."
  60. fi
  61. }
  62. TEMP_DIR=$(mktemp -d)
  63. pushd $TEMP_DIR >/dev/null
  64. # Download setup script
  65. wget -qL https://raw.githubusercontent.com/tteck/Proxmox/main/mariadb_setup.sh
  66. # Detect modules and automatically load at boot
  67. #load_module aufs
  68. load_module overlay
  69. # Select storage location
  70. while read -r line; do
  71. TAG=$(echo $line | awk '{print $1}')
  72. TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  73. FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  74. ITEM=" Type: $TYPE Free: $FREE "
  75. OFFSET=2
  76. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  77. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  78. fi
  79. STORAGE_MENU+=( "$TAG" "$ITEM" "OFF" )
  80. done < <(pvesm status -content rootdir | awk 'NR>1')
  81. if [ $((${#STORAGE_MENU[@]}/3)) -eq 0 ]; then
  82. warn "'Container' needs to be selected for at least one storage location."
  83. die "Unable to detect valid storage location."
  84. elif [ $((${#STORAGE_MENU[@]}/3)) -eq 1 ]; then
  85. STORAGE=${STORAGE_MENU[0]}
  86. else
  87. while [ -z "${STORAGE:+x}" ]; do
  88. STORAGE=$(whiptail --title "Storage Pools" --radiolist \
  89. "Which storage pool you would like to use for the container?\n\n" \
  90. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  91. "${STORAGE_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  92. done
  93. fi
  94. info "Using '$STORAGE' for storage location."
  95. # Get the next guest VM/LXC ID
  96. CTID=$(pvesh get /cluster/nextid)
  97. info "Container ID is $CTID."
  98. # Download latest Debian 11 LXC template
  99. msg "Updating LXC template list..."
  100. pveam update >/dev/null
  101. msg "Downloading LXC template..."
  102. OSTYPE=debian
  103. OSVERSION=${OSTYPE}-10
  104. mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($OSVERSION.*\)/\1/p" | sort -t - -k 2 -V)
  105. TEMPLATE="${TEMPLATES[-1]}"
  106. pveam download local $TEMPLATE >/dev/null ||
  107. die "A problem occured while downloading the LXC template."
  108. # Create variables for container disk
  109. STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}')
  110. case $STORAGE_TYPE in
  111. dir|nfs)
  112. DISK_EXT=".raw"
  113. DISK_REF="$CTID/"
  114. ;;
  115. zfspool)
  116. DISK_PREFIX="subvol"
  117. DISK_FORMAT="subvol"
  118. ;;
  119. esac
  120. DISK=${DISK_PREFIX:-vm}-${CTID}-disk-0${DISK_EXT-}
  121. ROOTFS=${STORAGE}:${DISK_REF-}${DISK}
  122. # Create LXC
  123. msg "Creating LXC container..."
  124. DISK_SIZE=4G
  125. pvesm alloc $STORAGE $CTID $DISK $DISK_SIZE --format ${DISK_FORMAT:-raw} >/dev/null
  126. if [ "$STORAGE_TYPE" == "zfspool" ]; then
  127. warn "Some containers may not work properly due to ZFS not supporting 'fallocate'."
  128. else
  129. mkfs.ext4 $(pvesm path $ROOTFS) &>/dev/null
  130. fi
  131. ARCH=$(dpkg --print-architecture)
  132. HOSTNAME=mariadb
  133. TEMPLATE_STRING="local:vztmpl/${TEMPLATE}"
  134. pct create $CTID $TEMPLATE_STRING -arch $ARCH -features nesting=1 \
  135. -hostname $HOSTNAME -net0 name=eth0,bridge=vmbr0,ip=dhcp -onboot 1 -cores 1 -memory 1024 \
  136. -ostype $OSTYPE -rootfs $ROOTFS,size=$DISK_SIZE -storage $STORAGE >/dev/null
  137. # Set container timezone to match host
  138. MOUNT=$(pct mount $CTID | cut -d"'" -f 2)
  139. ln -fs $(readlink /etc/localtime) ${MOUNT}/etc/localtime
  140. pct unmount $CTID && unset MOUNT
  141. # Setup container
  142. msg "Starting LXC container..."
  143. pct start $CTID
  144. pct push $CTID mariadb_setup.sh /mariadb_setup.sh -perms 755
  145. pct exec $CTID /mariadb_setup.sh
  146. # Get network details and show completion message
  147. IP=$(pct exec $CTID ip a s dev eth0 | sed -n '/inet / s/\// /p' | awk '{print $2}')
  148. info "Successfully created a MariaDB LXC Container to $CTID at IP Address ${IP}"