#!/bin/bash
dir=$(dirname "$0")
echo $dir
cd $dir
mkdir -p ./linux
git clone --depth 1 https://github.com/torvalds/linux.git ./linux
cat /boot/config-$(uname -r) > ./linux/.config
sed -i 's/CONFIG_SYSTEM_TRUSTED_KEYS=".*"/CONFIG_SYSTEM_TRUSTED_KEYS=""/' ./linux/.config
sed -i 's/CONFIG_SYSTEM_REVOCATION_KEYS=".*"/CONFIG_SYSTEM_REVOCATION_KEYS=""/' ./linux/.config
cat ./containerfile | podman build --volume /lib/modules:/lib/modules:z -f - -t kernelbuild:latest .
nice -n 10 /usr/bin/podman run --volume /lib/modules:/lib/modules:z --volume ./linux:/linux kernelbuild:latest
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/
cd initram/bin/
chmod +x busybox
busybox --install -s "$(dirname "$0")/initram/bin/"
printf '%s\n' "#!/bin/busybox sh" "/bin/busybox mount -t devtmpfs devtmpfs /dev" "/bin/busybox mount -t proc none /proc" "/bin/busybox mount -t sysfs none /sys" "ln -s /lib/modules/ /lib/modules/6.17.0-g98906f9d850e" "depmod -a" "/bin/busybox modprobe nbd max_part=2" "exec /bin/busybox sh" > $dir/initram/init
chmod +x $dir/initram/init
cd $dir/initram/
find .
find . -print0 | cpio --null -ov --format=newc | gzip -9 > $dir/initramfs.cpio.gz