Ubuntu 20.04 update-initramfs fails on low RAM GCE VMs

Ubuntu 20.04 update-initramfs fails on low RAM GCE VMs
Page content

Got an email from Google saying they are “improving the experience of the Free Tier” by discontinuing F1-micro type of instance in favor of E2-micro type and I need to switch if I want to continue receiving the Free Tier discount.

E2-micro instances get 12.5% of 2 vCPUs, are allowed to burst up to 2 full vCPUs for short periods and have 1 Gb of RAM.

After rebooting my VM into E2-micro type I decided to upgrade Ubuntu as well.

Evidently something has changed in the initramfs-tools package since the last time I did maintenance because instead of trying to adjust LZMA2 dictionary size while compressing an initramfs image using xz archive tool it just quit with an error. Initramfs image is built every time you install a new version of a kernel image so this prevented the upgrade from finishing properly.

me@muh-server:~$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
  gce-compute-image-packages linux-generic linux-headers-generic linux-headers-virtual linux-image-generic linux-image-virtual
  linux-virtual thermald ubuntu-advantage-tools
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Setting up initramfs-tools (0.136ubuntu6.6) ...
update-initramfs: deferring update (trigger activated)
Processing triggers for initramfs-tools (0.136ubuntu6.6) ...
update-initramfs: Generating /boot/initrd.img-5.4.0-77-generic
xz: Memory usage limit is too low for the given filter setup.
xz: 166 MiB of memory is required. The limit is 50 MiB.
E: mkinitramfs failure cpio 141 xz --check=crc32 --threads=0 1
update-initramfs: failed for /boot/initrd.img-5.4.0-77-generic with 1.
dpkg: error processing package initramfs-tools (--configure):
 installed initramfs-tools package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
 initramfs-tools
E: Sub-process /usr/bin/dpkg returned an error code (1)

Fix

You need to modify initramfs-tools config file and set memory limit around 200 - 250Mb so compression does not fail. In the vanilla config on my VM the limit was set to 5% of RAM available or ~50 Mb on 1Gb RAM instance.

me@muh-server: cat /etc/initramfs-tools/conf.d/99-gce.conf
# Use xz compression per GCE
COMPRESS=xz
XZ_OPT=--memlimit-compress=5%
export XZ_OPT

I increased the limit to 25% and initramfs update works now.

me@muh-server:/etc/initramfs-tools$ sudo update-initramfs -u
update-initramfs: Generating /boot/initrd.img-5.4.0-77-generic
xz: Adjusted the number of threads from 2 to 1 to not exceed the memory usage limit of 246 MiB
me@muh-server:/etc/initramfs-tools$

Might not affect you on a beefier server - if you have 4Gb+ of RAM you are unlikely to hit this particular issue.