Code optimization & comments
This commit is contained in:
parent
13a07c1c8d
commit
290b05f356
140
kernelcheck
140
kernelcheck
@ -2,25 +2,30 @@
|
||||
|
||||
# Copyright 2017 Michael De Roover
|
||||
|
||||
# Variables
|
||||
latest="$(wget -qO - 'https://kernel.org' | sed -n '/stable:/{n;p;}' | sed 's.[a-z]\|<\|>\|/\|[[:space:]]..g')"
|
||||
current="$(uname -r)"
|
||||
user="$(users)"
|
||||
cores=$(nproc)
|
||||
# Kernel version on kernel.org
|
||||
current="$(uname -r)" # Current kernel version
|
||||
user="$(users)" # User account
|
||||
cores="$(nproc)" # Processor cores
|
||||
|
||||
if [ -f /tmp/kernelcheck.pid ]
|
||||
if [ -f /tmp/kernelcheck/kernelcheck.pid ] # Create a pid file to avoid concurrent processes
|
||||
then
|
||||
exit 0
|
||||
elif [ ! -f /tmp/kernelcheck.pid ]
|
||||
elif [ ! -f /tmp/kernelcheck/kernelcheck.pid ]
|
||||
then
|
||||
touch /tmp/kernelcheck.pid
|
||||
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 body
|
||||
function verify(){
|
||||
function verify(){ # GPG signature verification
|
||||
local sign=$1 out=
|
||||
gkh="647F28654894E3BD457199BE38DBBDC86092693E" # Kroah-Hartman's key
|
||||
lt="ABAF11C65A2970B130ABE3C479BE3E4300411886" # Torvalds' key
|
||||
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
|
||||
@ -33,40 +38,60 @@ function verify(){
|
||||
fi
|
||||
}
|
||||
|
||||
if [[ "$latest" != "$current" ]] && [[ ! -z $latest ]]
|
||||
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 $download
|
||||
sudo -u $user wget -qP /tmp $(sed 's/.xz/.sign/' <<< $download)
|
||||
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
|
||||
sudo -u $user unxz /tmp/linux-$latest.tar.xz
|
||||
if verify /tmp/linux-$latest.tar.sign
|
||||
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.pid
|
||||
rm /tmp/kernelcheck/kernelcheck.pid
|
||||
exit 1
|
||||
fi
|
||||
tar xf /tmp/linux-$latest.tar
|
||||
cd /tmp/linux-$latest
|
||||
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" ]
|
||||
if [ -f "/root/.config/kernel/.config" ] # Copy config file into build directory if it exists
|
||||
then
|
||||
cp /root/.config/kernel/.config /tmp/linux-$latest/.config
|
||||
else
|
||||
/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.pid
|
||||
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
|
||||
/usr/local/sbin/notify_all "Kernel update tracker" "Starting kernel build..\nThis can take a while."
|
||||
make -j$cores &>/dev/null
|
||||
make modules_install &>/dev/null
|
||||
cp arch/$(uname -m)/boot/bzImage /boot/vmlinuz-$latest
|
||||
mkinitcpio -k $latest -g /boot/initramfs-$latest.img &>/dev/null
|
||||
function remove_distr_kernel(){
|
||||
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
|
||||
@ -80,28 +105,43 @@ then
|
||||
## 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
|
||||
/usr/local/sbin/notify_all "Kernel update tracker" "Removing the current kernel.." --icon=dialog-information
|
||||
rm -f /boot/initramfs-$current.img
|
||||
rm -f /boot/vmlinuz-$current
|
||||
rm -rf /usr/lib/modules/$current
|
||||
grub-mkconfig -o /boot/grub/grub.cfg &>/dev/null
|
||||
/usr/local/sbin/notify_all "Kernel update tracker" "New kernel installed\!" --icon=dialog-information
|
||||
elif [[ "$latest" == "$current" ]] && [[ ! -z $latest ]]
|
||||
if [ -f /boot/vmlinuz-$latest ] && [ -f /boot/initramfs-$latest.img ]
|
||||
then
|
||||
rm /tmp/kernelcheck.pid
|
||||
/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
|
||||
elif [[ -z $latest ]]
|
||||
then
|
||||
if ncat -zw1 kernel.org 443
|
||||
then
|
||||
/usr/local/sbin/notify_all "Kernel update tracker" "Website didn't return the required data." --icon=dialog-warning
|
||||
else
|
||||
/usr/local/sbin/notify_all "Kernel update tracker" "No network connection." --icon=dialog-information
|
||||
fi
|
||||
else
|
||||
/usr/local/sbin/notify_all "Kernel update tracker" "Internal error.. Debug time\!" --icon=dialog-warning
|
||||
rm /tmp/kernelcheck.pid
|
||||
/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
|
||||
|
||||
rm /tmp/kernelcheck.pid
|
||||
|
Reference in New Issue
Block a user