LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How can I make the kernel smaller? (https://www.linuxquestions.org/questions/linux-newbie-8/how-can-i-make-the-kernel-smaller-4175622560/)

Unturned3 01-28-2018 02:28 AM

[SOLVED] How can I make the kernel smaller?
 
Hello,
I am building a very small Linux system from scratch using just the kernel and busybox. So far I managed to setup a very basic bootable system with the components mentioned before and Syslinux, weighting at 12mb. I am trying to make it more tiny in order to squeeze it into a small virtual machine.
How can I make my kernel smaller? Currently, the compiled kernel takes up 8mb of space, and I found out that distributions like TinyCore Linux only uses a kernel of 3mb. I've tried to use the "make tinyconfig" option but the resulting kernel doesn't boot anymore (it seems like a lot of crucial components are missing)

Any help/advice would be greatly appreciated! Thank you very much.

Shadow_7 01-28-2018 08:46 PM

make localmodconfig will strip out all the modules that you don't have hardware for, or don't have plugged in when issued. It makes it smaller and compile faster. But if you want things not attached, like other filesystems to mount and read. You'll need to compile your kernel again.

jefro 01-29-2018 03:32 PM

Maybe try some different compression on kernel.

Unturned3 01-31-2018 03:13 AM

Thanks for the help!
I may need to try a XZ compressed kernel instead of GZ. Maybe that will shrink it down by one or two megabytes.
I tried the "make localmodconfig" and the resulting kernel is only 1760kB! However when I swapped this tiny kernel with my existing 6mb kernel the machine won't boot anymore. Syslinux is working fine, but the kernel just doesn't boot. I checked the configuration and it seems like that the all the major components has been selected. What's going on here?

Shadow_7 01-31-2018 04:47 AM

Did you make bzImage or other traditional kernel types. Make sure the modules needed to read the storage device that contains the / system is in the initrd or in the kernel and not modules. To include the filesystem and the hardware the storage device is connected to. I tend to cut and paste entire sections (like filesystems) from the default /boot/config that comes with a distro to a resulting make localmodconfig .config file. Although these days I mostly block or modprobe -r that modules I'm not using (webcam / wireless / bluetooth / ...) after bootup. To lower CPU usage and maximize RAM. Not that significant these days with multicore CPUs and more than 2GB of RAM.

Unturned3 02-01-2018 06:35 AM

Yes I double checked that I selected all the important components (block layer, ext4 file system support, PCI devices, storage drivers, etc).
All of these are checked and I don't think any component is getting compiled as a separate kernel module (because I unchecked the "use loadable module" flag). However, the resulting kernel still won't load, for some reason. Is there any way that I can check what's going on in there? I can't see any error messages printing after the boot prompt of Syslinux.

Shadow_7 02-01-2018 03:46 PM

Assuming all the usual logging happened... /var/log/kern.log tends to contain a lot of boot gripes / events. Which might be blank if it never got past the bootloader or the kernel never gained the ability to write logs to anything other than RAM.

jefro 02-01-2018 04:03 PM

Kind of a lot of unknown issues.

Syslinux may have some issues with any number of things. This kernel may have issue as well as the hardware it is running on. Not sure what way to go here.

Swapping loader or kernel or hardware or review your build example. ???


What web page are you using as a guide.


General ideas on this here. https://www.linuxjournal.com/content...-distributions

Unturned3 02-02-2018 04:08 AM

@ Shadow 7
I mounted the disk image that the system was on and checked /var/log/messages, and there weren't anything there. It seems like that there's a problem when the Syslinux is handing the control over to the kernel. The kernel didn't seem to run at all.

@ jefro
I am using this guide to build my small Linux system: https://github.com/MichielDerhaeg/build-linux
I'll get rid of syslinux and test out the system using Grub and see if it works. Hopefully this can narrow down the problem.
Thanks for the link to the article!

BTW I am using QEMU to boot the system. The original kernel (the 8mb one) works both under QEMU and Oracle VirtualBox

Unturned3 02-05-2018 08:20 AM

After about four hours of work, I finally managed to check all the necessary compartments (block layer, file system, ramdisk, SATA drivers, etc) from scratch (using make tinyconfig) and the kernel now works! The resulting kernel is just under 3.5 megabytes!
Thanks for the help guys!

sundialsvcs 02-06-2018 08:14 AM

When I was using Gentoo a lot, I started by booting a "rescue DVD" and noting exactly what kernel modules it identified. I'd also plug in and remove USB devices and note which drivers it selected for hotplugging. Then, I'd build a kernel configuration to suit ... taking care to make a backup copy of the hidden config-file ... in a non-hidden, locked, dated file which I never discarded. (This allowed me to use diff to instantly see how any config varied from any other.)

I would invariably "rename the hidden file out of the way," then run make distclean, then "rename it back" (after also making the aforesaid backup). In this way I could be certain that every kernel-compile started from scratch and recompiled everything at once.
Quote:

Originally Posted by Caution!!
make distclean :eek: will destroy(!) :eek: the kernel configuration file!

Hardware support for all the fixed hardware was compiled into the kernel, and support for USB devices was hotplug modules. (I compiled only the modules needed to support the removable devices that I actually owned.) Compiler options were set for the actual microprocessor-type, which was known. The initrd step was completely eliminated – it was unnecessary – and I wound up with a Linux that could start from cold in about seven seconds flat. The machine, which was originally sold with Windows-95 on it (poor thing ...), was suddenly ... fast! :)


All times are GMT -5. The time now is 05:14 AM.