[neon/neon/livecd-rootfs/Neon/release] /: Use flock to avoid races with systemd-udevd that cause loop device partitions to briefly disappear. (LP: #2045586)

Michael Hudson-Doyle null at kde.org
Sat Feb 24 21:21:02 GMT 2024


Git commit 11b5162b14f5af1e787dda20af38981d0e033a67 by Michael Hudson-Doyle.
Committed on 18/02/2024 at 17:55.
Pushed by jriddell into branch 'Neon/release'.

Use flock to avoid races with systemd-udevd that cause loop device partitions to briefly disappear. (LP: #2045586)

M  +8    -0    debian/changelog
M  +15   -4    live-build/functions

https://invent.kde.org/neon/neon/livecd-rootfs/-/commit/11b5162b14f5af1e787dda20af38981d0e033a67

diff --git a/debian/changelog b/debian/changelog
index 4d50e357..b52d022c 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,11 @@
+livecd-rootfs (2.765.39) UNRELEASED; urgency=medium
+
+  [ dann frazier ]
+  * Use flock to avoid races with systemd-udevd that cause loop device
+    partitions to briefly disappear. (LP: #2045586)
+
+ -- Michael Hudson-Doyle <michael.hudson at ubuntu.com>  Thu, 25 Jan 2024 07:45:58 +1300
+
 livecd-rootfs (2.765.38) jammy; urgency=medium
 
   * Add a largemem subarch for ubuntu-server that ships a 64k kernel variant
diff --git a/live-build/functions b/live-build/functions
index 049a1ca2..aa1c1e76 100644
--- a/live-build/functions
+++ b/live-build/functions
@@ -67,16 +67,23 @@ mount_image() {
         exit 1
     fi
 
+    # As explained in excruciating detail in LP: #2045586, losetup
+    # races with udev in a way that can cause partition device files
+    # to briefly vanish. systemd docs say we can hold udev off by using
+    # flocks: https://systemd.io/BLOCK_DEVICE_LOCKING/
+    # `udevadm lock` isn't yet usable in Ubuntu, so we'll use flock for now
+
     # Find the rootfs location
     rootfs_dev_mapper="${loop_device}p${rootpart}"
-    if [ ! -b "${rootfs_dev_mapper}" ]; then
+    if flock -x ${loop_device} [ ! -b "${rootfs_dev_mapper}" ]; then
         echo "${rootfs_dev_mapper} is not a block device";
         exit 1
     fi
 
     # Add some information to the debug logs
     echo "Mounted disk image ${backing_img} to ${rootfs_dev_mapper}"
-    blkid ${rootfs_dev_mapper}
+    flock -x ${loop_device} blkid ${rootfs_dev_mapper} \
+    || echo "blkid failed; continuing"
 
     return 0
 }
@@ -206,10 +213,14 @@ mount_disk_image() {
     mount_partition "${rootfs_dev_mapper}" $mountpoint
 
     local uefi_dev="${loop_device}p15"
-    if [ -b ${uefi_dev} -a -e $mountpoint/boot/efi ]; then
-        mount "${uefi_dev}" $mountpoint/boot/efi
+    if flock -x ${loop_device} \
+        [ -b ${uefi_dev} -a -e $mountpoint/boot/efi ]; then
+        flock -x ${loop_device} mount "${uefi_dev}" $mountpoint/boot/efi
     fi
 
+    # Having one partition mounted should avoid udev-triggered partition
+    # rescans on that device, so we no longer need to flock.
+
     # This is needed to allow for certain operations
     # such as updating grub and installing software
     cat > $mountpoint/usr/sbin/policy-rc.d << EOF



More information about the Neon-commits mailing list