composefs: Hard error except on ENOENT even in "optional" case
authorColin Walters <walters@verbum.org>
Mon, 21 Aug 2023 21:19:35 +0000 (17:19 -0400)
committerColin Walters <walters@verbum.org>
Fri, 25 Aug 2023 19:23:06 +0000 (15:23 -0400)
Since we enabled composefs at build time, the default (non-composefs)
case now always prints
`composefs: Optional support failed: No such file or directory`
But that's normal and expected.

Rework things here so that in the very special case where
we are in "maybe/optional" mode and we get ENOENT, then we
output a much more normal-looking message that doesn't include
the string "failed".

Now on the flip side - if I have explicitly enabled signature
checking, I think we *do* want to make that fatal even if
composefs is in "maybe" mode.

(This part is more debatable; perhaps we should just disallow
 the case of "maybe" + signatures at all; but I think this is
 an improvement in that direction)

src/switchroot/ostree-prepare-root.c

index a75c1981b47f28d52b7fed7994cd2e34103f57bb..f6c3a8140bf43c73fb0bcf5b56ffa30e0e35b56e 100644 (file)
@@ -264,6 +264,24 @@ validate_signature (GBytes *data, GVariant *signatures, GPtrArray *pubkeys)
 
   return FALSE;
 }
+
+// Output a friendly message based on an errno for common cases
+static const char *
+composefs_error_message (int errsv)
+{
+  switch (errsv)
+    {
+    case ENOVERITY:
+      return "fsverity not enabled on composefs image";
+    case EWRONGVERITY:
+      return "Wrong fsverity digest in composefs image";
+    case ENOSIGNATURE:
+      return "Missing signature for fsverity in composefs image";
+    default:
+      return strerror (errsv);
+    }
+}
+
 #endif
 
 typedef struct
@@ -495,29 +513,14 @@ main (int argc, char *argv[])
       else
         {
           int errsv = errno;
-          const char *errmsg;
-          switch (errsv)
-            {
-            case ENOVERITY:
-              errmsg = "fsverity not enabled on composefs image";
-              break;
-            case EWRONGVERITY:
-              errmsg = "Wrong fsverity digest in composefs image";
-              break;
-            case ENOSIGNATURE:
-              errmsg = "Missing signature for fsverity in composefs image";
-              break;
-            default:
-              errmsg = strerror (errno);
-              break;
-            }
-          if (composefs_config->enabled == OT_TRISTATE_MAYBE)
+          g_assert (composefs_config->enabled != OT_TRISTATE_NO);
+          if (composefs_config->enabled == OT_TRISTATE_MAYBE && errsv == ENOENT)
             {
-              g_print ("composefs: optional support failed: %s\n", errmsg);
+              g_print ("composefs: No image present\n");
             }
           else
             {
-              g_assert (composefs_config->enabled == OT_TRISTATE_YES);
+              const char *errmsg = composefs_error_message (errsv);
               errx (EXIT_FAILURE, "composefs: failed to mount: %s", errmsg);
             }
         }