Linux – Manual Kernel Build Results in Large Binary

compilinglinuxlinux-kernel

I'm using Linux Mint 13 MATE 32bit, I'm trying to build the kernel (primarily for experience and for fun).

For now, I like to build it with the same configuration as precompiled kernel, so firstly I've installed precompiled kernel 3.16.0-031600rc6 from kernel.ubuntu.com, booted to it successfully.

Then I've downloaded 3.16.rc6 kernel from kernel.org, unpacked it, configured it to use config from existing precompiled kernel:

$ make oldconfig

It didn't ask me anything, so, precompiled kernel contains all necessary information. Then I've built it (it took about 6 hours) :

$ make

And then installed:

$ sudo make modules_install install

Then I've booted into my manually-compiled kernel, and it works, boot process is somewhat slower though. But then I've found out that all the binaries (/boot/initrd.img-3.16.0-rc6 and all the *.ko modules in /lib/modules/3.16.0-rc6/kernel are about 10 times larger than precompiled versions! Say, initrd.img-3.16.0-rc6 is 160 658 665 bytes, but precompiled initrd.img-3.16.0-031600rc6-generic is 16 819 611 bytes. Each *.ko module is similarly larger.

Why is this? I haven't specified any special options for build (I typed exactly the same commands as I mentioned above). How to build it "correctly"?

Best Answer

Despite what file says, it turns out to be debugging symbols after all. A thread about this on the LKML led me to try:

make INSTALL_MOD_STRIP=1 modules_install

And low and behold, a comparison from within the /lib/modules/x.x.x directory; before:

> ls -hs kernel/crypto/anubis.ko 
112K kernel/crypto/anubis.ko

And after:

> ls -hs kernel/crypto/anubis.ko 
16K kernel/crypto/anubis.ko

More over, the total size of the directory (using the same .config) as reported by du -h went from 185 MB to 13 MB.

Keep in mind that beyond the use of disk space, this is not as significant as it may appear. Debugging symbols are not loaded during normal runtime, so the actual size of each module in memory is probably identical regardless of the size of the .ko file. I think the only significant difference it will make is in the size of the initramfs file, and the only difference it will make there is in the time needed to uncompress the fs. I.e., if you use an uncompressed initramfs, it won't matter.

strip --strip-all also works, and file reports them correctly as stripped either way. Why it says not stripped for the distro ones remains a mystery.

Related Question