Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
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. – frostschutz Mar 17 at 10:10
up vote 15 down vote accepted

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

share|improve this answer
    
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! – perror Mar 17 at 10:48
    
The should be a space between {} and +. – phk Mar 17 at 13:57

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.

share|improve this answer

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|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.