#!/usr/bin/env bash set -euo pipefail if [ "$#" -ne 2 ]; then echo "Usage: $0 " exit 1 fi INITRD="$1" FILE_TO_ADD="$2" # check file exists if [ ! -f "$INITRD" ]; then echo "Error: Initrd '$INITRD' does not exist." exit 1 fi if [ ! -f "$FILE_TO_ADD" ]; then echo "Error: File '$FILE_TO_ADD' does not exist." exit 1 fi # create temp directories TMPDIR=$(mktemp -d) WORKDIR="$TMPDIR/initrd" mkdir -p "$WORKDIR" # detect if gzipped GZIPPED=false if [[ "$INITRD" == *.gz ]]; then GZIPPED=true echo "Decompressing gzipped initrd..." gzip -dc "$INITRD" | (cd "$WORKDIR" && cpio -id --no-absolute-filenames) else echo "Extracting initrd..." (cd "$WORKDIR" && cpio -id < "$INITRD") fi # copy the file into the root of the initrd cp -v --preserve=mode "$FILE_TO_ADD" "$WORKDIR/" # rebuild initrd echo "Rebuilding initrd..." (cd "$WORKDIR" && find . | cpio -o -H newc | gzip > "$TMPDIR/new_initrd.cpio.gz") # overwrite original file, safely following symlink INITRD_REAL=$(readlink -f "$INITRD") mv "$TMPDIR/new_initrd.cpio.gz" "$INITRD_REAL" echo "Added '$FILE_TO_ADD' to initrd '$INITRD_REAL'." # clean up rm -rf "$TMPDIR"