forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_kata_config.sh
executable file
·108 lines (85 loc) · 2.94 KB
/
set_kata_config.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
# Copyright (c) 2019 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
readonly script_dir=$(dirname $(readlink -f "$0"))
readonly script_name="$(basename "${BASH_SOURCE[0]}")"
# Source to trap error line number
# shellcheck source=../lib/common.bash
source "${script_dir}/../lib/common.bash"
# shellcheck source=./lib.sh
source "${script_dir}/lib.sh"
declare -A sections
declare -A valid_requests
declare -A json_keys
readonly sections=([sandbox_cgroup_only]=runtime [shared_fs]=hypervisor.qemu)
readonly valid_requests=([sandbox_cgroup_only]="true false" [shared_fs]="virtio-fs virtio-9p")
readonly json_keys=([sandbox_cgroup_only]=.Runtime.SandboxCgroupOnly [shared_fs]=.Hypervisor.SharedFS)
option=${1:-}
request=${2:-}
usage(){
cat <<EOF
Usage:
${script_name} <option> <request>
Valid options and requests:
$(for opt in "${!valid_requests[@]}"
do
echo "${opt}": "${valid_requests["$opt"]}"
done)
The configuration changes are applied in kata user config:
${KATA_ETC_CONFIG_PATH}
Remove it if you want to use the stateless options.
EOF
exit 1
}
# need explicit handling of both cases because all errors are trapped
valid=$([[ -n "${option}" ]] && echo $? || echo $?)
# trailing space to match whole words
if [[ "${valid}" == 0 && ! "${!valid_requests[*]} " =~ "${option} " ]]
then
echo >&2 "ERROR: unknown option: '${option}'"
valid=1
fi
[[ "${valid}" == 0 ]] || usage
valid=$([[ -n "${request}" ]] && echo $? || echo $?)
if [[ "${valid}" == 0 && ! "${valid_requests["${option}"]} " =~ "${request} " ]]
then
echo >&2 "ERROR: unknown request: '${request}'"
valid=1
fi
[[ "${valid}" == 0 ]] || usage
# quote non-boolean request
[[ "$request" =~ ^(true|false)$ ]] || request="\"${request}\""
readonly section="${sections["${option}"]}"
readonly json_key="${json_keys["${option}"]}"
current_value=$(kata-runtime kata-env --json | jq "${json_key}")
if [ "$current_value" == "${request}" ]; then
info "already ${request}"
exit 0
fi
kata_config_path=$(kata-runtime kata-env --json | jq -r .Runtime.Config.Path)
bk_suffix="${option}-bk"
if [ -f "${KATA_ETC_CONFIG_PATH}" ] && [ "${KATA_ETC_CONFIG_PATH}" != ${kata_config_path} ]; then
bk_file="${KATA_ETC_CONFIG_PATH}-${bk_suffix}"
info "backup ${KATA_ETC_CONFIG_PATH} in ${bk_file}"
sudo cp "${KATA_ETC_CONFIG_PATH}" "${bk_file}"
fi
if [ "${KATA_ETC_CONFIG_PATH}" != "${kata_config_path}" ]; then
kata_etc_dir="/etc/kata-containers"
if [ ! -d "${kata_etc_dir}" ]; then
sudo mkdir -p "${kata_etc_dir}"
fi
info "Creating etc config based on ${kata_config_path}"
sudo cp "${kata_config_path}" "${KATA_ETC_CONFIG_PATH}"
fi
info "modifying config file : ${KATA_ETC_CONFIG_PATH}"
sudo crudini --set "${KATA_ETC_CONFIG_PATH}" "${section}" "${option}" "${request}"
info "Validate option is ${request}"
current_value=$(kata-runtime kata-env --json | jq "${json_key}")
[ "$current_value" == "${request}" ] || die "The option was not updated"
info "OK"