Hook /sbin/init and use kexec to boot custom kernel

Email this to someoneTweet about this on TwitterShare on FacebookShare on Google+Print this page

Disclaimer – useful only for machines that boot external kernel that can not be accessed or modified and /sbin/init as the FIRST executed process if it fails may run into kernel panic where machines will NEVER boot again so it is of NO guarantee and do NOT count on this thus try at your own risk.

Kexec is a system call that enables you to load and boot into another kernel from the currently running kernel.

Here is an example that successfully switched external kernel of a KVM VPS from DigitalOcean running CentOS 6 x64 to an OpenVZ kernel. Thanks for Sean Buckley and his youtube video for Arch and Debian. Here goes for CentOS.

First of all, install OpenVZ kernel and its utility.

Second, backup /sbin/init and init.bak will be executed by a newly replaced one.

The point is to inject into the boot process as follows and load custom kernel on the way.

-----------    ------------------------   ------------------------------
|          |   |                      |   |                            |
|   boot   |-->|   /sbin/init (new)   |-->|   /sbin/init.bak (orig)    |
|          |   |                      |   |                            |
-----------    ------------------------   ------------------------------

 
The new init file is actually a shell script, following is used here.

You may need to create an init file elsewhere and use -f to force overwrite /sbin/init and chmod +x /sbin/init. Just remember kexeced is an additional indicator to make sure to load only once.

Finally, install kexec-tools and reboot.

The result shows success.

Notice : The actual version of OpenVZ kernel may subject to change as there are many updates since then.

 

Nov 10, 2013 @ 00:00
Email this to someoneTweet about this on TwitterShare on FacebookShare on Google+Print this page

Leave a Reply

Your email address will not be published. Required fields are marked *

Please calculate * Time limit is exhausted. Please reload CAPTCHA.