-
Notifications
You must be signed in to change notification settings - Fork 14
/
functions.sh
executable file
·4347 lines (3796 loc) · 137 KB
/
functions.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
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/bash
#
# functions
#
# (c) 2007-2016, Hetzner Online GmbH
#
# nil settings parsed out of the config
PART_COUNT=""
PART_MOUNT=""
PART_FS=""
PART_SIZE=""
PARTS_SUM_SIZE=""
MOUNT_POINT_SIZE=""
HASROOT=0
SWRAID=""
SWRAIDLEVEL=""
LVM=""
LVM_VG_CHECK=""
IMAGE_PATH=""
IMAGE_PATH_TYPE=""
IMAGE_FILE=""
IMAGE_FILE_TYPE=""
IMAGE_SIGN=""
IMAGE_PUBKEY=""
IMAGE_PUBKEY_IMPORTED=""
IAM=""
IMG_VERSION=0
BOOTLOADER=""
GOVERNOR=""
# this var is probably not used anymore. keep it for safety
#SFDISKPARTS=""
COUNT_DRIVES=0
# this var is probably not used anymore. keep it for safety
#LAST_PART_START=""
# this var is probably not used anymore. keep it for safety
#LAST_PART_END=""
# this var is probably not used anymore. keep it for safety
#DISK_SIZE_SECTORS=""
SYSTEMROOTDEVICE=""
SYSTEMBOOTDEVICE=""
SYSTEMREALBOOTDEVICE=""
EXTRACTFROM=""
ETHDEV=""
HWADDR=""
IPADDR=""
BROADCAST=""
SUBNETMASK=""
GATEWAY=""
NETWORK=""
IP6ADDR=""
IP6PREFLEN=""
IP6GATEWAY=""
ROOTHASH=""
LILOEXTRABOOT=""
ERROREXIT="0"
FINALIMAGEPATH=""
PLESK_STD_VERSION="PLESK_12_5_30"
SYSMFC=$(dmidecode -s system-manufacturer 2>/dev/null | head -n1)
SYSTYPE=$(dmidecode -s system-product-name 2>/dev/null | head -n1)
MBTYPE=$(dmidecode -s baseboard-product-name 2>/dev/null | head -n1)
# functions
# show text in a different color
echo_red() {
echo -e "\\033[01;31m${*}\\033[00m"
}
echo_green() {
echo -e "\\033[01;32m${*}\\033[00m"
}
echo_bold() {
echo -e "\\033[0;1m${*}\\033[00m"
}
# generate submenus to choose which image to install
# generate_menu "SUBMENU"
generate_menu() {
# security check - just execute the function WITH parameters
if [ -n "$1" ]; then
# empty the menu
MENULIST=""
PROXMOX=false
# find image-files and generate raw list
# shellcheck disable=SC2153
FINALIMAGEPATH="$IMAGESPATH"
# don't go looking for old_openSUSE or suse images. That was a long time ago
# if [ "$1" == "openSUSE" ]; then
# RAWLIST=$(ls -1 "$IMAGESPATH" | grep -i -e "^$1\|^old_$1\|^suse\|^old_suse")
if [ "$1" == "Virtualization" ]; then
RAWLIST=""
RAWLIST=$(find "$IMAGESPATH"/ -maxdepth 1 -type f -name "CoreOS*" -a -not -name "*.sig" -a -not -name "*.metadata" -printf '%f\n'|sort)
RAWLIST="$RAWLIST Proxmox-Virtualization-Environment-on-Debian-Wheezy"
RAWLIST="$RAWLIST Proxmox-Virtualization-Environment-on-Debian-Jessie"
elif [ "$1" == "old_images" ]; then
# skip CPANEL images, metadata and signatures files from list
RAWLIST=$(find "$OLDIMAGESPATH"/ -maxdepth 1 -type f -not -name "*.sig" -a -not -name "*cpanel*" -a -not -name "*.metadata" -printf '%f\n'|sort)
FINALIMAGEPATH="$OLDIMAGESPATH"
else
# skip CPANEL images, metadata and signatures files from list
RAWLIST=$(find "$IMAGESPATH"/* -maxdepth 1 -type f -not -name "*cpanel*" -a -regextype sed -regex ".*/\\(old_\\)\\?$1.*" -a -not -regex '.*\.sig$' -a -not -name "*.metadata" -printf '%f\n'|sort)
fi
# check if 32-bit rescue is activated and disable 64-bit images then
if [ "$(uname -m)" != "x86_64" ]; then
RAWLIST="$(echo "$RAWLIST" |tr ' ' '\n' |grep -v -- '-64-[a-zA-Z]')"
fi
# generate formatted list for usage with "dialog"
for i in $RAWLIST; do
TEMPVAR="$i"
TEMPVAR=$(basename "$TEMPVAR" .bin)
TEMPVAR=$(basename "$TEMPVAR" .bin.bz2)
TEMPVAR=$(basename "$TEMPVAR" .txz)
TEMPVAR=$(basename "$TEMPVAR" .tar.xz)
TEMPVAR=$(basename "$TEMPVAR" .tgz)
TEMPVAR=$(basename "$TEMPVAR" .tar.gz)
TEMPVAR=$(basename "$TEMPVAR" .tbz)
TEMPVAR=$(basename "$TEMPVAR" .tar.bz)
TEMPVAR=$(basename "$TEMPVAR" .tar.bz2)
TEMPVAR=$(basename "$TEMPVAR" .tar)
TEMPVAR=$(basename "$TEMPVAR" .raw.xz)
TEMPVAR=$(basename "$TEMPVAR" .raw)
MENULIST="$MENULIST$TEMPVAR . "
done
# add "back to mainmenu" entry
MENULIST="$MENULIST"'back . '
# show menu and get result
# shellcheck disable=SC2086
dialog --backtitle "$DIATITLE" --title "$1 images" --no-cancel --menu "choose image" 0 0 0 $MENULIST 2> "$FOLD/submenu.chosen"
IMAGENAME=$(cat "$FOLD/submenu.chosen")
# create proxmox post-install file if needed
case $IMAGENAME in
Proxmox-Virtualization-Environment*)
case "$IMAGENAME" in
Proxmox-Virtualization-Environment-on-Debian-Wheezy) export PROXMOX_VERSION="3" ;;
Proxmox-Virtualization-Environment-on-Debian-Jessie) export PROXMOX_VERSION="4" ;;
esac
cp "$SCRIPTPATH/post-install/proxmox$PROXMOX_VERSION" /post-install
chmod 0755 /post-install
PROXMOX=true
IMAGENAME=$(eval echo \$PROXMOX${PROXMOX_VERSION}_BASE_IMAGE)
DEFAULTPARTS=""
DEFAULTPARTS="$DEFAULTPARTS\\nPART /boot ext3 512M"
DEFAULTPARTS="$DEFAULTPARTS\\nPART lvm vg0 all\\n"
DEFAULTPARTS="$DEFAULTPARTS\\nLV vg0 root / ext3 15G"
DEFAULTPARTS="$DEFAULTPARTS\\nLV vg0 swap swap swap 6G"
;;
CoreOS*)
PROXMOX=false
;;
*)
: # no proxmox installation
;;
esac
whoami "$IMAGENAME"
fi
}
# create new config file from standardconfig and misc options
# create_config "IMAGENAME"
create_config() {
if [ "$1" ]; then
CNF="$FOLD/install.conf"
getdrives; EXITCODE=$?
if [ $COUNT_DRIVES -eq 0 ] ; then
graph_notice "There are no drives in your server!\\nIf there is a raid controller in your server, please configure it!\\n\\nThe setup will quit now!"
return 1
fi
{
echo "## ======================================================"
echo "## $COMPANY - installimage - standard config"
echo "## ======================================================"
echo ""
} > "$CNF"
# first drive
{
echo ""
echo "## ===================="
echo "## HARD DISK DRIVE(S):"
echo "## ===================="
echo ""
} >> "$CNF"
[ $COUNT_DRIVES -gt 2 ] && echo "## PLEASE READ THE NOTES BELOW!" >> "$CNF"
echo "" >> "$CNF"
local found_optdrive=0
local optdrive_count=0
for ((i=1; i<=COUNT_DRIVES; i++)); do
DISK="$(eval echo \$DRIVE${i})"
OPTDISK="$(eval echo \$OPT_DRIVE${i})"
if [ -n "$OPTDISK" ] ; then
optdrive_count=$((optdrive_count+1))
found_optdrive=1
hdinfo "/dev/$OPTDISK" >> "$CNF"
echo "DRIVE$i /dev/$OPTDISK" >> "$CNF"
else
hdinfo "$DISK" >> "$CNF"
# comment drive out when not given via commandline
[ $found_optdrive -eq 1 ] && echo -n "# " >> "$CNF"
echo "DRIVE$i $DISK" >> "$CNF"
fi
done
# reset drive count to number of drives explicitly passed via command line
[ $optdrive_count -gt 0 ] && COUNT_DRIVES=$optdrive_count
echo "" >> "$CNF"
if [ $COUNT_DRIVES -gt 2 ] ; then
if [ $COUNT_DRIVES -lt 4 ] ; then
{
echo "## if you dont want raid over your three drives then comment out the following line and set SWRAIDLEVEL not to 5"
echo "## please make sure the DRIVE[nr] variable is strict ascending with the used harddisks, when you comment out one or more harddisks"
} >> "$CNF"
else
{
echo "## if you dont want raid over all of your drives then comment out the following line and set SWRAIDLEVEL not to 5 or 6 or 10"
echo "## please make sure the DRIVE[nr] variable is strict ascending with the used harddisks, when you comment out one or more harddisks"
} >> "$CNF"
fi
fi
echo "" >> "$CNF"
# software-raid
if [ $COUNT_DRIVES -gt 1 ]; then
{
echo ""
echo "## ==============="
echo "## SOFTWARE RAID:"
echo "## ==============="
echo ""
echo "## activate software RAID? < 0 | 1 >"
echo ""
} >> "$CNF"
case "$OPT_SWRAID" in
0) echo "SWRAID 0" >> "$CNF" ;;
1) echo "SWRAID 1" >> "$CNF" ;;
*) echo "SWRAID $DEFAULTSWRAID" >> "$CNF" ;;
esac
echo '' >> "$CNF"
# available raidlevels
local raid_levels="0 1 5 6 10"
# set default raidlevel
local default_level="$DEFAULTTWODRIVESWRAIDLEVEL"
if [ "$COUNT_DRIVES" -eq 3 ] ; then
default_level="$DEFAULTTHREEDRIVESWRAIDLEVEL"
elif [ "$COUNT_DRIVES" -gt 3 ] ; then
default_level="$DEFAULTFOURDRIVESWRAIDLEVEL"
fi
local set_level=""
local avail_level=""
# check for possible raidlevels
for level in $raid_levels ; do
# set raidlevel to given opt raidlevel
if [ -n "$OPT_SWRAIDLEVEL" ] ; then
[ "$OPT_SWRAIDLEVEL" -eq "$level" ] && set_level="$level"
fi
# no raidlevel 5 if less then 3 hdds
[ "$level" -eq 5 ] && [ "$COUNT_DRIVES" -lt 3 ] && continue
# no raidlevel 6 if less then 4 hdds
[ "$level" -eq 6 ] && [ "$COUNT_DRIVES" -lt 4 ] && continue
# no raidlevel 10 if less then 2 hdds
[ "$level" -eq 10 ] && [ "$COUNT_DRIVES" -lt 2 ] && continue
# create list of all possible raidlevels
if [ -z "$avail_level" ] ; then
avail_level="$level"
else
avail_level="$avail_level | $level"
fi
done
[ -z "$set_level" ] && set_level="$default_level"
printf '## Choose the level for the software RAID < %s >\n' "${avail_level}" >> "$CNF"
echo "SWRAIDLEVEL $set_level" >> "$CNF"
fi
# bootloader
# we no longer support lilo, so don't show this option if it isn't in the image
if [ "$IAM" == "arch" ] ||
[ "$IAM" == "coreos" ] ||
[ "$IAM" == "centos" ] ||
[ "$IAM" == "ubuntu" ] && [ "$IMG_VERSION" -ge 1204 ] ||
[ "$IAM" == "debian" ] && [ "$IMG_VERSION" -ge 70 ] ||
[ "$IAM" == "suse" ] && [ "$IMG_VERSION" -ge 122 ]; then
NOLILO="true"
else
NOLILO=''
fi
{
echo ""
echo "## ============"
echo "## BOOTLOADER:"
echo "## ============"
echo ""
} >> "$CNF"
if [ "$NOLILO" ]; then
{
echo ""
echo "## Do not change. This image does not include or support lilo (grub only)!:"
echo ""
echo "BOOTLOADER grub"
} >> "$CNF"
else
{
echo ""
echo "## which bootloader should be used? < lilo | grub >"
echo ""
} >> "$CNF"
case "$OPT_BOOTLOADER" in
lilo) echo "BOOTLOADER lilo" >> "$CNF" ;;
grub) echo "BOOTLOADER grub" >> "$CNF" ;;
*) echo "BOOTLOADER $DEFAULTLOADER" >> "$CNF" ;;
esac
echo "" >> "$CNF"
fi
# hostname
get_active_eth_dev
gather_network_information
{
echo ""
echo "## =========="
echo "## HOSTNAME:"
echo "## =========="
echo ""
echo "## which hostname should be set?"
echo "##"
echo ""
} >> "$CNF"
# set default hostname to image name
DEFAULT_HOSTNAME="$1"
# or to proxmox if chosen
if [ "$PROXMOX" == "true" ]; then
echo -e '## This must be a FQDN otherwise installation will fail\n## \n' >> "$CNF"
DEFAULT_HOSTNAME="Proxmox-VE.yourdomain.localdomain"
fi
# or to the hostname passed through options
[ "$OPT_HOSTNAME" ] && DEFAULT_HOSTNAME="$OPT_HOSTNAME"
echo "HOSTNAME $DEFAULT_HOSTNAME" >> "$CNF"
echo "" >> "$CNF"
## Calculate how much hardisk space at raid level 0,1,5,6,10
RAID0=0
local small_hdd; small_hdd="$(smallest_hd)"
local small_hdd_size="$(($(blockdev --getsize64 "$small_hdd")/1024/1024/1024))"
RAID0=$((small_hdd_size*COUNT_DRIVES))
RAID1="$small_hdd_size"
if [ $COUNT_DRIVES -ge 3 ] ; then
RAID5=$((RAID0-small_hdd_size))
fi
if [ $COUNT_DRIVES -ge 4 ] ; then
RAID6=$((RAID0-2*small_hdd_size))
RAID10=$((RAID0/2))
fi
# partitions
{
echo ""
echo "## =========================="
echo "## PARTITIONS / FILESYSTEMS:"
echo "## =========================="
echo ""
echo "## define your partitions and filesystems like this:"
echo "##"
echo "## PART <mountpoint/lvm> <filesystem/VG> <size in MB>"
echo "##"
echo "## * <mountpoint/lvm> mountpoint for this filesystem *OR* keyword 'lvm'"
echo "## to use this PART as volume group (VG) for LVM"
echo "## * <filesystem/VG> can be ext2, ext3, reiserfs, xfs, swap *OR* name"
echo "## of the LVM volume group (VG), if this PART is a VG"
echo "## * <size> you can use the keyword 'all' to assign all the"
echo "## remaining space of the drive to the *last* partition."
echo "## you can use M/G/T for unit specification in MIB/GIB/TIB"
echo "##"
echo "## notes:"
echo "## - extended partitions are created automatically"
echo "## - '/boot' cannot be on a xfs filesystem"
echo "## - '/boot' cannot be on LVM!"
echo "## - when using software RAID 0, you need a '/boot' partition"
echo "##"
echo "## example without LVM (default):"
echo "## -> 4GB swapspace"
echo "## -> 512MB /boot"
echo "## -> 10GB /"
echo "## -> 5GB /tmp"
echo "## -> all the rest to /home"
echo "#PART swap swap 4G"
echo "#PART /boot ext2 512M"
echo "#PART / ext4 10G"
echo "#PART /tmp xfs 5G"
echo "#PART /home ext3 all"
echo "#"
echo "##"
echo "## to activate LVM, you have to define volume groups and logical volumes"
echo "##"
echo "## example with LVM:"
echo "#"
echo "## normal filesystems and volume group definitions:"
echo "## -> 512MB boot (not on lvm)"
echo "## -> all the rest for LVM VG 'vg0'"
echo "#PART /boot ext3 512M"
echo "#PART lvm vg0 all"
echo "#"
echo "## logical volume definitions:"
echo "#LV <VG> <name> <mount> <filesystem> <size>"
echo "#"
echo "#LV vg0 root / ext4 10G"
echo "#LV vg0 swap swap swap 4G"
echo "#LV vg0 tmp /tmp reiserfs 5G"
echo "#LV vg0 home /home xfs 20G"
echo "#"
} >> "$CNF"
if [ -x "/usr/local/bin/hwdata" ]; then
{
echo "#"
echo "## your system has the following devices:"
echo "#"
/usr/local/bin/hwdata | grep "Disk /" | sed "s/^ /#/"
} >> "$CNF"
fi
if [ "$RAID1" ] && [ "$RAID0" ] ; then
{
echo "#"
echo "## Based on your disks and which RAID level you will choose you have"
echo "## the following free space to allocate (in GiB):"
echo "# RAID 0: ~$RAID0"
echo "# RAID 1: ~$RAID1"
} >> "$CNF"
[ "$RAID5" ] && echo "# RAID 5: ~$RAID5" >> "$CNF"
if [ "$RAID6" ]; then
{
echo "# RAID 6: ~$RAID6"
echo "# RAID 10: ~$RAID10"
} >> "$CNF"
fi
fi
{
echo "#"
echo ""
} >> "$CNF"
# check if there are 3TB disks inside and use other default scheme
local LIMIT=2096128
local THREE_TB=2861588
local DRIVE_SIZE; DRIVE_SIZE="$(sfdisk -s "$(smallest_hd)" 2>/dev/null)"
DRIVE_SIZE="$(echo "$DRIVE_SIZE" / 1024 | bc)"
# adjust swap dynamically according to RAM
# RAM < 2 GB : SWAP=2 * RAM
# RAM > 2GB - 8GB : SWAP=RAM
# RAM > 8GB - 64GB : SWAP = 0.5 RAM
# RAM > 64GB: SWAP = 4GB
# http://docs.fedoraproject.org/en-US/Fedora/18/html/Installation_Guide/s2-diskpartrecommend-x86.html
# https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Installation_Guide/s2-diskpartrecommend-x86.html
RAM=$(free -m | grep Mem: | tr -s ' ' | cut -d' ' -f2)
SWAPSIZE=4
if [ "$RAM" -lt 2048 ]; then
SWAPSIZE=$((RAM * 2 / 1024 + 1))
elif [ "$RAM" -lt 8192 ]; then
SWAPSIZE=$((RAM / 1024 + 1))
elif [ "$RAM" -lt 65535 ]; then
SWAPSIZE=$((RAM / 2 / 1024 + 1))
fi
DEFAULTPARTS=${DEFAULTPARTS/SWAPSIZE##/$SWAPSIZE}
DEFAULTPARTS_BIG=${DEFAULTPARTS_BIG/SWAPSIZE##/$SWAPSIZE}
DEFAULTPARTS_LARGE=${DEFAULTPARTS_LARGE/SWAPSIZE##/$SWAPSIZE}
# use ext3 for vservers, because ext4 is too trigger happy of device timeouts
if isVServer; then
if [ "$SYSTYPE" == "vServer" ]; then
DEFAULTPARTS=$DEFAULTPARTS_CLOUDSERVER
else
DEFAULTPARTS=$DEFAULTPARTS_VSERVER
fi
fi
# use /var instead of /home for all partition when installing plesk
if [ "$OPT_INSTALL" ]; then
if echo "$OPT_INSTALL" | grep -qi 'PLESK'; then
DEFAULTPARTS_BIG="${DEFAULTPARTS_BIG//home/var}"
fi
fi
if [ "$IAM" == "coreos" ]; then
echo "## NOTICE: This image does not support custom partition sizes." >> "$CNF"
echo "## NOTICE: Please keep the following lines unchanged. They are just placeholders." >> "$CNF"
fi
if [ "$DRIVE_SIZE" -gt "$LIMIT" ]; then
if [ "$DRIVE_SIZE" -gt "$THREE_TB" ]; then
[ "$OPT_PARTS" ] && echo -e "$OPT_PARTS" >> "$CNF" || echo -e "$DEFAULTPARTS_LARGE" >> "$CNF"
else
[ "$OPT_PARTS" ] && echo -e "$OPT_PARTS" >> "$CNF" || echo -e "$DEFAULTPARTS_BIG" >> "$CNF"
fi
else
[ "$OPT_PARTS" ] && echo -e "$OPT_PARTS" >> "$CNF" || echo -e "$DEFAULTPARTS" >> "$CNF"
fi
[ "$OPT_LVS" ] && echo -e "$OPT_LVS" >> "$CNF"
echo "" >> "$CNF"
# image
{
echo ""
echo "## ========================"
echo "## OPERATING SYSTEM IMAGE:"
echo "## ========================"
echo ""
echo "## full path to the operating system image"
echo "## supported image sources: local dir, ftp, http, nfs"
echo "## supported image types: tar, tar.gz, tar.bz, tar.bz2, tar.xz, tgz, tbz, txz"
echo "## examples:"
echo "#"
echo "# local: /path/to/image/filename.tar.gz"
echo "# ftp: ftp://<user>:<password>@hostname/path/to/image/filename.tar.bz2"
echo "# http: http://<user>:<password>@hostname/path/to/image/filename.tbz"
echo "# https: https://<user>:<password>@hostname/path/to/image/filename.tbz"
echo "# nfs: hostname:/path/to/image/filename.tgz"
echo "#"
echo "# for validation of the image, place the detached gpg-signature"
echo "# and your public key in the same directory as your image file."
echo "# naming examples:"
echo "# signature: filename.tar.bz2.sig"
echo "# public key: public-key.asc"
echo ""
} >> "$CNF"
if [ "$1" == "custom" ]; then
echo "IMAGE " >> "$CNF"
else
if [ "$OPT_IMAGE" ] ; then
if [ -f "$FINALIMAGEPATH/$OPT_IMAGE" ] ; then
echo "IMAGE $FINALIMAGEPATH/$OPT_IMAGE" >> "$CNF"
else
echo "IMAGE $OPT_IMAGE" >> "$CNF"
fi
else
[ -n "$IMG_EXT" ] && IMAGESEXT="$IMG_EXT"
echo "IMAGE $FINALIMAGEPATH$1.$IMAGESEXT" >> "$CNF"
fi
fi
echo "\$1 is ${1}" | debugoutput
echo "FINALIMAGEPATH is ${FINALIMAGEPATH}" | debugoutput
echo "IMAGESEXT is ${IMAGESEXT}" | debugoutput
echo "OPT_IMAGE is ${OPT_IMAGE}" | debugoutput
echo "" >> "$CNF"
fi
return 0
}
getdrives() {
declare -a drives;
mapfile -d $'\0' drives < <(find /sys/block/ \( -name 'nvme[0-9]n[0-9]' -o -name '[hvs]d[a-z]' \) -printf '%f\0')
local i=1
for drive in ${drives[*]} ; do
# if we have just one drive, add it. Otherwise check that multiple drives are at least HDDMINSIZE
if [ ${#drives[@]} -eq 1 ] || [ ! "$(awk -v DRIVE="${drive}" '{if($4==DRIVE){print $3}}' /proc/partitions)" -lt "$HDDMINSIZE" ] ; then
eval DRIVE$i="/dev/$drive"
(( i = i+1 ))
fi
done
[ -z "$DRIVE1" ] && DRIVE1="no valid drive found"
COUNT_DRIVES=$((i - 1))
return 0
}
# read all variables from config file
# read_vars "CONFIGFILE"
read_vars(){
if [ -n "$1" ]; then
# reset counter
HASROOT=0
# count disks again, for setting COUNT_DRIVES correct after restarting installimage
getdrives
# special hidden configure option: create RAID1 and 10 with assume clean to
# avoid initial resync
RAID_ASSUME_CLEAN="$(grep -m1 -e ^RAID_ASSUME_CLEAN "$1" |awk '{print $2}')"
export RAID_ASSUME_CLEAN
# special hidden configure option: GPT usage
# if set to 0, use GPT only on disks larger than 2TiB
# if set to 1, use GPT even on disks smaller than 2TiB
# if set to 2, always use GPT, even if the OS does not support it
if grep -q -e '^FORCE_GPT' "$1"; then
FORCE_GPT="$(awk '/^FORCE_GPT/{printf $2}' "$1" 2>/dev/null)"
fi
# another special hidden configure option: force image validation
# if set to 1: force validation
FORCE_SIGN="$(grep -m1 -e ^FORCE_SIGN "$1" |awk '{print $2}')"
export FORCE_SIGN
# hidden configure option:
# if set to 1: force setting root password even if ssh keys are
# provided
FORCE_PASSWORD="$(grep -m1 -e ^FORCE_PASSWORD "$1" |awk '{print $2}')"
export FORCE_PASSWORD
# special hidden configure option: activate DWC (drive write cache)
# if set to 0, disable DWC for all disks
# if set to 1, enable DWC for SSDs but not HDDs
# if set to 2, enable DWC for all disks
if grep -q -e '^DWC_ENABLE' "$1"; then
DWC_ENABLE="$(awk '/^DWC_ENABLE/{printf $2}' "$1" 2>/dev/null)"
fi
# get all disks from configfile
local used_disks=1
for ((i=1; i<=COUNT_DRIVES; i++)); do
disk="$(grep -m1 -e ^DRIVE$i "$1" | awk '{print $2}')"
if [ -n "$disk" ] ; then
export DRIVE$i
eval DRIVE$i="$disk"
(( used_disks = used_disks + 1 ))
else
unset DRIVE$i
fi
format_disk="$(grep -m1 -e ^FORMATDRIVE$i "$1" | awk '{print $2}')"
export FORMAT_DRIVE$i
eval FORMAT_DRIVE$i="0"
if [ -n "$format_disk" ] ; then
eval FORMAT_DRIVE$i="1"
fi
done
# get count of drives
COUNT_DRIVES=$((used_disks-1))
# is RAID activated?
SWRAID="$(grep -m1 -e '^SWRAID ' "$1" |awk '{print $2}')"
[ "$SWRAID" == "" ] && SWRAID="0"
# Software RAID Level
SWRAIDLEVEL="$(grep -m1 -e ^SWRAIDLEVEL "$1" | awk '{ print $2 }')"
[ "$SWRAIDLEVEL" == "" ] && SWRAIDLEVEL="1"
PARTS_SUM_SIZE="0"
PART_COUNT="$(grep -c -e '^PART' "$1")"
PART_LINES="$(grep -e '^PART ' "$1")"
echo "$PART_LINES" > /tmp/part_lines.tmp
i=0
while read -r PART_LINE ; do
i=$((i+1))
PART_MOUNT[$i]="$(echo "$PART_LINE" | awk '{print $2}')"
PART_FS[$i]="$(echo "$PART_LINE" | awk '{print $3}')"
PART_SIZE[$i]="$(translate_unit "$(echo "$PART_LINE" | awk '{ print $4 }')")"
MOUNT_POINT_SIZE[$i]=${PART_SIZE[$i]}
#calculate new partition size if software raid is enabled and it is not /boot or swap
if [ "$SWRAID" == "1" ]; then
if [ "${PART_MOUNT[$i]}" != "/boot" ] && [ "${PART_SIZE[$i]}" != "all" ] && [ "${PART_MOUNT[$i]}" != "swap" ]; then
if [ "$SWRAIDLEVEL" == "0" ]; then
PART_SIZE[$i]=$((${PART_SIZE[$i]}/COUNT_DRIVES))
elif [ "$SWRAIDLEVEL" == "5" ]; then
PART_SIZE[$i]=$((${PART_SIZE[$i]}/(COUNT_DRIVES-1)))
elif [ "$SWRAIDLEVEL" == "6" ]; then
PART_SIZE[$i]=$((${PART_SIZE[$i]}/(COUNT_DRIVES-2)))
elif [ "$SWRAIDLEVEL" == "10" ]; then
PART_SIZE[$i]=$((${PART_SIZE[$i]}/(COUNT_DRIVES/2)))
fi
fi
fi
echo "${PART_MOUNT[$i]} : ${PART_SIZE[$i]}" | debugoutput
if [ "${PART_SIZE[$i]}" != "all" ]; then
PARTS_SUM_SIZE=$(( ${PART_SIZE[$i]} + PARTS_SUM_SIZE ))
fi
if [ "${PART_MOUNT[$i]}" == "/" ]; then
(( HASROOT++ ))
fi
done < /tmp/part_lines.tmp
# get LVM volume group config
LVM_VG_COUNT="$(grep -c -e '^PART *lvm ' "$1")"
LVM_VG_ALL="$(grep -e '^PART *lvm ' "$1")"
# void the check var
LVM_VG_CHECK=""
for ((i=1; i<=LVM_VG_COUNT; i++)); do
LVM_VG_LINE="$(echo "$LVM_VG_ALL" | head -n$i | tail -n1)"
#LVM_VG_PART[$i]=$i #"$(echo $LVM_VG_LINE | awk '{print $2}')"
LVM_VG_PART[$i]=$(echo "$PART_LINES" | grep -n -e '^PART *lvm ' | head -n$i | tail -n1 | cut -d: -f1)
LVM_VG_NAME[$i]="$(echo "$LVM_VG_LINE" | awk '{print $3}')"
LVM_VG_SIZE[$i]="$(translate_unit "$(echo "$LVM_VG_LINE" | awk '{print $4}')")"
if [ "${LVM_VG_SIZE[$i]}" != "all" ] ; then
LVM_VG_CHECK="$i $LVM_VG_CHECK"
fi
done
# get LVM logical volume config
LVM_LV_COUNT="$(grep -c -e "^LV " "$1")"
LVM_LV_ALL="$(grep -e "^LV " "$1")"
for ((i=1; i<=LVM_LV_COUNT; i++)); do
LVM_LV_LINE="$(echo "$LVM_LV_ALL" | head -n"$i" | tail -n1)"
LVM_LV_VG[$i]="$(echo "$LVM_LV_LINE" | awk '{print $2}')"
LVM_LV_VG_SIZE[$i]="$(echo "$LVM_VG_ALL" | grep "${LVM_LV_VG[$i]}" | awk '{print $4}')"
LVM_LV_NAME[$i]="$(echo "$LVM_LV_LINE" | awk '{print $3}')"
LVM_LV_MOUNT[$i]="$(echo "$LVM_LV_LINE" | awk '{print $4}')"
LVM_LV_FS[$i]="$(echo "$LVM_LV_LINE" | awk '{print $5}')"
LVM_LV_SIZE[$i]="$(translate_unit "$(echo "$LVM_LV_LINE" | awk '{ print $6 }')")"
# we only add LV sizes to PART_SUM_SIZE if the appropiate volume group has
# "all" as size (otherwise we would count twice: SIZE of VG + SIZE of LVs of VG)
if [ "${LVM_LV_SIZE[$i]}" != "all" ] && [ "${LVM_LV_VG_SIZE[$i]}" == "all" ]; then
PARTS_SUM_SIZE=$(( ${LVM_LV_SIZE[$i]} + PARTS_SUM_SIZE ))
fi
if [ "${LVM_LV_MOUNT[$i]}" == "/" ]; then
(( HASROOT++ ))
fi
done
# is LVM activated?
[ "$LVM_VG_COUNT" != "0" ] && [ "$LVM_LV_COUNT" != "0" ] && LVM="1" || LVM="0"
IMAGE="$(grep -m1 -e ^IMAGE "$1" | awk '{print $2}')"
# shellcheck disable=SC2154
[ -e "$wd/$IMAGE" ] && IMAGE="$wd/$IMAGE"
IMAGE_PATH="$(dirname "$IMAGE")/"
IMAGE_FILE="$(basename "$IMAGE")"
case "$IMAGE_PATH" in
https:*|http:*|ftp:*) IMAGE_PATH_TYPE="http" ;;
/*) IMAGE_PATH_TYPE="local" ;;
*) IMAGE_PATH_TYPE="nfs" ;;
esac
case "$IMAGE_FILE" in
*.tar) IMAGE_FILE_TYPE="tar" ;;
*.tar.gz|*.tgz) IMAGE_FILE_TYPE="tgz" ;;
*.tar.bz|*.tbz|*.tbz2|*.tar.bz2) IMAGE_FILE_TYPE="tbz" ;;
*.tar.xz|*.txz) IMAGE_FILE_TYPE="txz" ;;
*.bin|*.raw) IMAGE_FILE_TYPE="bin" ;;
*.bin.bz2|*.bin.bz) IMAGE_FILE_TYPE="bbz" ;;
*.raw.xz) IMAGE_FILE_TYPE="rxz" ;;
esac
BOOTLOADER="$(grep -m1 -e ^BOOTLOADER "$1" |awk '{print $2}')"
if [ "$BOOTLOADER" == "" ]; then
BOOTLOADER=$(echo "$DEFAULTLOADER" | awk '{ print $2 }')
fi
BOOTLOADER="${BOOTLOADER,,}"
NEWHOSTNAME=$(grep -m1 -e ^HOSTNAME "$1" | awk '{print $2}')
GOVERNOR="$(grep -m1 -e ^GOVERNOR "$1" |awk '{print $2}')"
if [ "$GOVERNOR" == "" ]; then GOVERNOR="$DEFAULTGOVERNOR"; fi
SYSTEMDEVICE="$DRIVE1"
# if custom nameservers are set in the installimage config, replace the default
# v4/v6 nameservers from config.sh by those
local nameserver_count
nameserver_count=$(grep -c -e ^NAMESERVER "${1}")
if [ "${nameserver_count}" -gt 0 ]; then
declare -a nameserver_custom
while read -r nameserver; do
nameserver_custom+=("$nameserver")
done < <(grep -e ^NAMESERVER "${1}" | awk '{print $2}')
NAMESERVER=("${nameserver_custom[@]}")
fi
local nameserver_v6_count
nameserver_v6_count=$(grep -c -e ^DNSRESOLVER_V6 "${1}")
if [ "${nameserver_v6_count}" -gt 0 ]; then
declare -a nameserver_v6_custom
while read -r nameserver_v6; do
nameserver_v6_custom+=("$nameserver_v6")
done < <(grep -e ^DNSRESOLVER_V6 "${1}" | awk '{print $2}')
DNSRESOLVER_V6=("${nameserver_v6_custom[@]}")
fi
local postinstall_url
postinstall_url=$(grep -e ^POSTINSTALLURL "${1}" | awk '{print $2}')
if [ -n "${postinstall_url}" ]; then
POSTINSTALLURL=${postinstall_url}
fi
local company_custom
company_custom=$(grep -m1 -e ^COMPANY "${1}" | awk '{$1=""; print $0}')
if [ -n "${company_custom}" ]; then
COMPANY="${company_custom}"
fi
local c_short_custom
c_short_custom=$(grep -m1 -e ^C_SHORT "${1}" | awk '{print $2}')
if [ -n "${c_short_custom}" ]; then
C_SHORT="${c_short_custom}"
fi
# overwrite SSH key settings from commandline
# you can set that also via -K, but configfile is superior
local sshkeys_url
sshkeys_url=$(grep -m1 -e ^SSHKEYS_URL "${1}" | awk '{print $2}')
if [ -n "$sshkeys_url" ]; then
export OPT_SSHKEYS_URL="$sshkeys_url"
export OPT_USE_SSHKEYS=1
fi
fi
}
# validate all variables for correct values
# validate_vars "CONFIGFILE"
validate_vars() {
if [ "$1" ]; then
echo "generated config file: ${1}" | debugoutput
debugoutput < "${1}"
read_vars "$1"
# test if IMAGEPATH is given
if [ -z "$IMAGE_PATH" ]; then
graph_error "ERROR: No valid IMAGEPATH"
return 1
fi
# test if PATHTYPE is a supported type
CHECK="$(echo $IMAGE_PATH_TYPE |grep -i -e '^http$\|^nfs$\|^local$')"
if [ -z "$CHECK" ]; then
graph_error "ERROR: No valid PATHTYPE"
return 1
fi
# test if IMAGEFILE is given
if [ -z "$IMAGE_FILE" ]; then
graph_error "ERROR: No valid IMAGEFILE"
return 1
fi
# test if FILETYPE is a supported type
CHECK="$(echo $IMAGE_FILE_TYPE |grep -i -e '^tar$\|^tgz$\|^tbz$\|^txz$\|^bin$\|^bbz$')"
if [ -z "$CHECK" ]; then
graph_error "ERROR: $IMAGE_FILE_TYPE is no valid FILETYPE for images"
return 1
fi
whoami "$IMAGE_FILE"
# test if "$DRIVE1" is a valid block device and is able to create partitions
CHECK="$(test -b "$DRIVE1" && sfdisk -l "$DRIVE1" 2>/dev/null)"
if [ -z "$CHECK" ]; then
graph_error "ERROR: Value for DRIVE1 is not correct: $DRIVE1 "
return 1
fi
# test if "$DRIVE1" is not busy
# CHECK="$(hdparm -z $DRIVE1 2>&1 | grep 'BLKRRPART failed: Device or resource busy')"
# if [ "$CHECK" ]; then
# graph_error "ERROR: DRIVE1 is busy - cannot access device $DRIVE1 "
# return 1
# fi
# test if "$SWRAID" has not 0 or 1 as parameter
if [ "$SWRAID" != "0" ] && [ "$SWRAID" != "1" ]; then
graph_error "ERROR: Value for SWRAID is not correct"
return 1
fi
# test if "$SWRAIDLEVEL" is either 0 or 1
if [ "$SWRAID" == "1" ] && [ "$SWRAIDLEVEL" != "0" ] &&
[ "$SWRAIDLEVEL" != "1" ] && [ "$SWRAIDLEVEL" != "5" ] &&
[ "$SWRAIDLEVEL" != "6" ] && [ "$SWRAIDLEVEL" != "10" ]; then
graph_error "ERROR: Value for SWRAIDLEVEL is not correct"
return 1
fi
# check for valid drives
local drive_array=( "${DRIVE1}" )
for ((i=1; i<=COUNT_DRIVES; i++)); do
local format; format="$(eval echo "\$FORMAT_DRIVE$i")"
local drive; drive="$(eval echo "\$DRIVE$i")"
if [ "$i" -gt 1 ] ; then
for j in $(seq 0 $((${#drive_array[@]} - 1))); do
if [ "${drive_array[$j]}" == "$drive" ]; then
graph_error "Duplicate DRIVE definition. $drive used for DRIVE$((j+1)) and DRIVE$i"
fi
done
drive_array=( "${drive_array[@]}" "$drive" )
if [ "$format" != "0" ] && [ "$format" != "1" ]; then
graph_error "ERROR: Value for FORMATDRIVE$i is not correct"
return 1
fi
if [ "$format" == 1 ] && [ "$SWRAID" == 1 ] ; then
graph_error "ERROR: FORMATDRIVE$i _AND_ SWRAID are active - use one or none of these options, not both"
return 1
fi
fi
if [ "$SWRAID" == "1" ] || [ "$format" == "1" ] || [ "$i" -eq 1 ] ; then
# test if drive is a valid block device and is able to create partitions
CHECK="$(test -b "$drive" && sfdisk -l "$drive" 2>/dev/null)"
if [ -z "$CHECK" ]; then
graph_error "ERROR: Value for DRIVE$i is not correct: $drive"
return 1
fi
# test if drive is not busy
CHECK="$(hdparm -z "$drive" |& grep 'BLKRRPART failed: Device or resource busy')"
if [ "$CHECK" ]; then
graph_error "ERROR: DRIVE$i is busy - cannot access device $drive"
return 1
fi
fi
done
# test if there is more than 1 hdd if swraid enabled
if [ "$SWRAID" == "1" ] && [ $COUNT_DRIVES -le 1 ] ; then
graph_error "ERROR: You need to select at least 2 disks for creating software raid!"
return 1
fi
# test if enough disks for the choosen raid level
if [ "$SWRAID" == "1" ]; then
if [ "$SWRAIDLEVEL" == "5" ] && [ "$COUNT_DRIVES" -lt "3" ]; then
graph_error "ERROR: Not enough disks for RAID level 5"
return 1
elif [ "$SWRAIDLEVEL" == "6" ] && [ "$COUNT_DRIVES" -lt "4" ]; then
graph_error "ERROR: Not enough disks for RAID level 6"
return 1
elif [ "$SWRAIDLEVEL" == "10" ] && [ "$COUNT_DRIVES" -lt "2" ]; then
graph_error "ERROR: Not enough disks for RAID level 10"
return 1
fi
fi
# test if a /boot partition is defined when using software RAID 0
if [ "$SWRAID" == "1" ]; then
if [ "$SWRAIDLEVEL" == "0" ] || [ "$SWRAIDLEVEL" == "5" ] ||
[ "$SWRAIDLEVEL" == "6" ] || [ "$SWRAIDLEVEL" == "10" ]; then
TMPCHECK=0
for ((i=1; i<=PART_COUNT; i++)); do
if [ "${PART_MOUNT[$i]}" == "/boot" ]; then
TMPCHECK=1
fi
done
if [ $TMPCHECK -eq 0 ]; then
graph_error "ERROR: You need a /boot partition when using software RAID level 0, 5, 6 or 10"
return 1
fi
fi
fi
# calculate drive_sum_size
if [ "$SWRAID" == "0" ] ; then
# just the first hdd is used so we need the size of DRIVE1
DRIVE_SUM_SIZE=$(blockdev --getsize64 "$DRIVE1")
echo "Size of the first hdd is: $DRIVE_SUM_SIZE" | debugoutput
else
local smallest_hdd; smallest_hdd=$(smallest_hd)
DRIVE_SUM_SIZE="$(blockdev --getsize64 "$smallest_hdd")"
# this variable is used later when determining what disk to use as reference
# when drives of different sizes are in a system
SMALLEST_HDD_SIZE="$DRIVE_SUM_SIZE"
SMALLEST_HDD_SIZE=$((SMALLEST_HDD_SIZE / 1024 / 1024))
echo "Size of smallest drive is $DRIVE_SUM_SIZE" | debugoutput
if [ "$SWRAIDLEVEL" == "0" ]; then
DRIVE_SUM_SIZE=$((DRIVE_SUM_SIZE * COUNT_DRIVES))
elif [ "$SWRAIDLEVEL" == "5" ]; then
DRIVE_SUM_SIZE=$((DRIVE_SUM_SIZE * (COUNT_DRIVES - 1)))
elif [ "$SWRAIDLEVEL" == "6" ]; then
DRIVE_SUM_SIZE=$((DRIVE_SUM_SIZE * (COUNT_DRIVES - 2)))
elif [ "$SWRAIDLEVEL" == "10" ]; then
DRIVE_SUM_SIZE=$((DRIVE_SUM_SIZE * (COUNT_DRIVES / 2)))