Skip to content

Instantly share code, notes, and snippets.

@ashthespy
Created August 14, 2021 19:23
Show Gist options
  • Save ashthespy/5e856e832c3904bc600f22c61b0f8766 to your computer and use it in GitHub Desktop.
Save ashthespy/5e856e832c3904bc600f22c61b0f8766 to your computer and use it in GitHub Desktop.
Unpack, edit and repack an initrd image for Volumio
#!/usr/bin/env bash
# Basic usage: <init-editd -f filename>
# Example: ${SCRIPT} -f uIinitrd
# Notes: The script will determine how the initrd has been compressed and unpack/ repack accordingly
# Based on Gé's script (init-edit.sh)
SRC="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
SCRIPT=$(basename "${BASH_SOURCE[0]}")
compress=pigz
function exit_error() {
echo "${SCRIPT} failed at $1"
echo "Cleaning up ${TMPWORK}.."
rm -r "${TMPWORK}"
exit 1
}
trap 'exit_error ${LINENO}' INT ERR
TMPWORK="$(mktemp -d "${TMPDIR:-/var/tmp}/initramfs_XXXXXX")" || exit_error
# Help function
function HELP() {
cat <<-EOF
Help documentation for ${SCRIPT}
Basic usage: ${SCRIPT} <INITRDFILE>
Arguments:
-f <INITRDFILE> Path to the initrd image to edit
-v verbose
-a <arch> mkimage arch
-c <compression> comprssion tool (default pigz)
Example: Edit /boot/uIinitrd :
${SCRIPT} -f /boot/uIinitrd
EOF
exit 1
}
function input_prompt() {
read -rp "Do you wish to continue [Y/N] ? " response
echo
case $response in
[Yy]*)
echo ""
;;
[Nn]*)
echo "Aborting init-editing"
exit_error "init-editing"
;;
*)
echo "Assuming you mean No, please answer [Yy]es or [Nn]o the next time..."
exit_error "init-editing"
;;
esac
}
#if ! command -v nano &>/dev/null; then
# echo "This script requires text editor 'nano'"
# echo "Please install it with 'sudo apt install nano'"
# exit 1
#fi
NUMARGS=$#
if [ "$NUMARGS" -eq 0 ]; then
HELP
fi
while getopts a:f:h:v FLAG; do
case $FLAG in
a)
arch="${OPTARG}"
;;
c)
compress="${OPTARG}"
;;
f)
INITRD_PATH="${OPTARG}"
;;
h) #show help
HELP
;;
v)
verbose=y
;;
/?) #unrecognized option - show help
echo -e \\n"Option -${BOLD}${OPTARG}${NORM} not allowed."
HELP
;;
esac
done
if [[ -z ${INITRD_PATH} ]]; then
echo ""
echo "${SCRIPT}: missing argument(s)"
HELP
exit 1
fi
if [[ ! -f ${INITRD_PATH} ]]; then
echo ""
echo "${SCRIPT}: ${INITRD_PATH} does not exist"
HELP
exit 1
fi
if compgen -G "${TMPWORK}/*"; then
echo "Workarea exists, cleaning it..."
rm -r "${TMPWORK:?}"/* >/dev/null 2>&1
fi
INITRD_NAME=$(basename "${INITRD_PATH}")
INITRD_DIR=$(dirname "$(realpath "${INITRD_PATH}")")
echo "Copying ${INITRD_NAME} from ${INITRD_DIR} to ${TMPWORK}..."
cp "${INITRD_PATH}" "${TMPWORK}/${INITRD_NAME}"
WRK_INITRD="${TMPWORK}/${INITRD_NAME}"
WORK_DIR=${TMPWORK}/root
mkdir "${WORK_DIR}"
pushd "${WORK_DIR}" || exit 1
FORMAT=$(file "${WRK_INITRD}" | grep -o "RAMDisk Image")
if [[ "$FORMAT" == "RAMDisk Image" ]]; then
echo "Unpacking RAMDisk image ${WRK_INITRD}..."
dd if="${WRK_INITRD}" bs=64 skip=1 | gzip -dc | cpio -div
# Figure out arch
# shellcheck source=/dev/null
source ./conf/arch.conf
echo "Open ${WORK_DIR}/init to edit.."
input_prompt
echo "Creating a new ${INITRD_NAME} image (${DPKG_ARCH}), please wait..."
OPTS=("-o")
[[ "${verbose}" = y ]] && OPTS+=("-v") || OPTS+=("--quiet")
compress="${compress} -9"
find . -print0 | cpio "${OPTS[@]}" -0 --format=newc | ${compress} >"${INITRD_NAME}.new"
[[ -n "${arch}" ]] && [[ "${arch}" != "${DPKG_ARCH/hf/}" ]] && echo "Warning: Prescribed ${arch} != ${DPKG_ARCH/hf/}"
mkimage -A "${DPKG_ARCH/hf/}" -O linux -T ramdisk -C gzip -n uInitrd -d "${INITRD_NAME}.new" "${INITRD_DIR}/${INITRD_NAME}.new"
else
echo "Unpacking gzip compressed ${INITRD_NAME}..."
zcat "${WRK_INITRD}" | cpio -idmv >/dev/null 2>&1
input_prompt
echo "Creating a new ${INITRD_NAME}, please wait..."
find . -print0 | cpio --quiet -o -0 --format=newc | gzip -9 >"${INITRD_DIR}/${INITRD_NAME}.new"
fi
echo "Done. Moving ${INITRD_PATH} --> .old"
echo "Cleaning up..."
popd || exit 1
mv "${INITRD_PATH}" "${INITRD_PATH}.old"
mv "${INITRD_PATH}.new" "${INITRD_PATH}"
rm -r "${TMPWORK:?}"
ls -la --color "${INITRD_DIR}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment