-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathvirtualmin-install.sh
More file actions
2281 lines (2061 loc) · 68.9 KB
/
virtualmin-install.sh
File metadata and controls
2281 lines (2061 loc) · 68.9 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
#!/bin/sh
# shellcheck disable=SC2059 disable=SC2181 disable=SC2154 disable=SC2317 disable=SC2034 disable=SC2329
# virtualmin-install.sh
# Copyright 2005-2026 The Virtualmin Developers
# Simple script to install Virtualmin on a supported OS
# Different installation guides are available at:
# https://www.virtualmin.com/docs/installation/guides
# License and version
SERIAL="${SERIAL:-GPL}"
KEY="${KEY:-GPL}"
VER=8.1.2
vm_version=8
# Server
download_old_virtualmin_host="software.virtualmin.com"
download_virtualmin_host="${download_virtualmin_host:-download.virtualmin.com}"
download_virtualmin_host_lib="$download_virtualmin_host"
download_virtualmin_host_dev="${download_virtualmin_host_dev:-download.virtualmin.dev}"
download_virtualmin_host_rc="${download_virtualmin_host_rc:-rc.download.virtualmin.dev}"
download_webmin_host="${download_webmin_host:-download.webmin.com}"
download_webmin_host_dev="${download_webmin_host_dev:-download.webmin.dev}"
download_webmin_host_rc="${download_webmin_host_rc:-rc.download.webmin.dev}"
# Save current working directory
pwd="$PWD"
# License file
virtualmin_license_file="/etc/virtualmin-license"
# Script name
if [ "$0" = "--" ] || [ -z "$0" ]; then
script_name="virtualmin-install.sh"
else
script_name=$(basename -- "$0")
fi
# Set log type
log_file_name="${install_log_file_name:-virtualmin-install}"
# Set defaults
branch='stable'
bundle='LAMP' # Other option is LEMP
mode="${mode:-full}" # Other option is mini
skipyesno=0
config_excludes="${config_excludes:-}"
config_includes="${config_includes:-}"
extra_packages="${extra_packages:-}"
usage() {
# shellcheck disable=SC2046
echo
printf "Usage: %s [options]\\n" "$(basename "$0")"
echo
echo " If called without arguments, installs Virtualmin with default options."
echo
printf " --bundle|-b <LAMP|LEMP> bundle to install (default: LAMP)\\n"
printf " --type|-t <full|mini> install type (default: full)\\n"
printf " --os-grade|-g <A|B> operating system support grade (default: A)\\n"
printf " --branch|-B <stable|rc|devel> install branch (default: stable)\\n"
echo
printf " --extra|-E <name[,name..]> install extra packages before stack install\\n"
printf " --exclude|-e <name[,name..]> exclude plugin from configuration phase\\n"
printf " --include|-i <name[,name..]> include plugin in configuration phase\\n"
printf " --module|-o load custom module in post-install phase\\n"
echo
printf " --hostname|-n force hostname during install\\n"
printf " --no-package-updates|-x skip package updates during install\\n"
printf " --no-hostname-ssl|-nhs skip SSL certificate request for hostname\\n"
echo
printf " --uninstall|-u remove all packages and dependencies\\n"
printf " --setup|-s reconfigure repos without installing\\n"
printf " --connect|-C <ipv4|ipv6> test connectivity without installing\\n"
printf " --insecure-downloads|-I skip SSL certificate check for downloads\\n"
echo
printf " --force|-f|--yes|-y assume \"yes\" to all prompts\\n"
printf " --force-reinstall|-fr force complete reinstall (not recommended)\\n"
printf " --no-banner|-nb suppress installation messages and warnings\\n"
printf " --verbose|-V enable verbose mode\\n"
echo
printf " --version|-v show installer version\\n"
printf " --help|-h show this help\\n"
echo
}
# Bind hooks
bind_hook() {
hook="$1"
shift
pre_hook="pre_hook__$hook"
post_hook="post_hook__$hook"
# Do we want to completely override the original function?
if command -v "hook__$hook" > /dev/null 2>&1; then
"hook__$hook" "$@"
# Or do we want to run the original function wrapped by third-party functions?
else
if command -v "$pre_hook" > /dev/null 2>&1; then
"$pre_hook" "$@"
fi
if command -v "$hook" > /dev/null 2>&1; then
"$hook" "$@"
fi
if command -v "$post_hook" > /dev/null 2>&1; then
"$post_hook" "$@"
fi
fi
}
test_connection() {
input="$1"
ip_version="$2"
ip_version_nice=$(echo "$ip_version" | sed 's/ip/IP/')
timeout=5
http_protocol="http"
http_protocol_nice=$(echo "$http_protocol" | tr '[:lower:]' '[:upper:]')
# Setup colors for messages
GREEN="" BLACK="" RED="" RESET="" BOLD="" GRBG="" REDBG=""
if command -pv 'tput' > /dev/null; then
GREEN=$(tput setaf 2)
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
RESET=$(tput sgr0)
BOLD=$(tput bold)
GRBG=$(tput setab 22; tput setaf 10)
REDBG=$(tput setab 52; tput setaf 9)
fi
# Extract the domain from the input
domain=$(echo "$input" | awk -F[/:] '{print $4}')
[ -z "$domain" ] && domain="$input"
# Validate parameters
if [ -z "$domain" ] || [ -z "$ip_version" ]; then
echo "${RED}[ERROR] ${RESET} Domain and IP version are required" >&2
return 1
fi
# Setup protocol-specific flags
case "$ip_version" in
ipv4)
if ! getent ahostsv4 "$domain" >/dev/null 2>&1; then
echo "${RED}[ERROR] ${RESET} ${BOLD}$domain${RESET} — cannot find IPv4 address" >&2
return 1
fi
ping_cmd="ping -c 1 -W $timeout $domain"
http_cmd="curl -sS --ipv4 --max-time $timeout --head $http_protocol://$domain \
|| wget --spider -4 -T $timeout $http_protocol://$domain"
;;
ipv6)
if ! getent ahostsv6 "$domain" >/dev/null 2>&1; then
echo "${RED}[ERROR] ${RESET} ${BOLD}$domain${RESET} — cannot find IPv6 address" >&2
return 1
fi
ping_cmd="ping6 -c 1 -W $timeout $domain"
http_cmd="curl -sS --ipv6 --max-time $timeout --head $http_protocol://$domain \
|| wget --spider -6 -T $timeout $http_protocol://$domain"
;;
esac
# Try ping first
if eval "$ping_cmd" >/dev/null 2>&1; then
echo "${GREEN}[SUCCESS]${RESET} ${GRBG}[$ip_version_nice]${RESET} ${GRBG}[ICMP]${RESET} ${BOLD}$domain${RESET}"
else
echo "${RED}[ERROR] ${RESET} ${REDBG}[$ip_version_nice]${RESET} ${REDBG}[ICMP]${RESET} ${BOLD}$domain${RESET}"
fi
# HTTP test as well
if command -v 'curl' > /dev/null || command -v 'wget' > /dev/null; then
if eval "$http_cmd" >/dev/null 2>&1; then
echo "${GREEN}[SUCCESS]${RESET} ${GRBG}[$ip_version_nice]${RESET} ${GRBG}[$http_protocol_nice]${RESET} ${BOLD}$domain${RESET}"
return 0
else
echo "${RED}[ERROR] ${RESET} ${REDBG}[$ip_version_nice]${RESET} ${REDBG}[$http_protocol_nice]${RESET} ${BOLD}$domain${RESET}"
return 1
fi
fi
}
# Function to add extra packages to install before stack install
add_extra_packages() {
old_ifs=$IFS
IFS=,
set -f
for raw in $1; do
p=$(printf '%s' "$raw" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
[ -n "$p" ] || continue
# Don't allow leading dash or whitespace
case "$p" in
\*|-*|*[[:space:]]*|*\'*)
printf "Invalid extra package name: %s\n" "$p" >&2
bind_hook "usage"
exit 1
;;
esac
# Store as a shell-quoted token so globbing won't expand later
extra_packages="${extra_packages}${extra_packages:+ }'$p'"
done
set +f
IFS=$old_ifs
}
# Function to add config excludes
add_config_excludes() {
old_ifs=$IFS
IFS=,
set -f
for raw in $1; do
# Trim leading/trailing whitespace
x=$(printf '%s' "$raw" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
[ -n "$x" ] || continue
case "$x" in
-*|*[!A-Za-z0-9]*)
printf "Invalid exclude name: %s\n" "$x" >&2
exit 1
;;
esac
# Converts for some formatted names
case "$x" in
MariaDB)
x="MySQL"
;;
esac
config_excludes="${config_excludes} --exclude $x"
done
set +f
IFS=$old_ifs
}
# Function to add config includes
add_config_includes() {
old_ifs=$IFS
IFS=,
set -f
for raw in $1; do
# Trim leading/trailing whitespace
x=$(printf '%s' "$raw" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
[ -n "$x" ] || continue
case "$x" in
-*|*[!A-Za-z0-9]*)
printf "Invalid include name: %s\n" "$x" >&2
exit 1
;;
esac
# Converts for some formatted names
case "$x" in
MariaDB)
x="MySQL"
;;
esac
config_includes="${config_includes} --include $x"
done
set +f
IFS=$old_ifs
}
# Default function to parse arguments
parse_args() {
while [ "$1" != "" ]; do
case $1 in
--help | -h)
bind_hook "usage"
exit 0
;;
--bundle | -b)
shift
case "$1" in
LAMP)
shift
bundle='LAMP'
;;
LEMP)
shift
bundle='LEMP'
;;
*)
printf "Unknown bundle: $1\\n"
bind_hook "usage"
exit 1
;;
esac
;;
--minimal | -m)
shift
mode='mini'
;;
--type | -t)
shift
case "$1" in
full)
shift
mode='full'
;;
mini)
shift
mode='mini'
;;
*)
printf "Unknown type: $1\\n"
bind_hook "usage"
exit 1
;;
esac
;;
--branch | -B)
shift
case "$1" in
unstable|testing|development|devel|dev|nightly|bleeding-edge|cutting-edge)
shift
branch='unstable'
;;
prerelease|pre-release|rc|release-candidate)
shift
branch='prerelease'
;;
stable|production|release)
shift
branch='stable'
;;
*)
printf "Unknown branch: $1\\n"
bind_hook "usage"
exit 1
;;
esac
;;
--insecure-downloads | -I)
shift
insecure_download_wget_flag=' --no-check-certificate'
insecure_download_curl_flag=' -k'
;;
--no-package-updates | -x)
shift
noupdates=1
;;
--no-hostname-ssl | -nhs)
shift
add_config_excludes "SSL"
;;
--setup | -s)
shift
setup_only=1
mode='setup'
unstable='unstable'
log_file_name="${setup_log_file_name:-virtualmin-repos-setup}"
;;
--connect | -C)
shift
if [ -z "$1" ] || [ "${1#-}" != "$1" ]; then
test_connection_type="ipv4 ipv6"
else
if [ "$1" != "ipv4" ] && [ "$1" != "ipv6" ]; then
printf "Invalid protocol: $1\\n"
bind_hook "usage"
exit 1
fi
test_connection_type="$1"
shift
fi
;;
--os-grade | -g)
shift
case "$1" in
A|a)
shift
;;
B|b)
shift
unstable='unstable'
;;
*)
printf "Unknown OS grade: $1\\n"
bind_hook "usage"
exit 1
;;
esac
;;
--unstable | -U)
shift
unstable='unstable'
;;
--extra | -E)
shift
if [ -z "$1" ] || [ "${1#-}" != "$1" ]; then
printf "Missing value for extra flag\n"
bind_hook "usage"
exit 1
fi
add_extra_packages "$1"
shift
;;
--exclude | -e)
shift
if [ -z "$1" ] || [ "${1#-}" != "$1" ]; then
printf "Missing value for exclude flag\\n"
bind_hook "usage"
exit 1
fi
add_config_excludes "$1"
shift
;;
--include | -i)
shift
if [ -z "$1" ] || [ "${1#-}" != "$1" ]; then
printf "Missing value for include flag\\n"
bind_hook "usage"
exit 1
fi
add_config_includes "$1"
shift
;;
--module | -o)
shift
module_name=$1
shift
;;
--hostname | -n)
shift
forcehostname=$1
shift
;;
--force | -f | --yes | -y)
shift
skipyesno=1
;;
--force-reinstall | -fr)
shift
forcereinstall=1
;;
--no-banner | -nb)
shift
skipbanner=1
;;
--verbose | -V)
shift
VERBOSE=1
;;
--version | -v)
shift
showversion=1
;;
--uninstall | -u)
shift
mode="uninstall"
log_file_name="${uninstall_log_file_name:-virtualmin-uninstall}"
;;
*)
printf "Unrecognized option: $1\\n"
bind_hook "usage"
exit 1
;;
esac
done
}
# Hook arguments
bind_hook "parse_args" "$@"
# Default function to show installer version
show_version() {
echo "$VER"
exit 0
}
# Hook version
if [ -n "$showversion" ]; then
bind_hook "show_version"
fi
# Update variables based on branch
if [ "$branch" = 'unstable' ]; then
download_virtualmin_host_lib="$download_virtualmin_host_dev"
elif [ "$branch" = 'prerelease' ]; then
download_virtualmin_host_lib="$download_virtualmin_host_rc"
fi
# If connectivity test is requested
if [ -n "$test_connection_type" ]; then
for test_type in $test_connection_type; do
if [ "$branch" = "unstable" ]; then
test_connection "$download_webmin_host_dev" "$test_type"
test_connection "$download_virtualmin_host_dev" "$test_type"
elif [ "$branch" = "prerelease" ]; then
test_connection "$download_webmin_host_rc" "$test_type"
test_connection "$download_virtualmin_host_rc" "$test_type"
else
test_connection "$download_webmin_host" "$test_type"
test_connection "$download_virtualmin_host" "$test_type"
fi
done
exit 0
fi
# Force setup mode when VIRTUALMIN_SETUP_ONLY is set, ensuring users will not
# run an actual install script under any circumstances
if [ "${VIRTUALMIN_SETUP_ONLY:-}" = "1" ]; then
setup_only=1
mode='setup'
unstable='unstable'
fi
# Store new log each time
logpath=${log_dir_path:-"$pwd"}
log="$logpath/$log_file_name.log"
if [ -e "$log" ]; then
while true; do
logcnt=$((logcnt+1))
logold="$log.$logcnt"
if [ ! -e "$logold" ]; then
mv "$log" "$logold"
break
fi
done
fi
# If license file exists and both serial and key are set use them
if [ "$SERIAL" = "GPL" ] && [ "$KEY" = "GPL" ] && [ -f "$virtualmin_license_file" ]; then
virtualmin_license_existing_serial="$(grep 'SerialNumber=' "$virtualmin_license_file" | sed 's/SerialNumber=//')"
virtualmin_license_existing_key="$(grep 'LicenseKey=' "$virtualmin_license_file" | sed 's/LicenseKey=//')"
if [ -n "$virtualmin_license_existing_serial" ] && [ -n "$virtualmin_license_existing_key" ]; then
SERIAL="$virtualmin_license_existing_serial"
KEY="$virtualmin_license_existing_key"
fi
fi
arch="$(uname -m)"
if [ "$arch" = "i686" ]; then
arch="i386"
fi
if [ "$SERIAL" = "GPL" ]; then
PRODUCT="GPL"
else
PRODUCT="Professional"
fi
# Virtualmin-provided packages
vmgroup="'Virtualmin Core'"
vmgroupid="virtualmincore"
vmgrouptext="Virtualmin $vm_version provided packages"
debvmpackages="virtualmin-core"
deps=
if [ "$mode" = 'full' ]; then
if [ "$bundle" = 'LAMP' ]; then
rhgroup="'Virtualmin LAMP Stack'"
rhgroupid="virtualmin-lamp"
rhgrouptext="Virtualmin $vm_version LAMP stack"
debdeps="virtualmin-lamp-stack"
ubudeps="virtualmin-lamp-stack"
elif [ "$bundle" = 'LEMP' ]; then
rhgroup="'Virtualmin LEMP Stack'"
rhgroupid="virtualmin-lemp"
rhgrouptext="Virtualmin $vm_version LEMP stack"
debdeps="virtualmin-lemp-stack"
ubudeps="virtualmin-lemp-stack"
fi
elif [ "$mode" = 'mini' ]; then
if [ "$bundle" = 'LAMP' ]; then
rhgroup="'Virtualmin LAMP Stack Minimal'"
rhgroupid="virtualmin-lamp-minimal"
rhgrouptext="Virtualmin $vm_version LAMP stack mini"
debdeps="virtualmin-lamp-stack-minimal"
ubudeps="virtualmin-lamp-stack-minimal"
elif [ "$bundle" = 'LEMP' ]; then
rhgroup="'Virtualmin LEMP Stack Minimal'"
rhgroupid="virtualmin-lemp-minimal"
rhgrouptext="Virtualmin $vm_version LEMP stack mini'"
debdeps="virtualmin-lemp-stack-minimal"
ubudeps="virtualmin-lemp-stack-minimal"
fi
fi
# Find temp directory
if [ -z "$TMPDIR" ]; then
TMPDIR=/tmp
fi
# Check whether $TMPDIR is mounted noexec (everything will fail, if so)
# XXX: This check is imperfect. If $TMPDIR is a full path, but the parent dir
# is mounted noexec, this won't catch it.
TMPNOEXEC="$(grep "$TMPDIR" /etc/mtab | grep noexec)"
if [ -n "$TMPNOEXEC" ]; then
echo "Error: $TMPDIR directory is mounted noexec. Cannot continue."
exit 1
fi
if [ -z "$VIRTUALMIN_INSTALL_TEMPDIR" ]; then
VIRTUALMIN_INSTALL_TEMPDIR="$TMPDIR/.virtualmin-$$"
if [ -e "$VIRTUALMIN_INSTALL_TEMPDIR" ]; then
rm -rf "$VIRTUALMIN_INSTALL_TEMPDIR"
fi
mkdir "$VIRTUALMIN_INSTALL_TEMPDIR"
fi
# Export temp directory for Virtualmin Config
export VIRTUALMIN_INSTALL_TEMPDIR
# "files" subdir for libs
mkdir "$VIRTUALMIN_INSTALL_TEMPDIR/files"
srcdir="$VIRTUALMIN_INSTALL_TEMPDIR/files"
# Switch to temp directory or exit with error
goto_tmpdir() {
if ! cd "$srcdir" >>"$log" 2>&1; then
echo "Error: Failed to enter $srcdir temporary directory"
exit 1
fi
}
goto_tmpdir
# Function to check for HTTP client presence
pre_check_http_client() {
# Check for wget or curl or fetch
printf "Checking for HTTP client .." >>"$log"
while true; do
if [ -x "/usr/bin/wget" ]; then
download="/usr/bin/wget -nv$insecure_download_wget_flag"
break
elif [ -x "/usr/bin/curl" ]; then
download="/usr/bin/curl -f$insecure_download_curl_flag -s -L -O"
break
elif [ -x "/usr/bin/fetch" ]; then
download="/usr/bin/fetch"
break
elif [ "$wget_attempted" = 1 ]; then
printf " error: No HTTP client available. The installation of a download command has failed. Cannot continue.\\n" >>"$log"
return 1
fi
# Made it here without finding a downloader, so try to install one
wget_attempted=1
if [ -x /usr/bin/dnf ]; then
dnf -y install wget >>"$log"
elif [ -x /usr/bin/yum ]; then
yum -y install wget >>"$log"
elif [ -x /usr/bin/apt-get ]; then
apt-get update >>/dev/null
apt-get -y -q install wget >>"$log"
fi
done
if [ -z "$download" ]; then
printf " not found\\n" >>"$log"
return 1
else
printf " found %s\\n" "$download" >>"$log"
return 0;
fi
}
# Function to download content using available HTTP client
download_content() {
url="$1"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" 2>/dev/null && return 0
fi
if command -v wget >/dev/null 2>&1; then
wget -qO- "$url" 2>/dev/null && return 0
fi
if command -v fetch >/dev/null 2>&1; then
fetch -qo - "$url" 2>/dev/null && return 0
fi
return 1
}
# Download slib.sh utility library
download_slib() {
# If slib.sh is available locally in the same directory use it
if [ -f "$pwd/slib.sh" ]; then
chmod +x "$pwd/slib.sh"
# shellcheck disable=SC1091
. "$pwd/slib.sh"
# Download the slib (source: http://github.com/virtualmin/slib)
else
# We need HTTP client first
pre_check_http_client
$download "https://$download_virtualmin_host_lib/slib.sh" >>"$log" 2>&1
if [ $? -ne 0 ]; then
echo "Error: Failed to download utility function library. Cannot continue. Check your network connection and DNS settings, and verify that your system's time is accurately synchronized."
exit 1
fi
chmod +x slib.sh
# shellcheck disable=SC1091
. ./slib.sh
fi
}
# Check if already installed successfully
already_installed_block() {
log_error "Your system already has a successful Virtualmin installation deployed."
log_error "Re-installation is neither possible nor necessary. This script must be"
log_error "run on a freshly installed supported operating system. It is not meant"
log_error "for package updates or license changes. For further assistance, please"
log_error "visit the Virtualmin Community forum."
exit 100
}
# Utility function library
##########################################
download_slib # for production this block
# can be replaces with the
# content of slib.sh file,
# minus its header
##########################################
# Get OS type
get_distro
# Check the serial number and key
serial_ok "$SERIAL" "$KEY"
# Setup slog
LOG_PATH="$log"
# Setup run_ok
RUN_LOG="$log"
# Exit on any failure during shell stage
RUN_ERRORS_FATAL=1
# Console output level; ignore debug level messages.
if [ "$VERBOSE" = "1" ]; then
LOG_LEVEL_STDOUT="DEBUG"
else
LOG_LEVEL_STDOUT="INFO"
fi
# Log file output level; catch literally everything.
LOG_LEVEL_LOG="DEBUG"
# If already installed successfully, do not allow running again
if [ -f "/etc/webmin/virtual-server/installed-auto" ] &&
[ -z "$setup_only" ] && [ -z "$forcereinstall" ] &&
[ "$mode" != "uninstall" ]; then
bind_hook "already_installed_block"
fi
if [ -n "$setup_only" ]; then
log_info "Setup log is written to $LOG_PATH"
elif [ "$mode" = "uninstall" ]; then
log_info "Uninstallation log is written to $LOG_PATH"
else
log_info "Installation log is written to $LOG_PATH"
fi
log_debug "LOG_ERRORS_FATAL=$RUN_ERRORS_FATAL"
log_debug "LOG_LEVEL_STDOUT=$LOG_LEVEL_STDOUT"
log_debug "LOG_LEVEL_LOG=$LOG_LEVEL_LOG"
# log_fatal calls log_error
log_fatal() {
log_error "$1"
}
# Write chosen branch to file for future reference
write_virtualmin_branch() {
branch_dir=/etc/webmin/virtual-server
branch_file="$branch_dir/branch"
# If directory doesn't exist, do nothing
[ -d "$branch_dir" ] || return 0
# Write current $branch value
printf '%s\n' "$branch" >"$branch_file" 2>/dev/null || :
# Write major version
printf '%s\n' "$vm_version" >>"$branch_file" 2>/dev/null || :
}
# Check for old Virtualmin repo presence
is_old_virtualmin_repo() {
# Check for old Virtualmin repo presence by looking for known host in existing
# repo configs
host="$download_old_virtualmin_host"
# Check dnf repos
for d in /etc/yum.repos.d /etc/dnf/repos.d; do
[ -d "$d" ] || continue
if grep -RqsE "(^|[[:space:]])(baseurl|mirrorlist)=.*${host}" "$d"; then
return 0
fi
done
# Check apt sources
for f in /etc/apt/sources.list /etc/apt/sources.list.d/*.list; do
[ -f "$f" ] || continue
if grep -qsE "^[[:space:]]*deb[[:space:]].*${host}" "$f"; then
return 0
fi
done
return 1
}
# Configure Virtualmin repositories (stable, prerelease, or unstable) and keep
# matching Webmin repositories in sync.
manage_virtualmin_branch_repos() {
del_cmd="" found_type="" reinstalling=0
found_both=0 found_unstable=0 found_prerelease=0 found_stable=0
# Set paths based on package type
case "$package_type" in
deb)
repo_dir="/etc/apt/sources.list.d"
auth_dir="/etc/apt/auth.conf.d"
repo_ext="list"
;;
rpm)
repo_dir="/etc/yum.repos.d"
repo_ext="repo"
;;
*)
return 1
;;
esac
# Remove existing unstable, prerelease or stable repos if found
for repo in virtualmin-unstable virtualmin-prerelease virtualmin-stable virtualmin \
webmin-unstable webmin-prerelease webmin-stable webmin; do
repo_file="${repo_dir}/${repo}.${repo_ext}"
if [ -f "$repo_file" ]; then
del_cmd="${del_cmd:+$del_cmd && }rm -f $repo_file"
case "$repo" in
*unstable*)
found_unstable=1
found_type="unstable"
;;
*prerelease*)
found_prerelease=1
found_type="prerelease"
;;
*)
found_stable=1
found_type="stable"
;;
esac
fi
# Auth file check for deb
if [ "$package_type" = "deb" ]; then
case "$repo" in
virtualmin*)
auth_file="${auth_dir}/${repo}.conf"
[ -f "$auth_file" ] && del_cmd="${del_cmd:+$del_cmd && }rm -f $auth_file"
;;
esac
fi
done
# Execute removal if exists
if [ -n "$del_cmd" ]; then
if [ "$found_unstable" -eq 1 ] && [ "$found_prerelease" -eq 1 ]; then
msg="Uninstalling Virtualmin $vm_version unstable and prerelease repositories"
found_both=1
elif [ "$found_unstable" -eq 1 ]; then
msg="Uninstalling Virtualmin $vm_version unstable repository"
elif [ "$found_prerelease" -eq 1 ]; then
msg="Uninstalling Virtualmin $vm_version prerelease repository"
elif [ "$found_stable" -eq 1 ]; then
msg="Uninstalling Virtualmin $vm_version stable repository"
fi
# If removing only, update metadata
if [ -z "$branch" ]; then
del_cmd="$del_cmd && $update"
fi
# Remove any existing repo configs and keys first
remove_virtualmin_release
# Remove silently if reinstalling
if [ -n "$branch" ] && [ "$found_both" -eq 0 ] && [ "$found_type" = "$branch" ]; then
eval "$del_cmd"
reinstalling=1
else
run_ok "$del_cmd" "$msg"
fi
fi
# Save branch name
write_virtualmin_branch
# Configure repo based on requested branch
if [ "$reinstalling" -eq 1 ]; then
install_pre_msg="Reinstalling Virtualmin $vm_version"
else
install_pre_msg="Installing Virtualmin $vm_version"
fi
case "$branch" in
unstable)
down_cmd="$download https://$download_virtualmin_host_dev/install"
cmd="$down_cmd && sh install webmin unstable && \
sh install virtualmin unstable"
msg="$install_pre_msg unstable repository"
;;
prerelease)
down_cmd="$download https://$download_virtualmin_host_rc/install"
cmd="$down_cmd && sh install webmin prerelease && \
sh install virtualmin prerelease"
msg="$install_pre_msg prerelease repository"
;;
stable)
down_cmd="$download https://$download_virtualmin_host/install"
cmd="$down_cmd && sh install virtualmin stable"
msg="$install_pre_msg stable repository"
;;
*)
return 1
;;
esac
run_ok "$cmd" "$msg"
}
# Test if grade B system
grade_b_system() {
case "$os_type" in
rhel | centos | rocky | almalinux | debian)
return 1
;;
ubuntu)
case "$os_version" in
*\.10|*[13579].04) # non-LTS versions are unstable
return 0
;;
26.04) # 26.04 is in testing so far
return 0
;;
*)
return 1
;;
esac
;;
*)
return 0
;;
esac
}
if grade_b_system && [ "$unstable" != 'unstable' ]; then
log_error "Unsupported OS detected. For production, use a ${CYAN}${BOLD}Grade A${NORMAL} supported"
log_error "OS. If you want to proceed anyway, use ${YELLOW}${BOLD}--os-grade B${NORMAL} flag, but it"
log_error "is not recommended for production use."
exit 1
fi
remove_virtualmin_release() {
# Directories where Virtualmin and Webmin config or keys may live
for d in \
/etc/apt/sources.list.d \
/etc/apt/auth.conf.d \
/usr/share/keyrings \
/etc/apt/keyrings \
/etc/pki/rpm-gpg \
/etc/yum.repos.d
do
[ -d "$d" ] || continue
case "$d" in
/etc/yum.repos.d)
# Repo files
patterns="virtualmin* webmin*"
;;
/etc/pki/rpm-gpg)
# RPM GPG keys and/or any style keys
patterns="RPM-GPG-KEY-virtualmin* RPM-GPG-KEY-webmin* *-virtualmin* *-webmin*"
;;
*)
# APT dirs / keyring dirs, etc.
patterns="virtualmin* webmin* *-virtualmin* *-webmin*"
;;
esac
for p in $patterns; do
# shellcheck disable=SC2086
rm -f "$d"/$p 2>/dev/null || :
done
done
# Clean APT main sources file if it exists
if [ -f /etc/apt/sources.list ]; then
tmp="${VIRTUALMIN_INSTALL_TEMPDIR:-/tmp}/sources.list.$$"
grep -vi "virtualmin\|webmin" /etc/apt/sources.list >"$tmp" || :
mv "$tmp" /etc/apt/sources.list
fi
}
fatal() {
echo
log_fatal "Fatal Error Occurred: $1"
printf "${RED}Cannot continue installation.${NORMAL}\\n"
remove_virtualmin_release
if [ -x "$VIRTUALMIN_INSTALL_TEMPDIR" ]; then
log_warning "Removing temporary directory and files."
rm -rf "$VIRTUALMIN_INSTALL_TEMPDIR"
fi
log_fatal "If you are unsure of what went wrong, you may wish to review the log"
log_fatal "in $log"
exit 1
}