Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initrd-setup-root: Add selective OS reset handling #55

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions dracut/99setup-root/initrd-setup-root
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ function walksysroot() {
COREOS_BLANK_MACHINE_ID="42000000000000000000000000000042"
MACHINE_ID_FILE="/sysroot/etc/machine-id"

function selectiveosreset() {
local entry="/sysroot$1"
# Don't remove /sysroot itself
[ "${entry}" = "/sysroot" ] && return 0
[ "${entry}" = "/sysroot/" ] && return 0
# Don't remove the active /usr mount point
[ "${entry}" = "/sysroot/usr" ] && return 0
# Not really needed because find doesn't add a trailing slash but to be safe:
[ "${entry}" = "/sysroot/usr/" ] && return 0
if [ -d "${entry}" ]; then
# Try to delete dir, will fail if its contents are preserved
usrbin rmdir "${entry}" 2>/dev/null || true
else
# Delete file, Report wrong paths to nonexisting files or any other errors
# (journalctl -u initrd-setup-root) but don't hard fail the boot
rm "${entry}" || true
fi
true # Do not carry any last condition evaluation over as return code
}

# Do the selective OS reset as prepared by flatcar-reset
if [ -s /sysroot/selective-os-reset ]; then
walksysroot / selectiveosreset -regextype egrep -not -regex "$(cat /sysroot/selective-os-reset)"
rm -f /sysroot/selective-os-reset
# Always remove the machine-id file because otherwise it's not a first boot.
# The previous value can be preserved through the systemd.machine_id=
# kernel parameter.
rm -f /sysroot/etc/machine-id
fi

# This creates the modifiable users/groups in /sysroot/etc,
# initializing the shadow database in the process. This needs to
# happen early, so systemd-tmpfiles can read the user info from
Expand Down