Welcome! Log In Create A New Profile

Advanced

[HowTo] Shrink initramfs

Posted by bodhi 
[HowTo] Shrink initramfs
June 04, 2023 03:28PM
How To Shrink initramfs

The approach described here in this tutorial works perfectly for the kernel and rootfs I release here: Kirkwood and MVEBU.

In general, it should work for any Debian kernel. However, it aso depends on how a particular kernel is configured. IOW, whether the necessary modules were already built into the kernel, or some other loadable modules must be loaded early. Please post your question in this thread.


1. Change the default modules loading to dep.

Edit the file /etc/initramfs-tools/initramfs.conf

# MODULES: [ most | netboot | dep | list ]
# dep - Try and guess which modules to load.
MODULES=dep

2. Update initramfs

I'm using the kernel 6.3.5-kirkwood-tld-1 as an example here. Adjust this to the appropriate version number to the running kernel in your system (run uname -a to display current kernel version).
cd /boot
ls -lh initrd.img-6.3.5-kirkwood-tld-1
update-initramfs -u

It will take sometime for the update to complete. List the initramfs files to see the size change. It should shrink the iniramfs by 50% or more.

ls -lh initrd.img-6.3.5-kirkwood-tld-1

3. Recreate the boot file uInitrd

So now we have a new initrd.img-6.3.5-kirkwood-tld-1 file, we need to recreate uInitrd.
mkimage -A arm -O linux -T ramdisk -C gzip -a 0x00000000 -e 0x00000000 -n initramfs-6.3.5-kirkwood-tld-1  -d initrd.img-6.3.5-kirkwood-tld-1 uInitrd
sync

4. Reboot

If satisfied with the new size for initramfs, reboot the system.
sync
shutdown -r now

5. (Optional) Use lzma or zstd compression

Sometime you need to shrink the initramfs even more. For example, in order to flash it to a limited size NAND flash partition (like for boxes in the WD MyCloud NAS series with stock u-boot that requires <= 5MB initrd). Then change the compression to lzma or zstd (available in bullseye and bookworm). Note that to use zstd compression, you also need to install zstd in Debian (apt-get install zstd) first.

Warning: for low RAM boxes (128MB and perhaps also 256MB) like with a few Kirkwood plugs, I'd recommend that you use gzip only. Don't use lzma or zstd because it is too slow.

Edit the file /etc/initramfs-tools/initramfs.conf
# COMPRESS: [ gzip | bzip2 | lz4 | lzma | lzop | xz | zstd ]
COMPRESS=lzma

And then update the initramfs as described in Step 2.

It will take sometime for the update to complete. List the initramfs files to see the size change. It should shrink the iniramfs substantially (about 20% or more).

ls -lh initrd.img-6.3.5-kirkwood-tld-1

And then recreate the boot file uInitrd as shown in Step 3 above. But this time specify the compression.

mkimage -A arm -O linux -T ramdisk -C lzma -a 0x00000000 -e 0x00000000 -n initramfs-6.3.5-kirkwood-tld-1  -d initrd.img-6.3.5-kirkwood-tld-1 uInitrd

And then reboot the system.
sync
shutdown -r now

-bodhi
===========================
Forum Wiki
bodhi's corner (buy bodhi a beer)



Edited 8 time(s). Last edit at 08/21/2023 03:28AM by bodhi.
Re: [HowTo] Shrink initramfs
June 22, 2023 02:38PM
!! Awesome !!

============================
Breaking stuff since 1994 :-)
============================
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: