How to update-grub on a system running overlayroot?

We ship boxes configured with overlayroot, using the following overlayroot.conf:

overlayroot=device:dev=/dev/sda6,timeout=20,recurse=0

Which produces the following mount configuration:

$ mount
overlayroot on / type overlayfs (rw,errors=remount-ro)
/dev/sda5 on /media/root-ro type ext3 (ro,relatime,errors=continue,user_xattr,acl,barrier=1,data=ordered)
/dev/sda6 on /media/root-rw type ext3 (rw,relatime,errors=continue,user_xattr,acl,barrier=1,data=ordered)
/dev/sda1 on /boot type ext3 (rw)

As you can see, three key physical partitions: sda1 is /boot, sda5 is a read-only "factory" root, and sda6 is a "user" root which can be wiped at any point to restore the machine to its original factory state.

Now, the problem arises when update-grub is run for any reason:

$ sudo update-grub
[sudo] password for administrator: 
/usr/sbin/grub-probe: error: cannot find a device for / (is /dev mounted?).

Understandable, since / is an overlayfs.

The contents of /usr/sbin/update-grub are:

#!/bin/sh
set -e
exec grub-mkconfig -o /boot/grub/grub.cfg "$@"

With /usr/sbin/grub-mkconfig being the business part of things. But the actual problem is in /usr/sbin/grub-probe, called by grub-mkconfig, and grub-probe is a binary.

So my question is, is there a parameter or whatever which can make grub-probe do the right thing in the face of / being an overlayfs? And secondly, is there a way to hack/patch that in so that the update-grub script just does the right thing?

Thanks.


My workaround:

#!/bin/bash

# Patch grub-mkconfig to not probe / when GRUB_DEVICE is set.
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
echo 'Defaults env_keep +="GRUB_DEVICE"' > /etc/sudoers.d/keep-grub-device
chmod 0440 /etc/sudoers.d/keep-grub-device

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

export GRUB_DEVICE=/dev/sda5
sudo update-grub

Definitely not ideal, but could be a lot worse. Will welcome improvements to this method.


That looks like a bug that should be fixed in Grub itself. It would need to be taught that overlayroot exists, and when that situation is detected, it would need to dig a little deeper to get to the real root device.

If you want to file the bug in Launchpad, and add a comment here with the bug number, I'll get it triaged and prioritized:

  • https://bugs.launchpad.net/ubuntu/+source/grub2/+filebug

Full disclosure: I am the co-author and co-maintainer of overlayroot.