27 August 2021

Reduce /boot usage by decreasing initrd image size

 I had noticed that every time there was a new kernel being installed on my Asus EeeBox PC EB1033, it always failed due to lack of free disk space on /boot. The /boot partition is 232 MB in size and it couldn't fit 3 versions of the kernel (Ubuntu 18.04.5 LTS). Earlier, I had used the easy way out of manually uninstalling the older unused kernel and then installing the newer one. In normal circumstances, the newer kernel should get installed and then an apt autoremove should uninstall the oldest kernel (2 kernels are kept). When I noticed that the initrd image files on my Debian Unstable desktop were just 11 MB, I sat down and fixed the issue.

The main problem was that the kernel's initrd image file in /boot was being generated during the install process with all possible modules, even the ones which the hardware or I didn't need.

Before you make any changes, get a list of the modules which are currently loaded on your running system using:

$> sudo lsmod > modulesLoaded.before

and backup your configuration files

$> tar cf initramfs-tools.tar /etc/initramfs-tools

The main changes that are required are in /etc/initrd-tools/initramfs.conf. But as suggested in initramfs.conf manual, the changes will be done in /etc/initrd-tools/conf.d/ directory.

Size of initrd image files before changes

eeepc@eeepc:~$ ls -lh /boot/initrd.img-4.15.0-15*
-rw-r--r-- 1 root root 63M Aug 22 20:42 /boot/initrd.img-4.15.0-153-generic
-rw-r--r-- 1 root root 63M Aug 22 20:43 /boot/initrd.img-4.15.0-154-generic

After adding file loadmods in /etc/initrd-tools/conf.d/  with content MODULES=dep (default is most):

eeepc@eeepc:~$ sudo update-initramfs -u -k 4.15.0-154-generic
eeepc@eeepc:~$ ls -lh /boot/initrd.img-4.15.0-15*
-rw-r--r-- 1 root root 63M Aug 22 20:42 /boot/initrd.img-4.15.0-153-generic
-rw-r--r-- 1 root root 31M Aug 22 20:47 /boot/initrd.img-4.15.0-154-generic

After adding file compression in /etc/initrd-tools/conf.d/ with content COMPRESS=xz (default is gzip)

eeepc@eeepc:~$ sudo update-initramfs -u -k 4.15.0-154-generic
eeepc@eeepc:~$ ls -lh /boot/initrd.img-4.15.0-15*
-rw-r--r-- 1 root root 63M Aug 22 20:42 /boot/initrd.img-4.15.0-153-generic
-rw-r--r-- 1 root root 20M Aug 22 20:50 /boot/initrd.img-4.15.0-154-generic

That gives us a total reduction of 43 MB (68.25%) by using just two options.

Run sudo update-grub to apply changes and restart using the new kernel and compare the list of modules loaded with the original list that we saved before making changes. Check hardware functionality, e.g. video, networking, sound, etc. If you want to load some modules which you need (e.g. msdos, ntfs, etc.), just add the list to /etc/initrd-tools/modules file.

To regenerate the initrd image file for all kernels, use the following command:

sudo update-initramfs -u -k all && sudo update grub

Reference and files:

  1. initramfs-tools.conf manual
  2. /etc/initramfs-tools/conf.d/ directory
  3. /etc/initramfs-tools/modules file