Skip to content

Commit

Permalink
Support kargs.d directories for default kargs
Browse files Browse the repository at this point in the history
Add the following config directories for setting default kernel
arguments:

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

These directories contain files whose contents consist of a karg
snippet, which is a collection kernel parameter keys and values.
Example of a snippet:

```
KEY=VALUE ANOTHERKEY SAMEKEY=VALUE2 FIRSTKEY=1 SAMEKEY=VALUE3 SECONDKEY=2 KEY= KEY.KEY-KEY_KEY="some space-separated values"
```

The bootconfig key `ostree-kargs-generated-from-config` indicates
whether the kargs were generated by ostree from the kargs.d directories
(`true`), or copied from the previous deployment (`false`). Editing the
kargs through the command line (e.g. `ostree admin deploy --karg=`) will
set the flag to `false`, and kargs will be copied from the previous
deployment for subsequent deployments.

Closes: ostreedev#479
  • Loading branch information
Robert Fairley committed May 23, 2019
1 parent 7520b1c commit 2f582e0
Show file tree
Hide file tree
Showing 7 changed files with 650 additions and 22 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
391 changes: 372 additions & 19 deletions src/libostree/ostree-sysroot-deploy.c

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/libostree/ostree-sysroot-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ struct OstreeSysroot {
#define _OSTREE_SYSROOT_RUNSTATE_STAGED_LOCKED "/run/ostree/staged-deployment-locked"
#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.d"
#define _OSTREE_SYSROOT_KARGS_BASE "usr/lib/ostree-boot/kargs.d"

void
_ostree_sysroot_emit_journal_msg (OstreeSysroot *self,
Expand Down
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}
}

# TODO: deduplicate this with os_repository_new_commit()
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

${CMD_PREFIX} ostree --repo=${test_tmpdir}/${repo} commit --add-metadata-string "version=${version}" -b $branch -s "Build"
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
159 changes: 159 additions & 0 deletions tests/test-admin-deploy-karg-default.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/bin/bash
#
# Copyright (C) 2019 Robert Fairley <[email protected]>
#
# 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, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

set -euo pipefail

. $(dirname $0)/libtest.sh

# Exports OSTREE_SYSROOT so --sysroot not needed.
setup_os_repository "archive" "syslinux"

echo "1..7"

${CMD_PREFIX} ostree --repo=sysroot/ostree/repo pull-local --remote=testos testos-repo testos/buildmaster/x86_64-runtime

${CMD_PREFIX} ostree admin deploy --os=testos testos:testos/buildmaster/x86_64-runtime
assert_has_dir sysroot/boot/ostree/testos-${bootcsum}
# Check we generate kargs from the kargs.d configs from the first deployment.
assert_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'ostree.*options.*generated from.*kargs\.d'

initial_rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmaster/x86_64-runtime)
echo "initial_rev=${initial_rev}"

# Configure kargs stored in the ostree commit.
mkdir -p osdata/usr/lib/ostree-boot/kargs.d
os_tree_write_file "usr/lib/ostree-boot/kargs.d/4000_FOO" "FOO=USR_1"
os_tree_write_file "usr/lib/ostree-boot/kargs.d/4001_FOO2" "FOO2=USR_2"
os_repository_commit "testos-repo"

# Upgrade to tree with newly-committed kargs files.
${CMD_PREFIX} ostree --repo=sysroot/ostree/repo remote add --set=gpg-verify=false testos file://$(pwd)/testos-repo testos/buildmaster/x86_64-runtime
${CMD_PREFIX} ostree admin upgrade --os=testos
# Sanity check a new boot directory was created after upgrading.
assert_has_dir sysroot/boot/ostree/testos-${bootcsum}

assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*FOO=USR_1.*FOO2=USR_2'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree.*options.*generated from.*kargs\.d'

echo "ok default kargs base config"

# Configure kargs stored in the default configuration (/usr/etc).
mkdir -p osdata/usr/etc/ostree/kargs.d
os_tree_write_file "usr/etc/ostree/kargs.d/8000_MOO" "MOO=ETC_USR_1"
os_tree_write_file "usr/etc/ostree/kargs.d/8001_MOO2" "MOO2=ETC_USR_2"
os_repository_commit "testos-repo"

${CMD_PREFIX} ostree admin upgrade --os=testos
assert_has_dir sysroot/boot/ostree/testos-${bootcsum}

assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*FOO=USR_1.*FOO2=USR_2.*MOO=ETC_USR_1.*MOO2=ETC_USR_2'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree.*options.*generated from.*kargs\.d'

echo "ok default kargs default config"

# Configure kargs through the host config file.
rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmaster/x86_64-runtime)
echo "rev=${rev}"
etc=sysroot/ostree/deploy/testos/deploy/${rev}.0/etc
assert_has_dir ${etc}
mkdir -p ${etc}/ostree/kargs.d
# Configure a new karg (append).
echo "HELLO=ETC_1" > ${etc}/ostree/kargs.d/2000_HELLO
# Overwrite existing karg from /usr/etc/ostree/kargs.d (replace).
echo "MOO=ETC_2" > ${etc}/ostree/kargs.d/8000_MOO
# Overwrite existing karg from /usr/lib/ostree-boot/kargs.d (replace).
echo "FOO=ETC_3" > ${etc}/ostree/kargs.d/4000_FOO

# Re-deploy with host-configured kernel args.
${CMD_PREFIX} ostree admin deploy --os=testos testos:testos/buildmaster/x86_64-runtime

assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*HELLO=ETC_1.*FOO=ETC_3.*FOO2=USR_2.*MOO=ETC_2.*MOO2=ETC_USR_2'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'MOO=ETC_USR_1'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'FOO=USR_1'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree.*options.*generated from.*kargs\.d'

echo "ok default kargs host config"

rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmaster/x86_64-runtime)
echo "rev=${rev}"
etc=sysroot/ostree/deploy/testos/deploy/${rev}.1/etc
mkdir -p ${etc}/ostree/kargs.d
# Clear base kargs by writing an empty file which overrides them (delete).
echo "" > ${etc}/ostree/kargs.d/8000_MOO
echo "" > ${etc}/ostree/kargs.d/4000_FOO

${CMD_PREFIX} ostree admin deploy --os=testos testos:testos/buildmaster/x86_64-runtime

assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*HELLO=ETC_1.*FOO2=USR_2.*MOO2=ETC_USR_2'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'MOO\>'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'FOO\>'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree.*options.*generated from.*kargs\.d'

echo "ok default kargs delete empty file"

rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmaster/x86_64-runtime)
echo "rev=${rev}"
etc=sysroot/ostree/deploy/testos/deploy/${rev}.2/etc
mkdir -p ${etc}/ostree/kargs.d
rm ${etc}/ostree/kargs.d/8000_MOO
rm ${etc}/ostree/kargs.d/4000_FOO
# Clear base kargs by symlinking to /dev/null.
ln -s /dev/null ${etc}/ostree/kargs.d/8000_MOO
ln -s /dev/null ${etc}/ostree/kargs.d/4000_FOO

${CMD_PREFIX} ostree admin deploy --os=testos testos:testos/buildmaster/x86_64-runtime

assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*HELLO=ETC_1.*FOO2=USR_2.*MOO2=ETC_USR_2'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'MOO\>'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'FOO\>'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree.*options.*generated from.*kargs\.d'

echo "ok default kargs delete /dev/null"

${CMD_PREFIX} ostree admin upgrade --os=testos --allow-downgrade --override-commit=${initial_rev}

# Only the config in /etc/ostree/kargs.d remains.
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*HELLO=ETC_1'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'MOO=ETC_USR_1'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'MOO2=ETC_USR_2'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'FOO=USR_1'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'FOO2=USR_2'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree.*options.*generated from.*kargs\.d'

echo "ok default kargs downgrade"

${CMD_PREFIX} ostree admin deploy --os=testos --karg-append=TESTARG=TESTVALUE testos:testos/buildmaster/x86_64-runtime

# Check we carry over previous deployment kargs, after passing in a kargs
# override.
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*HELLO=ETC_1.*TESTARG=TESTVALUE'
# Check we won't regenerate from the config again.
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree.*options.*generated from.*kargs\.d'

echo "ok default kargs overridden"
23 changes: 21 additions & 2 deletions tests/test-admin-deploy-karg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ set -euo pipefail
# Exports OSTREE_SYSROOT so --sysroot not needed.
setup_os_repository "archive" "syslinux"

echo "1..3"
echo "1..5"

${CMD_PREFIX} ostree --repo=sysroot/ostree/repo pull-local --remote=testos testos-repo testos/buildmaster/x86_64-runtime
rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmaster/x86_64-runtime)
export rev
# This initial deployment gets kicked off with some kernel arguments
${CMD_PREFIX} ostree admin deploy --karg=root=LABEL=MOO --karg=quiet --os=testos testos:testos/buildmaster/x86_64-runtime
# Check the kargs are not being updated from the kargs.d configs.
assert_not_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'ostree-kargs-generated-from-config.*true'
${CMD_PREFIX} ostree admin deploy --karg=FOO=BAR --os=testos testos:testos/buildmaster/x86_64-runtime
${CMD_PREFIX} ostree admin deploy --karg=TESTARG=TESTVALUE --os=testos testos:testos/buildmaster/x86_64-runtime
assert_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'options.*FOO=BAR'
Expand Down Expand Up @@ -61,9 +63,26 @@ echo "ok deploy --karg-proc-cmdline"
${CMD_PREFIX} ostree admin status
${CMD_PREFIX} ostree admin undeploy 0

${CMD_PREFIX} ostree admin deploy --os=testos --karg-append=APPENDARG=VALAPPEND --karg-append=APPENDARG=2NDAPPEND testos:testos/buildmaster/x86_64-runtime
${CMD_PREFIX} ostree admin deploy --os=testos --karg-append=APPENDARG=VALAPPEND --karg-append=APPENDARG=2NDAPPEND testos:testos/buildmaster/x86_64-runtime
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*FOO=BAR'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*TESTARG=TESTVALUE'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*APPENDARG=VALAPPEND .*APPENDARG=2NDAPPEND'
# Check the kargs are still not being updated from the kargs.d configs.
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'

echo "ok deploy --karg-append"

${CMD_PREFIX} ostree admin status
${CMD_PREFIX} ostree admin undeploy 0

${CMD_PREFIX} ostree admin deploy --os=testos testos:testos/buildmaster/x86_64-runtime
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'

echo "ok no true kargs generated from config flag"

${CMD_PREFIX} ostree admin status

${CMD_PREFIX} ostree admin deploy --os=testos testos:testos/buildmaster/x86_64-runtime
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'

echo "ok no true kargs generated from config flag persists"
47 changes: 46 additions & 1 deletion tests/test-admin-instutil-set-kargs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,29 @@ set -euo pipefail
# Exports OSTREE_SYSROOT so --sysroot not needed.
setup_os_repository "archive" "syslinux"

echo "1..5"
echo "1..6"

${CMD_PREFIX} ostree --repo=sysroot/ostree/repo pull-local --remote=testos testos-repo testos/buildmaster/x86_64-runtime
${CMD_PREFIX} ostree admin deploy --os=testos testos:testos/buildmaster/x86_64-runtime
# Check we generate kargs from the kargs.d configs from the first deployment.
assert_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'ostree.*options.*generated from.*kargs\.d'

rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmaster/x86_64-runtime)
echo "rev=${rev}"
etc=sysroot/ostree/deploy/testos/deploy/${rev}.0/etc

${CMD_PREFIX} ostree admin instutil set-kargs FOO=BAR
${CMD_PREFIX} ostree admin instutil set-kargs FOO=BAZ FOO=BIF TESTARG=TESTVALUE KEYWORD EMPTYLIST=
assert_not_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'options.*FOO=BAR'
assert_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'options.*FOO=BAZ .*FOO=BIF'
assert_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'options.*TESTARG=TESTVALUE KEYWORD EMPTYLIST='

# Check that the configured kargs flags and snippet were written.
assert_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-1-testos.conf 'ostree.*options.*generated from.*kargs\.d'
assert_file_has_content ${etc}/ostree/kargs.d/4000_ostree_instutil 'FOO=BAZ .*FOO=BIF.*TESTARG=TESTVALUE KEYWORD EMPTYLIST='

echo "ok instutil set-kargs (basic)"

${CMD_PREFIX} ostree admin instutil set-kargs --merge FOO=BAR
Expand Down Expand Up @@ -67,3 +80,35 @@ for arg in $(cat /proc/cmdline); do
esac
done
echo "ok instutil set-kargs --import-proc-cmdline"

# Configure kargs stored in the ostree commit.
mkdir -p osdata/usr/lib/ostree-boot/kargs.d
os_tree_write_file "usr/lib/ostree-boot/kargs.d/4000_FOO" "FOO=USR_1"
os_tree_write_file "usr/lib/ostree-boot/kargs.d/4001_FOO2" "FOO2=USR_2"
os_repository_commit "testos-repo"

# Upgrade to tree with newly-committed kargs files.
${CMD_PREFIX} ostree --repo=sysroot/ostree/repo remote add --set=gpg-verify=false testos file://$(pwd)/testos-repo testos/buildmaster/x86_64-runtime
${CMD_PREFIX} ostree admin upgrade --os=testos
# Sanity check a new boot directory was created after upgrading.
assert_has_dir sysroot/boot/ostree/testos-${bootcsum}

assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*FOO=USR_1.*FOO2=USR_2'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree.*options.*generated from.*kargs\.d'

rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmaster/x86_64-runtime)
echo "rev=${rev}"
etc=sysroot/ostree/deploy/testos/deploy/${rev}.0/etc

# Check that set-kargs overrides any existing default kargs in /usr/lib/ostree-boot/kargs.d.
${CMD_PREFIX} ostree admin instutil set-kargs FOO=BAR

assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'options.*FOO=BAR'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'FOO=USR_1'
assert_not_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'FOO2=USR_2'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree-kargs-generated-from-config.*true'
assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf 'ostree.*options.*generated from.*kargs\.d'
assert_file_has_content ${etc}/ostree/kargs.d/4000_ostree_instutil 'FOO=BAR'

echo "ok instutil set-kargs default kargs"

0 comments on commit 2f582e0

Please sign in to comment.