LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (http://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   kernel installation script (http://www.linuxquestions.org/questions/linux-kernel-70/kernel-installation-script-632865/)

konsolebox 04-03-2008 11:47 PM

(sharing only) a kernel installation script - automates build and install
 
This is an install script that anyone can use for configuring, building and installing kernels. This is mostly useful when you're building and installing your kernel for the first time since most of the time it will require you to do the configure, build and install repetitively and doing cp arch/x86/boot/bzImage .. cp .config .. cp System.map again and again can be really tiresome.

This script was just made this morning and I thought that it might be nice to share it into the linux community.

Here it is.


----------
Changes for 1.0.1 (beta):
- new options for editing /etc/lilo.conf and running lilo after install
- added more program info
- and made messages a little bit more clearer
- fixed a typo: --oldconfig=1 should only be '--oldconfig' in case *; esac;...


Code:

#!/bin/bash
#

about="kernel installation script"
version=1.0.1-beta
author=konsolebox

# Notes:
# I can assure you that this script has alread been tested properly but
# I hope you understand that I still must not offer any warranty.
# Please use the script at your own risk.
#
# But of course please enjoy the program. I hope you'll find it useful.

# Messaging functions

message() { echo "$1"; }
error() { echo "[error] $1" >&2; }
warning() { echo "[warning] $1" >&2; }
verbose() { [[ verbose -eq 1 ]] && message "${1}"; }
errorexit() { error "$1"; exit 1; }
about() {
        cat <<-EOF
${about} ${version}
a script that automates kernel config, build and install

For more info or updates about this program please check this thread in LQ:
http://www.linuxquestions.org/questions/linux-kernel-70/kernel-installation-script-632865/

EOF
}
usage() {
        cat <<-EOF
usage: kernel-install options

${about} ${version}
a script that automates kernel config, build and install

For more info or updates about this program please check this thread in LQ:
http://www.linuxquestions.org/questions/linux-kernel-70/kernel-installation-script-632865/

options:
  [-lp|--linux-path <path>]        Set the location of the linux kernel source
  [-lv|--linux-version <version>]  Set the linux version
  [-bp|--boot-path <boot-path>]    Set where to install the kernel boot image
  [-nb|--no-build]                Do not build the kernel, just install
  [--config-file <config-file>]    The location of the config file to use
  [--oldconfig]                    Do 'make oldconfig' on build
  [-l|--lilo]                      Run lilo after install
  [-le|--lilo-edit]                Edit lilo.conf before running after install
                                    Do 'export EDITOR=[/path/]editor' to pick a
                                    different editor instead of default
  [-L]                            Same as '--lilo --lilo-edit'
  [--menuconfig]                  Do 'make menuconfig' on build
  [ -nmi|--no-modules-install]    Do not install modules
  [-o|--stdout <output-file>]      Use output-file instead of default stdout
  [-e|--stderr <error-file>]      Use error-file instead of default stderr
  [-h|--help]                      Show this help message
  [-v|--verbose]                  Be verbose
  [-V|--version]                  Show version only

EOF
        exit 1
}
showversion() {
        echo "${about} ${version}"
        echo "author: ${author}"
        exit 1
}

# Variables

declare linuxversion=
declare linuxpath=
declare bootpath=
declare -i build=1
declare -i oldconfig=0
declare -i menuconfig=0
declare configfile=
declare -i installmodules=1
declare outputfile=
declare -i verbose=0
declare -i lilo=0
declare -i liloedit=0

# Parse arguments

#if [[ $# -gt 100 ]]; then
#        error "Invalid number of parameters."
#        usage
#fi

for a in ${@}; do
        case ${a} in
        -h|--help)
                usage
                ;;
        -V|--version)
                showversion
                ;;
        esac
done

while [ $# -ge 1 ]; do
        case $1 in
        -lv|--linux-version)
                linuxversion=${2}
                shift
                ;;
        -lp|--linux-path)
                linuxpath=${2%\/}
                shift
                ;;
        -bp|--boot-path)
                bootpath=${2%\/}
                shift
                ;;
        -nb|--no-build)
                build=0
                ;;
        --config-file)
                configfile=${2}
                shift
                ;;
        --menuconfig)
                menuconfig=1
                ;;
        --oldconfig)
                oldconfig=1
                ;;
        -l|--lilo)
                lilo=1
                ;;
        -le|--lilo-edit)
                liloedit=1
                ;;
        -L)
                lilo=1
                liloedit=1
                ;;
        -nmi|--no-modules-install)
                installmodules=0
                ;;
        -o|--output-file)
                outputfile=${2}
                shift
                ;;
        -e|--error-file)
                errorfile=${2}
                shift
                ;;
        -v)
                verbose=1
                ;;
        *)
                error "invalid argument: ${1}"
                message
                usage
                ;;
        esac
        shift
done

# Print program info
about

# Output file
if [ -n "${outputfile}" ]; then
        exec >${outputfile}
fi

# Error file
if [ -n "${errorfile}" ]; then
        exec 2>${errorfile}
fi

# Linux path

if [ -z "${linuxpath}" ]; then
        declare currentversion=$(uname -r)
        for a in . /usr/src/linux /usr/src/linux-${currentversion} /usr/src/local/linux /usr/src/local/linux-${currentversion}; do
                if [[ -d ${a}/kernel && -f ${a}/Makefile ]]; then
                        linuxpath=${a}
                        break
                fi
        done
        if [ -z "${linuxpath}" ]; then
                errorexit "unable to find the default linux directory"
        fi
        verbose ". using ${linuxpath} as the default linux directory"
elif [ ! -d "${linuxpath}" ]; then
        errorexit "linux directory ${linuxpath} not found"
fi

# Linux version

if [ -n "${linuxversion}" ]; then
        ! [[ ${linuxversion} =~ ^2[.]6[.][0-9]+ ]] && errorexit "invalid or not supported linux version ${linuxversion}"
else
        verbose ". looking up for the kernel version"
        a=$(sed -n '/^VERSION = /{ s@VERSION = @@p; q; }' ${linuxpath}/Makefile)
        b=$(sed -n '/^PATCHLEVEL = /{ s@PATCHLEVEL = @@p; q; }' ${linuxpath}/Makefile)
        c=$(sed -n '/^SUBLEVEL = /{ s@SUBLEVEL = @@p; q; }' ${linuxpath}/Makefile)
        d=$(sed -n '/^EXTRAVERSION = /{ s@EXTRAVERSION = @@p; q; }' ${linuxpath}/Makefile)
        declare cf=
        if [[ -n "${configfile}" && -f "${configfile}" ]]; then
                cf=${configfile}
        elif [ -f "${linuxpath}/.config" ]; then
                cf=${linuxpath}/.config
        else
                errorexit "no config files found for searching of the local kernel version"
        fi
        eval e=$(sed -n '/^CONFIG_LOCALVERSION=/{ s@CONFIG_LOCALVERSION=@@p; q; }' ${cf})
        linuxversion=${a}.${b}.${c}${d}${e}
        verbose ". found version: ${linuxversion}"
fi

# Boot path

if [ -z "${bootpath}" ]; then
        bootpath=/boot
        verbose ". using ${bootpath} as the default boot directory"
elif [ ! -d "${bootpath}" ]; then
        errorexit "boot directory ${bootpath} not found"
fi

# Build

if [[ build -eq 1 ]]; then
        message "* building linux kernel ${linuxversion} in ${linuxpath}"

        # config file
        if [ -n "${configfile}" ]; then
                if [ -f "${configfile}" ]; then
                        verbose ". copying config-file ${configfile} to ${linuxpath}/.config"
                        cp "${configfile}" ${linuxpath}/.config
                else
                        errorexit "config file ${configfile} not found"
                fi
        fi

        pushd ${linuxpath} >/dev/null

        # oldconfig
        if [[ oldconfig -eq 1 ]]; then
                verbose ". make oldconfig"
                make oldconfig
        fi

        # menuconfig
        if [[ menuconfig -eq 1 ]]; then
                verbose ". make menuconfig"
                make menuconfig
        fi

        # make
        verbose ". make"
        make

        popd >/dev/null
fi

# Install

# (a) bzImage
message "* ${bootpath}/kernel-${linuxversion}"
declare bzimage=
for a in arch/x86/boot/bzImage arch/i386/boot/bzImage; do
        if [ -f "${linuxpath}/${a}" ]; then
                bzimage=${linuxpath}/${a}
                break
        fi
done
[ -z "${bzimage}" ] && errorexit "bzImage not found"
verbose ". ${bzimage} -> ${bootpath}/kernel-${linuxversion}"
cp "${bzimage}" "${bootpath}/kernel-${linuxversion}"

# (b) .config
message "* ${bootpath}/config-${linuxversion}"
if [ -f "${linuxpath}/.config" ]; then
        verbose ". ${linuxpath}/.config -> ${bootpath}/config-${linuxversion}"
        cp "${linuxpath}/.config" "${bootpath}/config-${linuxversion}"
else
        errorexit "${linuxpath}/.config not found"
fi

# (c) System.map
message "* ${bootpath}/System.map-${linuxversion}"
if [ -f "${linuxpath}/System.map" ]; then
        verbose ". ${linuxpath}/System.map -> ${bootpath}/System.map-${linuxversion}"
        cp "${linuxpath}/System.map" "${bootpath}/System.map-${linuxversion}"
fi

# (d) modules
if [[ installmodules -eq 1 ]]; then
        message "* modules install"
        verbose ". make modules_install"
        pushd ${linuxpath} >/dev/null
        make modules_install
        popd >/dev/null
fi

# (e) edit lilo
if [[ liloedit -eq 1 ]]; then
        [ -z "${EDITOR}" ] && EDITOR=vi
        message "* running lilo.conf edit"
        verbose ". ${EDITOR} /etc/lilo.conf"
        if ! ${EDITOR} /etc/lilo.conf; then
                warning "lilo.conf editing has done with errors"
                if [[ lilo -eq 1 ]]; then
                        warning "cancelling run of lilo"
                        lilo=0
                fi
        fi
fi

# (f) run lilo
if [[ lilo -eq 1 ]]; then
        message "* running lilo"
        verbose ". lilo path: $(which lilo 2>/dev/null)"
        lilo || warning "lilo had some errors"
fi

message "* done."

Instructions
------------

Save the script in a file like 'kernel-install.sh' or 'kernel-install' and change the file's ownership and permissions. You can also place file in a bin path if you like like /usr/local/bin.

After setting-up the script run 'kernel-install -h'. Please be warned that you should not run 'kernel-install' with no arguments yet.

You're testings and comments are always appreciated.
Thanks to all who made tests and/or gave comments about to the code.

jayjwa 04-29-2008 01:47 AM

Good idea, I'll check it out next kernel build. Two thoughts:

1. getopt(1) might help with your options parsing. The last or default option in a case block usually calls the usage() or help function in many scripts.

2. maybe activate lilo/whatever bootloader. I've forgotten that a few times and it makes you have to go back and re-run/reboot lilo & the kernel.

Smedley 04-29-2008 01:58 AM

Salamat............I'll check it out sometime and see how it works.

konsolebox 04-29-2008 09:02 PM

Quote:

Originally Posted by jayjwa (Post 3136283)
Good idea,

Thanks for giving me the first comments! :)
Quote:

I'll check it out next kernel build. Two thoughts:

1. getopt(1) might help with your options parsing. The last or default option in a case block usually calls the usage() or help function in many scripts.
hmmmm.. before i was always using getopts but i thought while ... is simpler.. In the first script i made last 2005 which also included installs for 2.4 kernels, i used getopts but the problem is that in this script, the short options like '-nmi' has more than 1 alphanum chars.. but ok i'll think about that thanks.
Quote:

2. maybe activate lilo/whatever bootloader. I've forgotten that a few times and it makes you have to go back and re-run/reboot lilo & the kernel.
thanks it's now included.. once you tested it and you have a time, please tell me if it worked ok.

Quote:

Originally Posted by Smedley (Post 3136290)
Salamat............I'll check it out sometime and see how it works.

walang ano man.. you're welcome


All times are GMT -5. The time now is 03:12 AM.