How to update-grub on a system running overlayroot?

My system is running overlayfs as root. When I update grub or any kernal related packages, I will encounter following error:

1
/usr/sbin/grub-probe: error: failed to get canonical path of `overlay'.

And here is the fix:

patch grub-mkconfig to not probe / when GRUB_DEVICE is set.

1
2
3
4
5
6
7
8
9
10
11
cat <<'PATCH' | patch /usr/sbin/grub-mkconfig

+++ /usr/sbin/grub-mkconfig 2013-10-28 11:33:15.000000000 -0400
@@ -129,7 +129,7 @@
mkdir -p ${GRUB_PREFIX}
# Device containing our userland. Typically used for root= parameter.
-GRUB_DEVICE="`${grub_probe} --target=device /`"
+GRUB_DEVICE=${GRUB_DEVICE-"`${grub_probe} --target=device /`"}
GRUB_DEVICE_UUID="`${grub_probe} --device ${GRUB_DEVICE} --target=fs_uuid 2> /dev/null`" || true
# Device containing our /boot partition. Usually the same as GRUB_DEVICE.
PATCH

Pass the GRUB_DEVICE parameter through sudo

1
2
3
echo 'Defaults env_keep +="GRUB_DEVICE"' > /etc/sudoers.d/keep-grub-device

chmod 0440 /etc/sudoers.d/keep-grub-device

set it (in bashrc or wherever else) and update away

1
2
export GRUB_DEVICE=/dev/sda5
sudo update-grub

Don’t remember to mount /boot, otherwise the problem will persist.