#!/bin/bash # # kbuild-install # ``````````````` # after user unpacks and configures the kernel, call this script to manage # the kernel build and install sequence. this script also installs to # local staging area and transfers new kernel to remote target. local # kernel install is handled by a companion script called as superuser. # # Copyright 2006,2007 Grant Coady GPLv2 # # home site # `````````` # # # installation # ````````````` # move to /usr/local/bin, along with kbuild-install-local companion script # # see also # ````````` # installkernel, /usr/local/bin/kbuild-install-local # # credits # ```````` # Sylvain Robitaille # pointed out the lack of security using an NFS mounted target / export # and suggesting transfer kernel to target with tar + ssh # # comp.unix.shell people # simplifying getting kernel version from $PWD # # history # ```````` # 2006-05-25 add kernel compile, more sanity checks and interactive # 2006-05-29 rework interactive logic # 2007-01-17 commentary added, fix syntax highlighting (echo -en) # #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # site customisation # ``````````````````` SOURCE_PARENT="linux" # ~/$SOURCE_PARENT contains the various kernel trees TARGET_PARENT="/opt/kbuild" # path from '/' to parent of target staging area DEBUG= # use 'DEBUG=' for normal operation QUIET=1 # use 'QUIET=' for verbose 'make install' (staging) #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ME="kbuild-install" # this script ME_LOCAL="kbuild-install-local" # companion script for local install go_ahead= confirm_option() # [prompt] [default] { local reply="" prompt=$1 default=$2 if [ -z "$prompt" ]; then prompt="continue"; fi if [ -z "$default" ] || [ "$default" = "y" ] then prompt="$prompt? (Y/n)"; go_ahead=1 else prompt="$prompt? (y/N)"; go_ahead= fi echo -en "\n$ME: $prompt: "; read reply case $default in n ) case $reply in y | Y ) go_ahead=1;; esac;; * ) case $reply in n | N ) go_ahead=;; esac;; esac } fatal() # error_message { echo -e "\n$ME: fatal: $1\n" exit 1 } # read Makefile, sanity check plus discover 2.4 or 2.6 kernel series k_series=0 read_kernel_makefile() { local label=(); value=() for i in 1 2 3 4; do read a b c rest # format: label = value label[${i}]=$a value[${i}]=$c [ $DEBUG ] && \ echo "debug: label: ${label[${i}]}, value: ${value[${i}]}" done < Makefile if [ "${label[1]}" != "VERSION" ] || [ ${value[1]} -ne 2 ] then fatal "unrecognised kernel top-level Makefile" fi #k_series="${value[1]}.${value[2]}" # "2.4", "2.6" k_series=${value[2]} # 4, 6 [ $DEBUG ] && \ echo "debug: host: ($target), version: ($k_version), series: ($k_series)" } # some sanity checks [ $UID -eq 0 ] && fatal "do not run this script as root user!" [ ! -f Makefile ] && fatal "called from invalid kernel source directory" # discover target host and kernel_version from caller's PWD target=${PWD%/*} target=${target##*/} k_version=${PWD#*-} INSTALL_PATH="/boot" INSTALL_MOD_PATH="/" MODULE_DIR="/lib/modules/$k_version" read_kernel_makefile if [ "$target" = "$SOURCE_PARENT" ] then target="local" echo -e "\n$ME: compile target is local" else INSTALL_PATH="$TARGET_PARENT/$target/boot" INSTALL_MOD_PATH="$TARGET_PARENT/$target" MODULE_DIR="$INSTALL_MOD_PATH/lib/modules/$k_version" echo -e "\n$ME: compile target is remote: $target" fi echo -e "\n$ME: target host: '$target', kernel version: '$k_version'" # some sanity checking before we perform stuff like 'rm -rf' ;) [ -z "$target" ] || [ -z "$k_version" ] && \ fatal "missing target ($target) or kernel version ($k_version)" [ "$target" != "local" ] && [ ! -d $TARGET_PARENT/$target ] && \ fatal "missing staging directory ($TARGET_PARENT/$target)" confirm_option "sanity checks okay, continue"; [ $go_ahead ] || exit 1 do_compile= go_ahead= # perhaps build the kernel if [ $k_series -eq 4 ] then confirm_option "run 'make dep'" "n" if [ $go_ahead ]; then do_makedep=1 fi confirm_option "run 'make clean'" "n" if [ $go_ahead ]; then do_makecln=1 fi if [ $do_makedep ]; then echo "run 'make dep' quietly" make dep >/dev/null || exit 1 fi if [ $do_makecln ]; then echo "run 'make clean' quietly" make clean >/dev/null || exit 1 fi if [ $do_makedep ] || [ $do_makecln ]; then do_compile=1 fi echo "do_makedep: $do_makedep, do_makecln: $do_makecln, do_compile: $do_compile" if [ $do_compile ]; then go_ahead=1 else confirm_option "run 'make bzImage modules'" "n" fi if [ $go_ahead ]; then do_compile=1 echo "run 'make bzImage && make modules' quietly" make bzImage >/dev/null && make modules >/dev/null || exit 1 fi elif [ $k_series -eq 6 ] then confirm_option "run 'make'" "n" if [ $go_ahead ]; then do_compile=1 echo "run 'make'" make || exit 1 fi else echo " fatal: unknown kernel series (2.$k_series)" exit 1 fi perhaps_transfer_kernel_to_remote() { confirm_option \ "ready to transfer kernel files to '$target', continue" [ $go_ahead ] || exit 1 staging_area="$TARGET_PARENT/$target" kernel="boot/bzImage-$k_version" system="boot/System.map-$k_version" config="boot/config-$k_version" module="lib/modules/$k_version" # perform kernel transfer to target tar cf - -C $staging_area $kernel $system $config $module | \ ssh root@$target "( \ cd / && \ rm -rf /lib/modules/$k_version && \ tar xvpf - && \ chown root:root /boot/*-$k_version && \ chown -R root:root /lib/modules/$k_version )" } perform_make_install() { if [ "$target" = "local" ] then # perform 'make install' to local box echo -en "\n$ME: local install, switch to superuser, Enter " su -c "/usr/local/bin/$ME_LOCAL $MODULE_DIR $QUIET" else # perform 'make install' to staging area echo -e "\n$ME: local install to staging area" export INSTALL_PATH INSTALL_MOD_PATH # perhaps remove old modules [ -d $MODULE_DIR ] && rm -rf $MODULE_DIR # do 'make install' if [ $QUIET ]; then echo "run 'make install' quietly" make install >/dev/null && \ make modules_install >/dev/null || exit 1 else make install && make modules_install || exit 1 fi perhaps_transfer_kernel_to_remote fi } # perform make install automagically after kernel compile, else ask if [ ! -d $MODULE_DIR ] || [ $do_compile ] then go_ahead=1 else confirm_option "ready to 'make install', continue" "n" fi [ $go_ahead ] && perform_make_install echo -e "\n$ME: Done!\n" # end