Skip to content

Commit

Permalink
coreos-teardown-initramfs-network: refactor into more functions
Browse files Browse the repository at this point in the history
This will make it easier for us to add a bit more code into this
file.
  • Loading branch information
dustymabe committed Mar 19, 2020
1 parent b1ddef5 commit fb5ce34
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions dracut/30ignition/coreos-teardown-initramfs-network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,45 @@
set -euo pipefail

down_interface() {
echo "info: taking down network device: $1"
ip link set $1 down
ip addr flush dev $1
rm -f -- /tmp/net.$1.did-setup
}

# We want to take down the bonded interfaces first
if [ -f "/sys/class/net/bonding_masters" ]; then
bonds="$(cat /sys/class/net/bonding_masters)"
for b in ${bonds[@]}; do
down_interface ${b}
echo -"${b}" > /sys/class/net/bonding_masters
done
fi
down_bonds() {
if [ -f "/sys/class/net/bonding_masters" ]; then
bonds="$(cat /sys/class/net/bonding_masters)"
for b in ${bonds[@]}; do
down_interface ${b}
echo -"${b}" > /sys/class/net/bonding_masters
done
fi
}

# Clean up the interfaces set up in the initramfs
# This mimics the behaviour of dracut's ifdown() in net-lib.sh
if ! [ -z "$(ls /sys/class/net)" ]; then
for f in /sys/class/net/*; do
interface=$(basename "$f")
# The `bonding_masters` entry is not a true interface and thus
# cannot be taken down. If they existed, the bonded interfaces
# were taken down earlier in this script.
if [ "$interface" == "bonding_masters" ]; then continue; fi
down_interface $interface
done
fi
# Note that in the futre we would like to possibly use `nmcli` networking off`
# for this. See the following two comments for details:
# https://github.com/coreos/fedora-coreos-tracker/issues/394#issuecomment-599721763
# https://github.com/coreos/fedora-coreos-tracker/issues/394#issuecomment-599746049
down_interfaces() {
if ! [ -z "$(ls /sys/class/net)" ]; then
for f in /sys/class/net/*; do
interface=$(basename "$f")
# The `bonding_masters` entry is not a true interface and thus
# cannot be taken down. If they existed, the bonded interfaces
# were taken down earlier in this script.
if [ "$interface" == "bonding_masters" ]; then continue; fi
down_interface $interface
done
fi
}

main() {
# We want to take down the bonded interfaces first
down_bonds
# Clean up the interfaces set up in the initramfs
down_interfaces
}

main

0 comments on commit fb5ce34

Please sign in to comment.