(Ubuntu/Arch)Header 127 Bug, Script to patch and compile the kernel

Hello Level1

Been recently active on the forum helping some people with the header 127 bug and compiling the kernel to fix that bug. I have taken some time today to create a Python script that will download and compile the current kernel and apply the patch for you, currently this is for Ubuntu.

Only created a couple of scripts in my Linux journey, Enjoy my dudes :wink:

2 Likes

Created a script for Arch as well

Using your script as reference, I wrote a script to build a 5.2 kernel in debian 10 buster. I think it could be improved but I will leave it if you want to test it.

EDIT: retested script and removed unecessary packages, changed the way it writes the repo file and few tweaks. Script is runned with sudo ./NAME_OF_SCRIPT.py

#!/usr/bin/env python3

import os
import sys
import fileinput
import shutil
import time

##Variables
homedir = os.path.expanduser("~")
systemInfo = os.uname()
KernelVersion = '5.2'
KernelVersionPackage = '5.2.0-0.bpo.2-amd64'
PatchURL = 'https://clbin.com/VCiYJ'
SuffixKernelString = 'patched'

##Script Start
print ("This script will download and compile the latest linux")
input("Press ENTER to continue...")

##Enable backports
print("Enable backports build sources")
time.sleep(1)
#os.system("sudo /usr/bin/echo 'deb http://deb.debian.org/debian buster-backports main contrib non-free' > /etc/apt/sources.list.d/backports.list")
backportsfile = open("/etc/apt/sources.list.d/backports.list", "w+")
backportsfile.write("deb http://deb.debian.org/debian buster-backports main contrib non-free") 
backportsfile.close()

os.system("sudo apt update")
time.sleep(1)

##Required packages
print("Downloading required packages")
os.system("apt-get -t buster-backports install build-essential libncurses-dev bison flex libssl-dev libelf-dev linux-image-"+KernelVersionPackage+" linux-headers-"+KernelVersionPackage+" linux-source-"+KernelVersion)

##Delete existing compiled kernel, and source folder, in case the script is being rerunned
os.system("rm -rif /usr/src/linux-*"+KernelVersion+"*.deb")
os.system("rm -rif /usr/src/linux-source-"+KernelVersion+"/")

##Extracting Kernel
print("Extracting kernel")
os.chdir("/usr/src")
os.system("tar -xvf linux-source-"+KernelVersion+".tar.xz")

##Applying Patch
os.system('clear')
os.chdir("/usr/src/linux-source-"+KernelVersion)
os.system("wget "+PatchURL+" -O /usr/src/linux-source-"+KernelVersion+"/pci.patch")
print("Applying kernel patch\n")
time.sleep(1)
os.system("patch -p1 < pci.patch")
time.sleep(1)

##Copy current config
os.system("cp /boot/config-"+KernelVersionPackage+" /usr/src/linux-source-"+KernelVersion+"/.config")

##Replace required line in debian config
for line in fileinput.input("/usr/src/linux-source-"+KernelVersion+"/.config", inplace=True):
    if line.strip().startswith('CONFIG_SYSTEM_TRUSTED_KEYS='):
        line = 'CONFIG_SYSTEM_TRUSTED_KEYS=""\n'
    sys.stdout.write(line)

##Compile kernel
time.sleep(1)
print("Are you ready to compile the kernel\n")
input("Press ENTER to continue...")
os.system("make -j`nproc` bindeb-pkg LOCALVERSION=-"+SuffixKernelString)

##Install compile kernel
answer = None
while answer not in ("yes", "no"):
	answer = input("Do you wish to install the kernel now?, Enter yes or no: ")
	if answer == "yes":
		os.chdir("/usr/src")
		os.system("dpkg -i linux-headers-*-"+SuffixKernelString+"_*_amd64.deb")
		os.system("dpkg -i linux-image-*-"+SuffixKernelString+"_*_amd64.deb")
		print("\nPlease reboot to apply changes and load the newly complied kernel")
	elif answer == "no":
		exit()
	else:
		print("Please enter yes or no.")
1 Like

Thanks man, i will give it a test

Tried the script, getting errors

  File "./Debcompile.py", line 45, in <module>
    os.chdir("/usr/src/linux-source-"+KernelVersion)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/src/linux-source-5.2'

Strange I tested in a recently in a clena install on a vm and worked.

The command:

apt-get install linux-source-5.2

install the necessary file /usr/src/linux-source.tar.xz (on the script is executed on line 32) and the script should extract it on line 41. did the script installed that file ?

by the way how do you run the script ? I run with

python3 NAME_OF_SCRIPT

can you test that way ?
and not

python NAME_OF_SCRIPT

checked for the kernel in usr/src and there was no file, and for running it i did it as ./DebCompile.py and i set it to use python3

retested, made a few changes and edited the post. try it again with sudo ./NAME_OF_SCRIPT.py