#!/bin/bash # Just a simple wrap to execute extra commands when building kernel and initramfs from genkernel. # ------------------------------------------------------------------------------- # "THE BEER-WARE LICENSE" (Revision 42): # wrote this file. As long as you retain this notice # you can do whatever you want with this stuff. If we meet some day, and you think # this stuff is worth it, you can buy me a beer in return. Aline Freitas # ------------------------------------------------------------------------------- # EFI boot dir. _efiPath=/boot/EFI/Boot _imagePath=/usr/src/linux/arch/x86/boot _rootDevice=/dev/sda # In _args we get any arguments except the last (_action). array=($@) len=${#array[@]} _action=${array[$len-1]} _args=${array[@]:0:$len-1} # Check if our $UID = 0 if [ "$(id -u)" != "0" ]; then echo "Sorry, you are not root." exit 1 fi # Check if our directories exist. if [[ ! -d $_efiPath ]]; then mount /boot fi if [[ ! -d $_imagePath ]] || [[ ! -d $_efiPath ]]; then echo "$_imagePath or $_efiPath does not exist" exit 1 fi # If the user gives only one argument, this one should be _action. if [ $# = 1 ]; then _action=$1 unset _args elif [ $# = 0 ]; then /usr/bin/genkernel --help fi # Get kernel version from kernel image. kernversion () { echo $(file $1 | grep -Po '(?<=version)\W*\K[^ ]*') } case $_action in all) # Execute genkernel and if successful check if the kernel image exist. echo "Executing genkernel $_args all..." /usr/bin/genkernel $_args all && [[ -f $_imagePath/bzImage ]] builtkernver=$(kernversion $_imagePath/bzImage) currentkernver=$(kernversion $_efiPath/bootx64.efi) # Compare built image kernel version with existent in EFI. if [[ $builtkernver != $currentkernver ]]; then echo "Moving current kernel to bootx64.old.efi..." cp $_efiPath/bootx64{,.old}.efi fi echo "Installing bzImage to bootx64.efi..." cp $_imagePath/bzImage $_efiPath/bootx64.efi if ! efibootmgr -v grep -qi 'efi\\boot\\bootx64.efi'; then find /boot -iwholename /boot/efi/boot/bootx64.efi && \ efibootmgr -c -d ${_rootDevice} -p 1 -L "Gentoo" -l "\EFI\Boot\Bootx64.efi" fi if ! efibootmgr -v grep -qi 'efi\\boot\\bootx64.old.efi'; then find /boot -iwholename /boot/efi/boot/bootx64.efi && \ efibootmgr -c -d ${_rootDevice} -p 1 -L "Gentoo OLD" -l "\EFI\Boot\Bootx64.old.efi" fi ;; *) /usr/bin/genkernel $_args $_action ;; esac