From 8c9d3e056a6aa8aa2f67ba1b5e27015eff0e1d15 Mon Sep 17 00:00:00 2001 From: Ethan Dye Date: Tue, 7 Jul 2020 23:56:39 -0600 Subject: [PATCH] Fix IPv6 enabling/disabling (#1016) The current implementation of IPv6 enabling/disabling had many issues and could not enable IPv6 after it was disabled because of broken functionality. This contains a simpler and fixed method to enable/disable IPv6. Signed-off-by: Ethan Dye --- functions/openhabian.bash | 101 +++++++++----------------------------- includes/S90force-ipv4 | 1 + openhabian-setup.sh | 2 +- 3 files changed, 24 insertions(+), 80 deletions(-) create mode 100644 includes/S90force-ipv4 diff --git a/functions/openhabian.bash b/functions/openhabian.bash index 11d2504f0..a6e7a7f7c 100644 --- a/functions/openhabian.bash +++ b/functions/openhabian.bash @@ -193,86 +193,29 @@ ua-netinst_check() { fi } -choose_ipv6() { - local IPV6_DISABLE="net.ipv6.conf.all.disable_ipv6" - - # shellcheck disable=SC2154 - if [[ "$ipv6" == "disable" ]]; then - if ! grep -qE "^${IPV6_DISABLE}" "${SYSCTL_INIT}"; then - disable_ipv6 yes - fi - else - if [[ "$ipv6" == "enable" ]]; then - disable_ipv6 no +## Enable / Disable IPv6 according to the users configured option in '$CONFIGFILE' +## +## config_ipv6() +## +config_ipv6() { + local aptConf + local sysctlConf + + aptConf="/etc/apt/apt.conf/S90force-ipv4" + sysctlConf="/etc/sysctl.d/99-sysctl.conf" + + if [[ "${ipv6:-enable}" == "disable" ]]; then + echo -n "$(timestamp) [openHABian] Disabling IPv6... " + if ! grep -qs "^[[:space:]]*# Disable all IPv6 functionality" "$sysctlConf"; then + echo -e "\\n# Disable all IPv6 functionality\\nnet.ipv6.conf.all.disable_ipv6=1\\nnet.ipv6.conf.default.disable_ipv6=1\\nnet.ipv6.conf.lo.disable_ipv6=1" >> "$sysctlConf" fi - fi - - sysctl -p -} - -# arguments: "yes", "no" -disable_ipv6() { - local APT_IPV6_DISABLE='Acquire::ForceIPv4 "true";' - local APT_INIT=/etc/apt/apt.conf/S90force-ipv4 - local IPV6_DISABLE="net.ipv6.conf.all.disable_ipv6" - local SYSCTL_INIT="/etc/sysctl.d/99-sysctl.conf" - - if [[ "$1" == "yes" ]]; then - echo "${APT_IPV6_DISABLE}" | sudo tee "${APT_INIT}" - - if ! grep -qE "^${IPV6_DISABLE}" "${SYSCTL_INIT}"; then - ( echo "" - echo "###################################################################" - echo "# disable all IPv6 functionality" >> "${SYSCTL_INIT}" - echo "${IPV6_DISABLE}=1" - ) >> "${SYSCTL_INIT}" - else - sed -i "s/^# ${IPV6_ENABLE}.*/${IPV6_ENABLE}/g" "${SYSCTL_INIT}" + cp "${BASEDIR:-/opt/openhabian}"/includes/S90force-ipv4 "$aptConf" + elif [[ "${ipv6:-enable}" == "enable" ]]; then + echo -n "$(timestamp) [openHABian] Enabling IPv6... " + if grep -qs "^[[:space:]]*# Disable all IPv6 functionality" "$sysctlConf"; then + sed -i '/# Disable all IPv6 functionality/d; /net.ipv6.conf.all.disable_ipv6=1/d; /net.ipv6.conf.default.disable_ipv6=1/d; /net.ipv6.conf.lo.disable_ipv6=1/d' "$sysctlConf" fi - else - rm -f "${APT_INIT}" - sed -i "s/^${IPV6_ENABLE}.*/# ${IPV6_ENABLE}/g" "${SYSCTL_INIT}" - fi -} - -choose_ipv6() { - local IPV6_DISABLE="net.ipv6.conf.all.disable_ipv6" - - # shellcheck disable=SC2154 - if [[ "$ipv6" == "disable" ]]; then - if ! grep -qE "^${IPV6_DISABLE}" "${SYSCTL_INIT}"; then - disable_ipv6 yes - fi - else - if [[ "$ipv6" == "enable" ]]; then - disable_ipv6 no - fi - fi - - sysctl -p -} - -# arguments: "yes", "no" -disable_ipv6() { - local APT_IPV6_DISABLE='Acquire::ForceIPv4 "true";' - local APT_INIT=/etc/apt/apt.conf/S90force-ipv4 - local IPV6_DISABLE="net.ipv6.conf.all.disable_ipv6" - local SYSCTL_INIT="/etc/sysctl.d/99-sysctl.conf" - - if [[ "$1" == "yes" ]]; then - echo "${APT_IPV6_DISABLE}" | sudo tee "${APT_INIT}" - - if ! grep -qE "^${IPV6_DISABLE}" "${SYSCTL_INIT}"; then - ( echo "" - echo "###################################################################" - echo "# disable all IPv6 functionality" >> "${SYSCTL_INIT}" - echo "${IPV6_DISABLE}=1" - ) >> "${SYSCTL_INIT}" - else - sed -i "s/^# ${IPV6_ENABLE}.*/${IPV6_ENABLE}/g" "${SYSCTL_INIT}" - fi - else - rm -f "${APT_INIT}" - sed -i "s/^${IPV6_ENABLE}.*/# ${IPV6_ENABLE}/g" "${SYSCTL_INIT}" + rm -f "$aptConf" fi + if cond_redirect sysctl --load; then echo "OK"; else echo "FAILED"; return 1; fi } diff --git a/includes/S90force-ipv4 b/includes/S90force-ipv4 new file mode 100644 index 000000000..490ee3e22 --- /dev/null +++ b/includes/S90force-ipv4 @@ -0,0 +1 @@ +Acquire::ForceIPv4 "true"; diff --git a/openhabian-setup.sh b/openhabian-setup.sh index c2767773e..2323fac7e 100755 --- a/openhabian-setup.sh +++ b/openhabian-setup.sh @@ -77,7 +77,7 @@ for shfile in "$BASEDIR"/functions/*.bash; do source "$shfile"; done OLDWD=$(pwd) && cd /opt || exit 1 # disable ipv6 if requested in openhabian.conf (eventually reboots) -choose_ipv6 +config_ipv6 if [[ -n "$UNATTENDED" ]]; then # apt/dpkg commands will not try interactive dialogs