Blame src/ostree/ot-admin-builtin-deploy.c

rpm-build 0fba15
/*
rpm-build 0fba15
 * Copyright (C) 2012,2013 Colin Walters <walters@verbum.org>
rpm-build 0fba15
 *
rpm-build 0fba15
 * SPDX-License-Identifier: LGPL-2.0+
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is free software; you can redistribute it and/or
rpm-build 0fba15
 * modify it under the terms of the GNU Lesser General Public
rpm-build 0fba15
 * License as published by the Free Software Foundation; either
rpm-build 0fba15
 * version 2 of the License, or (at your option) any later version.
rpm-build 0fba15
 *
rpm-build 0fba15
 * This library is distributed in the hope that it will be useful,
rpm-build 0fba15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0fba15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0fba15
 * Lesser General Public License for more details.
rpm-build 0fba15
 *
rpm-build 0fba15
 * You should have received a copy of the GNU Lesser General Public
rpm-build 0fba15
 * License along with this library; if not, write to the
rpm-build 0fba15
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build 0fba15
 * Boston, MA 02111-1307, USA.
rpm-build 0fba15
 *
rpm-build 0fba15
 * Author: Colin Walters <walters@verbum.org>
rpm-build 0fba15
 */
rpm-build 0fba15
rpm-build 0fba15
#include "config.h"
rpm-build 0fba15
rpm-build 0fba15
#include "ot-main.h"
rpm-build 0fba15
#include "ot-admin-builtins.h"
rpm-build 0fba15
#include "ot-admin-functions.h"
rpm-build 0fba15
#include "ostree.h"
rpm-build 0fba15
#include "otutil.h"
rpm-build 0fba15
rpm-build 0fba15
#include <glib/gi18n.h>
rpm-build 0fba15
rpm-build 0fba15
static gboolean opt_retain;
rpm-build 0fba15
static gboolean opt_stage;
rpm-build 0fba15
static gboolean opt_retain_pending;
rpm-build 0fba15
static gboolean opt_retain_rollback;
rpm-build 0fba15
static gboolean opt_not_as_default;
rpm-build 0fba15
static gboolean opt_no_prune;
rpm-build 0fba15
static gboolean opt_no_merge;
rpm-build 0fba15
static char **opt_kernel_argv;
rpm-build 0fba15
static char **opt_kernel_argv_append;
rpm-build 0fba15
static gboolean opt_kernel_proc_cmdline;
rpm-build 0fba15
static char *opt_osname;
rpm-build 0fba15
static char *opt_origin_path;
rpm-build 0fba15
static gboolean opt_kernel_arg_none;
rpm-build 0fba15
rpm-build 0fba15
static GOptionEntry options[] = {
rpm-build 0fba15
  { "os", 0, 0, G_OPTION_ARG_STRING, &opt_osname, "Use a different operating system root than the current one", "OSNAME" },
rpm-build 0fba15
  { "origin-file", 0, 0, G_OPTION_ARG_FILENAME, &opt_origin_path, "Specify origin file", "FILENAME" },
rpm-build 0fba15
  { "no-prune", 0, 0, G_OPTION_ARG_NONE, &opt_no_prune, "Don't prune the repo when done", NULL},
rpm-build 0fba15
  { "no-merge", 0, 0, G_OPTION_ARG_NONE, &opt_no_merge, "Do not apply configuration (/etc and kernel arguments) from booted deployment", NULL},
rpm-build 0fba15
  { "retain", 0, 0, G_OPTION_ARG_NONE, &opt_retain, "Do not delete previous deployments", NULL },
rpm-build 0fba15
  { "stage", 0, 0, G_OPTION_ARG_NONE, &opt_stage, "Complete deployment at OS shutdown", NULL },
rpm-build 0fba15
  { "retain-pending", 0, 0, G_OPTION_ARG_NONE, &opt_retain_pending, "Do not delete pending deployments", NULL },
rpm-build 0fba15
  { "retain-rollback", 0, 0, G_OPTION_ARG_NONE, &opt_retain_rollback, "Do not delete rollback deployments", NULL },
rpm-build 0fba15
  { "not-as-default", 0, 0, G_OPTION_ARG_NONE, &opt_not_as_default, "Append rather than prepend new deployment", NULL },
rpm-build 0fba15
  { "karg-proc-cmdline", 0, 0, G_OPTION_ARG_NONE, &opt_kernel_proc_cmdline, "Import current /proc/cmdline", NULL },
rpm-build 0fba15
  { "karg", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_kernel_argv, "Set kernel argument, like root=/dev/sda1; this overrides any earlier argument with the same name", "NAME=VALUE" },
rpm-build 0fba15
  { "karg-append", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_kernel_argv_append, "Append kernel argument; useful with e.g. console= that can be used multiple times", "NAME=VALUE" },
rpm-build 0fba15
  { "karg-none", 0, 0, G_OPTION_ARG_NONE, &opt_kernel_arg_none, "Do not import kernel arguments", NULL },
rpm-build 0fba15
  { NULL }
rpm-build 0fba15
};
rpm-build 0fba15
rpm-build 0fba15
gboolean
rpm-build 0fba15
ot_admin_builtin_deploy (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
rpm-build 0fba15
{
rpm-build 0fba15
  g_autoptr(GOptionContext) context =
rpm-build 0fba15
    g_option_context_new ("REFSPEC");
rpm-build 0fba15
rpm-build 0fba15
  g_autoptr(OstreeSysroot) sysroot = NULL;
rpm-build 0fba15
  if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
rpm-build 0fba15
                                          OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
rpm-build 0fba15
                                          invocation, &sysroot, cancellable, error))
rpm-build 0fba15
    return FALSE;
rpm-build 0fba15
rpm-build 0fba15
  if (argc < 2)
rpm-build 0fba15
    {
rpm-build 0fba15
      ot_util_usage_error (context, "REF/REV must be specified", error);
rpm-build 0fba15
      return FALSE;
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  if (opt_kernel_proc_cmdline && opt_kernel_arg_none)
rpm-build 0fba15
  {
rpm-build 0fba15
    ot_util_usage_error (context, "Can't specify both --karg-proc-cmdline and --karg-none", error);
rpm-build 0fba15
    return FALSE;
rpm-build 0fba15
  }
rpm-build 0fba15
rpm-build 0fba15
  const char *refspec = argv[1];
rpm-build 0fba15
rpm-build 0fba15
  OstreeRepo *repo = ostree_sysroot_repo (sysroot);
rpm-build 0fba15
rpm-build 0fba15
  /* Find the currently booted deployment, if any; we will ensure it
rpm-build 0fba15
   * is present in the new deployment list.
rpm-build 0fba15
   */
rpm-build 0fba15
  if (!ot_admin_require_booted_deployment_or_osname (sysroot, opt_osname,
rpm-build 0fba15
                                                     cancellable, error))
rpm-build 0fba15
    return glnx_prefix_error (error, "Looking for booted deployment");
rpm-build 0fba15
rpm-build 0fba15
  g_autoptr(GKeyFile) origin = NULL;
rpm-build 0fba15
  if (opt_origin_path)
rpm-build 0fba15
    {
rpm-build 0fba15
      origin = g_key_file_new ();
rpm-build 0fba15
rpm-build 0fba15
      if (!g_key_file_load_from_file (origin, opt_origin_path, 0, error))
rpm-build 0fba15
        return FALSE;
rpm-build 0fba15
    }
rpm-build 0fba15
  else
rpm-build 0fba15
    {
rpm-build 0fba15
      origin = ostree_sysroot_origin_new_from_refspec (sysroot, refspec);
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  g_autofree char *revision = NULL;
rpm-build 0fba15
  if (!ostree_repo_resolve_rev (repo, refspec, FALSE, &revision, error))
rpm-build 0fba15
    return FALSE;
rpm-build 0fba15
rpm-build 0fba15
  g_autoptr(OstreeDeployment) merge_deployment =
rpm-build 0fba15
    opt_no_merge ? NULL : ostree_sysroot_get_merge_deployment (sysroot, opt_osname);
rpm-build 0fba15
rpm-build 0fba15
  /* Here we perform cleanup of any leftover data from previous
rpm-build 0fba15
   * partial failures.  This avoids having to call
rpm-build 0fba15
   * glnx_shutil_rm_rf_at() at random points throughout the process.
rpm-build 0fba15
   *
rpm-build 0fba15
   * TODO: Add /ostree/transaction file, and only do this cleanup if
rpm-build 0fba15
   * we find it.
rpm-build 0fba15
   */
rpm-build 0fba15
  if (!ostree_sysroot_prepare_cleanup (sysroot, cancellable, error))
rpm-build 0fba15
    return glnx_prefix_error (error, "Performing initial cleanup");
rpm-build 0fba15
rpm-build 0fba15
  /* Initial set of kernel arguments; the default is to use the merge
rpm-build 0fba15
   * deployment, unless --karg-none or --karg-proc-cmdline are specified.
rpm-build 0fba15
   */
rpm-build 0fba15
  g_autoptr(OstreeKernelArgs) kargs = NULL;
rpm-build 0fba15
  if (opt_kernel_arg_none)
rpm-build 0fba15
    {
rpm-build 0fba15
      kargs = ostree_kernel_args_new ();
rpm-build 0fba15
    }
rpm-build 0fba15
  else if (opt_kernel_proc_cmdline)
rpm-build 0fba15
    {
rpm-build 0fba15
      kargs = ostree_kernel_args_new ();
rpm-build 0fba15
      if (!ostree_kernel_args_append_proc_cmdline (kargs, cancellable, error))
rpm-build 0fba15
        return FALSE;
rpm-build 0fba15
    }
rpm-build 0fba15
  else if (merge_deployment && (opt_kernel_argv || opt_kernel_argv_append))
rpm-build 0fba15
    {
rpm-build 0fba15
      OstreeBootconfigParser *bootconfig = ostree_deployment_get_bootconfig (merge_deployment);
rpm-build 0fba15
      g_auto(GStrv) previous_args = g_strsplit (ostree_bootconfig_parser_get (bootconfig, "options"), " ", -1);
rpm-build 0fba15
      kargs = ostree_kernel_args_new ();
rpm-build 0fba15
      ostree_kernel_args_append_argv (kargs, previous_args);
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  /* Now replace/extend the above set.  Note that if no options are specified,
rpm-build 0fba15
   * we should end up passing NULL as override_kernel_argv for
rpm-build 0fba15
   * ostree_sysroot_deploy_tree() so we get the defaults.
rpm-build 0fba15
   */
rpm-build 0fba15
  if (opt_kernel_argv)
rpm-build 0fba15
    {
rpm-build 0fba15
      if (!kargs)
rpm-build 0fba15
        kargs = ostree_kernel_args_new ();
rpm-build 0fba15
      ostree_kernel_args_replace_argv (kargs, opt_kernel_argv);
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  if (opt_kernel_argv_append)
rpm-build 0fba15
    {
rpm-build 0fba15
      if (!kargs)
rpm-build 0fba15
        kargs = ostree_kernel_args_new ();
rpm-build 0fba15
      ostree_kernel_args_append_argv (kargs, opt_kernel_argv_append);
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  g_autoptr(OstreeDeployment) new_deployment = NULL;
rpm-build 0fba15
  g_auto(GStrv) kargs_strv = kargs ? ostree_kernel_args_to_strv (kargs) : NULL;
rpm-build 0fba15
  if (opt_stage)
rpm-build 0fba15
    {
rpm-build 0fba15
      if (opt_retain_pending || opt_retain_rollback)
rpm-build 0fba15
        return glnx_throw (error, "--stage cannot currently be combined with --retain arguments");
rpm-build 0fba15
      if (opt_not_as_default)
rpm-build 0fba15
        return glnx_throw (error, "--stage cannot currently be combined with --not-as-default");
rpm-build 0fba15
      if (!ostree_sysroot_stage_tree (sysroot, opt_osname, revision, origin, merge_deployment,
rpm-build 0fba15
                                      kargs_strv, &new_deployment, cancellable, error))
rpm-build 0fba15
        return FALSE;
rpm-build 0fba15
      g_assert (new_deployment);
rpm-build 0fba15
    }
rpm-build 0fba15
  else
rpm-build 0fba15
    {
rpm-build 0fba15
      if (!ostree_sysroot_deploy_tree (sysroot, opt_osname, revision, origin, merge_deployment,
rpm-build 0fba15
                                       kargs_strv, &new_deployment, cancellable, error))
rpm-build 0fba15
        return FALSE;
rpm-build 0fba15
      g_assert (new_deployment);
rpm-build 0fba15
rpm-build 0fba15
      OstreeSysrootSimpleWriteDeploymentFlags flags = OSTREE_SYSROOT_SIMPLE_WRITE_DEPLOYMENT_FLAGS_NO_CLEAN;
rpm-build 0fba15
      if (opt_retain)
rpm-build 0fba15
        flags |= OSTREE_SYSROOT_SIMPLE_WRITE_DEPLOYMENT_FLAGS_RETAIN;
rpm-build 0fba15
      else
rpm-build 0fba15
        {
rpm-build 0fba15
          if (opt_retain_pending)
rpm-build 0fba15
            flags |= OSTREE_SYSROOT_SIMPLE_WRITE_DEPLOYMENT_FLAGS_RETAIN_PENDING;
rpm-build 0fba15
          if (opt_retain_rollback)
rpm-build 0fba15
            flags |= OSTREE_SYSROOT_SIMPLE_WRITE_DEPLOYMENT_FLAGS_RETAIN_ROLLBACK;
rpm-build 0fba15
        }
rpm-build 0fba15
rpm-build 0fba15
      if (opt_not_as_default)
rpm-build 0fba15
        flags |= OSTREE_SYSROOT_SIMPLE_WRITE_DEPLOYMENT_FLAGS_NOT_DEFAULT;
rpm-build 0fba15
rpm-build 0fba15
      if (!ostree_sysroot_simple_write_deployment (sysroot, opt_osname, new_deployment,
rpm-build 0fba15
                                                   merge_deployment, flags, cancellable, error))
rpm-build 0fba15
        return FALSE;
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  /* And finally, cleanup of any leftover data.  In stage mode, we
rpm-build 0fba15
   * don't do a full cleanup as we didn't touch the bootloader.
rpm-build 0fba15
   */
rpm-build 0fba15
  if (opt_no_prune || opt_stage)
rpm-build 0fba15
    {
rpm-build 0fba15
      if (!ostree_sysroot_prepare_cleanup (sysroot, cancellable, error))
rpm-build 0fba15
        return FALSE;
rpm-build 0fba15
    }
rpm-build 0fba15
  else
rpm-build 0fba15
    {
rpm-build 0fba15
      if (!ostree_sysroot_cleanup (sysroot, cancellable, error))
rpm-build 0fba15
        return FALSE;
rpm-build 0fba15
    }
rpm-build 0fba15
rpm-build 0fba15
  return TRUE;
rpm-build 0fba15
}