Linux Kernel – How to Reduce the Size of the initrd When Compiling Your Kernel?

compilinglinux-kernel

When I compile my own kernel, basically what I do is the following:

  1. I download the sources from www.kernel.org and uncompress it.

  2. I copy my previous .config to the sources and do a make menuconfig to watch for the new options and modify the configuration according to the new policy of the kernel.

  3. Then, I compile it: make -j 4

  4. Finally, I install it: su -c 'make modules_install && make install'.

  5. After a few tests, I remove the old kernel (from /boot and /lib/modules) and run fully with the new one (this last step saved my life several times! It's a pro-tip!).

The problem is that I always get a /boot/initrd.img-4.x.x which is huge compared to the ones from my distribution. Here the content of my current /boot/ directory as an example:

# ls -alFh
total 243M
drwxr-xr-x  5 root root 4.0K Mar 16 21:26 ./
drwxr-xr-x 25 root root 4.0K Feb 25 09:28 ../
-rw-r--r--  1 root root 2.9M Mar  9 07:39 System.map-4.4.0-1-amd64
-rw-r--r--  1 root root 3.1M Mar 11 22:30 System.map-4.4.5
-rw-r--r--  1 root root 3.2M Mar 16 21:26 System.map-4.5.0
-rw-r--r--  1 root root 170K Mar  9 07:39 config-4.4.0-1-amd64
-rw-r--r--  1 root root 124K Mar 11 22:30 config-4.4.5
-rw-r--r--  1 root root 126K Mar 16 21:26 config-4.5.0
drwxr-xr-x  5 root root  512 Jan  1  1970 efi/
drwxr-xr-x  5 root root 4.0K Mar 16 21:27 grub/
-rw-r--r--  1 root root  19M Mar 10 22:01 initrd.img-4.4.0-1-amd64
-rw-r--r--  1 root root 101M Mar 12 13:59 initrd.img-4.4.5
-rw-r--r--  1 root root 103M Mar 16 21:26 initrd.img-4.5.0
drwx------  2 root root  16K Apr  8  2014 lost+found/
-rw-r--r--  1 root root 3.5M Mar  9 07:30 vmlinuz-4.4.0-1-amd64
-rw-r--r--  1 root root 4.1M Mar 11 22:30 vmlinuz-4.4.5
-rw-r--r--  1 root root 4.1M Mar 16 21:26 vmlinuz-4.5.0

As you may have noticed, the size of my initrd.img files are about 10 times bigger than the ones from my distribution.

So, do I do something wrong when compiling my kernel? And, how can I reduce the size of my initrd.img?

Best Answer

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

Related Question