68

An ordinary stable 3* kernel is about 70 mb now. But there are little linux distributions of 30-10 mb with software and other stuff running out of the box.

How can the linux kernel be so small in these distros? Can stripping down only unneeded modules and drivers dramatically reduce the size? What else is done to reduce the kernel size?

Tobias Kienzler
  • 4,421
  • 7
  • 44
  • 76
puzzled tux
  • 683
  • 1
  • 5
  • 4
  • 4
    Another option is to use size optimizations when compiling, but I'm not sure how big impact can that make. – AndrejaKo Dec 22 '11 at 10:12
  • 7
    "Linus calls Linux 'bloated and huge'" http://www.theregister.co.uk/2009/09/22/linus_torvalds_linux_bloated_huge/ – Aki Dec 22 '11 at 10:22
  • 3
    @AndrejaKo - A lot. (If you go for small size.) – Apache Dec 22 '11 at 10:24
  • 22
    70 MB is the size of the source. A standard compiled and compressed kernel is around 2 MB, modules like 10 MB. – ott-- Dec 22 '11 at 11:42
  • 2
    yes, indeed, its 3.6 mb while initramfs is 21mb in my distro. – puzzled tux Dec 22 '11 at 12:15
  • As ott-- pointed out there is no kernel image of 70MB, that's surely the size of the sources! – David Costa Dec 22 '11 at 13:16
  • 1
    2MB? That's nothing - I have seen Tetris coded in [less than half a kilobyte](http://www.scene.org/file.php?file=/mags/hugi/compos/hc22fin.zip&fileinfo) :) – BlueRaja - Danny Pflughoeft Dec 22 '11 at 18:02
  • 1
    70 MB is not the size of the source. These really are the binaries. The Debian kernel ships with over 3000 modules, some of which are multiple megabytes in size. The current source tree is almost 900 MiB. – Daniel B Feb 10 '20 at 09:07
  • @BlueRaja-DannyPflughoeft there are a lot [boot sector games](https://www.youtube.com/watch?v=1UzTf0Qo37A) out there – phuclv Feb 13 '20 at 01:16

5 Answers5

43

Yes.

  • If you strip them down (compile only modules for the things that you actually need.)
  • Optimize the kernel for size (ie.: -Os)
  • Compress the kernel (such as XZ.)

These "options" are all available in the standard kernel and you can do this at the configuration step. You don't need any kind of special tool.

Using those three things will result in such a small kernel. You can make even smaller ones, for a targeted system. Like I know my PC, I know my stuff. So I'll just compile what I need. Like my own SATA drivers, a driver for USB, etc. Nothing else. No webcam, no old IDE drives, nothing.

The bigger ones you encounter are the WHOLE kernel. They are compiled with the most generic options to be able to run on most hardware without issues and they contain almost every driver available as modules. These modules don't make the kernel slower speed-wise as the modules are loaded on-demand.

(There are 3 states for modules at the configuration stage:
[ ] - Won't get compiled, won't be part of your kernel.
[M] - Will be compiled as a module.
(You can modprobe it, or your system will auto-load it when it's needed.
Depends on the system, userland.)
[X] - It'll be compiled into the kernel AND will be present - always.

Distros with small but still generic kernel:

Apache
  • 15,981
  • 25
  • 100
  • 152
  • 1
    Just a small addition - [X] means it'll be compiled **into** the kernel, hence the reason why it's always loaded. – Lawrence Jan 10 '14 at 12:02
  • 4
    "newbies expect" speaking as a professional computer tech and Linux veteran of 20 years... I expect things to just work too. – Cliff Armstrong Aug 23 '19 at 15:07
21

Early Linux distributions such as Tom's Root-Boot, MuLinux, Diet-router, (the now defunct) LOAF and many others now departed, all fit a working Linux system on one or two 1.44 MB diskettes.

The Linux kernel has grown but it is modular: kernel modules are loaded as needed. Thus it is still possible to produce a Linux distribution with a very small footprint.

See: Kernel Size Tuning Guide - eLinux.org

If you read Linux_tiny.pdf you will see this

  • historic 0.99pl15 kernel: Slackware 1.1.2, 1994: 301K
  • Fedora Core 2: 1.2M
  • SuSE 9.1: 1.5M
  • 2.6.5-tiny1 test config: IDE, ext2, TCP, NIC: 363K
wallyk
  • 1,348
  • 10
  • 18
RedGrittyBrick
  • 81,981
  • 20
  • 135
  • 205
11

The default kernel configuration is configured to support as many hardware as possible. A non-stripped kernel with default configuration resulted in a size of 1897996 kB (including kernel + modules). When stripping many unnecessary drivers and options (why would I need a HP module if I own a Clevo notebook), it resulted in a size of 892892 kB which is a size reduction of 53% compared to the stock kernel.

When installing the kernel modules, append the INSTALL_MOD_STRIP=1 option. This will strip all debugging symbols and reduced the size by 92% for me (from 892892 kB to 69356 kB). Note this will only affects modules to be installed and not the kernel (vmlinuz) itself:

make INSTALL_MOD_STRIP=1 modules_install

From Documentation/kbuild/kbuild.txt:

INSTALL_MOD_STRIP
--------------------------------------------------
INSTALL_MOD_STRIP, if defined, will cause modules to be
stripped after they are installed.  If INSTALL_MOD_STRIP is '1', then
the default option --strip-debug will be used.  Otherwise,
INSTALL_MOD_STRIP value will be used as the options to the strip command.
Worthwelle
  • 4,538
  • 11
  • 21
  • 32
Lekensteyn
  • 6,546
  • 5
  • 27
  • 48
  • 5
    wow, 1897996 kB is almost 2GB! – netvope Oct 23 '13 at 02:18
  • I very much doubt this information is accurate. It is most likely the size of the source tree after compilation. It contains many intermediate build artifacts that are not part of the actual “installation tree” of a kernel. – Daniel B Feb 10 '20 at 08:58
3

Most major distros configure their kernels with maximum hardware support so it will work out of the box with most computers and anything you plug in later on. They also use an initrd system which is not needed in most cases. There are also options in the kernel configuration that make the build process optimize for size, and a better (though slower) compression method can be selected as well. Lastly, if you are looking at the size of the kernel source code, keep in mind that it also includes support for several architectures beyond x86/amd64, network protocols, file systems, and other features that you'll probably never use and that can be disabled.

1

It is quite simple, a minimal system just need bootloader + kernel though it is almost useless. Most of commands used everyday, are optional binary for the system, like sh, ls, etc. If you are interested in internals, Linux From Scratch! is a good start point.

MariusMatutiae
  • 46,990
  • 12
  • 80
  • 129
pinxue
  • 111
  • 2