Skip to content

Instantly share code, notes, and snippets.

@d33tah
Last active August 16, 2022 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d33tah/a77e6763814f91c722498f54e511e5f2 to your computer and use it in GitHub Desktop.
Save d33tah/a77e6763814f91c722498f54e511e5f2 to your computer and use it in GitHub Desktop.
Generates a minimal Linux kernel source tree that actually builds. The code has been continued here: https://github.com/d33tah/minimal-kernel-build/
# sudo docker buildx build . --output minified
FROM python:3.10 as source
WORKDIR /
RUN grep -v '^#' /etc/apt/sources.list | sed 's/deb /deb-src /' >> /etc/apt/sources.list
RUN apt-get update && apt-get install strace bc bison flex -y && apt-get build-dep -y linux-image-amd64
RUN wget -nv https://git.kernel.org/torvalds/t/linux-5.19-rc8.tar.gz
RUN tar xf linux-5.19-rc8.tar.gz
ADD linux-genlist.py .
RUN cd linux-5.19-rc8 && python3 ../linux-genlist.py
FROM scratch
COPY --from=source /linux-5.19-rc8 .
import subprocess
import os
import pathlib
subprocess.check_call(
'strace -fff -e trace=file -y -o strace.allnoconfig make allnoconfig', shell=True)
subprocess.check_call(
'strace -fff -e trace=file -y -o strace.make make', shell=True)
subprocess.check_call('make mrproper', shell=True)
cwd = os.getcwd()
tokens = ['stat', 'lstat', 'execve']
b = set()
pdir = pathlib.Path('.')
for p in pdir.glob('strace*'):
with open(str(p)) as f:
for line in f:
if line.endswith('>\n'):
rev = line[::-1]
x = rev.split('<')[0][::-1][:-2]
b.add(x)
else:
for token in tokens:
if line.startswith(token + '("'):
x = pathlib.Path(line.split('"')[1])
b.add(str(x.absolute()))
a = {x.strip() for x in subprocess.check_output(
'find `pwd` -type f', shell=True, encoding='utf8').splitlines()}
for x in a.difference(b):
if x:
os.unlink(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment