#!/bin/bash # Author: Brian Hilstrom # Date: May 14, 2004 # # Usage: new_kernel # # 1) Compiles the new kernel (argument) # with old .config file # 2) Copies current kernel to linux.BAK # 3) Copies linux.BAK to linux.old # 4) Removes previous linux.old from # # ### ====== SET THESE VARIABLES !!! ======== TESTING=0 # set to 1 to run in testing mode ROOT_UID=0 KERNEL_PATH=/usr/src/ BOOT_PATH=/boot/ GRUB_PATH=/boot/grub/ KERNEL_CONFIG_PATH=/etc/kernels/ COMPILE_COMMAND="genkernel --kernel-config=/usr/src/linux.BAK/.config --kernel-cc=distcc --makeopts=-j5 all" TO_EMERGE="nvidia-kernel" ### ======================================= ### test for number of args if [ $# -ne 1 ] then echo "Wrong number of args." echo "Usage: new_kernel " exit 1 fi ### test for root access if [ "$UID" -ne "$ROOT_UID" ] then echo "Must be root to run this script." exit 1 fi ### set the path to the 1st argument ### without the beginning KERNEL_PATH ### (no ending '/') NEW_VER=${1#$KERNEL_PATH} ### if it has a trailing '/' remove it if [ ${NEW_VER:${#NEW_VER} - 1} == / ] then NEW_VER=${NEW_VER%%/} fi ### remove the linux- prefix NEW_VER=${NEW_VER#linux-} ### test if linux- is a directory in /usr/src cd $KERNEL_PATH if [ ! -d "linux-$NEW_VER" ] then echo "Path is not a directory in $KERNEL_PATH." echo "Usage: new_kernel " exit 1 fi ### set path and version variables BAK_PATH=`readlink linux` OLD_PATH=`readlink linux.BAK` DELETED_PATH=`readlink linux.OLD` BAK_VER=${BAK_PATH#linux-} OLD_VER=${OLD_PATH#linux-} DELETED_VER=${DELETED_PATH#linux-} ### set OLD_BASE_VER to major version of old kernel OLD_BASE_VER=${OLD_VER:0:3} if [ $TESTING -eq 1 ] then echo "BAK_PATH = $BAK_PATH" echo "OLD_PATH = $OLD_PATH" echo "DELETED_PATH = $DELETED_PATH" echo "BAL_VER = $BAK_VER" echo "OLD_VER = $OLD_VER" echo "DELETED_VER = $DELETED_VER" echo "OLD_BASE_VER = $OLD_BASE_VER" fi ### check for user acknowledgement echo $'\a'$'\n'== This will compile linux-$NEW_VER, do you wish to continue\? \(y to continue\) == read answer echo $'\n' if [ ! $answer == y ] then echo "Stopping execution." exit 0 fi ### delete the .OLD directory ### change the kernel pointers if [ $TESTING -eq 1 ] then echo "here is where I'd be changing all the symlinks" else rm -rf linux-$DELETED_VER rm linux.OLD ln -sf linux-$OLD_VER linux.OLD rm linux.BAK ln -sf linux-$BAK_VER linux.BAK rm linux ln -sf linux-$NEW_VER linux fi ### remove useless config if [ $TESTING -eq 1 ] then echo "now we delete $KERNEL_CONFIG_PATH kernel-config-x86-$DELETED_VER" else rm "$KERNEL_CONFIG_PATH"kernel-config-x86-$DELETED_VER fi ### compile it if [ $TESTING -eq 1 ] then echo "here we compile..." else $COMPILE_COMMAND fi ### if the compile command encounters an error, stop execution if [ $? -ne 0 ] then echo "The compile command encountered an error." exit 1 fi if [ $TESTING -eq 1 ] then echo "here we wrap up the process" exit fi ### delete the old kernel and initrd entries ### change the kernel and boot pointers cd $BOOT_PATH rm kernel-$DELETED_VER rm kernel.OLD ln -sf kernel-$OLD_VER kernel.OLD rm kernel.BAK ln -sf kernel-$BAK_VER kernel.BAK rm kernel ln -sf kernel-$NEW_VER kernel rm initrd-$DELETED_VER rm initrd.OLD ln -sf initrd-$OLD_VER initrd.OLD rm initrd.BAK ln -sf initrd-$BAK_VER initrd.BAK rm initrd ln -sf initrd-$NEW_VER initrd ### change the appropriate lines in grub.conf cd $GRUB_PATH cp grub.conf grub.conf.BAK cat grub.conf | sed "s/$BAK_VER/$NEW_VER/g" | sed "s/$OLD_VER/$BAK_VER/g" > grub.conf.new mv grub.conf.new grub.conf ### emerge packages that require recompilation emerge $TO_EMERGE ### if it's a new base version, update modules.autoload NEW_BASE_VER=`kernelversion` if [ $OLD_BASE_VER -ne $NEW_BASE_VER ] then cp /etc/modules.autoload.d/"$OLD_BASE_VER" /etc/modules.autoload.d/"$NEW_BASE_VER" else echo "Old and new kernels have same base version." fi echo " ==== Completed Successfully! ==== " echo " Remember to check your modules " exit