#!/usr/bin/env bash

nokms(){
sed -i '/^NO_KMS_IN_INITRD/s/no/yes/' /etc/sysconfig/kernel
}

kms(){
sed -i '/^NO_KMS_IN_INITRD/s/yes/no/' /etc/sysconfig/kernel
}

echo "cleanup /tmp"
rm -fr /tmp/initrd*

# Patch the virtio initrd
export VER=`uname -r`
export INITRD="/boot/initrd-$VER-virtio"
echo "Setting nokms"
nokms

echo "Building -virtio initrd ..."
"env" "rootfstype=ext4,resume=/dev/vg1/lvobsswap,rootdev=/dev/vg0/lvobsbuild,udevtimeout=2,udev_timeout=2,nosplash,noluks" "mkinitrd" "-B" "-d" "/dev/vg0/lvobsbuild" "-m" "ext4 btrfs binfmt_misc virtio_pci virtio_blk" "-k" "/boot/vmlinuz-$VER" "-i" "$INITRD"

ls -l $INITRD

echo "Setting kms"
kms

# copy the *.virtio initrd to /tmp
cp -vfa $INITRD /tmp/initrd-virtio.gz

# ungzip it gzip -d initrd
cd /tmp
mkdir initrd.build
gzip -d initrd-virtio.gz

cd initrd.build

# uncpio cpio -id < ../initrd
cpio -id < ../initrd-virtio

# remove luks.sh
rm -fv ./boot/71-luks.sh ./config/luks.sh
sed -i 's/71-luks\.sh//g' ./run_all.sh

# rebuild cpio find . | cpio --create --format='newc' > ../initrd
rm -fv ../initrd-virtio
echo "rebuilding initrd ..."
find . | cpio --create --format='newc' > ../initrd-virtio
cd ../
# gzip it gzip -9
echo "Compressing initrd ..."
gzip -9v initrd-virtio
# copy to /boot
cp -v initrd-virtio.gz $INITRD

