-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathethd
More file actions
executable file
·5888 lines (5343 loc) · 205 KB
/
ethd
File metadata and controls
executable file
·5888 lines (5343 loc) · 205 KB
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -Eeuo pipefail
__project_name="Eth Docker"
__app_name="Ethereum node"
__sample_service="consensus"
__docker_exe="docker"
__old_docker=0
__docker_sudo=""
__docker_major_version=""
__docker_minor_version=""
__docker_patch_version=""
__compose_exe="docker compose"
__old_compose=0
__compose_upgraded=0
__distro=""
__os_major_version=""
__os_minor_version=""
__target_pg=17
__min_ubuntu=22
__suggest_ubuntu="24.04 or 22.04."
__upgrade_ubuntu="24.04: https://gist.github.com/yorickdowne/94f1e5538007f4c9d3da7b22b0dc28a4"
__min_debian=11
__suggest_debian="12 or 11."
__upgrade_debian="12: https://gist.github.com/yorickdowne/ec9e2c6f4f8a2ee93193469d285cd54c"
__eol_os=0
__non_interactive=0
__debug=0
__found=0
__env_file=.env
__during_config=0
__during_update=0
__during_postgres=0
__during_migrate=0
__migrated=0
__keep_targets=1
__target_ver=1
__source_ver=1
__minty_fresh=0
__network_change=0
__handler_ran=0
__deployment=""
__command=""
__params=""
__me=./ethd
__auto_sudo=""
__cannot_sudo=0
__as_owner=""
__owner=""
__owner_group=""
__value="" # This is a global to hand a result back from __get_value_from_env
__free_space=0
__docker_dir="/var/lib/docker"
__keys_args=""
__final_msg=""
__ssv_operator_id=-1
__dodocker() {
${__docker_sudo} ${__docker_exe} "$@"
}
__docompose() {
# I want word splitting here
# shellcheck disable=SC2086
${__docker_sudo} ${__compose_exe} "$@"
}
__determine_distro() {
local uname
# Determine OS platform
uname=$(uname | tr "[:upper:]" "[:lower:]")
# If Linux, try to determine specific distribution
if [[ "${uname}" = "linux" ]]; then
# If available, use LSB to identify distribution
if [[ -n "$(command -v lsb_release 2>/dev/null)" ]]; then
__distro=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//)
# Otherwise, use release info file
else
__distro=$(find /etc -maxdepth 1 -type f -name '[A-Za-z]*[_-][rv]e[lr]*' \
| grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
fi
else
__distro=""
fi
# For everything else (or if above failed), just use generic identifier
[[ -z "${__distro}" ]] && __distro="${uname}"
__distro=$(echo "${__distro}" | tr "[:upper:]" "[:lower:]")
if [[ "${__distro}" = "ubuntu" ]]; then
if [[ "${__cannot_sudo}" -eq 0 ]]; then
if ! dpkg-query -W -f='${Status}' lsb-release 2>/dev/null | grep -q "ok installed"; then
echo "Installing lsb-release"
${__auto_sudo} apt-get update && ${__auto_sudo} apt-get -y install lsb-release
fi
fi
if [[ -n "$(command -v lsb_release 2>/dev/null)" ]]; then
__os_major_version=$(lsb_release -r | cut -d: -f2 | sed s/'^\t'// | cut -d. -f1)
__os_minor_version=$(lsb_release -r | cut -d: -f2 | sed s/'^\t'// | cut -d. -f2)
else
__os_major_version=24 # Without sudo and lsb_release let's just skip the check
__os_minor_version=04
fi
elif [[ "${__distro}" =~ "debian" ]]; then
if [[ "${__cannot_sudo}" -eq 0 ]]; then
if ! dpkg-query -W -f='${Status}' lsb-release 2>/dev/null | grep -q "ok installed"; then
echo "Installing lsb-release"
${__auto_sudo} apt-get update && ${__auto_sudo} apt-get -y install lsb-release
fi
fi
if [[ -n "$(command -v lsb_release 2>/dev/null)" && $(lsb_release -r | cut -f2 2>/dev/null) =~ ^[0-9]+$ ]]; then
__os_major_version=$(lsb_release -r | cut -f2)
__os_minor_version=0
else
__os_major_version=12 # Without sudo and lsb_release let's just skip the check
__os_minor_version=0
fi
fi
}
__handle_docker() {
local exitstatus
local var
local regex
set +e
if [[ "${__distro}" =~ (debian|ubuntu) ]] && ! grep -qi microsoft /proc/version; then
systemctl status docker >/dev/null
exitstatus=$?
if [[ ! "${exitstatus}" -eq 0 ]]; then
echo "The Docker daemon is not running. Please check Docker installation."
echo "\"sudo systemctl status docker\" and \"sudo journalctl -fu docker\" will be helpful."
echo "Aborting."
exit 1
fi
fi
set -e
__docker_version=$(docker --version | awk '{ gsub(/,/, "", $3); print $3 }')
__docker_major_version=$(echo "${__docker_version}" | awk '{ split($1, version, "."); print version[1]; }')
__docker_minor_version=$(echo "${__docker_version}" | awk '{ split($1, version, "."); print version[2]; }')
__docker_patch_version=$(echo "${__docker_version}" | awk '{ split($1, version, "."); print version[3]; }')
if [[ "${__docker_major_version}" -lt 23 ]]; then
__old_docker=1
else
__old_docker=0
fi
if ! docker images >/dev/null 2>&1; then
if [[ "${__cannot_sudo}" -eq 1 ]]; then
echo "Cannot call Docker and cannot use sudo. Please make your user part of the docker group"
exit 1
fi
echo "Will use sudo to access Docker"
__docker_sudo="sudo"
fi
if [[ -f "${__env_file}" && "${__distro}" =~ (debian|ubuntu) ]] && ! grep -qi microsoft /proc/version; then
# This gets used, but shellcheck doesn't recognize that
# shellcheck disable=SC2034
DOCKER_ROOT=$(__dodocker system info --format '{{.DockerRootDir}}')
var=DOCKER_ROOT
__update_value_in_env "${var}" "${!var}" "${__env_file}"
var=NODE_EXPORTER_IGNORE_MOUNT_REGEX
__get_value_from_env "${var}" "${__env_file}" "__value"
# Match either of the patterns Eth Docker uses. Leave alone if there are user changes to the var
# Match literal `'^/(dev|proc|sys|run|some/path/.+)($|/)'` or `'^/(dev|proc|sys|run|var/snap/.+|some/path/.+)($|/)'
regex="^'\^\/\(dev\|proc\|sys\|run\|(var\/snap\/\.\+\|)?[a-zA-Z/]+\/\.\+\)\(\\$\|\/\)'$"
if [[ "${__value}" =~ ${regex} ]]; then
# This gets used, but shellcheck doesn't recognize that
# shellcheck disable=SC2034
NODE_EXPORTER_IGNORE_MOUNT_REGEX="'^/(dev|proc|sys|run|var/snap/.+|${DOCKER_ROOT#/}/.+)($|/)'"
__update_value_in_env "${var}" "${!var}" "${__env_file}"
fi
fi
}
__handle_root() {
if [[ "${EUID}" -eq 0 ]]; then
__as_owner="sudo -u ${__owner}"
else
if groups | grep -q '\bsudo\b' || groups | grep -q '\badmin\b'; then
__auto_sudo="sudo"
else
__cannot_sudo=1
fi
fi
}
__upgrade_docker() {
local runc_version
local runc_major_version
local runc_minor_version
local runc_patch_version
local runc_dpkg_version
local runc_fixed_version
local yn
if [[ -n "${ETHDSECUNDO-}" || ! "${__command}" = "update" ]]; then # Don't run this twice
if (( __docker_major_version < 28 )) ||
(( __docker_major_version == 28 && __docker_minor_version < 5 )) ||
(( __docker_major_version == 28 && __docker_minor_version == 5 && __docker_patch_version < 2 )); then
runc_version=$(runc --version | awk '{ gsub(/[-\+~]/, " ", $3); $0=$0; print $3 }')
runc_major_version=$(echo "${runc_version}" | awk '{ split($1, version, "."); print version[1]; }')
runc_minor_version=$(echo "${runc_version}" | awk '{ split($1, version, "."); print version[2]; }')
runc_patch_version=$(echo "${runc_version}" | awk '{ split($1, version, "."); print version[3]; }')
if [[ "${__distro}" =~ (ubuntu|debian) ]]; then
if dpkg-query -W -f='${Status}' docker.io 2>/dev/null | grep -q "ok installed"; then
if [[ "${__distro}" =~ debian ]]; then
if [[ "${__os_major_version}" -gt 13 ]]; then # Assume Debian 14 and up are fine
return
fi
runc_dpkg_version=$(dpkg-query -W -f='${Version}\n' runc)
case ${__os_major_version} in
# These fixed versions are a guess and have not been released
13) runc_fixed_version="1.1.15+ds1-2+deb13u1";;
12) runc_fixed_version="1.1.5+ds1-1+deb12u2";; # I don't expect to hit this, because docker.io is old
11) runc_fixed_version="1.0.0~rc93+ds1-5+deb11u6";; # I don't expect to hit this, because docker.io is old
*) echo "Cannot check runc on Debian ${__os_major_version}. Please update to ${__min_debian} or later"; return;;
esac
if dpkg --compare-versions "${runc_dpkg_version}" lt "${runc_fixed_version}"; then
echo
echo "Docker ${__docker_version} with runc ${runc_version} detected"
echo "This version of runc is vulnerable"
echo "If an updated version of runc is available, please install it"
echo "Alternatively, consider uninstalling runc and replacing it with crun"
# Commented out until fixed versions have actually been released
# if [ "${__non_interactive}" -eq 0 ]; then
# while true; do
# read -rp "Do you want to update runc (yes/no) " yn
# case "${yn}" in
# [Nn]*) echo "Please be sure to update runc yourself!"; return;;
# *)
# ${__auto_sudo} apt-get update && ${__auto_sudo} apt-get install --only-upgrade runc
# break
# ;;
# esac
# done
# fi
fi
else # This is Ubuntu and they fixed it with runc 1.3.3
if (( runc_major_version == 1 && runc_minor_version < 3 )) ||
(( runc_major_version == 1 && runc_minor_version == 3 && runc_patch_version < 3 )); then
echo
echo "Docker ${__docker_version} with runc ${runc_version} detected"
echo "This version of runc is vulnerable"
__nag_os_version
if [ "${__eol_os}" -eq 1 ]; then
echo "${__project_name} cannot update runc on Ubuntu ${__os_major_version}."
return
fi
echo "It is recommended that you update runc"
if [ "${__non_interactive}" -eq 0 ]; then
while true; do
read -rp "Do you want to update runc (yes/no) " yn
case "${yn}" in
[Nn]*) echo "Please be sure to update runc yourself!"; return;;
*)
${__auto_sudo} apt-get update && ${__auto_sudo} apt-get install --only-upgrade runc
break
;;
esac
done
fi
fi
fi
elif dpkg-query -W -f='${Status}' docker-ce 2>/dev/null | grep -q "ok installed"; then
echo
echo "Docker ${__docker_version} detected"
echo "This version of Docker has a vulnerable runc binary"
__nag_os_version
if [[ "${__eol_os}" -eq 1 ]]; then
echo "${__project_name} cannot update Docker-CE on ${__distro} ${__os_major_version}."
return
fi
echo "It is recommended that you update Docker-CE"
if [[ "${__non_interactive}" -eq 0 ]]; then
while true; do
read -rp "Do you want to update Docker-CE? (yes/no) " yn
case "${yn}" in
[Nn]*) echo "Please be sure to update Docker CE yourself!"; return;;
*)
${__auto_sudo} apt-get update && ${__auto_sudo} apt-get install --only-upgrade docker-ce containerd.io
break
;;
esac
done
fi
fi
else
if (( runc_major_version == 1 && runc_minor_version < 2 )) ||
(( runc_major_version == 1 && runc_minor_version == 2 && runc_patch_version < 8 )) ||
(( runc_major_version == 1 && runc_minor_version == 3 && runc_patch_version < 3 )); then
echo
echo "Docker ${__docker_version} with runc ${runc_version} detected"
echo "This version of runc may be vulnerable, but ${__project_name} cannot update it on ${__distro}"
fi
fi
fi
fi
}
__upgrade_compose() {
if ! type -P docker-compose >/dev/null 2>&1; then
echo "Docker Compose has already been updated to V2"
return
fi
echo "Updating Docker Compose to V2"
if [[ "${__distro}" = "ubuntu" ]]; then
__nag_os_version
if [[ "${__eol_os}" -eq 1 ]]; then
echo "${__project_name} cannot update Docker Compose on Ubuntu ${__os_major_version}."
exit 1
fi
${__auto_sudo} apt-get update
${__auto_sudo} apt-get install -y docker-compose-v2 docker-buildx
echo "Installed docker-compose-v2"
__old_compose=0
__compose_upgraded=1
if dpkg-query -W -f='${Status}' docker.io 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-mark manual docker.io
elif dpkg-query -W -f='${Status}' docker-ce 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-mark manual docker-ce
fi
${__auto_sudo} apt-get remove -y docker-compose
echo "Removed docker-compose"
elif [[ "${__distro}" =~ "debian" ]]; then
${__auto_sudo} apt-get update && ${__auto_sudo} apt-get -y install ca-certificates curl gnupg
__nag_os_version
if [[ "${__eol_os}" -eq 1 ]]; then
echo "${__project_name} cannot update Docker Compose on Debian ${__os_major_version}."
exit 1
fi
${__auto_sudo} mkdir -p /etc/apt/keyrings
${__auto_sudo} curl -fsSL https://download.docker.com/linux/debian/gpg | ${__auto_sudo} gpg --dearmor --yes \
-o /etc/apt/keyrings/docker.gpg
${__auto_sudo} echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/debian $(lsb_release -cs) stable" \
| ${__auto_sudo} tee /etc/apt/sources.list.d/docker.list > /dev/null
${__auto_sudo} apt-get update
${__auto_sudo} apt-get install -y docker-compose-plugin docker-buildx-plugin
echo "Installed docker-compose-plugin"
__old_compose=0
__compose_upgraded=1
if dpkg-query -W -f='${Status}' docker.io 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-mark manual docker.io
elif dpkg-query -W -f='${Status}' docker-ce 2>/dev/null | grep -q "ok installed"; then
${__auto_sudo} apt-mark manual docker-ce
fi
${__auto_sudo} apt-get remove -y docker-compose
echo "Removed docker-compose"
else
echo "${__project_name} does not know how to update Docker Compose on ${__distro}"
fi
}
__check_compose_version() {
local yn
# Check for Compose V2 (docker compose) vs Compose V1 (docker-compose)
if docker compose version >/dev/null 2>&1; then
__compose_version=$(${__docker_sudo} docker compose version | sed -n -E -e "s/.*version [v]?([0-9.-]*).*/\1/ip")
__compose_major=${__compose_version%%.*}
__compose_minor=${__compose_version#*.}
__compose_minor=${__compose_minor%%.*}
if [[ "${__compose_major}" -eq 1 ]]; then
__old_compose=1
elif [[ "${__compose_minor}" -lt 18 ]]; then
__old_compose=1
else
__old_compose=0
fi
else
__old_compose=1
__compose_version=$(${__docker_sudo} docker-compose --version | sed -n -E -e "s/.*version [v]?([0-9.-]*).*/\1/ip")
__compose_major=${__compose_version%%.*}
__compose_minor=${__compose_version#*.}
__compose_minor=${__compose_minor%%.*}
fi
if [ "${__old_compose}" -eq 1 ]; then
if [[ "${__compose_major}" -eq 1 ]]; then
# This runs before the actual update command, in the main section. ethd exits if it can't upgrade, therefore
# no check for ${ETHDSECUNDO} or command update, here
echo
echo "You are using docker-compose ${__compose_version}, which is unsupported by Docker, Inc."
echo "${__project_name} only supports Compose V2."
echo
if [[ "${__distro}" = "ubuntu" ]]; then
echo "It is recommended that you replace Compose V1 with Compose V2."
if [[ "${__non_interactive}" -eq 0 ]]; then
while true; do
read -rp "Do you want to update Docker Compose to V2? (yes/no) " yn
case "${yn}" in
[Nn]*)
echo "Please be sure to update Docker Compose yourself!"
echo "You can install it with \"sudo apt update && sudo apt install docker-compose-v2\"."
echo "You can remove the old docker-compose:"
echo "\"sudo apt-mark manual docker.io && sudo apt --autoremove remove docker-compose\""
break
;;
*) __upgrade_compose; break;;
esac
done
fi
elif [[ "${__distro}" =~ debian ]]; then
if dpkg-query -W -f='${Status}' docker.io 2>/dev/null | grep -q "ok installed"; then
if [[ "${__os_major_version}" -lt 13 ]]; then
echo "Debian ${__os_major_version}'s docker.io does not ship with Compose V2. Please replace docker.io with docker-ce"
echo "See https://ethdocker.com/Usage/Prerequisites#switching-from-dockerio-to-docker-ce"
else
echo "Debian ${__os_major_version} ships with Compose V2."
echo "This is very unexpected, and ${__project_name} is not sure what to recommend."
echo "Please come to ethstaker Discord: http://discord.gg/ethstaker"
fi
else
echo "You appear to be using Docker CE, which ships with Compose V2."
echo "This is very unexpected, and ${__project_name} is not sure what to recommend."
echo "Please come to ethstaker Discord: http://discord.gg/ethstaker"
fi
else
echo "${__project_name} does not know how to update Docker Compose on ${__distro}"
fi
else # Old Compose V2
if [[ -n "${ETHDSECUNDO-}" || ! "${__command}" = "update" ]]; then # Don't run this twice
true # Nothing now for old V2; maybe in future we'll do the update for the user
fi
fi
fi
}
__prep_conffiles() {
# Create custom-prom.yml if it doesn't exist
if [[ ! -f "./prometheus/custom-prom.yml" ]]; then
${__as_owner} touch "./prometheus/custom-prom.yml"
fi
# Make sure the prometheus dir and its contents are accessible to user nobody
if find prometheus -type d \! -perm 755 | grep -q .; then
find prometheus -type d -exec chmod 755 {} \;
fi
if find prometheus -name '*.yml' \! -perm 664 | grep -q .; then
find prometheus -name '*.yml' -exec chmod 664 {} \;
fi
# Create config.yaml if it doesn't exist
if [[ ! -f "ssv-config/config.yaml" ]]; then
${__as_owner} cp ssv-config/config.yaml.sample ssv-config/config.yaml
fi
if [[ ! -f "ssv-config/dkg-config.yaml" ]]; then
${__as_owner} cp ssv-config/dkg-config.yaml.sample ssv-config/dkg-config.yaml
fi
# Make sure ssv-config yaml files are 664
if find ssv-config -maxdepth 0 \! -perm 755 | grep -q .; then
chmod 755 ssv-config
fi
if find ssv-config -name '*.yaml' \! -perm 664 | grep -q .; then
chmod 664 ssv-config/*.yaml
fi
# Make sure local user owns the dkg output dir and everything in it
if find .eth/dkg_output \( \! -user "${__owner}" -o \! -group "${__owner_group}" \) -print -quit | grep -q .; then
if [[ "${__cannot_sudo}" -eq 0 ]]; then
echo "Fixing ownership of .eth/dkg_output"
${__auto_sudo} chown -R "${__owner}:${__owner_group}" .eth/dkg_output
${__auto_sudo} chmod 755 .eth/dkg_output
else
echo "Ownership of .eth/dkg_output should be fixed, but this user can't sudo"
fi
fi
# Make sure the dkg output dir and its subdirs are mod 0755
if find .eth/dkg_output -type d \! -perm 755 | grep -q .; then
find .eth/dkg_output -type d -exec chmod 755 {} \;
fi
# Make sure the contents of the dkg output dir are mod 0644
if find .eth/dkg_output -type f \! -perm 644 | grep -q .; then
find .eth/dkg_output -type f -exec chmod 644 {} \;
fi
# Create cb-config.toml if it doesn't exist
if [[ ! -f "commit-boost/cb-config.toml" ]]; then
${__as_owner} cp commit-boost/cb-config.toml.sample commit-boost/cb-config.toml
fi
# Create tempo.yaml if it doesn't exist
if [[ ! -f "tempo/tempo.yaml" ]]; then
${__as_owner} cp tempo/tempo.yaml.sample tempo/tempo.yaml
fi
if [[ ! -f "tempo/overrides.yaml" ]]; then
${__as_owner} cp tempo/overrides.yaml.sample tempo/overrides.yaml
fi
# Make sure local user owns .env
if find . -name .env \( \! -user "${__owner}" -o \! -group "${__owner_group}" \) -print -quit | grep -q ./.env; then
if [[ "${__cannot_sudo}" -eq 0 ]]; then
echo "Fixing ownership of .env"
${__auto_sudo} chown -R "${__owner}:${__owner_group}" .env
${__auto_sudo} chmod -R 644 .env
if [[ -f .env.tmp ]]; then
${__auto_sudo} rm -f .env.tmp
fi
else
echo "Ownership of .env should be fixed, but this user can't sudo"
fi
fi
}
__check_for_snap() {
if [[ "${__distro}" = "ubuntu" && -n "$(command -v snap)" ]] && snap list 2>/dev/null | grep -qw 'docker'; then
echo
echo "WARNING! Snap Docker package detected. This WILL result in issues."
echo "Removing the package will delete volumes and require a resync."
echo
echo "Doing so is still highly recommended however."
echo
echo "The exact steps depend a little on whether there already is an apt version of Docker installed as well,"
echo "but in a nutshell \"${__me} stop\" followed by \"sudo snap remove --purge docker\" followed by a reboot,"
echo "and as needed install docker-ce or docker.io with apt."
echo
echo "Do join us on EthStaker Discord at http://discord.gg/ethstaker to work through this issue."
echo
echo "Aborting, this is not safe"
exit 1
fi
}
__install_bash_completions() {
if [[ "$OSTYPE" = "darwin"* ]]; then
echo "Skipping installation of tab completions (not supported on macOS)"
else
if [[ ! -f "$(pkg-config --variable=completionsdir bash-completion)/ethd" ]]; then
echo "Installing bash completions for ethd"
${__auto_sudo} ln -sf "$(dirname "$(realpath "${BASH_SOURCE[0]}")")/bash-completion" \
"$(pkg-config --variable=completionsdir bash-completion)/ethd" || true
fi
fi
}
# Persist noatime in fstab for a given mount point
__fstab_noatime() {
local mountpoint="$1"
local fstab="/etc/fstab"
local backup="${fstab}.bak"
local noatime="${fstab}.noatime"
# Check if fstab already has an entry for this mountpoint
if grep -E "^[^#].*\s${mountpoint//\//\\/}(\s|\$)" "${fstab}" >/dev/null; then
echo
echo "Found ${fstab} entry for ${mountpoint}, creating a version with \"noatime\""
echo
${__auto_sudo} cp ${fstab} ${backup}
# Edit the existing line: add noatime if not already there
awk -v mountpoint="${mountpoint}" '
/^[ \t]*#/ || NF < 6 { print; next }
{
line = $0
split(line, fields, /[ \t]+/)
mount = fields[2]
opts = fields[4]
if (mount == mountpoint && opts !~ /(^|,)noatime(,|$)/) {
# Match the byte offset of the first four fields
match(line, /^[ \t]*[^ \t]+[ \t]+[^ \t]+[ \t]+[^ \t]+[ \t]+/)
prefix = substr(line, 1, RLENGTH)
rest = substr(line, RLENGTH + 1)
# Extract first word from rest (the options field)
match(rest, /^[^ \t]+/)
opts_field = substr(rest, RSTART, RLENGTH)
opts_field = opts_field ",noatime"
suffix = substr(rest, RSTART + RLENGTH)
line = prefix opts_field suffix
}
print line
}' "${fstab}" | ${__auto_sudo} tee "${noatime}" >/dev/null
${__auto_sudo} chmod 664 "${noatime}"
echo "${fstab} has not been altered. A backup copy is in ${backup}"
echo "If the below diff looks good to you, you can \"sudo cp ${noatime} ${fstab}\""
echo "followed by \"sudo mount -o remount ${mountpoint}\" to verify everything still works."
echo
echo "Note: You may see a warning suggesting 'systemctl daemon-reload'. This is only relevant"
echo "for systemd's internal mount unit management and does not affect the active mount options"
echo "applied via 'mount'."
echo
diff ${noatime} ${fstab} || true
else
echo "No ${fstab} entry for ${mountpoint} — not adding one automatically."
echo "Manual intervention required for \"noatime\" persistence."
fi
}
__optimize_chrony() {
local file
local new_file
if [[ -f /etc/chrony/chrony.conf && -d /etc/chrony/sources.d ]]; then
echo
echo "Configuring chrony to use Google's time-smearing servers"
if ! ${__auto_sudo} sed -Ei.bak '/^\s*(pool|server|leapsectz)\b/ s/^/#/' "/etc/chrony/chrony.conf"; then
echo "Failed to alter /etc/chrony/chrony.conf"
echo "Manual intervention is required to use Google's time-smearing servers with chrony"
return 0
fi
while IFS= read -r -d '' file; do
new_file="${file}.bak"
if ! ${__auto_sudo} mv "${file}" "${new_file}"; then
printf "Failed to rename %s to %s\n" "${file}" "${new_file}"
return 0
fi
done < <(find "/etc/chrony/sources.d" -maxdepth 1 -type f -name '*.sources' -print0)
if ! ${__auto_sudo} tee "/etc/chrony/sources.d/google-ntp.sources" >/dev/null <<EOF
# Google's time-smearing NTP servers
# Requires leapsectz in chrony.conf to be commented out or not exist
pool time.google.com iburst minpoll 4 maxpoll 6 polltarget 16
EOF
then
printf "Failed to write Google servers to %s\n" "/etc/chrony/sources.d/google-ntp.sources"
return 0
fi
if ! ${__auto_sudo} systemctl restart chrony; then
echo "Failed to restart the chrony service"
echo "Manual intervention is required to have it use the new configuration"
fi
fi
}
# Apply common performance optimizations on behalf of the user
__optimize_host() {
local docker_root
local docker_mount
local docker_mount_opts
local exitstatus
local swappiness
local cache_pressure
local target_pressure
local memtotal
echo
docker_root=$(__dodocker system info --format '{{.DockerRootDir}}')
if command -v findmnt >/dev/null; then
docker_mount=$(findmnt -T "${docker_root}" -o TARGET -n)
docker_mount_opts=$(findmnt -T "${docker_root}" -o OPTIONS -n 2>/dev/null)
else
docker_mount=$(df --output=target "${docker_root}" | tail -1)
docker_mount_opts=$(awk -v path="${docker_mount}" '$2 == path {print $4}' /proc/mounts)
fi
if [[ "${docker_mount_opts}" == *noatime* ]]; then
echo "\"noatime\" already set on ${docker_mount}"
else
echo "Remounting ${docker_mount} with \"noatime\""
${__auto_sudo} mount -o remount,noatime "${docker_mount}" || true
exitstatus=$?
if [[ ${exitstatus} -eq 0 ]]; then
__fstab_noatime "${docker_mount}"
else
echo "Applying \"noatime\" to ${docker_mount} failed"
fi
fi
swappiness=$(${__auto_sudo} sysctl -n vm.swappiness)
if [[ "${swappiness}" -gt 1 ]]; then
echo "Adjusting swappiness down to 1 from ${swappiness}"
${__auto_sudo} sysctl -w vm.swappiness=1
if [[ -d /etc/sysctl.d ]]; then
echo "Persisting vm.swappiness=1 in /etc/sysctl.d/10-swappiness.conf"
echo "vm.swappiness=1" | ${__auto_sudo} tee /etc/sysctl.d/10-swappiness.conf >/dev/null
elif [[ -f /etc/sysctl.conf ]]; then
if ! grep -E '^\s*vm\.swappiness\s*=' /etc/sysctl.conf >/dev/null; then
echo "Appending vm.swappiness=1 to /etc/sysctl.conf"
echo "vm.swappiness=1" | ${__auto_sudo} tee -a /etc/sysctl.conf > /dev/null
else
echo "vm.swappiness is already set in /etc/sysctl.conf"
fi
else
echo "Unable to find /etc/sysctl.conf or /etc/sysctl.d"
echo "Manual intervention is required to set swappiness permanently"
fi
fi
cache_pressure=$(${__auto_sudo} sysctl -n vm.vfs_cache_pressure)
memtotal=$(awk '/MemTotal/ {printf "%d", int($2/1024/1024)}' /proc/meminfo)
if [[ "${memtotal}" -ge 60 ]]; then
target_pressure=10
elif [[ "${memtotal}" -ge 30 ]]; then
target_pressure=25
else
target_pressure=50
fi
if [[ "${cache_pressure}" -gt "${target_pressure}" ]]; then
echo "Adjusting vfs_cache_pressure down to ${target_pressure} from ${cache_pressure}"
${__auto_sudo} sysctl -w vm.vfs_cache_pressure="${target_pressure}"
if [[ -d /etc/sysctl.d ]]; then
echo "Persisting vm.vfs_cache_pressure=${target_pressure} in /etc/sysctl.d/10-vfs_cache_pressure.conf"
echo "vm.vfs_cache_pressure=${target_pressure}" | ${__auto_sudo} tee /etc/sysctl.d/10-vfs_cache_pressure.conf >/dev/null
elif [[ -f /etc/sysctl.conf ]]; then
if ! grep -E '^\s*vm\.vfs_cache_pressure\s*=' /etc/sysctl.conf >/dev/null; then
echo "Appending vm.vfs_cache_pressure=${target_pressure} to /etc/sysctl.conf"
echo "vm.vfs_cache_pressure=${target_pressure}" | ${__auto_sudo} tee -a /etc/sysctl.conf > /dev/null
else
echo "vm.vfs_cache_pressure is already set in /etc/sysctl.conf"
fi
else
echo "Unable to find /etc/sysctl.conf or /etc/sysctl.d"
echo "Manual intervention is required to set vfs_cache_pressure permanently"
fi
fi
__optimize_chrony
return 0
}
__install_docker() {
local repo
local groups
if [[ "${__distro}" = "ubuntu" ]]; then
repo=ubuntu
elif [[ "${__distro}" = *"debian"* ]]; then
repo=debian
else
echo
echo "__install_docker() was called on ${__distro}, which is neither Debian nor Ubuntu."
echo "This is a bug."
exit 70
fi
if [[ -z "$(command -v docker)" ]]; then
${__auto_sudo} mkdir -p /etc/apt/keyrings
curl -fsSL "https://download.docker.com/linux/${repo}/gpg" | ${__auto_sudo} gpg --dearmor \
--yes -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/${repo} $(lsb_release -cs) stable" \
| ${__auto_sudo} tee /etc/apt/sources.list.d/docker.list > /dev/null
${__auto_sudo} apt-get update
${__auto_sudo} apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin \
docker-buildx-plugin
${__auto_sudo} systemctl start systemd-logind
echo "Installed docker-ce and docker-compose-plugin"
else
echo "Docker is already installed"
fi
groups=$(${__as_owner} groups)
if [[ ! "${groups}" =~ "docker" ]]; then
echo "Making your user part of the docker group"
${__auto_sudo} usermod -aG docker "${__owner}"
echo "Please run newgrp docker or log out and back in"
else
echo "Your user is already part of the docker group"
fi
return 0
}
install() {
local yn
if ! [[ "${__distro}" =~ (ubuntu|debian) ]]; then
echo "${__project_name} does not know how to install Docker on ${__distro}"
return 0
fi
__nag_os_version
if [[ "${__eol_os}" -eq 1 ]]; then
echo "${__project_name} requires an upgraded Linux distribution to run install."
return 0
fi
if [[ "${__cannot_sudo}" -eq 1 ]]; then
echo "The install command requires the user to be part of the sudo group, or on macOS the admin group"
return 0
fi
echo "Installing packages that ${__project_name} requires or considers helpful"
echo
${__auto_sudo} apt-get update
${__auto_sudo} apt-get install -y ca-certificates curl gnupg whiptail chrony pkg-config screen ncdu gzip speedtest-cli jq iputils-ping
echo
read -rp "Do you want to install Docker and make your user part of the docker group? (no/yes) " yn
case "${yn}" in
[Yy]*) __install_docker;;
*) ;;
esac
if [[ -n "$(command -v docker)" ]]; then
__handle_docker
else
echo "${__project_name} requires Docker to be present to continue with install steps."
echo "Please install Docker and run \"${__me} install\" again."
return 0
fi
echo
read -rp "Do you want ${__project_name} to apply common performance optimizations? (no/yes) " yn
case "${yn}" in
[Yy]*) __optimize_host;;
*) ;;
esac
__install_bash_completions
if ! grep -q "alias ethd" ~/.profile; then # If an alias already exists, don't touch it
echo
read -rp "Do you want to be able to call 'ethd' from anywhere? (yes/no) " yn
case "${yn}" in
[Nn]*) return 0;;
*) ;;
esac
echo "alias ethd=$(realpath "${BASH_SOURCE[0]}")" >>~/.profile
echo "cat $(dirname "$(realpath "${BASH_SOURCE[0]}")")/.motd" >>~/.profile
echo "Go ahead and 'source ~/.profile' or log out and back in."
echo "After that, you can use the command 'ethd'."
fi
return 0
}
# set global __free_space to what's available to Docker
__get_docker_free_space() {
local regex
if [[ "$OSTYPE" = "darwin"* ]]; then # macOS doesn't expose docker root dir to the OS
__free_space=$(__dodocker run --rm -v macos-space-check:/dummy busybox df -P /dummy | awk '/[0-9]%/{print $(NF-2)}')
else
__docker_dir=$(__dodocker system info --format '{{.DockerRootDir}}')
__free_space=$(df -P "${__docker_dir}" | awk '/[0-9]%/{print $(NF-2)}')
fi
regex='^[0-9]+$'
if ! [[ "${__free_space}" =~ ${regex} ]] ; then
echo "Unable to determine free disk space. This is likely to be a bug."
if [[ "$OSTYPE" = "darwin"* ]]; then
echo "df reports $(__dodocker run --rm -v macos-space-check:/dummy busybox df -P /dummy) and __free_space is ${__free_space}"
else
echo "df reports $(df -P "${__docker_dir}") and __free_space is ${__free_space}"
fi
exit 70
fi
}
__display_docker_dir() {
if [[ "$OSTYPE" = "darwin"* ]]; then # macOS doesn't expose docker root dir to the OS
echo "Here's total and used space on Docker's virtual volume"
__dodocker run --rm -v macos-space-check:/dummy busybox df -h /dummy
else
echo "Here's total and used space on ${__docker_dir}"
df -h "${__docker_dir}"
fi
}
__display_docker_volumes() {
echo
# Assume project name and volume are delimited by _ and there is no _ in the volume name. Done to avoid catching project-2 while looking at project
if [[ -z "$(__dodocker volume ls -q -f "name=^$(basename "$(realpath .)" | tr '[:upper:]' '[:lower:]')_[^_]+$")" ]]; then
echo "There are no Docker volumes for this copy of ${__project_name}"
echo
else
echo "Here are the Docker volumes used by this copy of ${__project_name} and their space usage:"
__dodocker system df -v | grep -A 500 "VOLUME NAME" | grep -iE "^$(basename "$(dirname "$(realpath "${BASH_SOURCE[0]}")")")_[^_]+$"
echo
echo "If your Consensus Layer client takes more than 300 GiB, you can resync it with"
echo "\"${__me} resync-consensus\"."
echo
fi
if command -v ncdu >/dev/null 2>&1; then
echo "If there is some mystery space being taken up, try \"sudo ncdu /\"."
else
echo "If there is some mystery space being taken up, install ncdu, then try \"sudo ncdu /\"."
if [[ "$OSTYPE" == "darwin"* ]]; then
echo "To install ncdu, run \"brew install ncdu\"."
elif [[ "${__distro}" =~ (ubuntu|debian) ]]; then
echo "To install ncdu, run \"sudo apt update && sudo apt install ncdu\"."
else
echo "How to install ncdu will be specific to your distribution ${__distro}."
fi
fi
echo
}
space() {
local var
__get_docker_free_space
echo
if [[ "$OSTYPE" = "darwin"* ]]; then # macOS doesn't expose docker root dir to the OS
echo "You have $(( __free_space / 1024 / 1024 )) GiB free for Docker volumes"
else
echo "You have $(( __free_space / 1024 / 1024 )) GiB free on ${__docker_dir}"
fi
echo
__display_docker_dir
__display_docker_volumes
var=ANCIENT_DIR
__get_value_from_env "${var}" "${__env_file}" "ANCIENT_DIR"
if [[ -n "${ANCIENT_DIR}" ]]; then
echo
${__auto_sudo} du -sh "${ANCIENT_DIR}"
fi
}
# Warn user if space is low, so they can prune
# This runs after any ethd command: It is safe to assign to uppercase vars
__check_disk_space() {
local low=0
min_free=314572800
min_gib=300
safe_prune=250
__get_docker_free_space
var="COMPOSE_FILE"
__get_value_from_env "${var}" "${__env_file}" "__value"
var="AUTOPRUNE_NM"
__get_value_from_env "${var}" "${__env_file}" "AUTOPRUNE_NM"
var="NETWORK"
__get_value_from_env "${var}" "${__env_file}" "NETWORK"
var="EL_NODE_TYPE"
__get_value_from_env "${var}" "${__env_file}" "EL_NODE_TYPE"
if [[ "${NETWORK}" =~ ^(mainnet|gnosis)$ ]]; then
min_free=314572800
min_gib=300
safe_prune=250
else
min_free=31457280
min_gib=30
safe_prune=25
fi
# Literal match intended
# shellcheck disable=SC2076
if [[ "${__value}" =~ "nethermind.yml" && "${__free_space}" -lt "${min_free}" ]]; then
low=1
echo
echo "You are running Nethermind and have less than ${min_gib} GiB of free disk space."
# shellcheck disable=SC2154
if [[ "${AUTOPRUNE_NM}" = true ]]; then
echo "It should currently be auto-pruning, check logs with \"${__me} logs -f --tail 500 execution | grep \
Full\". Free space:"
else
echo "If the below reads above ${safe_prune} GiB free, prune it with \"${__me} prune-nethermind\""
fi
echo
__display_docker_dir
__display_docker_volumes
elif [[ "${__value}" =~ "geth.yml" && "${__free_space}" -lt 104857600 ]]; then
low=1
echo
echo "You are running Geth and have less than 100 GiB of free disk space."
echo "You may resync from scratch to use PBSS and slow on-disk DB growth, with \"${__me} resync-execution\"."
echo
__display_docker_dir
__display_docker_volumes
elif [[ "${__value}" =~ "besu.yml" && "${__free_space}" -lt 52428800 ]]; then
low=1
echo
echo "You are running Besu and have less than 50 GiB of free disk space."
echo
echo "If this is a long-running Besu, you may prune trie-logs with \"${__me} prune-besu\"."
echo
__display_docker_dir
__display_docker_volumes
elif [[ "${__free_space}" -lt 52428800 ]]; then
low=1
echo
echo "You have less than 50 GiB of free disk space:"
echo
__display_docker_dir
__display_docker_volumes
fi
if [[ "${low}" -eq 1 && "${NETWORK}" = "mainnet" ]]; then
#shellcheck disable=SC2154