A Guide to Using the kexec Command (with examples)
- Linux
- November 5, 2023
Load a new kernel
To load a new kernel using the kexec
command, you can use the following code:
kexec -l path/to/kernel --initrd=path/to/initrd --command-line=arguments
- Motivation: This use case is useful when you want to switch to a new kernel without rebooting the system. It can be handy during kernel development or testing.
- Explanation:
path/to/kernel
: Specify the path to the new kernel image that you want to load.--initrd=path/to/initrd
: Specify the path to the initial ramdisk (initrd) image that you want to provide to the new kernel.--command-line=arguments
: Specify additional command-line arguments for the new kernel (optional).
- Example Output: None. This command prepares the system to boot into the new kernel.
Load a new kernel with current boot parameters
To load a new kernel using the current boot parameters, you can use the following code:
kexec -l path/to/kernel --initrd=path/to/initrd --reuse-cmdline
- Motivation: This use case is helpful when you want to load a new kernel using the same command-line arguments as the currently running kernel. It saves you the effort of manually specifying arguments.
- Explanation:
path/to/kernel
: Specify the path to the new kernel image that you want to load.--initrd=path/to/initrd
: Specify the path to the initial ramdisk (initrd) image that you want to provide to the new kernel.--reuse-cmdline
: Reuse the command-line arguments from the currently running kernel.
- Example Output: None. This command prepares the system to boot into the new kernel using the same command-line arguments.
Execute a currently loaded kernel
To execute the currently loaded kernel, you can use the following code:
kexec -e
- Motivation: This use case is useful when you have loaded a new kernel using the
kexec
command and want to immediately execute it without rebooting the system. - Explanation: None.
- Example Output: None. The currently loaded kernel starts executing.
Unload current kexec target kernel
To unload the currently loaded kexec target kernel, you can use the following code:
kexec -u
- Motivation: This use case is beneficial when you want to unload the previously loaded kernel without rebooting the system, freeing up system resources.
- Explanation: None.
- Example Output: None. The currently loaded kexec target kernel is unloaded.
Note: The kexec command has several other use cases which are not covered in the examples above. Please refer to the official documentation for more information.