3

I need to register a specific kernel module (https://github.com/504ensicsLabs/LiME) in Ubuntu 18.04

sudo insmod /path/to/lime.ko path=~/dump.raw format=raw

from a python script. I tried using os.system() ; subprocess.run()/call()/Popen() - shell=True/False . I explicitely imported the .bashrc environment and it all failed with unknown symbol in module error while it works from the command line. The python script runs with root privileges. What can be the point I am missing ? What can be the difference between subprocess and the direct shell ? Is it linked to the special nature of the module I try to install ? The error is

insmod: error inserting 'path/to/lime.ko': Unknown symbol in module
2
  • 1
    Why are you importing .bashrc? That's probably the issue since many default .bashrc files are set up to exit if they are being read by non-interactive shells. More importantly, please edit your question and show us the exact error you get and the exact code you use to get the error.
    – terdon
    Jan 21 at 12:12
  • Effectively this was useless, just an attempt I made. Thank you
    – Vinzce81
    Jan 22 at 9:13
2

I don't know if it's the cause of your problem, but note that the ~ in cmd path=~/dump.raw is only expanded by the bash and mksh shells (in the case of bash only when what's left of the = looks like a valid variable name) when their respective posix option is not enabled (and for bash, that posix option is enabled when invoked as sh) and by the zsh shell when its magicequalsubst option is enabled.

Not in other shells. For sh, POSIX requires echo path=~ to output path=~.

So unless your sh is a build of mksh without the posix mode enabled by default, if run from within python using a shell (which will be sh), that ~ won't be expanded to your home directory.

Here, use:

sudo insmod /path/to/lime.ko path="$HOME/dump.raw" format=raw

instead. Or get python to retrieve the value of the HOME environment variable to construct that path=.... argument if not invoking a shell.

For further reading on the subject of ~ expansion, see Does ~ always equal $HOME

5
  • For what it's worth, on my Arch (/bin/sh -> bash), running os.system("ls ~") in a python3 shell correctly runs ls $HOME. Maybe python doesn't default to /bin/sh? I tested, and it also works if I first export SHELL=/bin/sh and then run python3. Could python be reading my shell from /etr/password?
    – terdon
    Jan 21 at 14:24
  • @terdon, ls ~ is not the same as echo path=~. See the linked Q&A for details. Jan 21 at 14:59
  • Oooh, I just straced ls ~ and /bin/sh ls ~ and saw the difference. sh will pass ~ to ls and it is ls itself that expands it. Thanks!
    – terdon
    Jan 21 at 15:07
  • @terdon. No ls doesn't expand tildes. /bin/sh ls ~ would try to interpret ls as a shell script. /bin/sh -c ls ~ would run ls without argument and list the current directory. But again here, the issue is about ~ expansion when that ~ occurs in the middle of shell word, not as the first character. Jan 21 at 15:14
  • Thank you guys for the quick and correct answer. Replacing the '~/file.raw' with '"$HOME/file.raw"' well solves this problem.
    – Vinzce81
    Jan 22 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.