Skip to content

Commit 333ea41

Browse files
committed
scripts: add many kernel_* scripts
I've been using these scripts to quickly push kernels to remote targets hosts and update grub configs for many many years. They have lived inside a git repo at Red Hat for my team, but they would likely be valuable for others to use too. Signed-off-by: Jesper Dangaard Brouer <[email protected]>
1 parent 3155eae commit 333ea41

File tree

4 files changed

+273
-0
lines changed

4 files changed

+273
-0
lines changed

scripts/kernel_install.sh

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
# Author: Jesper Dangaard Brouer <[email protected]>
3+
# License: GPL
4+
#
5+
# This script "kernel_install.sh" just installs/copies the binaries into
6+
# a directory (~/git/kernel/install) with the kernel version (extracted
7+
# from include/config/kernel.release).
8+
#
9+
# It is connected to "kernel_push.sh" which rsync the kernel
10+
# binaries+modules to a remote host (usually a KVM virtual guest).
11+
12+
export INSTALL_MOD_PATH=~/git/kernel/install/
13+
14+
export VER=`cat include/config/kernel.release`
15+
echo $VER
16+
17+
CPUS=24
18+
19+
if [ ! -d "$INSTALL_MOD_PATH" ]; then
20+
echo "ERROR: Install dir for kernel binaries \"$INSTALL_MOD_PATH\" does not exist"
21+
exit 3
22+
fi
23+
24+
if [ -z "$VER" ]; then
25+
echo "ERROR: Cannot detect version"
26+
exit 2
27+
fi
28+
29+
rm -rf $INSTALL_MOD_PATH/lib/modules/$VER/kernel/
30+
make -j${CPUS} modules_install
31+
32+
export BOOT="$INSTALL_MOD_PATH/boot/"
33+
[ -d $BOOT ] || mkdir $BOOT
34+
35+
#cp -v arch/x86/boot/bzImage $BOOT/vmlinuz-$VER
36+
#cp -v arch/i386/boot/bzImage $BOOT/vmlinuz-$VER
37+
cp -v arch/x86_64/boot/bzImage $BOOT/vmlinuz-$VER
38+
cp -v System.map $BOOT/System.map-$VER
39+
cp -v vmlinux $BOOT/vmlinux-$VER
40+
cp -v .config $BOOT/config-$VER
41+
cp -v Module.symvers $BOOT/Module.symvers-$VER
42+
43+
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
44+
echo "-=-=-=- DONE installed kernel:[$VER] -=-=-=-"
45+
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"

scripts/kernel_local_install.sh

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
# Author: Jesper Dangaard Brouer <[email protected]>
3+
# License: GPL
4+
#
5+
# Installs/copies the kerne binaries directly on localhost
6+
#
7+
8+
export VER=`cat include/config/kernel.release`
9+
echo $VER
10+
11+
if [ -z "$VER" ]; then
12+
echo "ERROR: Cannot detect version"
13+
exit 2
14+
fi
15+
16+
# Trick so, program can be run as normal user, will just use "sudo"
17+
if [ "$EUID" -ne 0 ]; then
18+
# Directly executable easy
19+
if [ -x $0 ]; then
20+
sudo "$0"
21+
exit $?
22+
fi
23+
echo "ERROR: cannot perform sudo run of $0"
24+
exit 4
25+
fi
26+
27+
#export INSTALL_MOD_PATH=/var/kernels/git/install/
28+
export INSTALL_MOD_PATH=/
29+
rm -rf $INSTALL_MOD_PATH/lib/modules/$VER/kernel/
30+
make -j 16 modules_install
31+
32+
export BOOT="$INSTALL_MOD_PATH/boot/"
33+
[ -d $BOOT ] || mkdir $BOOT
34+
35+
cp -v arch/x86/boot/bzImage $BOOT/vmlinuz-$VER
36+
cp -v arch/i386/boot/bzImage $BOOT/vmlinuz-$VER
37+
cp -v System.map $BOOT/System.map-$VER
38+
cp -v vmlinux $BOOT/vmlinux-$VER
39+
cp -v .config $BOOT/config-$VER
40+
cp -v Module.symvers $BOOT/Module.symvers-$VER || exit 3
41+
42+
if [ -e /etc/redhat-release ]; then
43+
# Red Hat version:
44+
rh_kernel_update="new-kernel-pkg --mkinitrd --depmod --install ${VER}"
45+
echo "- Detected Red Hat target,"
46+
echo " run: $rh_kernel_update"
47+
$rh_kernel_update
48+
else
49+
depmod -a ${KERNEL}
50+
mkinitramfs -o /boot/initrd.img-$VER $VER
51+
update-grub2
52+
fi
53+
54+
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
55+
echo "-=-=-=- DONE installed kernel:[$VER] -=-=-=-"
56+
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"

scripts/kernel_push.sh

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
# Author: Jesper Dangaard Brouer <[email protected]>
3+
# License: GPL
4+
#
5+
# This script "kernel_push.sh" rsync the kernel binaries+modules to a
6+
# remote host (usually a KVM virtual guest). And invokes "new-kernel-pkg"
7+
# on the remote target, which updates grub.conf appropriately (debian targets
8+
# uses depmod+mkinitramfs+update-grub2).
9+
#
10+
# It depends on kernel binaries have been installed, by the script
11+
# "kernel_install.sh", which installs/copies the binaries into a
12+
# directory (~/git/kernel/install)
13+
#
14+
BINARIES_PATH=~/git/kernel/install
15+
16+
#time usage debugging
17+
#t=time
18+
t=/usr/bin/time
19+
20+
if [ -n "$1" ]; then
21+
export HOST=$1
22+
else
23+
if [ -z $HOST ]; then
24+
echo "ERROR - Please specify HOSTNAME to push to!"
25+
exit 1
26+
fi
27+
fi
28+
29+
# Target host OS distro e.g. debian
30+
if [ -n "$2" ]; then
31+
export TARGET=$2
32+
echo "Using disto target: $TARGET"
33+
else
34+
export TARGET=redhat
35+
fi
36+
37+
38+
if [ -z "$VER" ]; then
39+
export VER=`cat include/config/kernel.release`
40+
fi
41+
export KERNEL=$VER
42+
43+
if [ -z "$KERNEL" ]; then
44+
echo "ERROR - Cannot detect the Kernel version you want to push"
45+
exit 2
46+
fi
47+
48+
echo "-=-=-=- Pushing kernel:[$VER] to host:[$HOST] -=-=-=-"
49+
50+
pushd $BINARIES_PATH
51+
$t rsync -e ssh -avz boot/vmlinuz-${KERNEL} root@${HOST}:/boot/
52+
$t rsync -e ssh -avz boot/vmlinux-${KERNEL} root@${HOST}:/boot/
53+
$t rsync -e ssh -avz boot/config-${KERNEL} root@${HOST}:/boot/
54+
$t rsync -e ssh -avz --delete lib/modules/${KERNEL} root@${HOST}:/lib/modules/
55+
$t rsync -e ssh -avz boot/System.map-${KERNEL} root@${HOST}:/boot/
56+
$t rsync -e ssh -avz boot/Module.symvers-${KERNEL} root@${HOST}:/boot/
57+
popd
58+
59+
echo "Executing on remote host: ${HOST}"
60+
61+
echo "Target install distro: $TARGET"
62+
if [ "$TARGET" == "debian" ]; then
63+
#Debian version:
64+
echo " - depmod -a ${KERNEL}"
65+
$t ssh root@${HOST} depmod -a ${KERNEL}
66+
echo " - mkinitramfs"
67+
$t ssh root@${HOST} mkinitramfs -o /boot/initrd.img-${KERNEL} ${KERNEL}
68+
$t ssh root@${HOST} update-grub2
69+
elif [ "$TARGET" == "redhat" ]; then
70+
# Red Hat version:
71+
rh_kernel_update="new-kernel-pkg --mkinitrd --depmod --install ${KERNEL}"
72+
# note mkinitrd is slow
73+
#rh_kernel_update="new-kernel-pkg --depmod --install ${KERNEL}"
74+
#
75+
# Try to use --host-only to limit the size of initrd
76+
#rh_kernel_update="new-kernel-pkg --mkinitrd --host-only --depmod --install ${KERNEL}"
77+
#
78+
# Try --host-only and --dracut
79+
rh_kernel_update="new-kernel-pkg --mkinitrd --dracut --host-only --depmod --install ${KERNEL}"
80+
81+
echo "- Assuming Red Hat target,"
82+
echo " run: $rh_kernel_update"
83+
$t ssh root@${HOST} "$rh_kernel_update"
84+
else
85+
echo "ERROR - Unknown install target: $TARGET"
86+
fi
87+
# Fedora 17:
88+
# grub2-mkconfig -o /boot/grub2/grub.cfg
89+
90+
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
91+
echo "-=-=-=- DONE Pushed kernel:[$VER] to host:[$HOST] -=-=-=-"
92+
echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"

scripts/kernel_remove.sh

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
# Author: Jesper Dangaard Brouer <[email protected]>
3+
# License: GPL
4+
#
5+
# Delete kernel binaries to save harddisk space
6+
7+
if [ -n "$1" ]; then
8+
export KERN=$1
9+
else
10+
echo " - ERROR - Please specify KERNEL to DELETE!"
11+
exit 1
12+
fi
13+
14+
if [ "$KERN" == "/" ]; then
15+
echo " - ERROR - dangerus input exit \"$KERN\""
16+
exit 2
17+
fi
18+
19+
echo "DELETE kernel:${KERN}"
20+
21+
# Trick so, program can be run as normal user, will just use "sudo"
22+
if [ "$EUID" -ne 0 ]; then
23+
# Directly executable easy
24+
if [ -x $0 ]; then
25+
sudo "$0" "$1"
26+
exit $?
27+
fi
28+
echo "ERROR: cannot perform sudo run of $0"
29+
exit 4
30+
fi
31+
32+
MOD_DIR=/lib/modules/${KERN}
33+
if [ -d ${MOD_DIR} ]; then
34+
echo " - Deleting modules dir:${MOD_DIR}"
35+
rm -rf ${MOD_DIR}
36+
else
37+
echo " - WARNING - Modules dir:${MOD_DIR} does NOT exist"
38+
#exit 4
39+
fi
40+
41+
function delete() {
42+
if [ -n "$1" ]; then
43+
local prefix=$1
44+
else
45+
echo " - WARNING - wrong input to delete()"
46+
return 1
47+
fi
48+
if [ -n "$2" ]; then
49+
local suffix=$2
50+
fi
51+
local file=/boot/${prefix}-${KERN}${suffix}
52+
53+
if [ -f ${file} ]; then
54+
echo " - Removing file: $file"
55+
rm $file
56+
else
57+
echo " - WARNING - cannot find file:\"$file\""
58+
return 2
59+
fi
60+
}
61+
62+
echo " Deleting kernel files"
63+
delete vmlinuz
64+
delete vmlinux
65+
delete System.map
66+
delete config
67+
delete initrd .img
68+
delete initramfs .img
69+
delete Module.symvers
70+
71+
# On RedHat: clean up grub
72+
if [ -e /etc/redhat-release ]; then
73+
# Red Hat version:
74+
rh_kernel_remove="new-kernel-pkg --remove ${KERN}"
75+
echo "- Detected Red Hat target, cleanup grub config"
76+
echo " run: $rh_kernel_remove"
77+
$rh_kernel_remove
78+
else
79+
echo "Implement cleanup of grub on your distro... please"
80+
fi

0 commit comments

Comments
 (0)