How to reduce the size of the initrd when compiling your kernel?

17,181

Solution 1

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

man strip

   --strip-unneeded
       Remove all symbols that are not needed for relocation processing.

Solution 2

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):

  1. Download the sources from www.kernel.org and uncompress it.

  2. Copy your 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, compile it:

    $> make -j 4
    
  4. Finally, install it:

    $> su -c 'make INSTALL_MOD_STRIP=1 modules_install && make install'
    
  5. 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.

Solution 3

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

Share:
17,181

Related videos on Youtube

Ueli Hofstetter
Author by

Ueli Hofstetter

Updated on September 18, 2022

Comments

  • Ueli Hofstetter
    Ueli Hofstetter almost 2 years

    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?

    • frostschutz
      frostschutz over 8 years
      wiki.gentoo.org/wiki/… and see what's making it so huge. It could be either tons of kernel modules or libraries... or something else entirely. If you don't look at the contents of the archive it's just guesswork.
  • Ueli Hofstetter
    Ueli Hofstetter over 8 years
    Wow, I would never have though that stripping the modules would divide the size of the initrd image by 10... Thanks a lot, it worked fine!
  • some bits flipped
    some bits flipped over 2 years
    dell precision, 5.11.0-43: most+lz4=150mb and hang at loading initramfs; switching to dep lowered ramdisk size to 95mb (still too large to load); switching to dep+xy compression lowers size to a workable 63mb.
  • Shawn Eary
    Shawn Eary over 2 years
    Is there a way to do the stripping at compile time before automatic kernel signing?