Skip to content

Commit

Permalink
Fix IPv6 enabling/disabling (openhab#1016)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ecdye authored Jul 8, 2020
1 parent b52d3a5 commit 8c9d3e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 80 deletions.
101 changes: 22 additions & 79 deletions functions/openhabian.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions includes/S90force-ipv4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Acquire::ForceIPv4 "true";
2 changes: 1 addition & 1 deletion openhabian-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c9d3e0

Please sign in to comment.