daemon: add handlation for kargs append

API functions from ostree-kernel-args.c
are copied to libpriv. The append functionality
reuses  _ostree_kernel_args_append_argv() for
collecting added kernel arguments.

Also added handlation in rpm-ostree upgrader
to allow deployments happen with kernel arguments.

Now, the user is able to add kernel arguments via
'rpm-ostree ex kargs --append key=value'
or 'rpm-ostree ex kargs --append key' if they
want to have an empty value with key.

The user is also able to display the current
kernel arguments via 'rpm-ostree ex kargs'

In addition, this functionality will create a pending deployment,
and will update the conf file in /boot/loader/entries/ostree-$osname-0.conf upon success.

Closes: #1013
Approved by: cgwalters
This commit is contained in:
Ruixin Bao 2017-09-30 00:07:08 +00:00 committed by Atomic Bot
parent fa593afd89
commit 50863c370d
8 changed files with 503 additions and 8 deletions

View File

@ -47,6 +47,8 @@ librpmostreepriv_la_SOURCES = \
src/libpriv/rpmostree-unpacker.h \
src/libpriv/rpmostree-output.c \
src/libpriv/rpmostree-output.h \
src/libpriv/rpmostree-kargs-process.c \
src/libpriv/rpmostree-kargs-process.h \
src/libpriv/libsd-locale-util.c \
src/libpriv/libsd-locale-util.h \
$(NULL)

View File

@ -32,7 +32,7 @@ static RpmOstreeCommand ex_subcommands[] = {
"unpack RPM into local OSTree repo", rpmostree_ex_builtin_unpack },
{ "container", RPM_OSTREE_BUILTIN_FLAG_LOCAL_CMD,
"Assemble local unprivileged containers", rpmostree_builtin_container },
{ "kargs", RPM_OSTREE_BUILTIN_FLAG_REQUIRES_ROOT,
{ "kargs", 0,
"Query or Modify the kernel arguments", rpmostree_ex_builtin_kargs },
{ NULL, 0, NULL, NULL }
};

View File

@ -80,6 +80,8 @@ struct RpmOstreeSysrootUpgrader {
gboolean pkgs_imported; /* Whether pkgs to be layered have been downloaded & imported */
char *base_revision; /* Non-layered replicated commit */
char *final_revision; /* Computed by layering; if NULL, only using base_revision */
char **kargs_strv; /* Kernel argument list to be written into deployment */
};
enum {
@ -214,7 +216,7 @@ rpmostree_sysroot_upgrader_finalize (GObject *object)
g_clear_pointer (&self->origin, (GDestroyNotify)rpmostree_origin_unref);
g_free (self->base_revision);
g_free (self->final_revision);
g_strfreev (self->kargs_strv);
g_clear_pointer (&self->overlay_packages, (GDestroyNotify)g_ptr_array_unref);
g_clear_pointer (&self->override_remove_packages, (GDestroyNotify)g_ptr_array_unref);
@ -1055,6 +1057,31 @@ rpmostree_sysroot_upgrader_import_pkgs (RpmOstreeSysrootUpgrader *self,
return TRUE;
}
/**
* rpmostree_sysroot_upgrader_deploy_set_kargs:
* @self: Self
* @kernel_args: A list of strings representing kernel_arguments
* @cancellable: Cancellable
* @error: Error
*
* A wrapper function that collects the kernel arguments then call the rpmostree_sysroot_upgrader_deploy
* to write a pending deployment to the disk
*
* Returns: FALSE if rpmostree_sysroot_upgrader_deploy fails
*
*/
gboolean
rpmostree_sysroot_upgrader_deploy_set_kargs (RpmOstreeSysrootUpgrader *self,
char **kernel_args,
GCancellable *cancellable,
GError **error)
{
/* Set the attribute kargs directly, not sure whether or not to have a setter */
/* Because .. currently pretty much here is the only place that we set it */
self->kargs_strv = g_strdupv (kernel_args);
return rpmostree_sysroot_upgrader_deploy (self, cancellable, error);
}
/**
* rpmostree_sysroot_upgrader_deploy:
* @self: Self
@ -1104,7 +1131,7 @@ rpmostree_sysroot_upgrader_deploy (RpmOstreeSysrootUpgrader *self,
if (!ostree_sysroot_deploy_tree (self->sysroot, self->osname,
target_revision, origin,
self->cfg_merge_deployment,
NULL,
self->kargs_strv,
&new_deployment,
cancellable, error))
return FALSE;

View File

@ -124,4 +124,9 @@ rpmostree_sysroot_upgrader_deploy (RpmOstreeSysrootUpgrader *self,
GCancellable *cancellable,
GError **error);
gboolean
rpmostree_sysroot_upgrader_deploy_set_kargs (RpmOstreeSysrootUpgrader *self,
char **kernel_args,
GCancellable *cancellable,
GError **error);
G_END_DECLS

View File

@ -76,7 +76,7 @@ sysroot_changed (RpmostreedSysroot *sysroot,
if (!rpmostreed_os_load_internals (self, error))
goto out;
out:
if (local_error)
g_warning ("%s", local_error->message);
@ -123,7 +123,8 @@ os_authorize_method (GDBusInterfaceSkeleton *interface,
{
g_ptr_array_add (actions, "org.projectatomic.rpmostree1.rebase");
}
else if (g_strcmp0 (method_name, "SetInitramfsState") == 0)
else if (g_strcmp0 (method_name, "SetInitramfsState") == 0 ||
g_strcmp0 (method_name, "KernelArgs") == 0)
{
g_ptr_array_add (actions, "org.projectatomic.rpmostree1.bootconfig");
}
@ -1434,7 +1435,7 @@ os_handle_get_cached_deploy_rpm_diff (RPMOSTreeOS *interface,
origin = rpmostree_origin_parse_deployment (base_deployment, error);
if (!origin)
goto out;
base_checksum = ostree_deployment_get_csum (base_deployment);
if (!rpmostreed_parse_revision (arg_revision,
@ -1695,7 +1696,7 @@ rpmostreed_os_new (OstreeSysroot *sysroot,
if (!rpmostreed_os_load_internals (obj, &local_error))
g_warning ("%s", local_error->message);
}
rpmostreed_daemon_publish (rpmostreed_daemon_get (), path, FALSE, obj);
return RPMOSTREE_OS (obj);

View File

@ -33,6 +33,7 @@
#include "rpmostree-core.h"
#include "rpmostree-unpacker.h"
#include "rpmostreed-utils.h"
#include "rpmostree-kargs-process.h"
static gboolean
change_origin_refspec (OstreeSysroot *sysroot,
@ -1453,9 +1454,77 @@ kernel_arg_transaction_execute (RpmostreedTransaction *transaction,
GCancellable *cancellable,
GError **error)
{
KernelArgTransaction *self = (KernelArgTransaction *) transaction;
OstreeSysroot *sysroot = rpmostreed_transaction_get_sysroot (transaction);
/* Set this boolean to have a default value */
gboolean import_from_cmd = FALSE;
return TRUE;
if (self->flags & RPMOSTREE_TRANSACTION_KERNEL_ARG_FLAG_IMPORT_CMD)
import_from_cmd = TRUE;
__attribute__((cleanup(_ostree_kernel_args_cleanup))) OstreeKernelArgs *kargs = _ostree_kernel_args_new ();
g_autoptr(RpmOstreeSysrootUpgrader) upgrader = rpmostree_sysroot_upgrader_new (sysroot, self->osname, 0,
cancellable, error);
/* We need the upgrader to perform the deployment */
if (upgrader == NULL)
return FALSE;
/* Will first merge existing Kernel Arguments from
* either two places, one from old deployment,
* or the other from the current kernel arguments */
if (import_from_cmd)
{
if (!_ostree_kernel_args_append_proc_cmdline (kargs, cancellable, error))
return FALSE;
}
else
{
/* We will default to merge if import option is not specified */
OstreeDeployment *default_deployment = rpmostree_sysroot_upgrader_get_merge_deployment (upgrader);
OstreeBootconfigParser *bootconfig = ostree_deployment_get_bootconfig (default_deployment);
g_auto(GStrv) previous_args = g_strsplit (ostree_bootconfig_parser_get (bootconfig, "options"), " ", -1);
_ostree_kernel_args_append_argv (kargs, previous_args);
}
/* When none of the arguments are present, we print out the merged/current kernel arguments and leave */
if (!(self->kernel_args_added) && !(self->kernel_args_deleted)
&& !(self->kernel_args_replaced))
{
g_autofree gchar* kargs_str = _ostree_kernel_args_to_string (kargs);
rpmostreed_transaction_emit_message_printf(transaction,
"The kernel arguments are:\n %s",
kargs_str);
return TRUE;
}
if (self->kernel_args_deleted)
{
/* Delete all the entries included in the kernel args */
for (char **iter = self->kernel_args_deleted; iter && *iter; iter++)
{
const char* arg = *iter;
if (!_ostree_kernel_args_delete(kargs, arg, error))
return FALSE;
}
}
else
{
if (self->kernel_args_replaced)
{
/* Waiting to be implemented */
}
if (self->kernel_args_added)
_ostree_kernel_args_append_argv (kargs, self->kernel_args_added);
}
/* After all the arguments are processed earlier, we convert it to a string list*/
g_auto(GStrv) kargs_strv = _ostree_kernel_args_to_strv (kargs);
return rpmostree_sysroot_upgrader_deploy_set_kargs (upgrader, kargs_strv,
cancellable, error);
}
static void

View File

@ -0,0 +1,331 @@
/*
* Copyright (C) 2013,2014 Colin Walters <walters@verbum.org>
*
* This program 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 licence 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, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "rpmostree-kargs-process.h"
#include "libglnx.h"
#include <string.h>
struct _OstreeKernelArgs {
GPtrArray *order;
GHashTable *table;
};
static char *
split_keyeq (char *arg)
{
char *eq;
eq = strchr (arg, '=');
if (eq)
{
/* Note key/val are in one malloc block,
* so we don't free val...
*/
*eq = '\0';
return eq+1;
}
else
{
/* ...and this allows us to insert a constant
* string.
*/
return "";
}
}
static gboolean
_arg_has_prefix (const char *arg,
char **prefixes)
{
char **strviter;
for (strviter = prefixes; strviter && *strviter; strviter++)
{
const char *prefix = *strviter;
if (g_str_has_prefix (arg, prefix))
return TRUE;
}
return FALSE;
}
OstreeKernelArgs *
_ostree_kernel_args_new (void)
{
OstreeKernelArgs *ret;
ret = g_new0 (OstreeKernelArgs, 1);
ret->order = g_ptr_array_new_with_free_func (g_free);
ret->table = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, (GDestroyNotify)g_ptr_array_unref);
return ret;
}
static void
_ostree_kernel_arg_autofree (OstreeKernelArgs *kargs)
{
if (!kargs)
return;
g_ptr_array_unref (kargs->order);
g_hash_table_unref (kargs->table);
g_free (kargs);
}
void
_ostree_kernel_args_cleanup (void *loc)
{
_ostree_kernel_arg_autofree (*((OstreeKernelArgs**)loc));
}
void
_ostree_kernel_args_replace_take (OstreeKernelArgs *kargs,
char *arg)
{
gboolean existed;
GPtrArray *values = g_ptr_array_new_with_free_func (g_free);
const char *value = split_keyeq (arg);
gpointer old_key;
g_ptr_array_add (values, g_strdup (value));
existed = g_hash_table_lookup_extended (kargs->table, arg, &old_key, NULL);
if (existed)
{
g_hash_table_replace (kargs->table, old_key, values);
g_free (arg);
}
else
{
g_ptr_array_add (kargs->order, arg);
g_hash_table_replace (kargs->table, arg, values);
}
}
void
_ostree_kernel_args_replace (OstreeKernelArgs *kargs,
const char *arg)
{
_ostree_kernel_args_replace_take (kargs, g_strdup (arg));
}
void
_ostree_kernel_args_append (OstreeKernelArgs *kargs,
const char *arg)
{
gboolean existed = TRUE;
GPtrArray *values;
char *duped = g_strdup (arg);
const char *val = split_keyeq (duped);
values = g_hash_table_lookup (kargs->table, duped);
if (!values)
{
values = g_ptr_array_new_with_free_func (g_free);
existed = FALSE;
}
g_ptr_array_add (values, g_strdup (val));
if (!existed)
{
g_hash_table_replace (kargs->table, duped, values);
g_ptr_array_add (kargs->order, duped);
}
else
{
g_free (duped);
}
}
void
_ostree_kernel_args_replace_argv (OstreeKernelArgs *kargs,
char **argv)
{
char **strviter;
for (strviter = argv; strviter && *strviter; strviter++)
{
const char *arg = *strviter;
_ostree_kernel_args_replace (kargs, arg);
}
}
void
_ostree_kernel_args_append_argv_filtered (OstreeKernelArgs *kargs,
char **argv,
char **prefixes)
{
char **strviter;
for (strviter = argv; strviter && *strviter; strviter++)
{
const char *arg = *strviter;
if (!_arg_has_prefix (arg, prefixes))
_ostree_kernel_args_append (kargs, arg);
}
}
void
_ostree_kernel_args_append_argv (OstreeKernelArgs *kargs,
char **argv)
{
_ostree_kernel_args_append_argv_filtered (kargs, argv, NULL);
}
gboolean
_ostree_kernel_args_append_proc_cmdline (OstreeKernelArgs *kargs,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GFile) proc_cmdline_path = g_file_new_for_path ("/proc/cmdline");
g_autofree char *proc_cmdline = NULL;
gsize proc_cmdline_len = 0;
g_auto(GStrv) proc_cmdline_args = NULL;
/* When updating the filter list don't forget to update the list in the tests
* e.g. tests/test-admin-deploy-karg.sh and
* tests/test-admin-instutil-set-kargs.sh
*/
char *filtered_prefixes[] = { "BOOT_IMAGE=", /* GRUB 2 */
"initrd=", /* sd-boot */
NULL };
if (!g_file_load_contents (proc_cmdline_path, cancellable,
&proc_cmdline, &proc_cmdline_len,
NULL, error))
return FALSE;
g_strchomp (proc_cmdline);
proc_cmdline_args = g_strsplit (proc_cmdline, " ", -1);
_ostree_kernel_args_append_argv_filtered (kargs, proc_cmdline_args,
filtered_prefixes);
return TRUE;
}
void
_ostree_kernel_args_parse_append (OstreeKernelArgs *kargs,
const char *options)
{
char **args = NULL;
char **iter;
if (!options)
return;
args = g_strsplit (options, " ", -1);
for (iter = args; *iter; iter++)
{
char *arg = *iter;
_ostree_kernel_args_append (kargs, arg);
}
g_strfreev (args);
}
OstreeKernelArgs *
_ostree_kernel_args_from_string (const char *options)
{
OstreeKernelArgs *ret;
ret = _ostree_kernel_args_new ();
_ostree_kernel_args_parse_append (ret, options);
return ret;
}
char **
_ostree_kernel_args_to_strv (OstreeKernelArgs *kargs)
{
GPtrArray *strv = g_ptr_array_new ();
guint i;
for (i = 0; i < kargs->order->len; i++)
{
const char *key = kargs->order->pdata[i];
GPtrArray *values = g_hash_table_lookup (kargs->table, key);
guint j;
g_assert (values != NULL);
for (j = 0; j < values->len; j++)
{
const char *value = values->pdata[j];
g_ptr_array_add (strv, g_strconcat (key, "=", value, NULL));
}
}
g_ptr_array_add (strv, NULL);
return (char**)g_ptr_array_free (strv, FALSE);
}
char *
_ostree_kernel_args_to_string (OstreeKernelArgs *kargs)
{
GString *buf = g_string_new ("");
gboolean first = TRUE;
guint i;
for (i = 0; i < kargs->order->len; i++)
{
const char *key = kargs->order->pdata[i];
GPtrArray *values = g_hash_table_lookup (kargs->table, key);
guint j;
g_assert (values != NULL);
for (j = 0; j < values->len; j++)
{
const char *value = values->pdata[j];
if (first)
first = FALSE;
else
g_string_append_c (buf, ' ');
if (value && *value)
{
g_string_append (buf, key);
g_string_append_c (buf, '=');
g_string_append (buf, value);
}
else
g_string_append (buf, key);
}
}
return g_string_free (buf, FALSE);
}
const char *
_ostree_kernel_args_get_last_value (OstreeKernelArgs *kargs, const char *key)
{
GPtrArray *values = g_hash_table_lookup (kargs->table, key);
if (!values)
return NULL;
g_assert (values->len > 0);
return (char*)values->pdata[values->len-1];
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (C) 2013,2014 Colin Walters <walters@verbum.org>
*
* This program 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 licence 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, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*/
#pragma once
#include <gio/gio.h>
G_BEGIN_DECLS
typedef struct _OstreeKernelArgs OstreeKernelArgs;
OstreeKernelArgs *_ostree_kernel_args_new (void);
void _ostree_kernel_args_free (OstreeKernelArgs *kargs);
void _ostree_kernel_args_cleanup (void *loc);
void _ostree_kernel_args_replace_take (OstreeKernelArgs *kargs,
char *key);
void _ostree_kernel_args_replace (OstreeKernelArgs *kargs,
const char *key);
void _ostree_kernel_args_replace_argv (OstreeKernelArgs *kargs,
char **argv);
void _ostree_kernel_args_append (OstreeKernelArgs *kargs,
const char *key);
void _ostree_kernel_args_append_argv (OstreeKernelArgs *kargs,
char **argv);
void _ostree_kernel_args_append_argv_filtered (OstreeKernelArgs *kargs,
char **argv,
char **prefixes);
gboolean _ostree_kernel_args_append_proc_cmdline (OstreeKernelArgs *kargs,
GCancellable *cancellable,
GError **error);
void _ostree_kernel_args_parse_append (OstreeKernelArgs *kargs,
const char *options);
const char *_ostree_kernel_args_get_last_value (OstreeKernelArgs *kargs, const char *key);
OstreeKernelArgs * _ostree_kernel_args_from_string (const char *options);
char ** _ostree_kernel_args_to_strv (OstreeKernelArgs *kargs);
char * _ostree_kernel_args_to_string (OstreeKernelArgs *kargs);
G_END_DECLS