create_lxc.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. # This sets verbose mode if the global variable is set to "yes"
  7. if [ "$VERBOSE" == "yes" ]; then set -x; fi
  8. # This function sets color variables for formatting output in the terminal
  9. YW=$(echo "\033[33m")
  10. BL=$(echo "\033[36m")
  11. RD=$(echo "\033[01;31m")
  12. GN=$(echo "\033[1;92m")
  13. CL=$(echo "\033[m")
  14. CM="${GN}✓${CL}"
  15. CROSS="${RD}✗${CL}"
  16. BFR="\\r\\033[K"
  17. HOLD="-"
  18. # This sets error handling options and defines the error_handler function to handle errors
  19. set -Eeuo pipefail
  20. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  21. # This function handles errors
  22. function error_handler() {
  23. local exit_code="$?"
  24. local line_number="$1"
  25. local command="$2"
  26. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  27. echo -e "\n$error_message\n"
  28. }
  29. # This function prints an informational message
  30. function msg_info() {
  31. local msg="$1"
  32. echo -ne " ${HOLD} ${YW}${msg}..."
  33. }
  34. # This function prints a success message
  35. function msg_ok() {
  36. local msg="$1"
  37. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  38. }
  39. # This function prints an error message
  40. function msg_error() {
  41. local msg="$1"
  42. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  43. }
  44. # This checks for the presence of valid Container Storage and Template Storage locations
  45. msg_info "Validating Storage"
  46. VALIDCT=$(pvesm status -content rootdir | awk 'NR>1')
  47. if [ -z "$VALIDCT" ]; then
  48. msg_error "Unable to detect a valid Container Storage location."
  49. exit 1
  50. fi
  51. VALIDTMP=$(pvesm status -content vztmpl | awk 'NR>1')
  52. if [ -z "$VALIDTMP" ]; then
  53. msg_error "Unable to detect a valid Template Storage location."
  54. exit 1
  55. fi
  56. # This function is used to select the storage class and determine the corresponding storage content type and label.
  57. function select_storage() {
  58. local CLASS=$1
  59. local CONTENT
  60. local CONTENT_LABEL
  61. case $CLASS in
  62. container)
  63. CONTENT='rootdir'
  64. CONTENT_LABEL='Container'
  65. ;;
  66. template)
  67. CONTENT='vztmpl'
  68. CONTENT_LABEL='Container template'
  69. ;;
  70. *) false || exit "Invalid storage class." ;;
  71. esac
  72. # This Queries all storage locations
  73. local -a MENU
  74. while read -r line; do
  75. local TAG=$(echo $line | awk '{print $1}')
  76. local TYPE=$(echo $line | awk '{printf "%-10s", $2}')
  77. local FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
  78. local ITEM=" Type: $TYPE Free: $FREE "
  79. local OFFSET=2
  80. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  81. local MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  82. fi
  83. MENU+=("$TAG" "$ITEM" "OFF")
  84. done < <(pvesm status -content $CONTENT | awk 'NR>1')
  85. # Select storage location
  86. if [ $((${#MENU[@]}/3)) -eq 1 ]; then
  87. printf ${MENU[0]}
  88. else
  89. local STORAGE
  90. while [ -z "${STORAGE:+x}" ]; do
  91. STORAGE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Storage Pools" --radiolist \
  92. "Which storage pool you would like to use for the ${CONTENT_LABEL,,}?\nTo make a selection, use the Spacebar.\n" \
  93. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  94. "${MENU[@]}" 3>&1 1>&2 2>&3) || exit "Menu aborted."
  95. done
  96. printf $STORAGE
  97. fi
  98. }
  99. # Test if required variables are set
  100. [[ "${CTID:-}" ]] || exit "You need to set 'CTID' variable."
  101. [[ "${PCT_OSTYPE:-}" ]] || exit "You need to set 'PCT_OSTYPE' variable."
  102. # Test if ID is valid
  103. [ "$CTID" -ge "100" ] || exit "ID cannot be less than 100."
  104. # Test if ID is in use
  105. if pct status $CTID &>/dev/null; then
  106. echo -e "ID '$CTID' is already in use."
  107. unset CTID
  108. exit "Cannot use ID that is already in use."
  109. fi
  110. # Get template storage
  111. TEMPLATE_STORAGE=$(select_storage template) || exit
  112. msg_ok "Using ${BL}$TEMPLATE_STORAGE${CL} ${GN}for Template Storage."
  113. # Get container storage
  114. CONTAINER_STORAGE=$(select_storage container) || exit
  115. msg_ok "Using ${BL}$CONTAINER_STORAGE${CL} ${GN}for Container Storage."
  116. # Update LXC template list
  117. msg_info "Updating LXC Template List"
  118. pveam update >/dev/null
  119. msg_ok "Updated LXC Template List"
  120. # Get LXC template string
  121. TEMPLATE_SEARCH=${PCT_OSTYPE}-${PCT_OSVERSION:-}
  122. mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V)
  123. [ ${#TEMPLATES[@]} -gt 0 ] || exit "Unable to find a template when searching for '$TEMPLATE_SEARCH'."
  124. TEMPLATE="${TEMPLATES[-1]}"
  125. # Download LXC template if needed
  126. if ! pveam list $TEMPLATE_STORAGE | grep -q $TEMPLATE; then
  127. msg_info "Downloading LXC Template"
  128. pveam download $TEMPLATE_STORAGE $TEMPLATE >/dev/null ||
  129. exit "A problem occured while downloading the LXC template."
  130. msg_ok "Downloaded LXC Template"
  131. fi
  132. # Combine all options
  133. DEFAULT_PCT_OPTIONS=(
  134. -arch $(dpkg --print-architecture))
  135. PCT_OPTIONS=(${PCT_OPTIONS[@]:-${DEFAULT_PCT_OPTIONS[@]}})
  136. [[ " ${PCT_OPTIONS[@]} " =~ " -rootfs " ]] || PCT_OPTIONS+=(-rootfs $CONTAINER_STORAGE:${PCT_DISK_SIZE:-8})
  137. # Create container
  138. msg_info "Creating LXC Container"
  139. pct create $CTID ${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE} ${PCT_OPTIONS[@]} >/dev/null ||
  140. exit "A problem occured while trying to create container."
  141. msg_ok "LXC Container ${BL}$CTID${CL} ${GN}was successfully created."