#!/bin/bash
set -e
cd "$(dirname "$0")"
dir=$(dirname "$0")
echo $dir
#mkdir -p /tmp/linux
[[ -d ./linux ]] || git clone --depth 1 https://github.com/torvalds/linux.git ./linux
cat ./build_containerfile | /usr/bin/podman build --volume /lib/modules:/lib/modules:z -f - -t patronagekernel:latest .
#rsync -av ./linux /mnt/tmpfs
#nice -n 20 /usr/bin/podman run -it --volume /lib/modules:/lib/modules:z --volume ./linux:/linux patronagekernel:latest
cd $dir
rm -rf initram/
mkdir -p initram
mkdir -p initram/{bin,dev,etc,lib,proc,sbin,sys,tmp,usr}
mkdir -p initram/usr/{bin,sbin}
mkdir -p initram/etc/{bin,sbin}
mkdir -p initram/lib/modules/
cp busybox ./initram/bin/
#cp ./linux/drivers/block/nbd.ko initram/lib/modules/
#cp ./linux/modules.builtin initram/lib/modules/
#cp ./linux/modules.order initram/lib/modules/

chmod +x $dir/initram/bin/busybox
$dir/initram/bin/busybox --install -s "$dir/initram/bin/"
cp $dir/init.sh $dir/initram/init
cp $dir/linux/arch/x86_64/boot/bzImage $dir/bzImage.efi
chmod +x $dir/initram/init
cd $dir/initram/
echo $dir
find . -print0 | cpio --null -ov --format=newc | gzip -9 > $dir/initramfs.cpio.gz

echo "created iniramfs"
