Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpm-ostree kargs command #1013

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile-libpriv.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Makefile-rpm-ostree.am
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ rpm_ostree_SOURCES = src/app/main.c \
src/app/rpmostree-libbuiltin.h \
src/app/rpmostree-polkit-agent.c \
src/app/rpmostree-polkit-agent.h \
src/app/rpmostree-builtin-kargs.c \
$(NULL)

if BUILDOPT_COMPOSE_TOOLING
Expand Down
5 changes: 5 additions & 0 deletions Makefile-tests.am
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ tests_check_test_utils_CPPFLAGS = $(testbin_cppflags)
tests_check_test_utils_CFLAGS = $(testbin_cflags)
tests_check_test_utils_LDADD = $(testbin_ldadd) libtest.la

tests_check_test_kargs_CPPFLAGS = $(testbin_cppflags)
tests_check_test_kargs_CFLAGS = $(testbin_cflags)
tests_check_test_kargs_LDADD = $(testbin_ldadd) libtest.la

uninstalled_test_programs = \
tests/check/jsonutil \
tests/check/postprocess \
tests/check/test-utils \
tests/check/test-kargs \
$(NULL)

uninstalled_test_scripts = \
Expand Down
2 changes: 2 additions & 0 deletions src/app/rpmostree-builtin-ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ 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", 0,
"Query or Modify the kernel arguments", rpmostree_ex_builtin_kargs },
{ NULL, 0, NULL, NULL }
};

Expand Down
209 changes: 209 additions & 0 deletions src/app/rpmostree-builtin-kargs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2017 Red Hat Inc.
*
* 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-ex-builtins.h"
#include "rpmostree-libbuiltin.h"
#include "rpmostree-dbus-helpers.h"

#include <libglnx.h>

/* TODO uncomment this when editor functionality is done
* static gboolean opt_editor;
*/
static gboolean opt_import_proc_cmdline;
static gboolean opt_reboot;
static char **opt_kernel_delete_strings;
static char **opt_kernel_append_strings;
static char **opt_kernel_replace_strings;
static char *opt_osname;
static char *opt_deploy_index;

static GOptionEntry option_entries[] = {
{ "os", 0, 0, G_OPTION_ARG_STRING, &opt_osname, "Operation on provided OSNAME", "OSNAME" },
{ "deploy-index", 0, 0, G_OPTION_ARG_STRING, &opt_deploy_index, "Modify the kernel args from a specific deployment based on index. Index is in the form of a number (e.g 0 means the first deployment in the list)", "INDEX"},
{ "reboot", 0, 0, G_OPTION_ARG_NONE, &opt_reboot, "Initiate a reboot after kernel arguments are modified", NULL},
{ "append", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_kernel_append_strings, "Append kernel argument; useful with e.g. console= that can be used multiple times. empty value for an argument is allowed", "KEY=VALUE" },
{ "replace", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_kernel_replace_strings, "Replace existing kernel argument, the user is also able to replace an argument with KEY=VALUE if only one value exist for that argument ", "KEY=VALUE=NEWVALUE" },
{ "delete", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_kernel_delete_strings, "Delete a specific kernel argument key/val pair or an entire argument with a single key/value pair", "KEY=VALUE"},
{ "import-proc-cmdline", 0, 0, G_OPTION_ARG_NONE, &opt_import_proc_cmdline, "Instead of modifying old kernel arguments, we modify args from current /proc/cmdline (the booted deployment)", NULL },
/* TODO: Add back the editor option once we decided how to handle the similar API from ostree
* { "editor", 0, 0, G_OPTION_ARG_NONE, &opt_editor, "Pops up an editor for users to change arguments", NULL },
*/
{ NULL }
};

static GVariant *
get_kargs_option_variant (void)
{
GVariantDict dict;

g_variant_dict_init (&dict, NULL);
g_variant_dict_insert (&dict, "reboot", "b", opt_reboot);

return g_variant_dict_end (&dict);
}

int
rpmostree_ex_builtin_kargs (int argc,
char **argv,
RpmOstreeCommandInvocation *invocation,
GCancellable *cancellable,
GError **error)
{
_cleanup_peer_ GPid peer_pid = 0;
glnx_unref_object RPMOSTreeSysroot *sysroot_proxy = NULL;
g_autoptr(GOptionContext) context = g_option_context_new ("");
gboolean display_kernel_args = FALSE;
if (!rpmostree_option_context_parse (context,
option_entries,
&argc, &argv,
invocation,
cancellable,
NULL, NULL,
&sysroot_proxy,
&peer_pid,
error))
return EXIT_FAILURE;

/* TODO: uncomment these lines when finish implementing editor feature
*
* if (opt_editor && (opt_import_proc || opt_kernel_delete_strings ||
* opt_kernel_replace_strings || opt_kernel_append_strings))
* {
* We want editor command to achieve all the other functionalities
* Thus erroring out ahead of time when multiple options exist
* g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
* "Cannot specify --editor with other options");
* return EXIT_FAILURE;
* }
*/

if (opt_kernel_delete_strings && opt_kernel_replace_strings)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
"Cannot specify both --delete and --replace");
return EXIT_FAILURE;
}
if (opt_kernel_delete_strings && opt_kernel_append_strings)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
"Cannot specify both --delete and --append");
return EXIT_FAILURE;
}
if (opt_import_proc_cmdline && opt_deploy_index)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
"Canno specify both --import-from-proc-cmdline and --deployid");
return EXIT_FAILURE;
}
if (opt_import_proc_cmdline && opt_osname)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
"Cannot specify both osname and --import-from-proc-cmdline");
return EXIT_FAILURE;
}
if (!(opt_kernel_delete_strings) && !(opt_kernel_append_strings)
&& !(opt_kernel_replace_strings))
display_kernel_args = TRUE;

if (opt_reboot && display_kernel_args)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
"Cannot reboot when kernel arguments not changed");
return EXIT_FAILURE;
}

glnx_unref_object RPMOSTreeOS *os_proxy = NULL;
if (!rpmostree_load_os_proxy (sysroot_proxy, opt_osname,
cancellable, &os_proxy, error))
return EXIT_FAILURE;

/* The proc cmdline is the kernel args from booted deployment
* if this option is not specified, we will default to find the first
* pending deployment that matches the osname if there is one
*/
gboolean is_pending = !opt_import_proc_cmdline;

/* Here we still keep the index as a string,
* so that we can tell whether user has
* input a index option, then in the backend,
* we will parse the string into a number if
* the index option is there
*/
const char* deploy_index_str = opt_deploy_index ?: "";
g_autoptr(GVariant) boot_config = NULL;
if (!rpmostree_os_call_get_deployment_boot_config_sync (os_proxy,
deploy_index_str,
is_pending,
&boot_config,
cancellable,
error))
return EXIT_FAILURE;

/* We extract the existing kernel arguments from the boot configuration */
const char *existing_kernel_arg_string = NULL;
if (!g_variant_lookup (boot_config, "options",
"&s", &existing_kernel_arg_string))
return EXIT_FAILURE;

if (display_kernel_args)
{
g_print("The kernel arguments are:\n%s\n",
existing_kernel_arg_string);
return EXIT_SUCCESS;
}

g_autofree char *transaction_address = NULL;
char *empty_strv[] = {NULL};

/* dbus does not allow NULL to mean the empty string array,
* assign them to be empty string array here to prevent erroring out
*/
if (!opt_kernel_replace_strings)
opt_kernel_replace_strings = empty_strv;
if (!opt_kernel_append_strings)
opt_kernel_append_strings = empty_strv;
if (!opt_kernel_delete_strings)
opt_kernel_delete_strings = empty_strv;

/* call the generearted dbus-function */
if (!rpmostree_os_call_kernel_args_sync (os_proxy,
existing_kernel_arg_string,
(const char* const*) opt_kernel_append_strings,
(const char* const*) opt_kernel_replace_strings,
(const char* const*) opt_kernel_delete_strings,
get_kargs_option_variant (),
&transaction_address,
cancellable,
error))
return EXIT_FAILURE;

if (!rpmostree_transaction_get_response_sync (sysroot_proxy,
transaction_address,
cancellable,
error))
return EXIT_FAILURE;

g_print("Kernel arguments updated.\nRun \"systemctl reboot\" to start a reboot\n");

return EXIT_SUCCESS;
}
1 change: 1 addition & 0 deletions src/app/rpmostree-ex-builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ G_BEGIN_DECLS
BUILTINPROTO(unpack);
BUILTINPROTO(livefs);
BUILTINPROTO(override);
BUILTINPROTO(kargs);

#undef BUILTINPROTO

Expand Down
18 changes: 18 additions & 0 deletions src/daemon/org.projectatomic.rpmostree1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@
<arg type="s" name="transaction_address" direction="out"/>
</method>

<!-- Available options:
"reboot" (type 'b')
-->
<method name="KernelArgs">
<arg type="s" name="existing_kernel_arg_string"/>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this value actually used by the daemon? It looks to me like we pass it all the way down but don't actually use it, rather it's just parsed on the client side. Is that correct? If so, let's just drop this from the DBus API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the review :). We actually do use it, it is this line

__attribute__((cleanup(_ostree_kernel_args_cleanup))) OstreeKernelArgs *kargs = _ostree_kernel_args_from_string (self->existing_kernel_args);

We use this attribute so that we do not need to do the extra loading of the wanted kargs again in the transaction execute, as we have already done the loading in rpmostree_os_call_get_deployment_boot_config_sync function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right OK.

<arg type="as" name="kernel_args_added" direction="in"/>
<arg type="as" name="kernel_args_replaced" direction="in"/>
<arg type="as" name="kernel_args_removed" direction="in"/>
<arg type="a{sv}" name="options" direction="in"/>
<arg type="s" name="transaction_address" direction="out"/>
</method>

<method name="GetDeploymentBootConfig">
<arg type="s" name="deployid" />
<arg type="b" name="is_pending" direction="in"/>
<arg type="a{sv}" name="bootconfig" direction="out"/>
</method>

<method name="Cleanup">
<arg type="as" name="elements" direction="in"/>
<arg type="s" name="transaction_address" direction="out"/>
Expand Down
31 changes: 29 additions & 2 deletions src/daemon/rpmostree-sysroot-upgrader.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ struct RpmOstreeSysrootUpgrader {
gboolean layering_changed; /* Whether changes to layering should result in a new commit */
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 {
Expand Down Expand Up @@ -213,7 +215,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);

Expand Down Expand Up @@ -1029,6 +1031,31 @@ rpmostree_sysroot_upgrader_prep_layering (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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with this, but I think down the line we should add the kernel args to rpmostree_sysroot_upgrader_deploy() and have the other callers pass NULL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey thanks for the review :). For this part, I believe other callers are still able to set NULL for the kernel args, as the kargs_strv is implemented as one of the attributes from the upgrader, similar to other attributes as well.
https://github.com/projectatomic/rpm-ostree/pull/1013/files#diff-af8b24e3bf31175f007dd70d4b1a46fcR83

My personal opinion, but I could be wrong, is .. it feels a bit unnecessary to change other code when you can just set NULL to the kargs_strv attribute prior to calling rpmostree_sysroot_upgrader_deploy? ( I kinda tried to avoid changing too much existing code base too =) ). But I am also fine if you want to change the function.

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
Expand Down Expand Up @@ -1072,7 +1099,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;
Expand Down
5 changes: 5 additions & 0 deletions src/daemon/rpmostree-sysroot-upgrader.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,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
Loading