I have a Python program that's designed to run only in certain distros of Linux (i.e. CentOS, Ubuntu, etc..). I'd like to get it running inside a CentOS7 container, but its failing because the following is returning '4.9.49-moby':
import platform
platform.release()
The program is expecting to find a linux kernel release, i.e '3.10.0-327.el7.x86_64'.
Suppose I'm unable to modify the source code of the program.
What are things that I can do that will workaround this issue?
I tried writing a wrapper script around 'uname -r' to return what I want. But this isn't helping since apparently Python is sourcing this directly from the kernel.
Python simply calls the
uname
system call to get that information, which is always going to return information about the currently running kernel. Overriding the return value without modifying the source is going to be tricky.You can accomplish this using function interposition, e.g. as described here. That requires either modifying the image to include both the wrapper library and the necessary environment setup, or it requires you to pass a number of additional parameters on the Docker run command line.
Here's a simple example. I start with a vanilla image and call
os.uname()
in Python:I would like the
release
field to show1.0.0
instead. I start by creating a wrapper for theuname
system call:And I compile the shared library:
Now I can inject that into the docker image and preload the shared library. The key additions are are the
-v
to inject the library and-e LD_PRELOAD
to cause the linker to preload it:And as you can see, that gives us the desired result: