]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shutdown: close DM block device before issuing DM_DEV_REMOVE ioctl
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 22 Nov 2024 20:47:40 +0000 (05:47 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 23 Nov 2024 08:31:36 +0000 (17:31 +0900)
Otherwise, the ioctl() may fail with EBUSY.

Follow-up for b4b66b26620bfaf5818c95d5cffafd85207694e7.
Hopefully fixes #35243.

src/shutdown/detach-dm.c

index bddd748d63446f2407a6b80a2d73ec14c447566b..88e6a47a2745b3a7aeb841ff42056bd37e148517 100644 (file)
@@ -98,15 +98,17 @@ static int delete_dm(DeviceMapper *m) {
         assert(major(m->devnum) != 0);
         assert(m->path);
 
-        fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
+        fd = open(m->path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
         if (fd < 0)
-                return -errno;
-
-        _cleanup_close_ int block_fd = open(m->path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
-        if (block_fd < 0)
                 log_debug_errno(errno, "Failed to open DM block device %s for syncing, ignoring: %m", m->path);
-        else
-                (void) sync_with_progress(block_fd);
+        else {
+                (void) sync_with_progress(fd);
+                fd = safe_close(fd);
+        }
+
+        fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
+        if (fd < 0)
+                return log_debug_errno(errno, "Failed to open /dev/mapper/control: %m");
 
         return RET_NERRNO(ioctl(fd, DM_DEV_REMOVE, &(struct dm_ioctl) {
                 .version = {