-
Notifications
You must be signed in to change notification settings - Fork 296
/
generate-guestos-config.sh
executable file
·249 lines (222 loc) · 8.58 KB
/
generate-guestos-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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/bash
set -e
# Generate the GuestOS configuration.
SCRIPT="$(basename $0)[$$]"
METRICS_DIR="/run/node_exporter/collector_textfile"
# Get keyword arguments
for argument in "${@}"; do
case ${argument} in
-c=* | --config=*)
CONFIG="${argument#*=}"
shift
;;
-d=* | --deployment=*)
DEPLOYMENT="${argument#*=}"
shift
;;
-h | --help)
echo 'Usage:
Generate GuestOS Configuration
Arguments:
-c=, --config= specify the config.ini configuration file (Default: /boot/config/config.ini)
-d=, --deployment= specify the deployment.json configuration file (Default: /boot/config/deployment.json)
-h, --help show this help message and exit
-i=, --input= specify the input template file (Default: /opt/ic/share/guestos.xml.template)
-m=, --media= specify the config media image file (Default: /run/ic-node/config.img)
-o=, --output= specify the output configuration file (Default: /var/lib/libvirt/guestos.xml)
'
exit 1
;;
-i=* | --input=*)
INPUT="${argument#*=}"
shift
;;
-m=* | --media=*)
MEDIA="${argument#*=}"
shift
;;
-o=* | --output=*)
OUTPUT="${argument#*=}"
shift
;;
*)
echo "Error: Argument is not supported."
exit 1
;;
esac
done
function validate_arguments() {
if [ "${CONFIG}" == "" -o "${DEPLOYMENT}" == "" -o "${INPUT}" == "" -o "${OUTPUT}" == "" ]; then
$0 --help
fi
}
# Set arguments if undefined
CONFIG="${CONFIG:=/boot/config/config.ini}"
DEPLOYMENT="${DEPLOYMENT:=/boot/config/deployment.json}"
INPUT="${INPUT:=/opt/ic/share/guestos.xml.template}"
MEDIA="${MEDIA:=/run/ic-node/config.img}"
OUTPUT="${OUTPUT:=/var/lib/libvirt/guestos.xml}"
write_log() {
local message=$1
if [ -t 1 ]; then
echo "${SCRIPT} ${message}" >/dev/stdout
fi
logger -t ${SCRIPT} "${message}"
}
write_metric() {
local name=$1
local value=$2
local help=$3
local type=$4
echo -e "# HELP ${name} ${help}\n# INDEX ${type}\n${name} ${value}" >"${METRICS_DIR}/${name}.prom"
}
function read_variables() {
# Read limited set of keys. Be extra-careful quoting values as it could
# otherwise lead to executing arbitrary shell code!
while IFS="=" read -r key value; do
case "$key" in
"ipv6_prefix") ipv6_prefix="${value}" ;;
"ipv6_gateway") ipv6_gateway="${value}" ;;
"ipv4_address") ipv4_address="${value}" ;;
"ipv4_prefix_length") ipv4_prefix_length="${value}" ;;
"ipv4_gateway") ipv4_gateway="${value}" ;;
"domain") domain="${value}" ;;
esac
done <"${CONFIG}"
}
function assemble_config_media() {
cmd=(/opt/ic/bin/build-bootstrap-config-image.sh ${MEDIA})
cmd+=(--nns_public_key "/boot/config/nns_public_key.pem")
cmd+=(--elasticsearch_hosts "$(/opt/ic/bin/fetch-property.sh --key=.logging.hosts --metric=hostos_logging_hosts --config=${DEPLOYMENT})")
cmd+=(--ipv6_address "$(/opt/ic/bin/hostos_tool generate-ipv6-address --node-type GuestOS)")
cmd+=(--ipv6_gateway "${ipv6_gateway}")
cmd+=(--ipv6_name_servers "$(/opt/ic/bin/fetch-property.sh --key=.dns.name_servers --metric=hostos_ipv6_dns_name_servers --config=${DEPLOYMENT})")
if [[ -n "$ipv4_address" && -n "$ipv4_prefix_length" && -n "$ipv4_gateway" && -n "$domain" ]]; then
cmd+=(--ipv4_address "${ipv4_address}/${ipv4_prefix_length}")
cmd+=(--ipv4_gateway "${ipv4_gateway}")
cmd+=(--domain "${domain}")
fi
cmd+=(--hostname "guest-$(/opt/ic/bin/fetch-mgmt-mac.sh | sed 's/://g')")
cmd+=(--nns_url "$(/opt/ic/bin/fetch-property.sh --key=.nns.url --metric=hostos_nns_url --config=${DEPLOYMENT})")
if [ -f "/boot/config/node_operator_private_key.pem" ]; then
cmd+=(--node_operator_private_key "/boot/config/node_operator_private_key.pem")
fi
if [ -f "/boot/config/default_firewall_whitelist.conf" ]; then
cmd+=(--default_firewall_whitelist "/boot/config/default_firewall_whitelist.conf")
fi
# Run the above command
"${cmd[@]}"
write_log "Assembling config media for GuestOS: ${MEDIA}"
}
function generate_guestos_config() {
RESOURCES_MEMORY=$(/opt/ic/bin/fetch-property.sh --key=.resources.memory --metric=hostos_resources_memory --config=${DEPLOYMENT})
MAC_ADDRESS=$(/opt/ic/bin/hostos_tool generate-mac-address --node-type GuestOS)
# NOTE: `fetch-property` will error if the target is not found. Here we
# only want to act when the field is set.
CPU_MODE=$(jq -r ".resources.cpu" ${DEPLOYMENT})
CPU_DOMAIN="kvm"
CPU_SPEC="/opt/ic/share/kvm-cpu.xml"
if [ "${CPU_MODE}" == "qemu" ]; then
CPU_DOMAIN="qemu"
CPU_SPEC="/opt/ic/share/qemu-cpu.xml"
fi
if [ ! -f "${OUTPUT}" ]; then
mkdir -p "$(dirname "$OUTPUT")"
sed -e "s@{{ resources_memory }}@${RESOURCES_MEMORY}@" \
-e "s@{{ mac_address }}@${MAC_ADDRESS}@" \
-e "s@{{ cpu_domain }}@${CPU_DOMAIN}@" \
-e "/{{ cpu_spec }}/{r ${CPU_SPEC}" -e "d" -e "}" \
"${INPUT}" >"${OUTPUT}"
restorecon -R "$(dirname "$OUTPUT")"
write_log "Generating GuestOS configuration file: ${OUTPUT}"
write_metric "hostos_generate_guestos_config" \
"1" \
"HostOS generate GuestOS config" \
"gauge"
else
write_log "GuestOS configuration file already exists: ${OUTPUT}"
write_metric "hostos_generate_guestos_config" \
"0" \
"HostOS generate GuestOS config" \
"gauge"
fi
}
TMP_MOUNT_DIR="/tmp/sev-guest-mount"
BOOT_COMPONENTS_DIR="/tmp/sev-guest-boot-components"
LOOP_DEVICE="/dev/loop0"
GUESTOS_LOCATION="/dev/mapper/hostlvm-guestos"
SEV_SNP_FILE="/opt/ic/share/SEV"
# Set up loop device and mount. Get the boot components
mount_guestos_image_and_copy_files() {
mkdir -p "$TMP_MOUNT_DIR"
losetup -P "$LOOP_DEVICE" /dev/mapper/hostlvm-guestos
mount "${LOOP_DEVICE}p4" "$TMP_MOUNT_DIR"
mkdir -p "$BOOT_COMPONENTS_DIR"
cp "$TMP_MOUNT_DIR"/vmlinuz "$BOOT_COMPONENTS_DIR"
cp "$TMP_MOUNT_DIR"/initrd.img "$BOOT_COMPONENTS_DIR"
cp "$TMP_MOUNT_DIR"/extra_boot_args "$BOOT_COMPONENTS_DIR"
}
# Clean up loop device and mount
unmount_guestos_image() {
umount -q "$TMP_MOUNT_DIR"
rmdir "$TMP_MOUNT_DIR"
losetup -d "$LOOP_DEVICE"
}
# Generate config for SEV GuestOS
# Use the qemu script template and mount the guestos image.
# Derive kernel, initrd and the cmdline and populate the qemu script.
function generate_sev_guestos_config() {
INPUT="/opt/ic/share/sev_guestos.sh.template"
OUTPUT="/var/lib/sev_guestos.sh"
RESOURCES_MEMORY=$(/opt/ic/bin/fetch-property.sh --key=.resources.memory --metric=hostos_resources_memory --config=${DEPLOYMENT})
MAC_ADDRESS=$(/opt/ic/bin/hostos_tool generate-mac-address --node-type GuestOS)
mount_guestos_image_and_copy_files
unmount_guestos_image
KERNEL="$BOOT_COMPONENTS_DIR/vmlinuz"
INITRD="$BOOT_COMPONENTS_DIR/initrd.img"
source "$BOOT_COMPONENTS_DIR"/extra_boot_args
if [ ! -f "${OUTPUT}" ]; then
mkdir -p "$(dirname "$OUTPUT")"
sed -e "s@{{ resources_memory }}@${RESOURCES_MEMORY}@" \
-e "s@{{ mac_address }}@${MAC_ADDRESS}@" \
-e "s@{{ kernel }}@${KERNEL}@" \
-e "s@{{ initrd }}@${INITRD}@" \
-e "s@{{ extra_boot_args }}@${EXTRA_BOOT_ARGS}@" \
"${INPUT}" >"${OUTPUT}"
chmod ug+x "${OUTPUT}"
restorecon -R "$(dirname "$OUTPUT")"
write_log "Generating SEV GuestOS configuration file: ${OUTPUT}"
write_metric "hostos_generate_sev_guestos_config" \
"1" \
"HostOS generate SEV GuestOS config" \
"gauge"
else
write_log "SEV GuestOS configuration file already exists: ${OUTPUT}"
write_metric "hostos_generate_sev_guestos_config" \
"0" \
"HostOS generate SEV GuestOS config" \
"gauge"
fi
}
# Check if SEV-SNP if enabled on host
function is_sev_snp_enabled() {
if [ -f "$SEV_SNP_FILE" ]; then
return 0
fi
return 1
}
function main() {
# Establish run order
validate_arguments
read_variables
assemble_config_media
if is_sev_snp_enabled; then
# TODO: Also, fetch and load the SEV certs.
# snptool get-certs
# sev-host-set-cert-chain -r ark.pem -s ask.pem -v vcek.pem
generate_sev_guestos_config
else
generate_guestos_config
fi
}
main