]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
dissect: make using a generic partition as root partition optional
authorLennart Poettering <lennart@poettering.net>
Thu, 15 Dec 2016 16:38:11 +0000 (17:38 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 21 Dec 2016 18:09:30 +0000 (19:09 +0100)
In preparation for reusing the image dissector in the GPT auto-discovery
logic, only optionally fail the dissection when we can't identify a root
partition.

In the GPT auto-discovery we are completely fine with any kind of root,
given that we run when it is already mounted and all we do is find some
additional auxiliary partitions on the same disk.

src/dissect/dissect.c
src/machine/image-dbus.c
src/nspawn/nspawn.c
src/shared/dissect-image.c
src/shared/dissect-image.h
src/test/test-dissect-image.c

index 78ec88fa35475c4423761e5651702b4fb52ff3eb..fd9db5ba8786b4016ff4edb2bb50fbdbcdd7b2d8 100644 (file)
@@ -35,7 +35,7 @@ static enum {
 } arg_action = ACTION_DISSECT;
 static const char *arg_image = NULL;
 static const char *arg_path = NULL;
-static DissectImageFlags arg_flags = DISSECT_IMAGE_DISCARD_ON_LOOP;
+static DissectImageFlags arg_flags = DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_DISCARD_ON_LOOP;
 static void *arg_root_hash = NULL;
 static size_t arg_root_hash_size = 0;
 
@@ -191,7 +191,7 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        r = dissect_image(d->fd, arg_root_hash, arg_root_hash_size, 0, &m);
+        r = dissect_image(d->fd, arg_root_hash, arg_root_hash_size, arg_flags, &m);
         if (r == -ENOPKG) {
                 log_error_errno(r, "Couldn't identify a suitable partition table or file system in %s.", arg_image);
                 goto finish;
index 2b168b267b2b391f020b5dc8029274553b1aaf05..1891f0758605e58c5de4ff6589e44f0299036dad 100644 (file)
@@ -336,7 +336,7 @@ static int raw_image_get_os_release(Image *image, char ***ret, sd_bus_error *err
         if (r < 0)
                 return sd_bus_error_set_errnof(error, r, "Failed to set up loop block device for %s: %m", image->path);
 
-        r = dissect_image(d->fd, NULL, 0, 0, &m);
+        r = dissect_image(d->fd, NULL, 0, DISSECT_IMAGE_REQUIRE_ROOT, &m);
         if (r == -ENOPKG)
                 return sd_bus_error_set_errnof(error, r, "Disk image %s not understood: %m", image->path);
         if (r < 0)
index 01d89df1a4f74e3d1d1d940e81bf5c66687c3e99..224d30fca681a48c34001d495475665fb8db99ad 100644 (file)
@@ -3743,7 +3743,11 @@ int main(int argc, char *argv[]) {
                         goto finish;
                 }
 
-                r = dissect_image(loop->fd, arg_root_hash, arg_root_hash_size, 0, &dissected_image);
+                r = dissect_image(
+                                loop->fd,
+                                arg_root_hash, arg_root_hash_size,
+                                DISSECT_IMAGE_REQUIRE_ROOT,
+                                &dissected_image);
                 if (r == -ENOPKG) {
                         log_error_errno(r, "Could not find a suitable file system or partition table in image: %s", arg_image);
 
index 5b6e78dd3d2ba15a13c46dce3dd6b3402655f6f8..878cb008aa9b146fd4d278f45013d08e5d2f2c6b 100644 (file)
@@ -174,7 +174,8 @@ int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectI
         if (!m)
                 return -ENOMEM;
 
-        if ((flags & DISSECT_IMAGE_GPT_ONLY) == 0) {
+        if (!(flags & DISSECT_IMAGE_GPT_ONLY) &&
+            (flags & DISSECT_IMAGE_REQUIRE_ROOT)) {
                 const char *usage = NULL;
 
                 (void) blkid_probe_lookup_value(b, "USAGE", &usage, NULL);
@@ -490,7 +491,7 @@ int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectI
                  * either, then check if there's a single generic one, and use that. */
 
                 if (m->partitions[PARTITION_ROOT_VERITY].found)
-                        return -ENXIO;
+                        return -EADDRNOTAVAIL;
 
                 if (m->partitions[PARTITION_ROOT_SECONDARY].found) {
                         m->partitions[PARTITION_ROOT] = m->partitions[PARTITION_ROOT_SECONDARY];
@@ -499,8 +500,19 @@ int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectI
                         m->partitions[PARTITION_ROOT_VERITY] = m->partitions[PARTITION_ROOT_SECONDARY_VERITY];
                         zero(m->partitions[PARTITION_ROOT_SECONDARY_VERITY]);
 
-                } else if (generic_node && !root_hash) {
+                } else if (flags & DISSECT_IMAGE_REQUIRE_ROOT) {
+
+                        /* If the root has was set, then we won't fallback to a generic node, because the root hash
+                         * decides */
+                        if (root_hash)
+                                return -EADDRNOTAVAIL;
 
+                        /* If we didn't find a generic node, then we can't fix this up either */
+                        if (!generic_node)
+                                return -ENXIO;
+
+                        /* If we didn't find a properly marked root partition, but we did find a single suitable
+                         * generic Linux partition, then use this as root partition, if the caller asked for it. */
                         if (multiple_generic)
                                 return -ENOTUNIQ;
 
@@ -514,14 +526,11 @@ int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectI
                         };
 
                         generic_node = NULL;
-                } else
-                        return -ENXIO;
+                }
         }
 
-        assert(m->partitions[PARTITION_ROOT].found);
-
         if (root_hash) {
-                if (!m->partitions[PARTITION_ROOT_VERITY].found)
+                if (!m->partitions[PARTITION_ROOT_VERITY].found || !m->partitions[PARTITION_ROOT].found)
                         return -EADDRNOTAVAIL;
 
                 /* If we found the primary root with the hash, then we definitely want to suppress any secondary root
index 76104e578071ec1a88b58a6912d4c3bc9ba42bbd..26319bd8e7e1f7739c12d54dfc6319907f9c2c99 100644 (file)
@@ -69,6 +69,7 @@ typedef enum DissectImageFlags {
                                     DISSECT_IMAGE_DISCARD |
                                     DISSECT_IMAGE_DISCARD_ON_CRYPTO,
         DISSECT_IMAGE_GPT_ONLY = 16,         /* Only recognize images with GPT partition tables */
+        DISSECT_IMAGE_REQUIRE_ROOT = 32,     /* Don't accept disks without root partition */
 } DissectImageFlags;
 
 struct DissectedImage {
index ddaf3a0d8b44aa8ed9bf7d80b18a49fe73995fae..2bb68be0db8d3f21c4ef5cf63c1dc47409df577e 100644 (file)
@@ -43,7 +43,7 @@ int main(int argc, char *argv[]) {
                 return EXIT_FAILURE;
         }
 
-        r = dissect_image(d->fd, NULL, 0, 0, &m);
+        r = dissect_image(d->fd, NULL, 0, DISSECT_IMAGE_REQUIRE_ROOT, &m);
         if (r < 0) {
                 log_error_errno(r, "Failed to dissect image: %m");
                 return EXIT_FAILURE;