Kernel options setter

Check for and set options in the Linux kernel
Python Linux Kernel JCZD

From time to time, one needs to check if a number of options are enabled in the kernel. When it’s a matter of just a few options this is not really a problem and it gives us a chance to read the documentation ; however, when the list of options grows, this can be a time-consumig, tedious and error-prone process. This script was written just for this.

#!/usr/bin/python
# vim: noet ts=4 number nowrap

from sys import argv

f"""
shitty script to enable a bunch of kernel options in kernel config.

Author: JCZD
Date:	2021-12-06

TODO: close the files explicitly

cd /usr/src/linux
gunzip /proc/config.gz
{argv[0]} config <opts>	# <opts> is a file containing the options to be searched for
cp config.new /usr/src/linux/.config
make menuconfig							# save and quit
cp /usr/src/linux/.config config.1
{argv[0]} config.1 <opts> --no-modules	# options for which ='m' was refused by menuconfig will be written ='y'
cp config.1.new /usr/src/linux/.config
make menuconfig							# save and quit
cp /usr/src/linux/.config config.2
{argv[0]} config.2 <opts> --no-modules	# lines in red should be checked manually with make menuconfig
cp config.2.new /usr/src/linux/.config
make menuconfig							# edit, save and quit
"""

from termcolor import cprint

newconf = []

conf = open(argv[1],'r').read().split('\n')
opts = open(argv[2],'r').read().split('\n')

class GotIt(Exception):
	pass

for line in conf:
	try:
		for opt in opts:
			if opt and opt in line:
				raise GotIt
	except GotIt:
		if line[:len(opt)] == opt:
			cprint(line,'green')
			newconf.append(line)
		elif '--no-modules' in argv:
			cprint(opt+"=y",'red')
			newconf.append(opt+"=y")
		else:
			cprint(opt+"=m",'red')
			newconf.append(opt+"=m")
	else:
		newconf.append(line)

try:
    with open(argv[1]+".new",'x') as New:
	    New.write('\n'.join(newconf))
except FileExistsError:
    cprint(argv[1]+".new already exists, skipping write.",'yellow')