1

I want to add and compile my custom application code into my kernel.

How should I add my .c and .o files into my kernel 'bin' directory and compile them?

I made a hello.c and hello.o file and I want to add them into my kernel such way that when kernel start this hello.o file runs. Do I need to edit some makefile?

1
  • You didnt mentioned what Linux you are using but as I have seen your question at Ask Ubuntu i have assumed that you wanted this for Ubuntu. If you want to recompile kernel with your customization Then this link can help you help.ubuntu.com/community/Kernel/Compile
    – rɑːdʒɑ
    Jul 16, 2014 at 10:16

1 Answer 1

2

I want to add these file into my kernel such way that when kernel start this hello.o file execute and run

What you are trying to achieve shouldn't be made through kernel edition. Executing a program at boot time can be handled in much simpler ways, without need for kernel programming experience. You can:

Execute it when your shell starts: Write /path/to/hello/executable at the end of /etc/profile, ~/.profile or ~/.bashrc. Those files are sourced every time you start a shell (more specifically, bash). This also means that your executable will run every time you open a terminal.

Use the init.d system: Compile your code and add an init script to the init system. You'll find all the information you may need on this page, or here for Ubuntu specifics.

Here a quick example of an init script for your hello executable:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          hello
# Default-Start:     1 2 3 4 5
# Short-Description: Prints Hello World somewhere.
# Description: Some more description...
### END INIT INFO

case "$1" in
    start)
        /path/to/hello/executable
        ;;
    stop|restart|force-reload)
        ;;
    *)
        ;;
esac

exit 0

Now you can execute hello using :

service hello start

Assuming that the init script is named hello, and is executable. To make it run at boot time, use:

update-rc.d hello defaults

The kernel relies more on libraries than executables actually. You may also want to have a look at The Kernel Module Development Guide, which can introduce you to the basics of kernel modules. Again, this is a horrible overkill for what you're trying to do.

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.