Last active
November 16, 2023 15:05
-
-
Save idkrn123/85f39d4d88377ecbb1ae15096e3da7a0 to your computer and use it in GitHub Desktop.
gpt-4 turbo's attempt at a distro-agnostic build script. tested only on debian and alpine. takes tarball path as argument
This file contains 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 | |
# Exit on errors and unset variables | |
set -euo pipefail | |
# Function to detect package manager and install packages | |
install_packages() { | |
local debian_packages=(build-essential libncurses-dev bison flex libssl-dev libelf-dev dwarves) | |
local redhat_packages=(make gcc ncurses-devel bison flex openssl-devel elfutils-libelf-devel pahole) | |
local suse_packages=(make gcc ncurses-devel bison flex libopenssl-devel libelf-devel dwarves) | |
local arch_packages=(base-devel ncurses bison flex openssl elfutils pahole) | |
local alpine_packages=(build-base ncurses-dev bison flex openssl-dev elfutils-dev dwarves) | |
if command -v apt-get &>/dev/null; then | |
sudo apt-get update | |
sudo apt-get install -y "${debian_packages[@]}" | |
elif command -v dnf &>/dev/null; then | |
sudo dnf install -y "${redhat_packages[@]}" | |
elif command -v yum &>/dev/null; then | |
sudo yum install -y "${redhat_packages[@]}" | |
elif command -v zypper &>/dev/null; then | |
sudo zypper install -y "${suse_packages[@]}" | |
elif command -v pacman &>/dev/null; then | |
sudo pacman -Sy --needed "${arch_packages[@]}" | |
elif command -v apk &>/dev/null; then | |
sudo apk add --update "${alpine_packages[@]}" | |
else | |
echo "Unsupported package manager. Install the required packages manually." | |
exit 1 | |
fi | |
} | |
# Check if a tarball path was provided as an argument | |
if [ $# -eq 0 ]; then | |
echo "Usage: $0 <path/to/linux-x.y.z.tar.xz>" | |
exit 1 | |
fi | |
TARBALL="$1" | |
# Check if the kernel tarball exists | |
if [ ! -f "${TARBALL}" ]; then | |
echo "The kernel tarball ${TARBALL} does not exist. Please provide the correct path." | |
exit 1 | |
fi | |
# Install required packages for building the kernel | |
install_packages | |
# Extract the kernel version from the tarball name | |
KERNEL_VERSION=$(basename "${TARBALL}" .tar.xz | sed 's/linux-//') | |
# Unpack the kernel source and move into the directory | |
tar -xvf "${TARBALL}" | |
cd "linux-${KERNEL_VERSION}" | |
# Copy current config if available | |
if [ -f "/boot/config-$(uname -r)" ]; then | |
cp -v "/boot/config-$(uname -r)" .config | |
else | |
echo "No config found in /boot. Default config will be used." | |
fi | |
# Use old config and set new version | |
yes '' | make oldconfig | |
# Compile the kernel with default options | |
make -j"$(nproc)" | |
# Strip symbols from modules to save space | |
make INSTALL_MOD_STRIP=1 modules_install | |
# Install the kernel | |
sudo make install | |
# Strip the kernel image if it exists | |
if [ -f "/boot/vmlinuz-${KERNEL_VERSION}" ]; then | |
sudo strip --strip-debug "/boot/vmlinuz-${KERNEL_VERSION}" | |
fi | |
# Update bootloader | |
if command -v update-grub &>/dev/null; then | |
sudo update-grub | |
elif command -v grub2-mkconfig &>/dev/null; then | |
sudo grub2-mkconfig -o /boot/grub2/grub.cfg | |
elif command -v grub-mkconfig &>/dev/null; then | |
sudo grub-mkconfig -o /boot/grub/grub.cfg | |
else | |
echo "Bootloader update command not found. Update bootloader manually." | |
fi | |
# Prompt for reboot | |
read -p "Kernel installation is complete. Would you like to reboot now? [y/N] " response | |
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then | |
sudo reboot | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment