status: Introduce tool to quickly check if we are booted as default
authorEric Curtin <ecurtin@redhat.com>
Fri, 5 Jan 2024 15:49:39 +0000 (15:49 +0000)
committerEric Curtin <ecurtin@redhat.com>
Fri, 5 Jan 2024 17:40:41 +0000 (17:40 +0000)
Generally in ostree based systems you would expect to boot into
deployment 0, in rollback conditions triggered by greenboot-related
rollbacks this might not be the case. This is a tool to detect this.

Signed-off-by: Eric Curtin <ecurtin@redhat.com>
src/ostree/ot-admin-builtin-status.c

index bf94843f26be0bbe69e7b532a4b05d8c99b6d377..d4822793ef67901db3ec295e3de4b97069978a97 100644 (file)
 
 static gboolean opt_verify;
 static gboolean opt_skip_signatures;
-
-static GOptionEntry options[]
-    = { { "verify", 'V', 0, G_OPTION_ARG_NONE, &opt_verify, "Print the commit verification status",
-          NULL },
-        { "skip-signatures", 'S', 0, G_OPTION_ARG_NONE, &opt_skip_signatures,
-          "Print the commit verification status", NULL },
-        { NULL } };
+static gboolean opt_query_booted;
+
+static GOptionEntry options[] = {
+  { "verify", 'V', 0, G_OPTION_ARG_NONE, &opt_verify, "Print the commit verification status",
+    NULL },
+  { "skip-signatures", 'S', 0, G_OPTION_ARG_NONE, &opt_skip_signatures,
+    "Print the commit verification status", NULL },
+  { "query-booted", 'Q', 0, G_OPTION_ARG_NONE, &opt_query_booted,
+    "Output the string \"default\" if the default deployment is the booted one, \"not-default\" if "
+    "we are booted in a non-default deployment (e.g. the user interactively chose a different "
+    "entry in  the bootloader menu, or the bootloader rolled back automatically, etc.). If we are "
+    "not in a booted OSTree system, an error is returned.",
+    NULL },
+  { NULL }
+};
 static gboolean
 deployment_print_status (OstreeSysroot *sysroot, OstreeRepo *repo, OstreeDeployment *deployment,
                          gboolean is_booted, gboolean is_pending, gboolean is_rollback,
@@ -200,7 +208,15 @@ ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocat
   if (booted_deployment)
     ostree_sysroot_query_deployments_for (sysroot, NULL, &pending_deployment, &rollback_deployment);
 
-  if (deployments->len == 0)
+  if (opt_query_booted)
+    {
+      if (deployments->len == 0)
+        return glnx_throw (error, "Not in a booted OSTree system");
+
+      const gboolean is_default_booted = deployments->pdata[0] == booted_deployment;
+      g_print ("%s\n", is_default_booted ? "default" : "not-default");
+    }
+  else if (deployments->len == 0)
     {
       g_print ("No deployments.\n");
     }