kernelUpdateScript / kernelUpdateScript.sh
kernelUpdateScript.sh
Raw
# kernelUpdateScript.sh
# Morris, Jarod
# Fall 2020
#
# Description
#    Installs current linux stable kernel source code onto Redhat EC2 instance
# Specifications
#    -g
#        git clone kernel for download
#    -v
#        display current stable kernel version
#    -h
#        display list of options
#    -D Subdir
#        downloads kernel source into give subdirectory
#
# Examples
#    ./kernelUpdateScript.sh -v  : Displays version
#    ./kernelUpdateScript.sh -g -D /src/2020/install : install kernel using git to spec dir
#    ./kernelUpdateScript.sh -v -h  : Displays help
#    ./kernelUpdateScript.sh -D /src/test  : installs kernel using wget into directory
#    ./kernelUpdateScript.sh -g  : installs kernel to default dir using git
#    nohup bash ./kernelUpdateScript.sh 2>nohup.err &  : use nohup to avoid timeouts
#
#!/bin/bash

# reading command line arguments
while getopts gvhD: flag
	do
	    case "$flag" in
	        g) git=true
			;;
	        v) version=true;;
	        h) help=true;;
			D) directory=${OPTARG}
			isDirectory=true;;
			*)
			echo "Invalid input"
			exit 1 ;;
	    esac
	done

# check for help / version
if [ "$help" == true ]
then
echo "usage: ./p01.sh [OPTIONS]
    -g git clone kernel for download
    -v display current stable kernel version
    -h display list of options
    -D Subdir installs kernel into give subdirectory
Examples:
    ./p01.sh -v  : Displays version
    ./p01.sh -g -D /src/2020/install : install kernel using git to spec dir
    ./p01.sh -v -h  : Displays help
    ./p01.sh -D /src/test  : installs kernel using wget into directory
    ./p01.sh -g  : installs kernel to default dir using git"

# should exit if help is called, even with multiple commands
exit 0
fi

# scrape website for stable release
curl -s https://www.kernel.org/ | grep -A 2 '<td>stable:<' \
| grep -Po '(?<=<strong>)[^<]*' > version.out
kernelversion=$(cat version.out)
#rm version.out

if [ "$version" == true ]
then
echo $kernelversion
# should exit if version is called, even with multiple commands
exit 0
fi

# create swap
sudo swapoff -a
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# update system /install development tools
sudo yum group install -y "Development Tools"
sudo yum install -y ncurses-devel make gcc bc bison flex \
elfutils-libelf-devel openssl-devel grub2 wget
sudo yum -y upgrade

mkdir src
cd src

# install ccache
wget https://github.com/ccache/ccache/releases/download/v3.7.11/ccache-3.7.11.tar.xz
wget \
https://github.com/ccache/ccache/releases/download/v3.7.11/ccache-3.7.11.tar.xz.asc

# verify ccache
gpg2 --locate-keys torvalds@kernel.org gregkh@kernel.org joel@debian.org
gpg --verify ccache-3.7.11.tar.xz.asc ccache-3.7.11.tar.xz
gpg --keyserver pgpkeys.mit.edu --list-sigs joel@debian.org | grep RSAKEY
echo -e "trust\n5\ny\nq\n" | gpg --command-fd 0 --edit-key joel@debian.org
gpg --verify ccache-3.7.11.tar.xz.asc ccache-3.7.11.tar.xz
unxz ccache-3.7.11.tar.xz
tar xvf ccache-3.7.11.tar
cd ccache-3.7.11
./configure
yes '' | make
yes '' | make install

# handle possible directory
if [ "$isDirectory" == true ]
then
   if [ -d "$directory" ]
   then
   cd "$directory"
   else
   mkdir -p "$HOME""$directory"
   cd "$HOME""$directory"
   fi
fi

# check for git
if [ "$git" == true ]
then
git clone -v --depth 1 --branch v"$kernelversion" \
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
cd linux-stable

# verify keys
git verify-tag v"$kernelversion"
gpg --keyserver pgpkeys.mit.edu --list-sigs gregkh@kernel.org | grep RSAKEY
echo -e "trust\n5\ny\nq\n" | gpg --command-fd 0 --edit-key gregkh@kernel.org
git verify-tag v"$kernelversion"
git checkout v"$kernelversion"

else

wget \
https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-"$kernelversion".tar.xz
wget \
https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-"$kernelversion".tar.sign

unxz linux-"$kernelversion".tar.xz

gpg --verify linux-"$kernelversion".tar.sign  linux-"$kernelversion".tar
gpg --keyserver pgpkeys.mit.edu --list-sigs gregkh@kernel.org | grep RSAKEY

echo -e "trust\n5\ny\nq\n" | gpg --command-fd 0 --edit-key gregkh@kernel.org
gpg --verify linux-"$kernelversion".tar.sign  linux-"$kernelversion".tar
tar xvf linux-"$kernelversion".tar
rm linux-"$kernelversion".tar
cd linux-"$kernelversion"
fi

cp -v /boot/config-$(uname -r) .config

yes '' | make localmodconfig
scripts/config --disable DEBUG_INFO
sudo rm /etc/dracut.conf.d/xen.conf

yes '' | nohup sudo make -j 2 && sudo make modules_install -j 2 \
&& sudo make install -j 2 &

exit 0