views:

58

answers:

2

Would it be possible and not incredibly difficult to build a linux kernel, with a python interpreter built in or accessible from the kernel, that could run a python file as it's init process?

+2  A: 

Can't you just replace /sbin/init or provide an init=... option to the boot loader? Just make sure you put python + libs on the root filesystem.

edit I didn't feel like thrashing a system, so it is untested, but looking at linux/init/main.c:

static void run_init_process(char *init_filename)
{
    argv_init[0] = init_filename;
    kernel_execve(init_filename, argv_init, envp_init);
}

I see no reason why a (python) script cannot replace the init process; execve is the same call that fires any normal process. And I think stdin and stdout are just connected to /dev/console, for init=/bin/sh also works. (but why on earth would you?!)

mvds
+1  A: 

I don't think init needs to be a C binary; it can be a script with a #! at the beginning; if that is the case, then you can have it be a python program with little effort.

Having said that, it is pretty trivial to write an inittab where init just runs a single program once (Although it's usually more useful to do other stuff too).

Given that you will probably want to do some things on your system which can't easily be done with python (for example, try mounting filesystems without a "mount" binary), you will probably need a busybox (for example) anyway; adding "init" to a busybox binary increases its size very little.

MarkR
I though the shebang was interpreted by the shell, no the kernel, which would mean this wouldn't work for an init replacement, since there isn't a shell running when the init= parameter's parsed.
Marc B
The shebang is interpreted by the kernel!
mvds
#! is interpreted by the kernel, see http://lxr.linux.no/#linux+v2.6.35/fs/binfmt_script.c
MarkR