This repository was archived by the owner on Aug 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
coreos-teardown-initramfs-network: propagate hostname, support coreos.no_persist_ip #174
Merged
dustymabe
merged 5 commits into
coreos:master
from
dustymabe:dusty-teardown-network-improvements
Apr 23, 2020
+90
−5
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b1604e9
coreos-teardown-initramfs-network: do SELinux relabel on copied files
dustymabe 70c2792
coreos-teardown-initramfs-network: also skip local loopback interface
dustymabe 2cb06d6
coreos-teardown-initramfs-network: consider ifcfg files in real root …
dustymabe 0665bbe
coreos-teardown-initramfs-network: support coreos.no_persist_ip karg
dustymabe d6805df
coreos-teardown-initramfs-network: also propagate hostname
dustymabe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,34 @@ | |
|
|
||
| set -euo pipefail | ||
|
|
||
| # Load dracut libraries. Using getargbool() and getargs() from | ||
| # dracut-lib and ip_to_var() from net-lib | ||
| load_dracut_libs() { | ||
| # dracut is not friendly to set -eu | ||
| set +euo pipefail | ||
| type getargbool &>/dev/null || . /lib/dracut-lib.sh | ||
| type ip_to_var &>/dev/null || . /lib/net-lib.sh | ||
|
Comment on lines
+12
to
+13
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, it's totally fine IMO to just source these upfront and only doing our |
||
| set -euo pipefail | ||
| } | ||
|
|
||
| dracut_func() { | ||
| # dracut is not friendly to set -eu | ||
| set +euo pipefail | ||
| "$@"; rc=$? | ||
| set -euo pipefail | ||
| return $rc | ||
| } | ||
|
|
||
| selinux_relabel() { | ||
| # If we have access to coreos-relabel then let's use that because | ||
| # it allows us to set labels on things before switching root | ||
| # If not, fallback to tmpfiles. | ||
| if command -v coreos-relabel; then | ||
| coreos-relabel $1 | ||
| else | ||
| echo "Z $1 - - -" >> "/run/tmpfiles.d/$(basename $0)-relabel.conf" | ||
| fi | ||
| } | ||
|
|
||
| # Propagate initramfs networking if desired. The policy here is: | ||
| # | ||
|
|
@@ -16,20 +44,62 @@ set -euo pipefail | |
| # | ||
| # See https://github.com/coreos/fedora-coreos-tracker/issues/394#issuecomment-599721173 | ||
| propagate_initramfs_networking() { | ||
| if [ -n "$(ls -A /sysroot/etc/NetworkManager/system-connections/)" ]; then | ||
| # Check the two locations where a user could have provided network configuration | ||
| # On FCOS we only support keyfiles, but on RHCOS we support keyfiles and ifcfg | ||
| if [ -n "$(ls -A /sysroot/etc/NetworkManager/system-connections/)" -o \ | ||
| -n "$(ls -A /sysroot/etc/sysconfig/network-scripts/)" ]; then | ||
jlebon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| echo "info: networking config is defined in the real root" | ||
| echo "info: will not attempt to propagate initramfs networking" | ||
| else | ||
| echo "info: no networking config is defined in the real root" | ||
| if [ -n "$(ls -A /run/NetworkManager/system-connections/)" ]; then | ||
| echo "info: propagating initramfs networking config to the real root" | ||
| cp /run/NetworkManager/system-connections/* /sysroot/etc/NetworkManager/system-connections/ | ||
| selinux_relabel /etc/NetworkManager/system-connections/ | ||
| else | ||
| echo "info: no initramfs networking information to propagate" | ||
| fi | ||
| fi | ||
| } | ||
|
|
||
| # Propagate the ip= karg hostname if desired. The policy here is: | ||
| # | ||
| # - IF a hostname is specified in static networking ip= kargs | ||
| # - AND no hostname was set via Ignition (realroot `/etc/hostname`) | ||
| # - THEN we make the last hostname specified in an ip= karg apply | ||
| # permanently by writing it into `/etc/hostname` | ||
| # | ||
| # This may no longer be needed when the following bug is fixed: | ||
| # https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/419 | ||
| propagate_initramfs_hostname() { | ||
| if [ -e '/sysroot/etc/hostname' ]; then | ||
| echo "info: hostname is defined in the real root" | ||
| echo "info: will not attempt to propagate initramfs hostname" | ||
| return 0 | ||
| fi | ||
| # Detect if any hostname was provided via static ip= kargs | ||
| # run in a subshell so we don't pollute our environment | ||
| hostnamefile=$(mktemp) | ||
| ( | ||
| last_nonempty_hostname='' | ||
| # Inspired from ifup.sh from the 40network dracut module. Note that | ||
| # $hostname from ip_to_var will only be nonempty for static networking. | ||
| for iparg in $(dracut_func getargs ip=); do | ||
| dracut_func ip_to_var $iparg | ||
| [ -n "${hostname:-}" ] && last_nonempty_hostname="$hostname" | ||
| done | ||
| echo -n "$last_nonempty_hostname" > $hostnamefile | ||
| ) | ||
| hostname=$(<$hostnamefile); rm $hostnamefile | ||
| if [ -n "$hostname" ]; then | ||
| echo "info: propagating initramfs hostname (${hostname}) to the real root" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally there'd be some way for us to note that the "origin" of |
||
| echo $hostname > /sysroot/etc/hostname | ||
| selinux_relabel /etc/hostname | ||
| else | ||
| echo "info: no initramfs hostname information to propagate" | ||
| fi | ||
| } | ||
|
|
||
| down_interface() { | ||
| echo "info: taking down network device: $1" | ||
| # On recommendation from the NM team let's try to delete the device | ||
|
|
@@ -54,14 +124,21 @@ down_interfaces() { | |
| 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 [ "$interface" == "bonding_masters" ]; then continue; fi | ||
| # cannot be taken down. Also skip local loopback | ||
| case "$interface" in | ||
| "lo" | "bonding_masters") | ||
| continue | ||
| ;; | ||
| esac | ||
| down_interface $interface | ||
| done | ||
| fi | ||
| } | ||
|
|
||
| main() { | ||
| # Load libraries from dracut | ||
| load_dracut_libs | ||
|
|
||
| # Take down all interfaces set up in the initramfs | ||
| down_interfaces | ||
|
|
||
|
|
@@ -70,8 +147,16 @@ main() { | |
| ip route flush table main | ||
| ip route flush cache | ||
|
|
||
| # Propagate initramfs networking if needed | ||
| propagate_initramfs_networking | ||
| # Hopefully our logic is sound enough that this is never needed, but | ||
| # user's can explicitly disable initramfs network/hostname propagation | ||
| # with the coreos.no_persist_ip karg. | ||
| if dracut_func getargbool 0 'coreos.no_persist_ip'; then | ||
| echo "info: coreos.no_persist_ip karg detected" | ||
| echo "info: skipping propagating initramfs settings" | ||
| else | ||
| propagate_initramfs_hostname | ||
| propagate_initramfs_networking | ||
| fi | ||
|
|
||
| # Now that the configuration has been propagated (or not) | ||
| # clean it up so that no information from outside of the | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouuff. What issues were you hitting with
set -euo pipefailleft on? Can we just turn it on after importing the library?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unset variables, commands that error, etc. We'd have to turn it off/on when we import the library and also each time we called a function in the library. Believe me I started doing it that way but declared bankruptcy. It's ugly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh. Hmm, one thing we could do is have a wrapper function like:
then call it as e.g.
dracut_fn ip_to_var ....Anyway, IMO worth trying something along those lines if it works to maintain our sanity, but will leave it up to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok added a wrapper function!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice idea!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have this dream that someday 30 years from now, something will win and software engineers will see shell script like we see COBOL...