skobkin
BASH
2020.07.05 22:54
Simple script to build current kernel with initramfs and install them afterwards.
#!/bin/bash
ESP_MOUNTPOINT=/boot/efi
KERNEL_INSTALL_PATH=$ESP_MOUNTPOINT/EFI/gentoo
THREADS=`nproc`
# TODO detect from .config
KERNEL_VERSION_SUFFIX="-skobkin"
 
echo 'Mounting ESP to $ESP_MOUNTPOINT'
# Mounting EFI System Partition
mount $ESP_MOUNTPOINT
# Going to current Linux source directory
cd /usr/src/linux
# Removing verion record
#rm .version
# Checking if we already have config or it's a new kernel
[[ ! -f .config ]] && echo 'Restoring missing kernel config from running kernel' && zcat /proc/config.gz > .config
# Completing merging kernel config options with current version
echo "Running oldconfig"
make -s oldconfig &&
## Showing kernel configurations interface
#make -j8 nconfig &&
echo "Running kernel build using $THREADS threads"
make -s -j$THREADS bzImage modules &&
# Installing kernel modules
echo 'Installing modules' && sleep 3
make modules_install &&
echo "----------------------------------------------"
# Rebuilding 3rd party kernel modules
echo 'Running external module rebuild'
emerge -vq @module-rebuild &&
echo "----------------------------------------------"
# Doing backup of current kernel version and copying new version to ESP partition
(
cd $KERNEL_INSTALL_PATH
echo 'Rotating old kernel files'
mv -f linux-previous.efi linux-old.efi &>/dev/null;
mv -f linux-latest.efi linux-previous.efi &>/dev/null;
mv -f initrd-previous initrd-old &>/dev/null;
mv -f initrd-latest initrd-previous &>/dev/null;
echo "Installing kernel to $KERNEL_INSTALL_PATH"
cp /usr/src/linux/arch/x86/boot/bzImage linux-latest.efi
echo "----------------------------------------------"
# TODO install initramfs
) &&
 
# Building initramfs
(
# Detecting kernel version from .config
KERNEL_VERSION=`fgrep Linux/x86 .config | awk '{print $3}'`
KERNEL_VERSION_FULL=$KERNEL_VERSION$KERNEL_VERSION_SUFFIX
KERNEL_IMAGE_FILE=$KERNEL_INSTALL_PATH/linux-latest.efi
 
DRACUT_ARGS="--kver $KERNEL_VERSION_FULL --kernel-image $KERNEL_IMAGE_FILE $KERNEL_INSTALL_PATH/initrd-latest"
 
echo "Building Initramfs for $KERNEL_VERSION_FULL in $KERNEL_IMAGE_FILE"
dracut $DRACUT_ARGS
) &&
(
echo "----------------------------------------------"
echo 'Finished!'
echo 'Current kernel files:'
file $KERNEL_INSTALL_PATH/*
)