Add a sysctl-append-value script

The sysctl element works on the assumption that sysctl values are
going to be set by elements that are not used in the same image, and
that therefore, conflicting options would be bad. Some elements would
like to append options to a list, allowing multiple elements to
change the same value.

Change-Id: I4bbfbac716b842210cea2b6af289da78043fd4a7
This commit is contained in:
Steve Kowalik 2014-06-19 15:11:26 +10:00 committed by Richard Su
parent 23c7c0f509
commit 20c9db91d5
2 changed files with 41 additions and 2 deletions

View File

@ -1,6 +1,6 @@
Manages sysctl settings.
There are three ways to make use of this element:
There are four ways to make use of this element:
1) Elements can make use of sysctl-set-value directly by requiring
this element and calling /usr/local/bin/sysctl-set-value. This will
@ -15,7 +15,12 @@ does not verify values, or set them on the build host, it only writes the
files into the image, so that the values will be set when the image is
booted.
3) Alternately you can make use of the element via Heat by adding
3) Elements can make use of sysctl-append-value to set values that
complement values set by other elements, rather than overwriting or
conflicting with them. This script also does not set the sysctl value
on the build host.
4) Alternately you can make use of the element via Heat by adding
metadata in the configuration example below. sysctl-set-value
will take care of applying these settings during configuration time.

View File

@ -0,0 +1,34 @@
#!/bin/bash
# Append values to a sysctl value.
# The script is called with name/value pairs which are stored
# in the system default sysctl.d directory. Existing values are
# fetched, and the new value appended using ',' to seperate them.
# Once finished sysctl is used to activate the changes.
set -eu
set -o pipefail
NAME=${1:-}
NEW_VALUE=${2:-}
# Optional comment used to describe the setting
COMMENT=${3:-"This file is managed via the TripleO sysctl image element."}
if [ -z "$NAME" -o -z "$NEW_VALUE" ]; then
echo "Usage: sysctl-append-value <name> <new value> [comment]"
exit 1
fi
FILENAME="/etc/sysctl.d/${NAME}.conf"
if [ -f "$FILENAME" ]; then
if grep "$NEW_VALUE" $FILENAME; then
echo "Info: $NEW_VALUE already present in $NAME"
fi
VALUE=$(tail -n 1 $FILENAME | awk '{ print $3 }')
VALUE="$VALUE,$NEW_VALUE"
else
VALUE=$NEW_VALUE
fi
sysctl-write-value $NAME $VALUE $COMMENT