Skip to content

Instantly share code, notes, and snippets.

@aaannz
Created December 20, 2023 15:35
Show Gist options
  • Select an option

  • Save aaannz/16387ae9995915d3a62875ff9fe23385 to your computer and use it in GitHub Desktop.

Select an option

Save aaannz/16387ae9995915d3a62875ff9fe23385 to your computer and use it in GitHub Desktop.
Naive local initrd updater
#!/usr/bin/bash
# SPDX-FileCopyrightText: 2023 SUSE LLC
#
# SPDX-License-Identifier: GPL-2.0-only
# Naive emergency tool to update existing initrd from RPM.
# Primary usecase is to PTF update saltboot initrd
set -e
function usage() {
cat << EOF
$0 - Update existing initrd based on RPM
Usage: $0 initrd rpm
If RPM poinst to the directory, it will source all present RPMs and update
initrd in alphabetical order.
EOF
exit 1
}
if [ -z "$1" ]; then
echo "ERROR: Missing initrd argument!"
usage
fi
if [ ! -f "$1" ]; then
echo "Provided initrd '$1' does not exists!"
usage
fi
INITRD=$(realpath "$1")
if [ -z "$2" ]; then
echo "EROR: Missing RPM argument!"
usage
fi
if [ ! -e "$2" ]; then
echo "Provided RPM path '$2' does not exists!"
usage
fi
RPMDIR=$(realpath "$2")
RPMS=()
if [ -d "$RPMDIR" ]; then
readarray -d '' RPMS < <(find "$RPMDIR" -type f -print0)
else
RPMS=("$RPMDIR")
fi
WORKDIR=$(dirname "$INITRD")
BACKUP=$(mktemp --tmpdir="$WORKDIR" "$(basename ${INITRD}).XXX")
echo "Backing up initrd to the '$BACKUP'"
cp -f "$INITRD" "$BACKUP"
TMPDIR=$(mktemp -d --tmpdir="$WORKDIR")
pushd "$TMPDIR"
for RPM in "${RPMS[@]}"; do
echo "Extracting $RPM"
rpm2cpio "$RPM" | cpio -idm
done
echo "Updating initrd"
find . | cpio -H newc -o | zstd >> "$INITRD"
HASH=$(md5sum "$INITRD")
SIZE=$(stat -c "%s" "$INITRD")
popd
rm -r "$TMPDIR"
echo -e "All done.\n New initrd size: $SIZE\n New initrd hash: $HASH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment