add-tailscale-lxc.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. function header_info {
  7. cat <<"EOF"
  8. ______ _ __ __
  9. /_ __/___ _(_) /_____________ _/ /__
  10. / / / __ `/ / / ___/ ___/ __ `/ / _ \
  11. / / / /_/ / / (__ ) /__/ /_/ / / __/
  12. /_/ \__,_/_/_/____/\___/\__,_/_/\___/
  13. EOF
  14. }
  15. clear
  16. header_info
  17. while true; do
  18. read -p "This will add Tailscale to an existing LXC Container ONLY. Proceed(y/n)?" yn
  19. case $yn in
  20. [Yy]*) break ;;
  21. [Nn]*) exit ;;
  22. *) echo "Please answer yes or no." ;;
  23. esac
  24. done
  25. set -o errexit
  26. set -o errtrace
  27. set -o nounset
  28. set -o pipefail
  29. shopt -s expand_aliases
  30. alias die='EXIT=$? LINE=$LINENO error_exit'
  31. trap die ERR
  32. function error_exit() {
  33. trap - ERR
  34. local reason="Unknown failure occured."
  35. local msg="${1:-$reason}"
  36. local flag="\e[1;31m‼ ERROR\e[0m $EXIT@$LINE"
  37. echo -e "$flag $msg" 1>&2
  38. exit $EXIT
  39. }
  40. function msg() {
  41. local TEXT="$1"
  42. echo -e "$TEXT"
  43. }
  44. NODE=$(hostname)
  45. MSG_MAX_LENGTH=0
  46. while read -r line; do
  47. TAG=$(echo "$line" | awk '{print $1}')
  48. ITEM=$(echo "$line" | awk '{print substr($0,36)}')
  49. OFFSET=2
  50. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  51. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  52. fi
  53. CTID_MENU+=("$TAG" "$ITEM " "OFF")
  54. done < <(pct list | awk 'NR>1')
  55. while [ -z "${CTID:+x}" ]; do
  56. CTID=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Containers on $NODE" --radiolist \
  57. "\nSelect a container to add Tailscale to:\n" \
  58. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  59. "${CTID_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  60. done
  61. CTID_CONFIG_PATH=/etc/pve/lxc/${CTID}.conf
  62. cat <<EOF >>$CTID_CONFIG_PATH
  63. lxc.cgroup2.devices.allow: c 10:200 rwm
  64. lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file
  65. EOF
  66. msg "Installing Tailscale..."
  67. lxc-attach -n $CTID -- bash -c "$(curl -fsSL https://tailscale.com/install.sh)" &>/dev/null || exit
  68. msg "Installed Tailscale"
  69. sleep 2
  70. msg "\e[1;32m ✔ Completed Successfully!\e[0m"
  71. msg "\e[1;31m Reboot ${CTID} LXC to apply the changes, then run tailscale up in the LXC console\e[0m"