assert(device);
fd = open(device, O_RDONLY|O_CLOEXEC);
- if (fd < 0)
+ if (fd < 0) {
+ log_debug_errno(errno, "Failed to open loopback device %s: %m", device);
return errno == ENOENT ? 0 : -errno;
+ }
+
+ /* Loopback block devices don't sync in-flight blocks when we clear the fd, hence sync explicitly
+ * first */
+ if (fsync(fd) < 0)
+ log_debug_errno(errno, "Failed to sync loop block device %s, ignoring: %m", device);
if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
if (errno == ENXIO) /* Nothing bound, didn't do anything */