[v2] expand-on-first-boot: wait for udev to create symlink

Message ID 20221209103540.13429-1-tobias.schaffner@siemens.com
State Superseded, archived
Headers show
Series [v2] expand-on-first-boot: wait for udev to create symlink | expand

Commit Message

Tobias Schaffner Dec. 9, 2022, 10:35 a.m. UTC
From: Tobias Schaffner <tobias.schaffner@siemens.com>

systemd-growfs depends on a symlink to the partition of the filesystem
that should be resized. This symlink is created by udev in /dev/block/.

If this symlink is not yet created for example because systemd-udev is
not up yet systemd-growfs will fail.

We could use Require and After to depend on the systemd-udev service
but this could again create a race condition if udev is up but not
fast enough after the partx -u.

Run systemd-growfs periodically until the symlink appears.
---
 .../files/expand-last-partition.sh            | 30 +++++++++++++++++--
 1 file changed, 27 insertions(+), 3 deletions(-)

Patch

diff --git a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
index 57055cc..c5f96ec 100755
--- a/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
+++ b/meta/recipes-support/expand-on-first-boot/files/expand-last-partition.sh
@@ -79,7 +79,31 @@  if [ ! -d "${MOUNT_POINT}" ]; then
 	exit 1
 fi
 
+# Send message to stdout, unmount mountpoint and exit.
+# $1 The message that should be send to stdout
+# $2 The mount point that should be unmounted
+# $3 The exit code that shout be used on exit
+growfs_cleanup_and_exit () {
+    echo $1
+    umount $2
+    rmdir $2
+    exit $3
+}
+
 mount "${LAST_PART}" "${MOUNT_POINT}"
-/lib/systemd/systemd-growfs "${MOUNT_POINT}"
-umount "${MOUNT_POINT}"
-rmdir "${MOUNT_POINT}"
+
+# If systemd-udevd if not up yet or was not fast enough the symlinks in
+# /dev/block/ might be missing. Retry in that case.
+# This retry logic is only needed up to systemd-version 252
+for run in $(seq 0 50); do
+    if GROWFS_OUTPUT=$(/lib/systemd/systemd-growfs "${MOUNT_POINT}" 2>&1); then
+        growfs_cleanup_and_exit "${GROWFS_OUTPUT}" "${MOUNT_POINT}" 0
+    else:
+        if ! echo ${GROWFS_OUTPUT} | grep -q "^Failed to open \"/dev/block/.*\": No such file or directory$"; then
+            growfs_cleanup_and_exit "${GROWFS_OUTPUT}" "${MOUNT_POINT}" 1
+        fi
+    fi
+    sleep 0.1
+done
+growfs_cleanup_and_exit "${GROWFS_OUTPUT}" "${MOUNT_POINT}" 1
+