Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petri152/90aaadc4ccad1f9ad8c368c8cd307710 to your computer and use it in GitHub Desktop.
Save petri152/90aaadc4ccad1f9ad8c368c8cd307710 to your computer and use it in GitHub Desktop.
Download, patch, compile and install the newest stable Linux kernel with the ACS override patch (Ubuntu / Debian)
#!/bin/bash
function install_dependencies() {
sudo apt -qq update
sudo apt -qq install curl git wget
sudo apt -qq build-dep linux-image-generic
}
function init() {
echo "Initializing..."
kernel_config=$(ls /boot/config-* | grep generic | sort -Vr | head -n 1)
grub_config="/etc/default/grub"
current_dir=$(pwd)
working_dir=$(mktemp -d)
cd ${working_dir}
install_dependencies
}
function get_newest_versions() {
stable_releases="https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/"
stable_version=$(curl -s "${stable_releases}" | egrep -o 'linux-([0-9]{1,}\.)+[0-9]{1,}' | sort -Vr | head -n 1 | cut -d '-' -f 2)
stable_link="${stable_releases}linux-${stable_version}.tar.xz"
mainline_link=$(curl -s https://www.kernel.org/ | grep https://git.kernel.org/torvalds/t/linux- | grep -Po '(?<=href=")[^"]*')
if ! [ -z ${mainline_link} ]
then
mainline_version=$(echo ${mainline_link} | cut -d '-' -f 2,3 | cut -d '.' -f 1,2)
else
mainline_version="unavailable"
fi
repo_string=$(apt search 'linux-source-' | grep 'linux-source' | sort -Vr | head -n 1)
repo_pkg=$(echo ${repo_string} | egrep -o '^[^/]+')
repo_version=$(echo ${repo_string} | cut -d ' ' -f 2)
}
function stable_or_mainline_or_repo() {
get_newest_versions
echo "Newest stable version is: ${stable_version}"
echo "Mainline version is: ${mainline_version}"
echo "Repository version is: ${repo_version}"
echo -n "Do you want to get a [s]table, the newest [m]ainline release candidate or the newest kernel from your [r]epositories? [S/m/r] "
read s_or_m_or_r
echo -n "Do you want to apply the acs override patch? Kernels below 4.10 are not supported. [Y/n] "
read acso
echo -n "Do you want to install the kernel and its headers after compilation? [Y/n] "
read install
if [ -z ${install} ] || [ ${install} == "y" ]
then
echo -n "Do you want to make this kernel the new default? [Y/n] "
read default
fi
if [ -z ${s_or_m_or_r} ] || [ ${s_or_m_or_r} == "s" ]
then
stable_preparations
else
if [ ${s_or_m_or_r} == "m" ]
then
if [ ${mainline_version} == "unavailable" ]
then
echo "Mainline version currently unavailable. Exiting."
exit
else
mainline_preparations
fi
else
if [ ${s_or_m_or_r} == "r" ]
then
repo_preparations
else
echo "Not a valid option. Exiting."
exit
fi
fi
fi
}
function try_acso_patch() {
acso_patch_4_18="https://gitlab.com/Queuecumber/linux-acs-override/raw/master/workspaces/4.18/acso.patch"
acso_patch_4_17="https://gitlab.com/Queuecumber/linux-acs-override/raw/master/workspaces/4.17/acso.patch"
acso_patch_4_14="https://gitlab.com/Queuecumber/linux-acs-override/raw/master/workspaces/4.14/acso.patch"
acso_patch_4_10="https://gitlab.com/Queuecumber/linux-acs-override/raw/master/workspaces/4.10/acso.patch"
echo "Trying to apply acs override patch for 4.18+."
wget -O ../acso_4_18.patch "${acso_patch_4_18}"
if $(git apply --check ../acso_4_18.patch)
then
echo "Applying acs override patch for 4.18+."
git apply ../acso_4_18.patch
else
echo "Trying to apply acs override patch for 4.17+."
wget -O ../acso_4_17.patch "${acso_patch_4_17}"
if $(git apply --check ../acso_4_17.patch)
then
echo "Applying acs override patch for 4.17+."
git apply ../acso_4_17.patch
else
echo "Trying to apply acs override patch for 4.14+."
wget -O ../acso_4_14.patch "${acso_patch_4_14}"
if $(git apply --check ../acso_4_14.patch)
then
echo "Applying acs override patch for 4.14+."
git apply ../acso_4_14.patch
else
echo "Trying to apply acs override patch for 4.10+."
wget -O ../acso_4_10.patch "${acso_patch_4_10}"
if $(git apply --check ../acso_4_10.patch)
then
echo "Applying acs override patch for 4.10+."
git apply ../acso_4_10.patch
else
echo "[ERROR]: Failed to apply acs override patch. Exiting."
exit
fi
rm -f ../acso_4_10.patch
fi
rm -f ../acso_4_14.patch
fi
rm -f ../acso_4_17.patch
fi
rm -f ../acso_4_18.patch
kernel_localversion="-acso"
}
function stable_preparations() {
echo "The newest available stable kernel version is ${stable_version}. Kernels below 4.10 are not supported."
echo -n "Which version do you want to download? [${stable_version}] "
read user_version
if ! [ -z ${user_version} ]
then
stable_version=${user_version}
fi
kernel_version=${stable_version}
kernel_name="linux-${kernel_version}"
wget "${stable_link}"
tar xvf ${kernel_name}.tar.xz
cd ${kernel_name}
independent_code
}
function mainline_preparations() {
kernel_version=${mainline_version}
kernel_name="linux-${kernel_version}"
wget "${mainline_link}"
tar xvf ${kernel_name}.tar.gz
cd ${kernel_name}
independent_code
}
function repo_preparations() {
kernel_name=${repo_pkg}
sudo apt install ${repo_pkg}
tar xvf /usr/src/${kernel_name}.tar.bz2
cd ${kernel_name}
makefile_version=$(cat Makefile | grep "^VERSION" | tr -d '[:space:]' | cut -d '=' -f 2)
makefile_patchlevel=$(cat Makefile | grep "^PATCHLEVEL" | tr -d '[:space:]' | cut -d '=' -f 2)
makefile_sublevel=$(cat Makefile | grep "^SUBLEVEL" | tr -d '[:space:]' | cut -d '=' -f 2)
#makefile_extraversion=$(cat Makefile | grep "^EXTRAVERSION" | tr -d '[:space:]' | cut -d '=' -f 2)
if ! [ -z ${makefile_version} ]
then
kernel_version=${makefile_version}
if ! [ -z ${makefile_patchlevel} ]
then
kernel_version=${makefile_version}.${makefile_patchlevel}
if ! [ -z ${makefile_sublevel} ]
then
kernel_version=${makefile_version}.${makefile_patchlevel}.${makefile_sublevel}
fi
fi
fi
from_repo="true"
independent_code
}
function independent_code() {
kernel_localversion="-localversion"
if [ -z ${acso} ] || [ ${acso} != "n" ]
then
try_acso_patch
fi
cp ${kernel_config} .config
yes '' | make oldconfig
make clean
if [ "${from_repo}" != "true" ]
then
##<Ubuntu specifics>
ubuntu_codename=$(lsb_release -s -c)
cd ..
cp -a /usr/share/kernel-package ubuntu-package
##error on bionic
if [ "${ubuntu_codename}" != "bionic" ]
then
git clone --depth=1 git://kernel.ubuntu.com/ubuntu/ubuntu-${ubuntu_codename}.git
cp ubuntu-${ubuntu_codename}/debian/control-scripts/{postinst,postrm,preinst,prerm} ubuntu-package/pkg/image/
cp ubuntu-${ubuntu_codename}/debian/control-scripts/headers-postinst ubuntu-package/pkg/headers/
fi
cd ${kernel_name}
CONCURRENCY_LEVEL=$(getconf _NPROCESSORS_ONLN) fakeroot make-kpkg --initrd --append-to-version=${kernel_localversion} --overlay-dir=../ubuntu-package kernel_image kernel_headers
##</Ubuntu specifics>
else
CONCURRENCY_LEVEL=$(getconf _NPROCESSORS_ONLN) fakeroot make-kpkg --initrd --append-to-version=${kernel_localversion} kernel_image kernel_headers
fi
if [ -z ${install} ] || [ ${install} == "y" ]
then
sudo dpkg -i ../*.deb
if [ -z ${default} ] || [ ${default} == "y" ]
then
##removing previous commented line
sudo sed -i -e 's/^#GRUB_DEFAULT=.*//g' ${grub_config}
##commenting current line
sudo sed -i 's/^GRUB_DEFAULT=/#GRUB_DEFAULT=/' ${grub_config}
##adding line
grub_line="GRUB_DEFAULT=\"Advanced options for Ubuntu>Ubuntu, with Linux ${kernel_version}${kernel_localversion}\""
sudo sed -i -e "s/^#GRUB_DEFAULT=.*/\0\n${grub_line}/" ${grub_config}
sudo update-grub
fi
fi
cd ${current_dir}
rm -rf ${working_dir}
}
##actual logic
init
stable_or_mainline_or_repo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment