View previous topic :: View next topic |
Author |
Message |
Cocker68 Apprentice

Joined: 16 Jan 2003 Posts: 227 Location: Germany
|
Posted: Thu Nov 11, 2004 9:02 am Post subject: [Script] easy kernel installation |
|
|
Since quite a time I use a selfmade script to easily install fresh compiled kernels, and keeping in the same time maximum order in /boot.
- Edit Your /boot/grub/grub.conf and add the two comment-lines before Your first kernel-statement:
Code: | default 0
timeout 5
# don't remove this anchor for script installkernel:
#@@kernelinstall
title=2.6.9-gentoo-r1-b3 (APIC enabled)
root (hd0,0)
kernel /2.6.9-gentoo-r1-b3 root=/dev/hda2 vga=extended
[... other entries following] |
... or if You use lilo, add the lines
Code: | # don't remove this anchor for script installkernel:
#@@kernelinstall | in front of Your kernel-section.
Create a script /root/bin/kernelinstall:
Code: | #!/bin/bash
#@@CCK 2004-11-05 11:51
#
# Installs kernel into boot-dir together with config-file
# Makes entry in grub.conf
COMMENT="$1"
CONFIGFILE="/root/bin/kernelinstall.config"
source "$CONFIGFILE"
if [ $? -ne 0 ]; then
echo "Error reading config file \"$CONFIGFILE\""
exit 1
fi
if [ ! "`mount|grep /boot`" ]; then
echo "Error: boot-partition not mounted"
exit 3
fi
if [ ! -e "$KERNELCONFIG" ]; then
echo "Error: File not found: \"$KERNELCONFIG\" Run this script in the base of kernel source tree."
exit 1
fi
if [ ! -e "$BZIMAGE" ]; then
echo "Error: File not found: \"$BZIMAGE\" Compile a kernel first. Run this script in the base of kernel source tree."
exit 2
fi
# assuming that name of kernel source tree base dir equals the kernel name
KERNELVERSION=`pwd -P`
KERNELVERSION=`basename $KERNELVERSION`
# remove the leading "linux-"
KERNELVERSION=${KERNELVERSION#linux-*}
if [ ! -e "$BUILDFILE" ]; then
echo "Warning: File not found: \"$BUILDFILE\" Cannot distinguish build version -> assuming build 1."
BUILD=1
else
BUILD=`cat "$BUILDFILE"`
fi
KERNELNAME="${KERNELVERSION}-b${BUILD}"
CMD="cp -pi \"$KERNELCONFIG\" \"${BOOTDIR}/${KERNELNAME}.config\""
echo "$CMD"
eval $CMD
CMD="mv -i \"$BZIMAGE\" \"${BOOTDIR}/${KERNELNAME}\""
echo "$CMD"
eval $CMD
CMD="cp -pi \"$SYSTEMMAP\" \"${BOOTDIR}/${KERNELNAME}.System.map\""
echo "$CMD"
eval $CMD
if [ -z "$ANCHOR" ]; then
echo "Warning: variable \$ANCHOR empty. Not processing boot-loader config files"
else
if [ "$BOOTLOADER" = "grub" ]; then
#################################################################
# applying to grub.conf
if [ ! -e "$BOOTLOADERCONF" ]; then
echo "Warning: config file for $BOOTLOADER not found: \"$BOOTLOADERCONF\""
else
grep -q "$ANCHOR" "$BOOTLOADERCONF"
if [ $? -ne 0 ]; then
echo "Warning: Please place anchor string \"$ANCHOR\" in \"$BOOTLOADERCONF\" to mark the location of new entry. \"$BOOTLOADERCONF\" not modified..."
else
echo "Applying new kernel to \"$BOOTLOADERCONF\". Keeping old config in \"${BOOTLOADERCONF}.old\""
cp -p "$BOOTLOADERCONF" "${BOOTLOADERCONF}.old"
SEDCMD="${ANCHOR}\n\ntitle=${KERNELNAME} (${COMMENT})\n${ROOT}\nkernel /${KERNELNAME} ${APPEND}"
sed -e "s°${ANCHOR}°${SEDCMD}°" < $BOOTLOADERCONF.old > ${BOOTLOADERCONF}
fi
fi
elif [ "$BOOTLOADER" = "lilo" ]; then
#################################################################
# applying to lilo.conf
if [ ! -e "$BOOTLOADERCONF" ]; then
echo "Warning: config file for $BOOTLOADER not found: \"$BOOTLOADERCONF\""
else
grep -q "$ANCHOR" "$BOOTLOADERCONF"
if [ $? -ne 0 ]; then
echo "Warning: Please place anchor string \"$ANCHOR\" in \"$BOOTLOADERCONF\" to mark the location of new entry. \"$BOOTLOADERCONF\" not modified..."
else
echo "Applying new kernel to \"$BOOTLOADERCONF\". Keeping old config in \"${BOOTLOADERCONF}.old\""
cp -p "$BOOTLOADERCONF" "${BOOTLOADERCONF}.old"
SEDCMD="${ANCHOR}\n\n# ${COMMENT}\nimage=${BOOTDIR}/${KERNELNAME}\n label=${KERNELNAME}\n ${ROOT}\n ${APPEND}"
sed -e "s°${ANCHOR}°${SEDCMD}°" < $BOOTLOADERCONF.old > ${BOOTLOADERCONF}
# execute lilo command
lilo
fi
fi
else
echo "Warning: variable \$BOOTDIR empty. Not processing boot-loader config files"
fi
fi | and make it executable.
Create its config-file /root/bin/kernelinstall.config:
Code: | # configuration file for script "kernelinstall"
# directory, where boot kernels lie - usually "/boot"
BOOTDIR="/boot"
### IMPORTANT ###################################################
# type of boot-loader - usually "lilo" or "grub"
BOOTLOADER="grub"
# name of boot-loader config file -
# usually "${BOOTDIR}/grub/grub.conf" or "/etc/lilo.conf"
BOOTLOADERCONF="${BOOTDIR}/grub/grub.conf"
# entry for root partition
ROOT="root (hd0,0)"
# append string for kernel boot
APPEND="root=/dev/hda2 vga=extended"
#################################################################
# name of kernel config file - usually "./.config"
KERNELCONFIG="./.config"
# name of new compiled kernel - usually "./arch/i386/boot/bzImage"
BZIMAGE="./arch/i386/boot/bzImage"
# name of System.map file - usually "./System.map"
SYSTEMMAP="./System.map"
# name of build version file - usually ".version"
BUILDFILE=".version"
# anchor string in grub config file to mark position for new entry
ANCHOR="#@@kernelinstall"
#################################################################
|
Compile a kernel as usual, and install it using the above script:
Code: | # cd /usr/src/linux
# make menuconfig
# make && make modules_install
# mount /boot
# ~/bin/kernelinstall "<your comment>"
# umount /boot |
This way Your /boot directory alway stays clearly arranged:
Code: | # ls -1 /boot
2.6.8-gentoo-r7-b3
2.6.8-gentoo-r7-b3.System.map
2.6.8-gentoo-r7-b3.config
2.6.9-gentoo-r1-b1
2.6.9-gentoo-r1-b1.System.map
2.6.9-gentoo-r1-b1.config |
The number after "b" shows Your build-version.
Tell me how You like it, and how to possibly improve it.
- Cocker :wq |
|
Back to top |
|
 |
thechris Veteran

Joined: 12 Oct 2003 Posts: 1203
|
Posted: Thu Nov 11, 2004 10:45 am Post subject: |
|
|
there are some security issues with source "file"
you can always use grep or egrep to find lines:
with &, ;, |, >, < or ` and that don't start with whitespace followed by a #.
this is so someone couldn't modify you're config with:
x="2";killall bash
or
x=`virii.sh`
ect...
if non-root administrators are allowed to use the script, they may be able to modify the config. then if root ran the script, well, it would be tricked into running bad commands. i know this is unlikely though.
also, with grub, if you compile a kernel, then realize you forgot something and re-ran the script, would it add another option. i used to have a simple script for similar purposes that basically checked grub.conf for the kernel version before it insterted it. |
|
Back to top |
|
 |
Cocker68 Apprentice

Joined: 16 Jan 2003 Posts: 227 Location: Germany
|
Posted: Thu Nov 11, 2004 11:07 am Post subject: |
|
|
thechris wrote: | there are some security issues with source "file"
you can always use grep or egrep to find lines:
with &, ;, |, >, < or ` and that don't start with whitespace followed by a #. |
Yes You are right, but I don't understand Your relief.
What would You do instead of source?
thechris wrote: | also, with grub, if you compile a kernel, then realize you forgot something and re-ran the script, would it add another option. |
This is correct, but with intention, and in the way I use to handle kernels without this script.
E.g. I install a kernel with ~/bin/kernelinstall "new release"
Then I want to test a WLAN-card, so I compile with WLAN and run ~/bin/kernelinstall "with WLAN", which adds a further kernel to /boot as well as to grub.conf.
This way no kernel gets lost, until I manually remove it from /boot and from grub.conf.
If You run the script without having newly compiled, nothing gets installed, because bzImage gets moved to /boot instead of being copied.
- Cocker :wq |
|
Back to top |
|
 |
dkure n00b

Joined: 20 Apr 2004 Posts: 51 Location: Sydney, Australia
|
Posted: Thu Nov 11, 2004 12:17 pm Post subject: |
|
|
you can have this script automatically mount /boot for you, instead of manually needing to mount it.
Code: |
# /boot CHECK
cboot="`cat /etc/mtab | grep boot | awk '{print $2}'`"
if [ -z $cboot ]
then
echo "Mounting /boot"
mount /boot
echo "/boot mounted"
fi
#Unmount boot
umount /boot
|
|
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|