How to reduce the size of the initrd when compiling your kernel?
This is because all the kernel modules are not stripped. You need to strip it to down its size.
Use this command:
SHW@SHW:/tmp# cd /lib/modules/<new_kernel>
SHW@SHW:/tmp# find . -name *.ko -exec strip --strip-unneeded {} +
This will drastically reduce the size. After executing above command, you can proceed to create initramfs/initrd
I did some extra research on the problem to know what is the best way to get the modules stripped and here is the full process I found (still SHW did bring the answer but the process I found is somehow more standardized):
Download the sources from
www.kernel.org
and uncompress it.Copy your previous
.config
to the sources and do amake menuconfig
to watch for the new options and modify the configuration according to the new policy of the kernel.Then, compile it:
$> make -j 4
Finally, install it:
$> su -c 'make INSTALL_MOD_STRIP=1 modules_install && make install'
After a few tests, remove the old kernel from
/boot
and/lib/modules
directories.
The INSTALL_MOD_STRIP
when set to 1
add a strip --strip-debug
when installing the module, which is enough to reduce the size drastically.
See: INSTALL_MOD_STRIP in Documentation/kbuild/kbuild.txt
.
You could also change the configuration of your initramfs.conf
Find the file at /etc/initramfs-tools/initramfs.conf
There is a setting that says MODULES=most
this includes most of the modules kn your initrd image.
Change it to MODULES=dep
this makes the initramfs generator guess which modules to include.
Check out the manpage for initramfs.conf here.
NOTE 1: After performing the above steps the size of my initramfs image reduced from 282 MB to 99 MB.( this is still large enough but its a significant improvement)
NOTE 2: I also tried stripping the kernel modules at /lib/modules/<kernel version>
. The modules supplied by the OS updates are stripped (size = 211 MB) and thus the corresponding intiramfs image is around 15 MB. After stripping the modules of the vanilla kernel that I compiled myself the size of the folder was 185 MB and the intramfs image was 16 MB. So after all optimizations the size came down from 282 MB to 16 MB!! For stripping use this code
find /lib/modules/<kernel_release>/ -iname "*.ko" -exec strip --strip-unneeded {} \;
In the above code replace <kernel_release>
with the kernel version that you wish to strip the modules from.
For more discussion view this link.
The above code must be run as sudo
or su