Last active
March 3, 2019 11:37
-
-
Save lilianmoraru/36ebaea3e59f601fe41c2b1119229d32 to your computer and use it in GitHub Desktop.
pop_os self-compiling the Linux Kernel and installing inside EFI(also installs as backup the distro-provided kernel)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
set -euo pipefail | |
IFS=$'\n\t' | |
sudo update-alternatives --auto gcc | |
mtune_patch_path="${mtune_patch_path:-${HOME:?}/HelperScripts/enable_additional_cpu_optimizations_for_gcc_v8.1+_kernel_v4.13+.patch}" | |
kernel_config_path="${kernel_config_path:-${HOME:?}/HelperScripts/MyKernelConfig}" | |
extract_location="${extract_location:-/dev/shm/linux-source}" | |
CFLAGS="-O3 -DNDEBUG -D_GNU_SOURCE -fomit-frame-pointer -fno-asynchronous-unwind-tables -ftree-vectorize -floop-strip-mine -floop-block -fgraphite-identity -m64 -mavx -march=native -mtune=native" | |
CXXFLAGS="-O3 -DNDEBUG -D_GNU_SOURCE -fomit-frame-pointer -fno-asynchronous-unwind-tables -ftree-vectorize -floop-strip-mine -floop-block -fgraphite-identity -m64 -mavx -march=native -mtune=native" | |
error() { | |
local -r message="${1:?}" | |
echo "${message}" | |
exit 1 | |
} | |
set_linux_version() { | |
cat Makefile | head -5 > Makefile.linux_version | |
echo ' | |
all: | |
@echo $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION) | |
.PHONY: all | |
' >> Makefile.linux_version | |
linux_version="$(make -f Makefile.linux_version)" | |
} | |
download_and_extract() { | |
local -r link="${1:?}" | |
local -r pre_link="$(echo "${link}" | sed 's|/linux-[0-9]*\.[0-9]*.*tar.*$||')/" | |
local -r linux_archive="$(echo "${link}" | sed "s|${pre_link:?}||")" | |
linux_directory="$(echo "${linux_archive}" | sed 's|.tar.*||')" | |
mkdir -p "${extract_location}" | |
cd "${extract_location}" | |
if [ -f "${HOME:?}/Downloads/${linux_archive}" ]; then | |
echo "Copying ${linux_archive} from Downloads" | |
cp -a "${HOME:?}/Downloads/${linux_archive}" ./ | |
ls -hs "${linux_archive}" | |
echo "SHA256SUM: $(sha256sum "${linux_archive}")" | |
else | |
wget "${link}" || error "Could not download ${linux_archive}" | |
cp -a "${linux_archive}" "${HOME:?}/Downloads/" | |
fi | |
rm -rf "${linux_directory}" | |
tar xf "${linux_archive}" || error "Could not extract ${linux_archive}" | |
rm -f "${linux_archive}" | |
cd "${linux_directory}" || error "No \"${linux_directory}\" directory" | |
} | |
compile() { | |
set_linux_version | |
# touch .scmversion | |
patch -p1 < "${mtune_patch_path:?}" | |
cp -f "${kernel_config_path:?}" ./.config | |
make olddefconfig | |
nproc="$(( $(nproc) + 1 ))" | |
make -j ${nproc} | |
make -j ${nproc} modules | |
make -j ${nproc} bzImage | |
make INSTALL_MOD_STRIP=1 -j ${nproc} -C tools/ turbostat | |
make LDFLAGS=-fPIC INSTALL_MOD_STRIP=1 -j ${nproc} -C tools/ perf | |
} | |
install() { | |
sudo make INSTALL_MOD_STRIP=1 -j ${nproc} modules_install | |
sudo make INSTALL_MOD_STRIP=1 ARCH=x86 -j ${nproc} headers_install | |
sudo make INSTALL_MOD_STRIP=1 -j ${nproc} install | |
sudo make INSTALL_MOD_STRIP=1 -j ${nproc} -C tools/ turbostat_install | |
sudo make prefix=/usr LDFLAGS=-fPIC INSTALL_MOD_STRIP=1 -j ${nproc} -C tools/ perf_install | |
sudo dkms install -m nvidia -v ${nvidia_version} -k "${linux_version}" --force | |
# Backup "stable" kernel | |
sudo kernelstub \ | |
-k "/boot/vmlinuz-${latest_stable_kernel_version}" \ | |
-i "/boot/initrd.img-${latest_stable_kernel_version}" | |
sudo kernelstub \ | |
-k "/boot/vmlinuz-${linux_version}" \ | |
-i "/boot/initrd.img-${linux_version}" | |
} | |
main() { | |
test -z "$1" && error "Please pass the link to the linux kernel .xz" | |
latest_stable_kernel_version="$(ls /boot/vmlinuz-*-generic | sort -r | head -1 | sed 's|/boot/vmlinuz-||')" | |
nvidia_version="$(ls /usr/src | grep nvidia- | sort -r | head -1 | cut -d '-' -f2)" | |
test -z "${latest_stable_kernel_version}" && error "Could not obtain the latest stable kernel version for Backup" | |
test -z "${nvidia_version}" && error "Could not obtain the latest nvidia version from /usr/src" | |
# Prepare upfront for `make install` | |
sudo true | |
download_and_extract "$1" | |
compile | |
install | |
echo "Finished installing Linux ${linux_version}" | |
} | |
# If canceled, cleanup - if it finishes successfully, leave the source code | |
trap "rm -rf \"${extract_location}\"" SIGINT SIGTERM | |
( | |
main "$@" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment