#!/bin/bash
# This script creates new initramdisk with LUKS config within
set -euo pipefail

old_initrd=$1
new_initrd=$2
currdir=$PWD

# Copying existing initramdisk
cp ${old_initrd} ${new_initrd}

# Appending LUKS root keys and crypttab config to the end of initrd
workdir=$(mktemp -d -p /tmp se-initramfs-XXXXXX)
cd ${workdir}
mkdir -p etc/luks
cp -f /etc/luks/* etc/luks/
cp -f /etc/crypttab etc/
find . -mindepth 1 | cpio --quiet -H newc -o | gzip -9 -n >> ${new_initrd}

# Cleanup
cd ${currdir}
rm -rf ${workdir}
