]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dissect: use sd-device to find and open loopback block device
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 30 Nov 2022 04:00:42 +0000 (13:00 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 1 Dec 2022 17:52:06 +0000 (02:52 +0900)
src/dissect/dissect.c

index 3cf250e8ec3aa504e5d629cb6d13b10eea57d2e1..395294ebb42d1c44b23d3eb3a5ae906af7b73366 100644 (file)
@@ -1176,9 +1176,9 @@ static int action_list_or_mtree_or_copy(DissectedImage *m, LoopDevice *d) {
 
 static int action_umount(const char *path) {
         _cleanup_close_ int fd = -1;
-        _cleanup_free_ char *canonical = NULL, *devname = NULL;
+        _cleanup_free_ char *canonical = NULL;
         _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
-        dev_t devno;
+        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
         int r;
 
         fd = chase_symlinks_and_open(path, NULL, 0, O_DIRECTORY, &canonical);
@@ -1193,18 +1193,13 @@ static int action_umount(const char *path) {
         if (r < 0)
                 return log_error_errno(r, "Failed to determine whether '%s' is a mount point: %m", canonical);
 
-        r = fd_get_whole_disk(fd, /*backing=*/ true, &devno);
+        r = block_device_new_from_fd(fd, BLOCK_DEVICE_LOOKUP_WHOLE_DISK | BLOCK_DEVICE_LOOKUP_BACKING, &dev);
         if (r < 0)
                 return log_error_errno(r, "Failed to find backing block device for '%s': %m", canonical);
 
-        r = devname_from_devnum(S_IFBLK, devno, &devname);
+        r = loop_device_open(dev, 0, LOCK_EX, &d);
         if (r < 0)
-                return log_error_errno(r, "Failed to get devname of block device " DEVNUM_FORMAT_STR ": %m",
-                                       DEVNUM_FORMAT_VAL(devno));
-
-        r = loop_device_open_from_path(devname, 0, LOCK_EX, &d);
-        if (r < 0)
-                return log_error_errno(r, "Failed to open loop device '%s': %m", devname);
+                return log_device_error_errno(dev, r, "Failed to open loopback block device: %m");
 
         /* We've locked the loop device, now we're ready to unmount. To allow the unmount to succeed, we have
          * to close the O_PATH fd we opened earlier. */