Fix race condition with blockdev/BLKRRPART due to lack of fsync
authorMichael Prokop <mika@grml.org>
Tue, 17 Nov 2020 22:45:23 +0000 (23:45 +0100)
committerMichael Prokop <mika@grml.org>
Tue, 17 Nov 2020 22:46:46 +0000 (23:46 +0100)
Quoting dann frazier from Debian's #975015:

| grml2usb autopkgtest frequently fail in Ubuntu's CI.
| [...]
| 2020-11-17 14:14:49,433 Probing device via 'blockdev --rereadpt /dev/loop0'
| blockdev: ioctl error on BLKRRPART: Device or resource busy
| 2020-11-17 14:14:49,452 Execution failed: ("Couldn't execute blockdev on '%s' (install util-linux?)", '/dev/loop0')
|
| I am able to reproduce this on an OpenStack instance with a failure rate of
| 33% (36 failures, 110 passes). My theory is that the sync is not sufficient,
| and that we really need to do a targeted fsync of the file. With the
| attached patch, I've yet to see a failure in 42 iterations.

Thanks: dann frazier <dannf@dannf.org> for bug report and patch
Closes: #975015

grml2usb

index e2d9279..d8c5053 100755 (executable)
--- a/grml2usb
+++ b/grml2usb
@@ -806,7 +806,9 @@ def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
     set_rw(device)
 
     logging.debug(
-        "executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc", tmpf.name, device
+        "executing: dd if='%s' of='%s' bs=512 count=1 conv=notrunc,fsync",
+        tmpf.name,
+        device,
     )
     proc = subprocess.Popen(
         [
@@ -815,7 +817,7 @@ def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
             "of=%s" % device,
             "bs=512",
             "count=1",
-            "conv=notrunc",
+            "conv=notrunc,fsync",
         ],
         stderr=open(os.devnull, "r+"),
     )
@@ -824,11 +826,6 @@ def install_mbr(mbrtemplate, device, partition, ismirbsdmbr=True):
         raise Exception("error executing dd (third run)")
     del tmpf
 
-    # make sure we sync filesystems before returning
-    logging.debug("executing: sync")
-    proc = subprocess.Popen(["sync"])
-    proc.wait()
-
     logging.debug("Probing device via 'blockdev --rereadpt %s'", device)
     proc = subprocess.Popen(["blockdev", "--rereadpt", device])
     proc.wait()