#!/bin/bash ################################################################################ # Copyright 2007 by Tobias Schlitt # ################################################################################ # # # This program is free software; you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation; either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # ################################################################################ # Run this program as root with the name of your new kernel as the parameter. # E.g. # $ sudo makekernel.sh linux-2.6.22-gentoo-r8 # to build the kernel stored in /usr/src/linux-2.6.22-gentoo-r8 as the new kernel # # Configuration # # Edit the following values to suite your needs # Base dir where the kernel config is stored KERNEL_SOURCE="/usr/src" # Mount point for the boot partition BOOT_DIR="/boot" # The grub configuration file GRUB_CONF="$BOOT_DIR/grub/grub.conf" # The file where makekernel.sh will create the new grub.conf GRUB_CONF_NEW="$BOOT_DIR/grub/.grub.conf.new" # The file where the old grub.conf will be backed up GRUB_CONF_BACKUP="$BOOT_DIR/grub/grub.conf.bak" # Title line in grub.conf. %s will be replaced by the kernel version GRUB_TITLE_LINE="Gentoo-%s" # The part after 'root ' in grub.conf GRUB_ROOT_OPTION="(hd0,0)" # Options to pass to the new kernel GRUB_KERNEL_OPTIONS="root=/dev/sda3" # Parameters to the "module-rebuild" tool (will only affect you if this is installed) MODULE_REBUILD_OPTIONS="-X" # # Starting up, do NOT change anything below this line unless you know what you are doing! # if [[ `whoami` == "root" ]] then echo "Good to see you are root! ;)" else echo "You need to be root to use this script!" exit -1 fi if [ $# -lt 1 ] then echo "Please specify new kernel name" exit 1; fi # # Auto detection of config values # # Strip trailing slash if there KERNEL_NAME=`echo "$1" | sed 's/\/$//g'`; KERNEL_DIR="$KERNEL_SOURCE/$KERNEL_NAME" KERNEL_VERSION=`echo "$KERNEL_NAME" | sed 's/linux-//g'` LINUX_DIR="$KERNEL_SOURCE/linux" LINUX_OLD_DIR="$KERNEL_SOURCE/linux.old" OLD_DIR=`pwd` # # Move around linux links # if [[ -h "$LINUX_OLD_DIR" ]] then rm "$LINUX_OLD_DIR" || ( echo "--- Could not delete $LINUX_OLD_DIR ---" && exit 1 ) fi if [[ -h "$LINUX_DIR" ]] then mv "$LINUX_DIR" "$LINUX_OLD_DIR" fi ln -s "$KERNEL_DIR" "$LINUX_DIR" || ( echo "--- Could not create link from $KERNEL_DIR to $LINUX_DIR ---" && exit 1 ) # # Copy old config # if [[ ! -e "$LINUX_DIR/.config" ]] then cp "$LINUX_OLD_DIR/.config" "$LINUX_DIR/.config" || ( echo "--- Could not copy .config ---" && exit 1 ) fi # # Change into kernel dir # cd "$LINUX_DIR" || ( echo "--- Could not change to $LINUX_DIR ---" && exit 1 ) # # Perform make operations echo "Performing make oldconfig, this may produce a lot of output..." make oldconfig || ( echo "--- make oldconfig failed ---" && exit 1 ) echo "" echo "" echo "---------------------------------------------------------------------" echo "------------------- makekernel.sh back in action! -------------------" echo "---------------------------------------------------------------------" echo "" echo "" echo "Now doing make clean..." make clean &>"$LINUX_DIR/makekernel.clean.log" || ( echo "--- make clean failed, see $LINUX_DIR/makekernel.clean.log for more infos ---" && exit 1 ) rm "$LINUX_DIR/makekernel.clean.log" echo "Now performing make, this can take a while." echo "You can watch progress with 'tail -f $LINUX_DIR/makekernel.make.log' on another shell." echo "Hang on..." make &>"$LINUX_DIR/makekernel.make.log" || ( echo "--- make failed, see $LINUX_DIR/makekernel.make.log for more infos ---" && exit 1 ) rm "$LINUX_DIR/makekernel.make.log" echo "Now performing make, this can take a while." echo "You can watch progress with 'tail -f $LINUX_DIF/makekernel.modules.log' on another shell." echo "Hang on..." make modules_install &>"$LINUX_DIF/makekernel.modules.log" || ( echo "--- make modules_install failed, see $LINUX_DIR/makekernel.make.log for more infos ---" && exit 1 ) rm "$LINUX_DIF/makekernel.modules.log" # # Mount /boot # if mount | grep -q "$BOOT_DIR" then echo "$BOOT_DIR allready mounted, will not be unmounted later." BOOT_DIR_MOUNTED=true else echo "Mounting $BOOT_DIR..." mount $BOOT_DIR || ( echo "--- Could not mount $BOOT_DIR, not in /etc/fstab? ---" && exit 1 ) fi # # Copying boot files # echo "Copying kernel files..." cp "arch/i386/boot/bzImage" "$BOOT_DIR/kernel-$KERNEL_VERSION" || ( echo "--- Could not copy bzImage ---" && exit 1 ) cp ".config" "$BOOT_DIR/.config-$KERNEL_VERSION" || echo "--- Could not copy .config ---" cp "System.map" "$BOOT_DIR/System.map-$KERNEL_VERSION" || echo "--- Could not copy System.map ---" # # Alter grub.conf # if grep -q "$KERNEL_VERSION" "$GRUB_CONF" then echo "Kernel version $KERNEL_VERSION already in $GRUB_CONF." else echo "Creating new $GRUB_CONF including kernel version $KERNEL_VERSION in $GRUB_CONF_NEW..." # Determine first kernel entry HEAD_LINE_COUNT=`grep -n 'title' "$GRUB_CONF" | sed 's/^\([0-9]*\):.*/\1/g' | head -n 1` # Go 2 line backwards let "HEAD_LINE_COUNT -= 1" head -n"$HEAD_LINE_COUNT" "$GRUB_CONF" > "$GRUB_CONF_NEW" echo "# Generated by makekernel.sh on" `date` >> "$GRUB_CONF_NEW" echo "title $GRUB_TITLE_LINE" | sed "s/%s/$KERNEL_VERSION/" >> "$GRUB_CONF_NEW" echo "root $GRUB_ROOT_OPTION" >> "$GRUB_CONF_NEW" echo "kernel /boot/kernel-$KERNEL_VERSION $GRUB_KERNEL_OPTIONS" >> "$GRUB_CONF_NEW" echo "" >> "$GRUB_CONF_NEW" let "TAIL_LINE_COUNT = `cat $GRUB_CONF | wc -l` - $HEAD_LINE_COUNT" tail -n "$TAIL_LINE_COUNT" "$GRUB_CONF" >> "$GRUB_CONF_NEW" echo "Successfully wrote new $GRUB_CONF in $GRUB_CONF_NEW." echo "Backing up old $GRUB_CONF to $GRUB_CONF_BACKUP." mv "$GRUB_CONF" "$GRUB_CONF_BACKUP" || ( echo "--- Could not move $GRUB_CONF to $GRUB_CONF_BACKUP ---" && exit 1 ) echo "Overwriting $GRUB_CONF with newly created $GRUB_CONF_NEW." mv "$GRUB_CONF_NEW" "$GRUB_CONF" || ( echo "--- Could not move $GRUB_CONF_NEW to $GRUB_CONF ---" && exit 1 ) fi # Unmounit /boot if [ $BOOT_DIR_MOUNTED ] then echo "$BOOT_DIR dir was already mounted, do not unmount" else echo "Unmounting $BOOT_DIR" umount "$BOOT_DIR" || echo "--- Could not unmount $BOOT_DIR, do this manually! ---" fi if [[ -e /usr/sbin/module-rebuild ]] then echo "Rebuilding external modules..." ( /usr/sbin/module-rebuild "$MODULE_REBUILD_OPTIONS" rebuild 2>&1 > "$LINUX_DIR/makekernel.rebuild.log" && rm "$LINUX_DIR/makekernel.rebuild.log" ) || echo "--- Rebuilding modules failed! Try module-rebuild manually or manually rebuild your external modules. See $LINUX_DIR/makekernel.rebuild.log for details. ---" echo "External modules rebuilt successfully" fi echo "Going back to where you came from..." cd "$OLD_DIR" echo "Kernel install complete. Your new kernel will be active on next reboot." echo "" echo "Thanks for using this script."