ipv6_controller.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env bash
  2. # _modules/ipv6_controller.sh
  3. # 1) Check if the host supports IPv6
  4. get_ipv6_support() {
  5. if grep -qs '^1' /proc/sys/net/ipv6/conf/all/disable_ipv6 2>/dev/null \
  6. || ! ip -6 route show default &>/dev/null; then
  7. ENABLE_IPV6_LINE="ENABLE_IPV6=false"
  8. echo "IPv6 not detected on host – disabling IPv6 support."
  9. else
  10. ENABLE_IPV6_LINE="ENABLE_IPV6=true"
  11. echo "IPv6 detected on host – leaving IPv6 support enabled."
  12. fi
  13. }
  14. # 2) Ensure Docker daemon.json has the required IPv6 settings
  15. docker_daemon_edit(){
  16. DOCKER_DAEMON_CONFIG="/etc/docker/daemon.json"
  17. MISSING=()
  18. # helper: check for a key/value in the JSON
  19. _has_kv() { grep -Eq "\"$1\"\s*:\s*$2" "$DOCKER_DAEMON_CONFIG" 2>/dev/null; }
  20. if [[ -f "$DOCKER_DAEMON_CONFIG" ]]; then
  21. # Validate JSON syntax if jq is available
  22. if command -v jq &>/dev/null; then
  23. if ! jq empty "$DOCKER_DAEMON_CONFIG" &>/dev/null; then
  24. echo "ERROR: Invalid JSON in $DOCKER_DAEMON_CONFIG – please correct it manually."
  25. exit 1
  26. fi
  27. else
  28. echo "WARNING: jq not found – JSON syntax not validated."
  29. fi
  30. # Check required settings
  31. ! _has_kv ipv6 true && MISSING+=("ipv6: true")
  32. ! grep -Eq '"fixed-cidr-v6"\s*:\s*".+"' "$DOCKER_DAEMON_CONFIG" \
  33. && MISSING+=('fixed-cidr-v6: "fd00:dead:beef:c0::/80"')
  34. # Determine Docker major version
  35. DOCKER_MAJOR=$(docker version --format '{{.Server.Version}}' 2>/dev/null | cut -d. -f1)
  36. if [[ -n "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 27 ]]; then
  37. _has_kv ipv6 true && ! _has_kv ip6tables true \
  38. && MISSING+=("ip6tables: true")
  39. ! _has_kv experimental true \
  40. && MISSING+=("experimental: true")
  41. else
  42. echo "Docker ≥27 detected – skipping ip6tables/experimental checks."
  43. fi
  44. # If anything is missing, offer to auto-fix
  45. if ((${#MISSING[@]}>0)); then
  46. echo "Your daemon.json is missing: ${MISSING[*]}"
  47. read -p "Would you like to update $DOCKER_DAEMON_CONFIG now? [Y/n] " ans
  48. ans=${ans:-Y}
  49. if [[ $ans =~ ^[Yy]$ ]]; then
  50. cp "$DOCKER_DAEMON_CONFIG" "${DOCKER_DAEMON_CONFIG}.bak"
  51. if command -v jq &>/dev/null; then
  52. TMP=$(mktemp)
  53. JQ_FILTER='.ipv6 = true | .["fixed-cidr-v6"] = "fd00:dead:beef:c0::/80"'
  54. [[ "$DOCKER_MAJOR" && "$DOCKER_MAJOR" -lt 27 ]] \
  55. && JQ_FILTER+=' | .ip6tables = true | .experimental = true'
  56. jq "$JQ_FILTER" "$DOCKER_DAEMON_CONFIG" >"$TMP" && mv "$TMP" "$DOCKER_DAEMON_CONFIG"
  57. echo "daemon.json updated. Restarting Docker..."
  58. (command -v systemctl &>/dev/null && systemctl restart docker) \
  59. || service docker restart
  60. echo "Docker restarted. Please rerun this script."
  61. exit 1
  62. else
  63. echo "Please install jq or manually update daemon.json and restart Docker."
  64. exit 1
  65. fi
  66. else
  67. ENABLE_IPV6_LINE="ENABLE_IPV6=false"
  68. echo "User declined update – disabling IPv6 support."
  69. fi
  70. fi
  71. else
  72. echo "WARNING: $DOCKER_DAEMON_CONFIG not found – skipping Docker config check."
  73. fi
  74. }
  75. # 3) Wrapper to integrate into both generate_config.sh and update.sh
  76. configure_ipv6() {
  77. get_ipv6_support
  78. # Only edit Docker config if IPv6 is enabled on host
  79. if [[ "$ENABLE_IPV6_LINE" == "ENABLE_IPV6=true" ]]; then
  80. docker_daemon_edit
  81. else
  82. echo "Skipping Docker IPv6 configuration because host does not support IPv6."
  83. fi
  84. # Write ENABLE_IPV6 into mailcow.conf (generate_config.sh) or export in current shell (update.sh)
  85. if [[ -n "$MAILCOW_CONF" && -f "$MAILCOW_CONF" ]]; then
  86. # generate_config.sh: append or replace in mailcow.conf
  87. if grep -q '^ENABLE_IPV6=' "$MAILCOW_CONF"; then
  88. sed -i "s/^ENABLE_IPV6=.*/$ENABLE_IPV6_LINE/" "$MAILCOW_CONF"
  89. else
  90. echo "$ENABLE_IPV6_LINE" >> "$MAILCOW_CONF"
  91. fi
  92. else
  93. # update.sh: export into the running environment
  94. export "$ENABLE_IPV6_LINE"
  95. fi
  96. echo "IPv6 configuration complete: $ENABLE_IPV6_LINE"
  97. }