]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
loop-util: accept loopback flags when creating loopback device
authorLennart Poettering <lennart@poettering.net>
Sun, 23 Dec 2018 18:23:58 +0000 (19:23 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 2 Dec 2019 09:05:09 +0000 (10:05 +0100)
This way callers can choose if they want partition scanning or not.

src/core/namespace.c
src/dissect/dissect.c
src/nspawn/nspawn.c
src/portable/portable.c
src/shared/loop-util.c
src/shared/loop-util.h
src/shared/machine-image.c
src/test/test-dissect-image.c

index bbb372459b02b0a20129593de67c67e52bea2e9f..180730b1e86ce776577dcacdf78ebcfeb9367e05 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <errno.h>
+#include <linux/loop.h>
 #include <sched.h>
 #include <stdio.h>
 #include <sys/mount.h>
@@ -1219,6 +1220,7 @@ int setup_namespace(
 
                 r = loop_device_make_by_path(root_image,
                                              dissect_image_flags & DISSECT_IMAGE_READ_ONLY ? O_RDONLY : O_RDWR,
+                                             LO_FLAGS_PARTSCAN,
                                              &loop_device);
                 if (r < 0)
                         return log_debug_errno(r, "Failed to create loop device for root image: %m");
index 50de0afce6f006cac3d36b72434f51a04618e82d..c1be6c034c5619714e0992283294c345c12ecbbf 100644 (file)
@@ -1,8 +1,9 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <fcntl.h>
-#include <stdio.h>
 #include <getopt.h>
+#include <linux/loop.h>
+#include <stdio.h>
 
 #include "architecture.h"
 #include "dissect-image.h"
@@ -171,7 +172,7 @@ static int run(int argc, char *argv[]) {
         if (r <= 0)
                 return r;
 
-        r = loop_device_make_by_path(arg_image, (arg_flags & DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : O_RDWR, &d);
+        r = loop_device_make_by_path(arg_image, (arg_flags & DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : O_RDWR, LO_FLAGS_PARTSCAN, &d);
         if (r < 0)
                 return log_error_errno(r, "Failed to set up loopback device: %m");
 
index 873a76596f0b485906b51e31372cdddf18cafbcb..9fac32621964d3a9e42edcc0d8fa926d5cd19e55 100644 (file)
@@ -5036,7 +5036,7 @@ static int run(int argc, char *argv[]) {
                         goto finish;
                 }
 
-                r = loop_device_make_by_path(arg_image, arg_read_only ? O_RDONLY : O_RDWR, &loop);
+                r = loop_device_make_by_path(arg_image, arg_read_only ? O_RDONLY : O_RDWR, LO_FLAGS_PARTSCAN, &loop);
                 if (r < 0) {
                         log_error_errno(r, "Failed to set up loopback block device: %m");
                         goto finish;
index 34b123e846925b4a5d62ccc83a22d9712f8fb154..7a86398a4b5d8fe6c1edc12dd9a1544729e881f7 100644 (file)
@@ -1,5 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
+#include <linux/loop.h>
+
 #include "bus-common-errors.h"
 #include "bus-error.h"
 #include "conf-files.h"
@@ -359,7 +361,7 @@ static int portable_extract_by_path(
 
         assert(path);
 
-        r = loop_device_make_by_path(path, O_RDONLY, &d);
+        r = loop_device_make_by_path(path, O_RDONLY, LO_FLAGS_PARTSCAN, &d);
         if (r == -EISDIR) {
                 /* We can't turn this into a loop-back block device, and this returns EISDIR? Then this is a directory
                  * tree and not a raw device. It's easy then. */
index 559d7f8174eff6813b6594e1da87ea0ecdf8b691..9c1d8a391051c66f1c2d69876d1fa7db3b3001dc 100644 (file)
 #include "loop-util.h"
 #include "stat-util.h"
 
-int loop_device_make(int fd, int open_flags, LoopDevice **ret) {
+int loop_device_make(int fd, int open_flags, uint32_t loop_flags, LoopDevice **ret) {
         const struct loop_info64 info = {
-                .lo_flags = LO_FLAGS_AUTOCLEAR|LO_FLAGS_PARTSCAN|(open_flags == O_RDONLY ? LO_FLAGS_READ_ONLY : 0),
+                /* Use the specified flags, but configure the read-only flag from the open flags, and force autoclear */
+                .lo_flags = (loop_flags & ~LO_FLAGS_READ_ONLY) | ((loop_flags & O_ACCMODE) == O_RDONLY ? LO_FLAGS_READ_ONLY : 0) | LO_FLAGS_AUTOCLEAR,
         };
 
         _cleanup_close_ int control = -1, loop = -1;
@@ -103,7 +104,7 @@ int loop_device_make(int fd, int open_flags, LoopDevice **ret) {
         return d->fd;
 }
 
-int loop_device_make_by_path(const char *path, int open_flags, LoopDevice **ret) {
+int loop_device_make_by_path(const char *path, int open_flags, uint32_t loop_flags, LoopDevice **ret) {
         _cleanup_close_ int fd = -1;
 
         assert(path);
@@ -114,7 +115,7 @@ int loop_device_make_by_path(const char *path, int open_flags, LoopDevice **ret)
         if (fd < 0)
                 return -errno;
 
-        return loop_device_make(fd, open_flags, ret);
+        return loop_device_make(fd, open_flags, loop_flags, ret);
 }
 
 LoopDevice* loop_device_unref(LoopDevice *d) {
index d78466c5ee68369620f7e0383cd0aed1330ce7da..c881a43cdc2cdddd484fdab5be430c800456e95a 100644 (file)
@@ -14,8 +14,8 @@ struct LoopDevice {
         bool relinquished;
 };
 
-int loop_device_make(int fd, int open_flags, LoopDevice **ret);
-int loop_device_make_by_path(const char *path, int open_flags, LoopDevice **ret);
+int loop_device_make(int fd, int open_flags, uint32_t loop_flags, LoopDevice **ret);
+int loop_device_make_by_path(const char *path, int open_flags, uint32_t loop_flags, LoopDevice **ret);
 
 LoopDevice* loop_device_unref(LoopDevice *d);
 DEFINE_TRIVIAL_CLEANUP_FUNC(LoopDevice*, loop_device_unref);
index a11803e731adfa5424f87666c900fca83b2ecf21..15fd514353a78f3fc9cc2deb8d51eb24413076d9 100644 (file)
@@ -2,13 +2,14 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <linux/fs.h>
+#include <linux/loop.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <unistd.h>
-#include <linux/fs.h>
 
 #include "alloc-util.h"
 #include "btrfs-util.h"
@@ -1166,7 +1167,7 @@ int image_read_metadata(Image *i) {
                 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
                 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
 
-                r = loop_device_make_by_path(i->path, O_RDONLY, &d);
+                r = loop_device_make_by_path(i->path, O_RDONLY, LO_FLAGS_PARTSCAN, &d);
                 if (r < 0)
                         return r;
 
index 7b32e8373f920a01ca44e3b67958c3375cbca783..12685dad1373de390555d5b7a779f3221470d798 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
 #include <fcntl.h>
+#include <linux/loop.h>
 #include <stdio.h>
 
 #include "dissect-image.h"
@@ -21,7 +22,7 @@ int main(int argc, char *argv[]) {
                 return EXIT_FAILURE;
         }
 
-        r = loop_device_make_by_path(argv[1], O_RDONLY, &d);
+        r = loop_device_make_by_path(argv[1], O_RDONLY, LO_FLAGS_PARTSCAN, &d);
         if (r < 0) {
                 log_error_errno(r, "Failed to set up loopback device: %m");
                 return EXIT_FAILURE;