Skip to content

Commit

Permalink
Add support for default kargs files
Browse files Browse the repository at this point in the history
Instead of merging kargs from the previous deployment, by default
regenerate kargs from the following config files:

/etc/ostree/kargs (host config)
/usr/lib/ostree-boot/kargs (base config)

The base config is sourced from the repo at the commit (revision)
being deployed, so that newly committed kargs will take effect
immediately after upgrading. The host config is sourced from the
merged /etc configuration of the new deployment, so that host
edits as well as edits to /usr/etc/ostree/kargs are reflected.

Kernel arguments present in the base config are appended to
kernel arguments in the host config.

Using the commands that modify kargs (of the form `ostree admin
deploy --karg*`) will cause kargs in later deployments to be copied
from the merge deployment, rather than regenerate from the config
files.

Resolves: ostreedev#479
  • Loading branch information
Robert Fairley committed Apr 12, 2019
1 parent 65fe4e9 commit 5e8531f
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 16 deletions.
1 change: 1 addition & 0 deletions Makefile-tests.am
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ _installed_or_uninstalled_test_scripts = \
tests/test-admin-deploy-syslinux.sh \
tests/test-admin-deploy-2.sh \
tests/test-admin-deploy-karg.sh \
tests/test-admin-deploy-karg-default.sh \
tests/test-admin-deploy-switch.sh \
tests/test-admin-deploy-etcmerge-cornercases.sh \
tests/test-admin-deploy-uboot.sh \
Expand Down
141 changes: 125 additions & 16 deletions src/libostree/ostree-sysroot-deploy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2568,9 +2568,101 @@ get_var_dfd (OstreeSysroot *self,
return glnx_opendirat (base_dfd, base_path, TRUE, ret_fd, error);
}

static gboolean
read_file_allow_noent (OstreeSysroot *sysroot,
int dfd,
char *path,
char **contents_out,
GCancellable *cancellable,
GError **error)
{
struct stat stbuf;
if (!glnx_fstatat_allow_noent (dfd, path, &stbuf, 0, error))
return FALSE;
const gboolean file_exists = (errno == 0);

g_autofree char *ret_contents = NULL;
if (file_exists)
{
ret_contents = glnx_file_get_contents_utf8_at (dfd,
path,
NULL,
cancellable,
error);
if (!ret_contents)
return FALSE;
}

*contents_out = g_steal_pointer (&ret_contents);

return TRUE;
}

/* Get a GFile* referring to the commit object at ref for the file at path. */
static GFile*
get_file_from_repo (OstreeRepo *repo,
const char *ref,
const char *path,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GFile) root = NULL;
if (!ostree_repo_read_commit (repo, ref, &root,
NULL, cancellable, error))
return NULL;
return g_file_resolve_relative_path (root, path);
}

static gboolean
sysroot_regenerate_kargs (OstreeSysroot *self,
const char *revision,
int deployment_dfd,
char **kargs_out,
GCancellable *cancellable,
GError **error)
{
g_autoptr(GPtrArray) kargs_configs = g_ptr_array_new_with_free_func (g_free);

/* Load host kargs configuration from the merge deployment, so that any
* configuration done on the host is carried to the next deployment. */
g_autofree char *host_karg_contents = NULL;
if (!read_file_allow_noent (self, deployment_dfd,
_OSTREE_SYSROOT_KARGS_HOST,
&host_karg_contents,
cancellable, error))
return FALSE;
if (host_karg_contents)
g_ptr_array_add (kargs_configs, g_strstrip (g_steal_pointer (&host_karg_contents)));

/* Load base kargs configuration from the current commit, so that kargs will
* be written to the bootconfig as soon as the current deployment is
* finished (in install_deployment_kernel()). */
g_autofree char *base_karg_contents = NULL;
g_autoptr(GFile) base_karg_file = get_file_from_repo (ostree_sysroot_repo (self),
revision,
_OSTREE_SYSROOT_KARGS_BASE,
cancellable, error);
if (!ot_file_load_contents_allow_not_found (base_karg_file, &base_karg_contents,
cancellable, error))
return FALSE;
if (base_karg_contents)
g_ptr_array_add (kargs_configs, g_strstrip (g_steal_pointer (&base_karg_contents)));

g_autoptr(OstreeKernelArgs) kargs = _ostree_kernel_args_new ();
for (guint i = 0; i < kargs_configs->len; i++)
_ostree_kernel_args_parse_append (kargs, kargs_configs->pdata[i]);

g_autofree char *kargs_contents = _ostree_kernel_args_to_string (kargs);

*kargs_out = g_steal_pointer (&kargs_contents);

return TRUE;
}

static gboolean
sysroot_finalize_deployment (OstreeSysroot *self,
OstreeDeployment *deployment,
const char *revision,
char **override_kernel_argv,
OstreeDeployment *merge_deployment,
GCancellable *cancellable,
Expand All @@ -2581,36 +2673,52 @@ sysroot_finalize_deployment (OstreeSysroot *self,
if (!glnx_opendirat (self->sysroot_fd, deployment_path, TRUE, &deployment_dfd, error))
return FALSE;

/* If we didn't get an override in this deployment, copy kargs directly from
* the merge deployment. */
/* If we didn't get an override in this deployment, check if an override
* has been done previously. */
gboolean kargs_previously_overridden = FALSE;
if (!override_kernel_argv)
{
OstreeBootconfigParser *merge_bootconfig = NULL;
gboolean kargs_overridden = FALSE;
if (merge_deployment)
{
merge_bootconfig = ostree_deployment_get_bootconfig (merge_deployment);
OstreeBootconfigParser *merge_bootconfig = ostree_deployment_get_bootconfig (merge_deployment);
if (merge_bootconfig)
{
/* Copy kargs from the merge deployment. */
const char *opts = ostree_bootconfig_parser_get (merge_bootconfig, "options");
ostree_bootconfig_parser_set (ostree_deployment_get_bootconfig (deployment), "options", opts);
kargs_overridden = g_strcmp0 (ostree_bootconfig_parser_get (merge_bootconfig, "ostree-kargs-override"),
"true") == 0;
kargs_previously_overridden = g_strcmp0 (ostree_bootconfig_parser_get (merge_bootconfig, "ostree-kargs-override"),
"true") == 0;
if (kargs_previously_overridden)
{
/* Copy kargs from the merge deployment. */
const char *opts = ostree_bootconfig_parser_get (merge_bootconfig, "options");
ostree_bootconfig_parser_set (ostree_deployment_get_bootconfig (deployment), "options", opts);
/* Write the override flag again so it persists to the next deployment. */
ostree_bootconfig_parser_set (ostree_deployment_get_bootconfig (deployment), "ostree-kargs-override", "true");
}
}
if (kargs_overridden)
ostree_bootconfig_parser_set (ostree_deployment_get_bootconfig (deployment), "ostree-kargs-override", "true");
}
}

if (merge_deployment)
{
/* And do the /etc merge */
/* Do the /etc merge. */
if (!merge_configuration_from (self, merge_deployment, deployment, deployment_dfd,
cancellable, error))
return FALSE;
}

/* If we didn't get an override in this deployment, and an override has not
* been done before, then regenerate from the config files of this
* deployment. */
if (!override_kernel_argv && !kargs_previously_overridden)
{
/* Note this occurs after merge_configuration_from() so that
* /etc/ostree/kargs as a result of the /etc merge is used. */
g_autofree char *opts = NULL;
if (!sysroot_regenerate_kargs (self, revision, deployment_dfd, &opts,
cancellable, error))
return FALSE;
ostree_bootconfig_parser_set (ostree_deployment_get_bootconfig (deployment), "options", opts);
}

const char *osdeploypath = glnx_strjoina ("ostree/deploy/", ostree_deployment_get_osname (deployment));
glnx_autofd int os_deploy_dfd = -1;
if (!glnx_opendirat (self->sysroot_fd, osdeploypath, TRUE, &os_deploy_dfd, error))
Expand Down Expand Up @@ -2689,7 +2797,8 @@ ostree_sysroot_deploy_tree (OstreeSysroot *self,
&deployment, cancellable, error))
return FALSE;

if (!sysroot_finalize_deployment (self, deployment, override_kernel_argv,
if (!sysroot_finalize_deployment (self, deployment, revision,
override_kernel_argv,
provided_merge_deployment,
cancellable, error))
return FALSE;
Expand Down Expand Up @@ -2948,8 +3057,8 @@ _ostree_sysroot_finalize_staged (OstreeSysroot *self,
if (!glnx_unlinkat (AT_FDCWD, _OSTREE_SYSROOT_RUNSTATE_STAGED, 0, error))
return FALSE;

if (!sysroot_finalize_deployment (self, self->staged_deployment, kargs, merge_deployment,
cancellable, error))
if (!sysroot_finalize_deployment (self, self->staged_deployment, self->staged_deployment->csum,
kargs, merge_deployment, cancellable, error))
return FALSE;

/* Now, take ownership of the staged state, as normally the API below strips
Expand Down
2 changes: 2 additions & 0 deletions src/libostree/ostree-sysroot-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ struct OstreeSysroot {
#define _OSTREE_SYSROOT_RUNSTATE_STAGED "/run/ostree/staged-deployment"
#define _OSTREE_SYSROOT_DEPLOYMENT_RUNSTATE_DIR "/run/ostree/deployment-state/"
#define _OSTREE_SYSROOT_DEPLOYMENT_RUNSTATE_FLAG_DEVELOPMENT "unlocked-development"
#define _OSTREE_SYSROOT_KARGS_HOST "etc/ostree/kargs"
#define _OSTREE_SYSROOT_KARGS_BASE "usr/lib/ostree-boot/kargs"

void
_ostree_sysroot_emit_journal_msg (OstreeSysroot *self,
Expand Down
36 changes: 36 additions & 0 deletions src/libotutil/ot-gio-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,39 @@ ot_file_get_path_cached (GFile *file)

return path;
}

/**
* ot_file_load_contents_allow_not_found:
*
* Load the contents of file, allowing G_IO_ERROR_NOT_FOUND.
* If file exists, return the contents in out_contents (otherwise out_contents
* holds NULL).
*
* Return FALSE for any other error.
*/
gboolean
ot_file_load_contents_allow_not_found (GFile *file,
char **out_contents,
GCancellable *cancellable,
GError **error)
{
g_return_val_if_fail (file != NULL, FALSE);

GError *local_error = NULL;
g_autofree char *ret_contents = NULL;
if (!g_file_load_contents (file, cancellable, &ret_contents, NULL, NULL, &local_error))
{
if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
{
g_clear_error (&local_error);
}
else
{
g_propagate_error (error, local_error);
return FALSE;
}
}

ot_transfer_out_value (out_contents, &ret_contents);
return TRUE;
}
6 changes: 6 additions & 0 deletions src/libotutil/ot-gio-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ gs_file_get_path_cached (GFile *file)
return ot_file_get_path_cached (file);
}

gboolean
ot_file_load_contents_allow_not_found (GFile *file,
char **out_contents,
GCancellable *cancellable,
GError **error);

G_END_DECLS
49 changes: 49 additions & 0 deletions tests/libtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,55 @@ os_repository_new_commit ()
cd ${test_tmpdir}
}

os_repository_commit ()
{
repo=${1:-testos-repo}
boot_checksum_iteration=${2:-0}
content_iteration=${3:-0}
branch=${4:-testos/buildmaster/x86_64-runtime}
export version=${5:-$(date "+%Y%m%d.${content_iteration}")}
echo "BOOT ITERATION: $boot_checksum_iteration"
cd ${test_tmpdir}/osdata
kver=3.6.0
if test -f usr/lib/modules/${kver}/vmlinuz; then
bootdir=usr/lib/modules/${kver}
else
if test -d usr/lib/ostree-boot; then
bootdir=usr/lib/ostree-boot
else
bootdir=boot
fi
fi
rm ${bootdir}/*
kernel_path=${bootdir}/vmlinuz
initramfs_path=${bootdir}/initramfs.img
if [[ $bootdir != usr/lib/modules/* ]]; then
kernel_path=${kernel_path}-${kver}
initramfs_path=${bootdir}/initramfs-${kver}.img
fi
echo "new: a kernel ${boot_checksum_iteration}" > ${kernel_path}
echo "new: an initramfs ${boot_checksum_iteration}" > ${initramfs_path}
bootcsum=$(cat ${kernel_path} ${initramfs_path} | sha256sum | cut -f 1 -d ' ')
export bootcsum
if [[ $bootdir != usr/lib/modules/* ]]; then
mv ${kernel_path}{,-${bootcsum}}
mv ${initramfs_path}{,-${bootcsum}}
fi

commithash=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/${repo} commit --add-metadata-string "version=${version}" -b $branch -s "Build")
export commithash
cd ${test_tmpdir}
}

os_tree_write_file ()
{
path=${1}
contents="${2}"
cd ${test_tmpdir}/osdata
echo "${contents}" > ${path}
cd ${test_tmpdir}
}

_have_user_xattrs=''
have_user_xattrs() {
assert_has_setfattr
Expand Down
Loading

0 comments on commit 5e8531f

Please sign in to comment.