LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Search this Thread
Old 04-03-2008, 10:47 PM   #1
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
(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.

Last edited by konsolebox; 04-29-2008 at 08:15 PM. Reason: updated to 1.0.1 (beta)
 
Old 04-29-2008, 12:47 AM   #2
jayjwa
Member
 
Registered: Jul 2003
Location: NY
Distribution: None (src & compile)
Posts: 253

Rep: Reputation: 36
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.
 
Old 04-29-2008, 12:58 AM   #3
Smedley
Registered User
 
Registered: Apr 2008
Distribution: Slackware
Posts: 74

Rep: Reputation: 15
Salamat............I'll check it out sometime and see how it works.
 
Old 04-29-2008, 08:02 PM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Original Poster
Rep: Reputation: 233Reputation: 233Reputation: 233
Thumbs up

Quote:
Originally Posted by jayjwa View Post
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 View Post
Salamat............I'll check it out sometime and see how it works.
walang ano man.. you're welcome
 
  


Reply

Tags
bash, install, installation, kernel, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Running post script after installation. bbmak Linux - Enterprise 1 11-20-2007 10:10 AM
Post Installation Script and others Guitarist88 Suse/Novell 2 02-17-2007 01:27 AM
kernel panic: try passing init= option to kernel...installation with Red Hat 9 kergen Linux - Hardware 1 09-30-2004 03:28 AM
how do I run installation script after unpacking??? beat_researcher Linux - Software 4 02-26-2004 06:57 PM
Creating an automated installation script Kaze13d Slackware 2 11-30-2003 12:08 AM


All times are GMT -5. The time now is 09:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration