summaryrefslogtreecommitdiff
path: root/src/ostree/ot-admin-builtin-finalize-staged.c
blob: e408f44730397ae0317f20415bbfaf71cd915f4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (C) 2018 Red Hat, Inc.
 * Copyright (C) 2022 Endless OS Foundation LLC
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib-unix.h>
#include <sched.h>
#include <signal.h>
#include <stdlib.h>

#include "ot-main.h"
#include "ot-admin-builtins.h"
#include "ot-admin-functions.h"
#include "ostree.h"
#include "otutil.h"

#include "ostree-cmd-private.h"
#include "ostree.h"

static gboolean opt_hold;

static GOptionEntry options[] = {
  { "hold", 0, 0, G_OPTION_ARG_NONE, &opt_hold, "Hold /boot open during finalization", NULL },
  { NULL }
};

/* Called by ostree-finalize-staged.service, and in turn
 * invokes a cmdprivate function inside the shared library.
 */
gboolean
ot_admin_builtin_finalize_staged (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
  /* Just a sanity check; we shouldn't be called outside of the service though.
   */
  struct stat stbuf;
  if (fstatat (AT_FDCWD, OSTREE_PATH_BOOTED, &stbuf, 0) < 0)
    return TRUE;

  g_autoptr(GOptionContext) context = g_option_context_new ("");
  g_autoptr(OstreeSysroot) sysroot = NULL;

  /* First parse the args without loading the sysroot to see what options are
   * set. */
  if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
                                          OSTREE_ADMIN_BUILTIN_FLAG_NO_LOAD,
                                          invocation, &sysroot, cancellable, error))
    return FALSE;

  if (opt_hold)
    {
      /* Load the sysroot unlocked so that a separate namespace isn't
       * created. */
      if (!ostree_admin_sysroot_load (sysroot,
                                      OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER | OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED,
                                      cancellable, error))
        return FALSE;

      /* In case it's an automount, open /boot so that the automount doesn't
       * expire until before this process exits. If it did expire and got
       * unmounted, the service would be stopped and the deployment would be
       * finalized earlier than expected.
       */
      int sysroot_fd = ostree_sysroot_get_fd (sysroot);
      glnx_autofd int boot_fd = -1;
      g_debug ("Opening /boot directory");
      if (!glnx_opendirat (sysroot_fd, "boot", TRUE, &boot_fd, error))
        return FALSE;

      /* We want to keep /boot open until the deployment is finalized during
       * system shutdown, so block until we get SIGTERM which systemd will send
       * when the unit is stopped.
       */
      pause ();
    }
  else
    {
      /* Load the sysroot with the normal flags and actually finalize the
       * deployment. */
      if (!ostree_admin_sysroot_load (sysroot,
                                      OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
                                      cancellable, error))
        return FALSE;

      if (!ostree_cmd__private__()->ostree_finalize_staged (sysroot, cancellable, error))
        return FALSE;
    }

  return TRUE;
}