From 11b5162b14f5af1e787dda20af38981d0e033a67 Mon Sep 17 00:00:00 2001 From: Michael Hudson-Doyle Date: Mon, 19 Feb 2024 06:54:39 +1300 Subject: [PATCH] Use flock to avoid races with systemd-udevd that cause loop device partitions to briefly disappear. (LP: #2045586) --- debian/changelog | 8 ++++++++ live-build/functions | 19 +++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) 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 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