forked from nestybox/sysbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-cfg
executable file
·597 lines (484 loc) · 16.6 KB
/
docker-cfg
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#!/bin/bash
#
# Copyright 2019-2020 Nestybox, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Configure Docker to use Sysbox.
#
# Type "sudo ./docker-cfg --help". for usage info.
#
progname=$(basename $0)
config_only=0
verbose=0
dry_run=0
force_restart=0
systemd_reload_daemon=0
# Dockerd default configuration dir/file.
dockerCfgDir="/etc/docker"
dockerCfgFile="${dockerCfgDir}/daemon.json"
# Docker config vars.
docker_network_config_changed="false"
docker_userns_config_changed="false"
docker_runtime_config_changed="false"
docker_cgroup_config_changed="false"
dockerd=$(which dockerd)
# Temp copy of the docker daemon.json file
tmpCfgFile=$(mktemp /tmp/docker-cfg.XXXXXX)
trap 'rm -f "${tmpCfgFile}"' EXIT
# Temp file for jq write operations.
tmpJqFile=$(mktemp /tmp/docker-cfg-jq.XXXXXX)
trap 'rm -f "${tmpJqFile}"' EXIT
# Log helper
function log() {
if [ "$verbose" -eq 1 ]; then
printf "$1"
fi
}
# Retry a command $1 times until it succeeds. Wait $2 seconds between retries.
# (copied from runc/tests/integration/helpers.bash)
function retry() {
local attempts=$1
shift
local delay=$1
shift
local i
for ((i = 0; i < attempts; i++)); do
$@ > /dev/null 2>&1
if [ "$?" -eq 0 ]; then
return 0
fi
sleep $delay
done
echo "Command \"$@\" failed $attempts times. Output: $?"
return 1
}
# Returns true if dockerd is running
function dockerd_running() {
docker ps > /dev/null 2>&1
}
# Returns true if dockerd is stopped
function dockerd_stopped() {
! pgrep $dockerd
}
# Restarts dockerd manually (if it's running).
function dockerd_manual_restart() {
if dockerd_pid=$(pidof $dockerd); then
dockerd_cmd=$(ps -eo args | grep $dockerd | head -n 1)
kill -s SIGTERM $dockerd_pid
retry 10 1 dockerd_stopped
if [ $? -ne 0 ]; then
printf "Failed to stop Docker. Please stop and restart manually to apply config.\n"
exit 1
fi
rm -f /var/run/docker.pid
$dockerd_cmd > /var/log/dockerd.log 2>&1 &
retry 10 1 dockerd_running
if [ $? -ne 0 ]; then
printf "Failed to restart Docker. Please restart manually to apply config.\n"
exit 1
fi
fi
}
# Stops and removes any existing Docker containers
function docker_remove_containers() {
docker_conts=$(docker ps -aq)
if [[ "$docker_conts" != "" ]]; then
ret=$(docker stop -t0 $(docker ps -aq))
fi
docker_conts=$(docker ps -aq)
if [[ "$docker_conts" != "" ]]; then
ret=$(docker rm -f $(docker ps -aq))
fi
}
#
# Returns 'true' if passed ipv4 address overlaps with any of the system local
# subnets. Return 'false' otherwise.
#
function system_local_subnet() {
if ip route get ${1} | egrep -q "via $(ip route | awk '/default/ {print $3}')"; then
return 1
fi
return 0
}
# Returns 'true' if host is managed by systemd; false otherwise.
function systemd_host() {
ret=$(ps --no-headers -o comm 1)
if [[ "$ret" == "systemd" ]]; then
return 0
fi
return 1
}
function docker_config_network_bip() {
jq --arg bip ${bip_subnet} --indent 4 '. + {"bip": $bip}' ${tmpCfgFile} \
> ${tmpJqFile} && cp ${tmpJqFile} ${tmpCfgFile}
docker_network_config_changed="true"
log "Docker config: configured bridge-ip network to $bip_subnet.\n"
}
function docker_config_network_pool() {
jq --arg subnet ${pool_subnet} --indent 4 \
'."default-address-pools"[0] |= . + {"base": $subnet, "size": 24}' ${tmpCfgFile} \
> ${tmpJqFile} && cp ${tmpJqFile} ${tmpCfgFile}
docker_network_config_changed="true"
log "Docker config: configured default-address-pool to $pool_subnet.\n"
}
function docker_check_network_config() {
local subnet
# If bip address overlaps with an existing local subnet, then
# dump a warning message to the user.
subnet=""
if [ -n "$bip_subnet" ]; then
subnet=${bip_subnet}
else
if [ -f ${dockerCfgFile} ]; then
subnet=$(jq '.bip' ${dockerCfgFile})
fi
fi
if [[ "$subnet" != "" ]] && [[ "$subnet" != "null" ]]; then
subnet=$(echo $subnet | tr -d '"')
local bip_ip=$(echo ${subnet} | cut -d'/' -f 1)
if dockerd_running && system_local_subnet ${bip_ip} &&
! ip -4 address show dev docker0 | egrep -q "${bip_subnet}"; then
echo -e "WARN: Docker bridge-ip network (${subnet}) overlaps"\
"with existing system subnet. Try another subnet to avoid connectivity issues."
return 1
fi
fi
# Same as above but for default-address-pool
subnet=""
if [ -n "$pool_subnet" ]; then
subnet=${pool_subnet}
else
if [ -f ${dockerCfgFile} ]; then
subnet=$(jq '."default-address-pools" | .[0] | ."base"' ${dockerCfgFile})
fi
fi
if [[ "$subnet" != "" ]] && [[ "$subnet" != "null" ]]; then
subnet=$(echo $subnet | tr -d '"')
local pool_ip=$(echo ${subnet} | cut -d'/' -f 1)
if system_local_subnet ${pool_ip}; then
echo -e "WARN: Docker default-address-pool to configure (${subnet}) overlaps"\
"with existing system subnet. Try another IP range to avoid connectivity issues."
return 1
fi
fi
return 0
}
function docker_config_network() {
docker_check_network_config
if [ $? -ne 0 ]; then
printf "INFO: Refusing to configure Docker networking due to IP address overlap with existing system subnet(s).\n"
return
fi
if [ -n "$bip_subnet" ]; then
docker_config_network_bip
fi
if [ -n "$pool_subnet" ]; then
docker_config_network_pool
fi
}
function docker_config_runtime() {
if [[ "$sysbox_runtime" == "enable" ]]; then
# If no 'runtimes' key-entry is present, proceed to add one.
if [ $(jq 'has("runtimes")' ${tmpCfgFile}) = "false" ]; then
jq --indent 4 '. + {"runtimes": {"sysbox-runc": {"path": "/usr/bin/sysbox-runc"}}}' \
${tmpCfgFile} > ${tmpJqFile} && cp ${tmpJqFile} ${tmpCfgFile}
docker_runtime_config_changed="true"
# If no 'sysbox-runc' runtime entry is present, proceed to add it.
elif [ $(jq '.runtimes | has("sysbox-runc")' ${tmpCfgFile}) = "false" ]; then
jq --indent 4 '.runtimes |= . + {"sysbox-runc": {"path": "/usr/bin/sysbox-runc"}}' \
${tmpCfgFile} > ${tmpJqFile} && cp ${tmpJqFile} ${tmpCfgFile}
docker_runtime_config_changed="true"
fi
if [ ${docker_runtime_config_changed} = true ]; then
log "Docker config: added Sysbox runtime.\n"
fi
elif [[ "$sysbox_runtime" == "disable" ]]; then
# Eliminate sysbox's runtime entry if present.
if [ $(jq 'has("runtimes")' ${tmpCfgFile}) = "true" ] &&
[ $(jq '.runtimes | has("sysbox-runc")' ${tmpCfgFile}) = "true" ]; then
jq 'del(.runtimes."sysbox-runc")' \
${tmpCfgFile} > ${tmpJqFile} && cp ${tmpJqFile} ${tmpCfgFile}
docker_runtime_config_changed="true"
fi
log "Docker config: removed Sysbox runtime.\n"
fi
}
function docker_config_default_runtime() {
if [ -n "$default_runtime" ]; then
jq --arg dr $default_runtime '. + {"default-runtime": $dr}' \
${tmpCfgFile} > ${tmpJqFile} && cp ${tmpJqFile} ${tmpCfgFile}
log "Docker config: set default-runtime to $default_runtime.\n"
fi
}
function docker_config_cgroup_driver() {
# NOTE: for some reason configuring the cgroup-driver via the
# /etc/docker/daemon.json file does not work (i.e., dockerd does not pick up
# the cgroup driver config). Thus, we resort to configuring via the dockerd
# command line.
docker_systemd_unit="/lib/systemd/system/docker.service"
if [[ "$cgroup_driver" == "systemd" ]]; then
if [ ! -f $docker_systemd_unit ]; then
printf "Error: Docker systemd unit file at $docker_systemd_unit does not exist.\n"
exit 1
fi
docker_cmd=$(grep ExecStart $docker_systemd_unit | awk -F "ExecStart=" '{print $2_}')
ret=$(echo $docker_cmd | grep "exec-opt native.cgroupdriver=systemd")
if [ "$?" -eq 0 ]; then
return
fi
docker_cmd_new="${docker_cmd} --exec-opt native.cgroupdriver=systemd"
sed -i "s@${docker_cmd}@${docker_cmd_new}@g" $docker_systemd_unit
log "Docker config: cgroup driver set to $cgroup_driver.\n"
systemd_reload_daemon=1
docker_cgroup_config_changed="true"
else
if [ ! -f $docker_systemd_unit ]; then
return
fi
docker_cmd=$(grep ExecStart $docker_systemd_unit | awk -F "ExecStart=" '{print $2_}')
ret=$(echo $docker_cmd | grep "exec-opt native.cgroupdriver=systemd")
if [ "$?" -eq 0 ]; then
sed -i "s@--exec-opt native.cgroupdriver=systemd@@g" $docker_systemd_unit
log "Docker config: cgroup driver set to $cgroup_driver.\n"
systemd_reload_daemon=1
docker_cgroup_config_changed="true"
fi
fi
}
function docker_config_userns() {
if [[ "$userns_remap" == "enable" ]]; then
# If no 'userns-remap' key-entry is present, or if its associated value
# is empty, proceed to create a key and set its value to 'sysbox' user.
# Note that 'jq' does not provide 'in-place' editing capabilities (i.e.
# it displays inconsistent behavior), hence the need for the auxiliar
# 'tmpJqFile'.
if [ $(jq 'has("userns-remap")' ${tmpCfgFile}) = "false" ] ||
[ $(jq '."userns-remap"' ${tmpCfgFile}) = "\"\"" ]; then
jq --indent 4 '. + {"userns-remap": "sysbox"}' \
${tmpCfgFile} > ${tmpJqFile} && cp ${tmpJqFile} ${tmpCfgFile}
docker_userns_config_changed="true"
log "Docker config: enabled userns-remap.\n"
fi
elif [[ "$userns_remap" == "disable" ]]; then
# If present, eliminate the userns-remap entry.
if [ $(jq 'has("userns-remap")' ${tmpCfgFile}) = "true" ]; then
jq 'del(."userns-remap")' \
${tmpCfgFile} > ${tmpJqFile} && cp ${tmpJqFile} ${tmpCfgFile}
docker_userns_config_changed="true"
log "Docker config: disabled userns-remap.\n"
fi
fi
}
function docker_notify() {
# Restart docker if disruptive changes have been made.
if [[ ${docker_userns_config_changed} = "true" ]] ||
[[ ${docker_network_config_changed} = "true" ]] ||
[[ ${docker_cgroup_config_changed} = "true" ]] ||
[[ ${force_restart} -eq 1 ]]; then
# Check if Docker is running; otherwise skip
ret=$(pidof $dockerd)
if [ $? -eq 1 ]; then
log "Skipping Docker restart (Docker is not running).\n"
return
fi
# If existing containers are found then skip docker-restart to avoid disruption (unless force_restart is set).
ret=$(docker ps -a | wc -l | egrep -q "1$")
if [ $? -ne 0 ] && [ ${force_restart} -ne 1 ]; then
echo -e "\nModified the Docker config but did not restart Docker as there are containers running.\n" \
"Please remove them and restart Docker by doing:\n" \
"\t\"docker rm \$(docker ps -a -q) -f &&" \
"sudo systemctl restart docker\"\n"
else
docker_remove_containers
if systemd_host; then
if [ "$systemd_reload_daemon" -eq 1 ]; then
systemctl daemon-reload
if [ $? -ne 0 ]; then
printf "Failed to reload the systemd config. Do \"systemctl status\" to get further info."
exit 1
fi
fi
systemctl restart docker
if [ $? -ne 0 ]; then
printf "Failed to restart the Docker systemd service unit. Do \"systemctl status docker\" to get further info."
exit 1
fi
else
dockerd_manual_restart
fi
log "Restarted Docker.\n"
return
fi
fi
# If non-disruptive changes have been made to docker config, then send it a
# sighup to have its config file getting re-parsed (no need to cold-boot).
if [ ${docker_runtime_config_changed} = true ]; then
kill -SIGHUP $(pidof $dockerd)
log "Notified Docker of config changes.\n"
fi
}
function docker_config() {
# If the dockerd default config-file exist, copy it to a temp file; we
# operate on the temp file and copy it back to the real config file at the
# end (unless --dry-run is set).
if [[ -f ${dockerCfgFile} ]]; then
cp ${dockerCfgFile} ${tmpCfgFile}
else
touch ${tmpCfgFile}
fi
# If there's no content on it, create one with a bare json layout.
if [[ ! -s ${tmpCfgFile} ]]; then
echo -e "{\n}" > ${tmpCfgFile}
fi
docker_config_userns
docker_config_runtime
docker_config_default_runtime
docker_config_cgroup_driver
docker_config_network
if [ "$dry_run" -eq 1 ]; then
cat ${tmpCfgFile}
return
else
mkdir -p ${dockerCfgDir}
mv ${tmpCfgFile} ${dockerCfgFile}
log "Wrote to ${dockerCfgFile}.\n"
fi
if [ "$config_only" -eq 0 ]; then
# Return here if docker is not installed.
if ! command -v docker >/dev/null 2>&1; then
return
fi
docker_notify
fi
}
function show_usage() {
printf "\n"
printf "Usage: $progname [OPTIONS]\n"
printf "\n"
printf 'Configures Docker to use Sysbox by changing the "/etc/docker/daemon.json" file\n'
printf 'and restarting Docker as needed. Must run as root.\n'
printf "\n"
printf "Example: $ sudo $progname --sysbox-runtime=enable\n"
printf "\n"
printf "It's best to run this script when no Docker containers are running.\n"
printf "\n"
printf 'Use the "--dry-run" option to see the would-be changes to the Docker\n'
printf 'config without actually changing the config.\n'
printf "\n"
printf "Options:\n"
printf " --sysbox-runtime=<enable|disable> Adds or removes the Sysbox runtime to the Docker daemon config and restarts Docker. If omitted, no change to the runtime config occurs.\n"
printf " --userns-remap=<enable|disable> Adds or removes userns-remap mode to the Docker daemon config and restarts Docker. If omitted, no change to the userns-remap config occurs.\n"
printf " --default-runtime=<runtime> Configures the Docker default runtime (e.g., sysbox-runc, runc, etc.) If omitted, no change to the Docker default runtime is made.\n"
printf " --bip=<ip-addr> Configures the IP address for the docker0 bridge (e.g., 172.20.0.1/16).\n"
printf " --default-address-pool=<ip-addr> Configures the starting IP address for user-defined Docker networks (e.g., 172.25.0.0/16).\n"
printf " --cgroup-driver=<cgroupfs|systemd> Configures the cgroup driver to be used. If omitted, no change to the Docker cgroup driver is made.\n"
printf " -c, --config-only Writes to the Docker daemon config file but does not restart Docker. Docker will not pick up the new config until it's restarted.\n"
printf " -f, --force-restart Forces restart of the Docker daemon (even if no config changes have occurred). This will stop and remove any existing Docker containers.\n"
printf " --dry-run Does not modify the Docker config; instead outputs the changes to stdout.\n"
printf " -v, --verbose Enables verbose logging (useful for debugging).\n"
printf " -h, --help Display usage.\n"
printf "\n"
}
function parse_args() {
options=$(getopt -o cvhf -l verbose,help,config-only,dry-run,force-restart,bip:,default-address-pool:,default-runtime:,sysbox-runtime:,userns-remap:,cgroup-driver: -- "$@")
eval set -- "$options"
while true; do
case "$1" in
-h | --help)
show_usage
exit 1
;;
-v | --verbose)
verbose=1
;;
-c | --config-only)
config_only=1
;;
--dry-run)
dry_run=1
;;
--sysbox-runtime)
shift;
sysbox_runtime=$1
if [[ ${sysbox_runtime} != "enable" ]] && [[ ${sysbox_runtime} != "disable" ]]; then
printf "\nInvalid value for sysbox-runtime.\n"
show_usage
exit 1
fi
;;
--userns-remap)
shift;
userns_remap=$1
if [[ ${userns_remap} != "enable" ]] && [[ ${userns_remap} != "disable" ]]; then
printf "\nInvalid value for userns-remap.\n"
show_usage
exit 1
fi
;;
--bip)
shift;
bip_subnet=$1
;;
--default-address-pool)
shift;
pool_subnet=$1
;;
--cgroup-driver)
shift;
cgroup_driver=$1
if [[ ${cgroup_driver} != "cgroupfs" ]] && [[ ${cgroup_driver} != "systemd" ]]; then
printf "\nInvalid value for cgroup-driver.\n"
show_usage
exit 1
fi
;;
--default-runtime)
shift;
default_runtime=$1
;;
-f | --force-restart)
force_restart=1
;;
--)
shift
break
;;
-*)
show_usage
exit 1
;;
*)
show_usage
exit 1
;;
esac
shift
done
[[ "$1" == "" ]] || {
show_usage
exit 1
}
}
function main() {
parse_args "$@"
if [ "$EUID" -ne 0 ]; then
echo "Please run as root."
exit 1
fi
docker_config
log "Done.\n"
exit 0
}
main "$@"