This repository has been archived on 2020-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
kernelcheck/kernelcheck

150 lines
6.9 KiB
Bash
Executable File

#!/bin/sh
# Copyright 2017 Michael De Roover
latest="$(wget -qO - 'https://kernel.org' | sed -n '/stable:/{n;p;}' | sed 's.[a-z]\|<\|>\|/\|[[:space:]]..g')"
# Kernel version on kernel.org
current="$(uname -r)" # Current kernel version
uid=1000 # User ID. Change as per your needs.
user="$(awk -v val=$uid -F ":" '$3==val{print $1}' /etc/passwd)" # User account (used by sudo -u)
cores="$(nproc)" # Processor cores
if [ -f /tmp/kernelcheck/kernelcheck.pid ] # Create a pid file to avoid concurrent processes
then
exit 0
elif [ ! -f /tmp/kernelcheck/kernelcheck.pid ]
then
sudo -u $user mkdir /tmp/kernelcheck
touch /tmp/kernelcheck/kernelcheck.pid
if [ ! -f /tmp/kernelcheck/kernelcheck.pid ] # Failsafe in case we can't create pid file
then
/usr/local/sbin/notify_all "Kernel update tracker" "Couldn't create a pid file. Quitting\!" --icon=dialog-warning
exit 1
fi
fi
function verify(){ # GPG signature verification
local sign=$1 out=
gkh="647F28654894E3BD457199BE38DBBDC86092693E" # Greg Kroah-Hartman's key
lt="ABAF11C65A2970B130ABE3C479BE3E4300411886" # Linus Torvalds' key
if out=$(sudo -u $user gpg --status-fd 1 --verify "$sign" 2>/dev/null) && echo "$out" | grep -qs "^\[GNUPG:\] VALIDSIG $gkh"
then
return 0
elif out=$(sudo -u $user gpg --status-fd 1 --verify "$sign" 2>/dev/null) && echo "$out" | grep -qs "^\[GNUPG:\] VALIDSIG $lt"
then
return 0
else
echo "$out" >&2
return 1
fi
}
if [ "$latest" != "$current" ] && [ ! -z "$latest" ] # Executed if kernel versions don't match
# Can't distinguish between branches, stable assumed!
then
/usr/local/sbin/notify_all "Kernel update tracker" "There's a new kernel available\!\nGetting the kernel for you.." --icon=dialog-information
download="$(wget -qO - 'https://kernel.org' | sed -n '/latest_link/{n;p;}' | cut -d '"' -f2)"
sudo -u $user wget -qP /tmp/kernelcheck $download
sudo -u $user wget -qP /tmp/kernelcheck $(sed 's/.xz/.sign/' <<< $download)
/usr/local/sbin/notify_all "Kernel update tracker" "Kernel downloaded\!\nNow extracting and verifying." --icon=dialog-information
unxz /tmp/kernelcheck/linux-$latest.tar.xz
if verify /tmp/kernelcheck/linux-$latest.tar.sign # Call GPG verification function
then
/usr/local/sbin/notify_all "Kernel update tracker" "Verification success\!\nStarting compilation.." --icon=dialog-information
else
/usr/local/sbin/notify_all "Kernel update tracker" "Couldn't verify the kernel. Quitting\!" --icon=dialog-warning
rm /tmp/kernelcheck/kernelcheck.pid
exit 1
fi
cd /tmp/kernelcheck
tar xf /tmp/kernelcheck/linux-$latest.tar
while [ ! -d /tmp/kernelcheck/linux-$latest ] # Executed if tar xf command failed to write directory
do
((inc++))
sleep 2
tar xf /tmp/kernelcheck/linux-$latest.tar
if [ $inc == 5 ] # Exit script after 5 loop iterations
then
/usr/local/sbin/notify_all "Kernel update tracker" "Couldn't extract the archive after 5 attempts. Quitting\!\!" --icon=dialog-warning
rm /tmp/kernelcheck/kernelcheck.pid
exit 1
fi
done
cd /tmp/kernelcheck/linux-$latest
make clean &>/dev/null
make mrproper &>/dev/null
if [ -f "/root/.config/kernel/.config" ] # Copy config file into build directory if it exists
then
cp /root/.config/kernel/.config /tmp/kernelcheck/linux-$latest/.config
if [ ! -f "/tmp/kernelcheck/linux-$latest/.config" ] # Exit if config hasn't been copied over
then
/usr/local/sbin/notify_all "Kernel update tracker" "Couldn't copy config file into build directory\!" --icon=dialog-warning
rm /tmp/kernelcheck/kernelcheck.pid
exit 1
fi
elif [ ! -f "/root/.config/kernel/.config" ] # Exit if config file isn't found.
then
/usr/local/sbin/notify_all "Kernel update tracker" "No config file found\!\nAdd it to /root/.config/kernel/.config" --icon=dialog-warning
rm /tmp/kernelcheck/kernelcheck.pid
exit 1
fi
/usr/local/sbin/notify_all "Kernel update tracker" "Starting kernel build..\nThis can take a while." --icon=dialog-information
make -j$cores &>/dev/null # Compile kernel
make modules_install &>/dev/null # Install modules
cp arch/$(uname -m)/boot/bzImage /boot/vmlinuz-$latest # Copy kernel image
mkinitcpio -k $latest -g /boot/initramfs-$latest.img &>/dev/null# Make initramfs
function remove_distr_kernel(){ # Function to remove distribution kernel. For advanced users only!
if [ "$(echo $(uname -r) | sed 's/ARCH//')" != "$(uname -r)" ]
then
/usr/local/sbin/notify_all "Kernel update tracker" "Removing the Arch distribution kernel.." --icon=dialog-information
pacman -R --noconfirm linux
elif [ "$(echo $(uname -r) | sed 's/MANJARO//')" != "$(uname -r)" ]
then
/usr/local/sbin/notify_all "Kernel update tracker" "Removing the Manjaro distribution kernel.." --icon=dialog-information
pacman -R --noconfirm linux
fi
}
## Uncomment this function only if you really know what you are doing!!
## It removes the Arch / Manjaro distribution kernel, and should only be done if you know how to recover from chroot!
# remove_distr_kernel
if [ -f /boot/vmlinuz-$latest ] && [ -f /boot/initramfs-$latest.img ]
then
/usr/local/sbin/notify_all "Kernel update tracker" "New kernel successfully installed.\nRemoving the old one.." --icon=dialog-information
rm -f /boot/vmlinuz-$current # Remove old kernel
rm -f /boot/initramfs-$current.img # Remove old initramfs
rm -rf /usr/lib/modules/$current # Remove old kernel modules
grub-mkconfig -o /boot/grub/grub.cfg &>/dev/null # Update grub config file
/usr/local/sbin/notify_all "Kernel update tracker" "Finished\!\nPlease reboot now to apply the changes." --icon=dialog-information
exit 0
else
/usr/local/sbin/notify_all "Kernel installation failed\!" --icon=dialog-warning
rm /tmp/kernelcheck/kernelcheck.pid
exit 1
fi
elif [ "$latest" == "$current" ] && [ ! -z $latest ] # Executed if running kernel version matches that on kernel.org
then
rm /tmp/kernelcheck/kernelcheck.pid
exit 0
elif [ -z $latest ] # Executed if $latest variable is empty
then
if ncat -zw1 kernel.org 443 # Check if port 443 on kernel.org is reachable
then
/usr/local/sbin/notify_all "Kernel update tracker" "Website didn't return the required data." --icon=dialog-warning
rm /tmp/kernelcheck/kernelcheck.pid
exit 1
elif ncat -zw1 google.com 443 # Check if port 443 on google.com is reachable, in case kernel.org is down.
then
/usr/local/sbin/notify_all "Kernel update tracker" "There seems to be a problem with kernel.org.\nPlease try again later." --icon=dialog-warning
rm /tmp/kernelcheck/kernelcheck.pid
exit 1
else # Execute if both kernel.org and google.com didn't respond.
/usr/local/sbin/notify_all "Kernel update tracker" "No network connection." --icon-dialog-warning
rm /tmp/kernelcheck/kernelcheck.pid
exit 1
fi
else # This part should never execute!!
/usr/local/sbin/notify_all "Kernel update tracker" "An unexpected error happened.\nPlease notify me about it on GitHub." --icon=dialog-warning
rm /tmp/kernelcheck/kernelcheck.pid
exit 1
fi