-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup-partitions
1211 lines (1053 loc) · 35.5 KB
/
setup-partitions
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
MBR=${MBR:-"/usr/share/syslinux/mbr.bin"}
GPTMBR=${GPTMBR:-"/usr/share/syslinux/gptmbr.bin"}
ROOTFS=${ROOTFS:-ext4}
BOOTFS=${BOOTFS:-ext2}
# default location for mounted root
SYSROOT=${SYSROOT:-/mnt}
ACTIVE_DISK=
ACTIVE_DEV=
ACTIVE_MNT=
ACTIVE_FS=
ACTIVE_TYPE=
MSG=
# luks defaults
CIPHER=aes
MODE=xts
IV=plain64
KEYSIZE=256
HASH=sha512
ITER=5000
init_progs_luks() {
local i= iface_list= iface_count= iface= route_error=$(route 2>&1 > /dev/null)
if [ -n "$route_error" ]; then
setup-interfaces
iface_list=$(ls /sys/class/net |grep -v lo |tr "\n" " ")
iface_count=$(echo $iface_list |wc -w)
case "$iface_count" in
1) ifup $iface_list;;
0) echo "No interfaces found";;
*) echo -en "\n\033[4mChoose interface to bring up\033[0m\n\n"
until echo $iface_list | grep -w $iface &> /dev/null; do
echo -en "\033[1A\033[K"
echo -en "\033[1;32m>>>\033[37m[ $iface_list]: \033[0m"; read iface
done
ifup $iface;;
esac
fi
if [ "$(basename $(grep "^http://" /etc/apk/repositories) 2>/dev/null)" != "main" ]; then
setup-apkrepos; echo -e;
fi
pkgs="nano haveged lvm2 cryptsetup device-mapper sfdisk cfdisk sgdisk gptfdisk e2fsprogs btrfs-progs syslinux dialog util-linux"
for i in $pkgs; do
if ! grep "^$i" /etc/apk/world 1>/dev/null; then
echo -e "Installing applications for setup ...\n"
apk add --quiet --progress $pkgs
break
fi
done
if [ ! -f "/var/run/haveged.pid" ]; then rc-service haveged restart; echo -e; fi
}
confirm_wipe() {
#wipe partition with random data
echo -n "Wipe LUKS partition: '$1' with random data? [ y/N ] : "
read answer
case "$answer" in
y*|Y*) `haveged -n 0 | dd of=$1`; return 0;;
esac
}
# wrapper to only show given device
_blkid() {
blkid | grep "^$1:"
}
find_mapper_disk() {
cryptsetup status $1|grep device|awk '{ print $2 }'
}
find_crypts() {
if [ "$(dmsetup ls --target=crypt)" != "No devices found" ]; then
dmsetup ls --target=crypt |awk '{print $1}'
fi
}
find_dm_by_device() {
local crypt_list=$(find_crypts)
local dm= device=$1
for dm in $crypt_list; do
if [ "$(find_mapper_disk $dm)" = "$device" ]; then
echo "/dev/mapper/$dm"; exit 0
fi
done
}
find_vg_by_device() {
local device=${1##*/}
local index=$(expr index ${device##*/} "-" - 1)
local vg=${device::$index}
echo $vg
}
find_lv_by_device() {
local device=${1##*/}
local index=$(expr index ${device##*/} "-")
local lv=${device:$index}
echo $lv
}
find_fs() {
blkid -o value -s TYPE $1
}
convert_mapper_to_lvm() {
local mapper="$1"
local vg=$(find_vg_by_device $mapper)
local lv=$(find_lv_by_device $mapper)
echo "/dev/$vg/$lv"
}
# find device for given mounted dir
find_mount_dev() {
local mnt="$1"
awk "\$2 == \"$mnt\" { print \$1 }" /proc/mounts | tail -n 1
}
find_mount_point() {
local device="$1"
awk "\$1 == \"$device\" {print \$2}" /proc/mounts | tail -n 1
}
requested_mnt_fs() {
local device="$1"
awk "\$1 == \"$device\" {print \$2,\$3}" /tmp/mnt_list.in 2>/dev/null | tail -n 1
}
find_mount_fs() {
local mnt="$1"
awk "\$2 == \"$mnt\" {print \$3}" /proc/mounts | tail -n 1
}
find_lvm_partition() {
if [ "$(check_scheme $ACTIVE_DISK)" = "dos" ]; then
local type=8e
sfdisk -d $1 | awk "/Id=$type/ {print \$1}"
else
local part= type=8E00
for part in $(list_partitions); do
if [ "$(find_part_type $part)" = "$type" ]; then
echo "/dev/$part"
break
fi
done
fi
}
find_boot_partition() {
if [ "$(check_scheme $ACTIVE_DISK)" = "dos" ]; then
sfdisk -d $ACTIVE_DISK | awk '/bootable/ {print $1}'
else
local i=1; while [ $i -le 128 ]; do
if sgdisk $ACTIVE_DISK -A $i:show |grep "legacy BIOS bootable" 1>/dev/null; then
echo "${ACTIVE_DISK}${i}"
break
fi
i=$(( $i + 1 ))
done
fi
}
find_vg() {
local device="$1"
if is_pv $device; then
if [ $(pvs --noheadings $device | wc -w) = 6 ]; then
pvs --noheadings $device |awk '{ print $2 }'
fi
fi
}
is_pv() {
pvs "$1" &>/dev/null
}
find_pvs_in_vg() {
local vg="$1"
pvs --noheadings | awk "\$2 == \"$vg\" {print \$1}"
}
find_dev_type() {
local device=$(basename $1) # remove unicode
lsblk -no NAME,TYPE $ACTIVE_DISK 2>/dev/null | iconv -c -f utf-8 -t ascii|tr -d '[:*:]' |awk "\$1 == \"$device\" {print \$2}"
}
detect_mounts() {
local device= part= uuid= mnt= fs= mlist=/tmp/mnt_list.in
local device_list=$(list_devices)
for part in $device_list; do
device=$(device_from_name $part)
uuid=$(_uuid $device)
mnt=$(find_mount_point $device)
fs=$(find_fs $device)
if [ -n "$mnt" ]; then
echo "$device $mnt $fs $uuid" >> $mlist
elif [ "$(find_fs $device)" = "swap" ]; then
echo "$device swap swap $uuid" >> $mlist
elif [ "$(find_part_type $part)" = "EF02" ]; then
echo "$device GPT-BIOS $fs $uuid" >> $mlist
fi
done
}
unmount_parts() {
local line=
local config=/tmp/rootdev.last
local mlist=/tmp/mnt_list.in
if [ -f $mlist ]; then # create list with root partition LAST & longest path FIRST
awk '{ print length ($2), $1, $2, $3 }' $mlist | sort -nr | cut -d" " -f2- > $config
while read line #$1 = device
do #$2 = mount point
set -- $line #$3 = file system
if [ -n "$(find_mount_point $1)" ]; then
echo "unmounting $1 @ $2"; umount $2 2>/dev/null
elif grep ^$1 /proc/swaps 1>/dev/null; then
swapoff $1
fi
done < $config
fi
}
mount_parts() {
local line=
local config=/tmp/rootdev.first
local mlist=/tmp/mnt_list.in
local no_mount_list="swap GPT-BIOS"
if [ -f $mlist ]; then # create list with root partition FIRST & longest path LAST
awk '{ print length ($2), $1, $2, $3 }' $mlist | sort -n | cut -d" " -f2- > $config
while read line #$1 = device
do #$2 = mount point
set -- $line #$3 = file system
if [ -n "$(find_fs $1)" ]; then
if ! echo "$no_mount_list" |grep -w $2 1>/dev/null; then
echo "mounting $1 @ $2";
mkdir -p $2; mount -t $3 $1 $2;
fi
else
echo -e "\033[1A\033[K\033[1;31m$1: NO filesystem: NOT mounting.\033[0m"
fi
done < $config
echo -e; lsblk -e 1,7,11 -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT,PARTLABEL,UUID
else
echo "$mlist is missing"
fi
}
tidy_mounts() {
local device= fstype= mnt_list= device_list= msgfile=/tmp/part.msg
local config=/tmp/mnt_list.in
if [ -f "$config" ]; then
mnt_list=$(awk '{ print $1 }' $config)
device_list=$(device_from_name $(list_devices))
for device in $mnt_list; do
fstype=$(find_fs $device) # mappers are mounted not crypt disks or pv's
if ! echo $device_list |grep $device 1>/dev/null || echo $fstype |egrep -w -q "LVM2_member|crypto_LUKS"; then
echo "$device removed from mount queue" >> $msgfile
sed -i "\|^$device|d" $config
fi # note delimiter escaped
done
fi
if [ $(cat $msgfile 2>/dev/null |wc -l) -gt 0 ]; then echo -e >> $msgfile; fi
}
format_warning() {
local answer= config=/tmp/mnt_list.in
if [ -f "$config" ]; then
awk '$3 != /none/ { print $1,$3}' $config
echo -en "\nFormat the above partitions? [ y/N ] : "; read answer
case "$answer" in
y*|Y*) format_parts;;
*) exit 1;;
esac
else
exit 1
fi
}
format_parts() {
local line= config=/tmp/mnt_list.in
local fs_list="ext2 ext3 ext4 swap btrfs"
#$1 = device
while read line; do #$2 = mount point
set -- $line #$3 = file system
if echo $fs_list |grep $3 1>/dev/null; then
echo "formatting $1 as $3"
case "$3" in
ext*) mkfs.$3 -q $1;;
swap) mkswap $1;swapon $1;;
btrfs) mkfs.$3 -f $1;;
*) echo "NOT formatting $1";;
esac
fi
done < $config
}
set_lvm_type() {
local part= type= part_number=
for part in $(pvs --noheading |awk '{print $1}'); do
if echo $part |grep "^/dev/mapper/" 1>/dev/null; then
part=$(find_mapper_disk $part)
fi
type=$(find_part_type $part)
part_number=$(echo $part |tr -cd '[:digit:]')
if [ "$type" != "8E00" ]; then
case "$(check_scheme $ACTIVE_DISK)" in
gpt) echo "Correcting $part to partition type: 8e00"
sgdisk $ACTIVE_DISK --typecode ${part_number}:8e00;;
dos) echo "Correcting $part to partition type: 8e"
sfdisk --part-type $ACTIVE_DISK ${part_number} 8e;;
esac
fi
done
}
set_swap_type() {
local part= type= part_number=
for part in $(grep ^$ACTIVE_DISK /proc/swaps |awk '{print $1}'); do
if echo $part |grep "^$ACTIVE_DISK[[:digit:]\?]" 1>/dev/null; then
type=$(find_part_type $part)
part_number=$(echo $part |tr -cd '[:digit:]')
if [ "$type" != "8200" ]; then
case "$(check_scheme $ACTIVE_DISK)" in
gpt) echo "Correcting $part to partition type: 8200"
sgdisk $ACTIVE_DISK --typecode ${part_number}:8200;;
dos) echo "Correcting $part to partition type: 82"
sfdisk --part-type $ACTIVE_DISK ${part_number} 82;;
esac
fi
fi
done
}
dialog_cipher() {
local current_choice="$CIPHER"
CIPHER=$(dialog --item-help --no-tags --default-item "$CIPHER" --keep-tite --ascii-lines \
--title "CIPHER" \
--menu "\nChoose LUKS Cipher:" 11 30 3 \
"aes" "aes" "LUKS default & fastest if your hardware supports AES-NI ('grep aes /proc/cpuinfo')." \
"twofish" "twofish" "Recommended by Gentoo." \
"serpent" "serpent" "Most secure but also the slowest." 3>&1 1>&2 2>&3)
if [ -z "$CIPHER" ]; then CIPHER="$current_choice"; fi
}
dialog_mode() {
local current_choice="$MODE"
MODE=$(dialog --item-help --no-tags --default-item "$MODE" --keep-tite --ascii-lines \
--title "CIPHER MODE" \
--menu "\nChoose LUKS Cipher Mode:" 11 30 3 \
"cbc" "cbc" "Do NOT use with 'plain' or 'plain64' IV's." \
"xts" "xts" "LUKS default: recommended mode & potentially more secure than cbc-essiv." \
"ctr" "ctr" "Counter Mode: stream cipher suited to multi-processor systems." 3>&1 1>&2 2>&3)
if [ -z "$MODE" ]; then MODE="$current_choice"; fi
}
dialog_iv() {
local current_choice="$IV"
IV=$(dialog --item-help --no-tags --default-item "$IV" --keep-tite --ascii-lines \
--title "IV" \
--menu "\nChoose Initialization Vector:" 12 35 4 \
"essiv:sha256" "essiv:sha256" "The **only** IV recommended for 'cbc' mode." \
"plain64" "plain64" "LUKS default IV - no performance penalty compared to 'plain'" \
"plain" "plain" "32 bit - do NOT use on disks > 2TiB" \
"plain:wd256" "plain:wd256" "Recommended by Gentoo with 'twofish-xts'" 3>&1 1>&2 2>&3)
if [ -z "$IV" ]; then IV="$current_choice"; fi
}
dialog_key_size() {
local current_choice="$KEYSIZE"
KEYSIZE=$(dialog --item-help --no-tags --default-item "$KEYSIZE" --keep-tite --ascii-lines \
--title "KEY SIZE" \
--menu "\nChoose LUKS Key Size:" 12 30 4 \
"128" "128" "128 bits: for passphrases < 128 bits of entropy larger keys are not more secure." \
"192" "192" "192 bits: do not use in XTS mode (XTS requires a 256 or 512 bit key)" \
"256" "256" "256 bits: LUKS default Key Size (in XTS mode equivalent to 128 bit key)" \
"512" "512" "512 bits: in XTS mode equivalent to 256 bit key." 3>&1 1>&2 2>&3)
if [ -z "$KEYSIZE" ]; then KEYSIZE="$current_choice"; fi
}
dialog_hash() {
local current_choice="$HASH"
HASH=$(dialog --item-help --no-tags --default-item "$HASH" --keep-tite --ascii-lines \
--title "ENCRYPTION HASH" \
--menu "\nChoose Encryption Hash:" 13 35 5 \
"sha512" "sha512" "512 bit digest with computed 64 bit words: Recommended on modern hardware." \
"sha256" "sha256" "256 bit digest with computed 32 bit words: Easier for GPU's to crack." \
"sha1" "sha1" "160 bit: fastest hash & LUKS default." \
"ripemd160" "ripemd160" "160 bit: 2nd fastest hash & dm-crypt default." \
"whirlpool" "whirlpool" "512 bit: not curently recommended - see cryptsetup FAQ / gcrypt bug." 3>&1 1>&2 2>&3)
if [ -z "$HASH" ]; then HASH="$current_choice"; fi
}
dialog_iter_time() {
local current_choice="$ITER"
ITER=$(dialog --keep-tite --ascii-lines --title "ITER TIME" \
--inputbox "\nChoose Iter Time" 8 35 "$current_choice" 3>&1 1>&2 2>&3)
if [ -z "$ITER" ]; then ITER="$current_choice"; fi
}
dialog_benchmark() {
dialog --title "Cryptsetup Benchmark" --keep-tite --ascii-lines --prgbox " " "cryptsetup benchmark" 25 62
}
dialog_luks_editor() {
local tempfile=/tmp/luksdialog.choice
while true; do
dialog --clear --default-item "$CHOICE" --item-help --nocancel --extra-button --extra-label "Edit" \
--default-button "extra" --help-button --help-label "Benchmark" --keep-tite --ascii-lines \
--title "ALPINE LINUX LUKS EDITOR" "$@" \
--menu "\nChoose LUKS Encryption Scheme:" 14 39 6 \
"Cipher" "$CIPHER" "Edit Cipher" \
"Mode" "$MODE" "Edit Encryption Mode" \
"IV" "$IV" "Edit Initialization Vector" \
"Key Size" "$KEYSIZE" "Edit Key Size" \
"Hash" "$HASH" "Edit Encryption Hash" \
"Iter Time" "$ITER" "Time in milliseconds to delay container opening (to slow down Attacks)" 2>$tempfile
retval=$?
CHOICE=$(cat $tempfile)
case $retval in
0) break;;
2) dialog_benchmark;;
3) dialog_$(cat $tempfile |tr '[:upper:]' '[:lower:]' |sed 's/ /_/g');;
255) break;;
esac
done
}
create_crypt(){
local device=$1
local answer= config=/tmp/mnt_list.in
if ! cryptsetup isLuks $device 2>/dev/null; then
echo -ne "Create LUKS device on '$device' ? [ y/N ] : "; read answer
case $answer in
y*|Y*) if is_pv $device; then
read -p "$device is an LVM pv. Destroy by creating a LUKS device ? [ y/N ] : " answer
case $answer in
y*|Y*) manage_pv $device remove;;
esac
fi
if [ $? -eq 0 ]; then
confirm_wipe $device
format_crypt $device
fi;;
esac
fi
}
format_crypt() {
local answer= device=$1
read -p "LUKS settings: -c $CIPHER-$MODE-$IV -s $KEYSIZE -h $HASH -i $ITER [ c to configure ] : " answer
case $answer in
c*|C*) dialog_luks_editor;;
esac
case "$MODE" in
cbc) if [ "$IV" != "essiv:sha256" ]; then
IV='essiv:sha256' # prevent non recommended IV's for CBC Mode
echo "Correcting IV for mode: '$MODE' to: '$IV'"
fi;;
xts) if [ "$IV" = "essiv:sha256" ]; then
IV='plain64' # prevent non recommended IV's for XTS Mode
echo "Correcting IV for mode: '$MODE' to: '$IV'"
fi;;
esac
case "$KEYSIZE" in
512) if [ "$MODE" != "xts" ]; then
KEYSIZE=256 # in XTS mode a key of 512 actually = 256 bit key
echo "Correcting Key Size of 512 bits for mode: '$MODE' to: '$KEYSIZE bits' (maximum)"
fi;;
esac
echo -e "Creating crypt on $device: -c $CIPHER-$MODE-$IV -s $KEYSIZE -h $HASH -i $ITER --use-random"
cryptsetup -c $CIPHER-$MODE-$IV -s $KEYSIZE -h $HASH -i $ITER --use-random luksFormat $device
if [ $? -ne 0 ]; then
echo -en "\nPress Enter to continue "; read answer
return 1
else
tidy_mounts $device
fi
}
wipe_crypt() {
local answer= device=$1
local msgfile=/tmp/part.msg
local cryptdisk=$(find_mapper_disk $device)
echo -n "Destroy crypt on: '$device' ? [ y/N ] : "
read answer
case "$answer" in
y*|Y*) vgchange -a n &>/dev/null;
cryptsetup close $device
dd if=/dev/zero count=2048 of=$cryptdisk &>/dev/null
activate_vgs
echo -e "LUKS Header on '$device' wiped" >> $msgfile
tidy_mounts
esac
}
close_all_crypts() {
local crypt_list=$(dmsetup ls --target crypt |awk '{ print $1 }')
for mapper in $crypt_list; do
if cryptsetup status $mapper &>/dev/null; then
echo "closing LUKS device: $mapper"
cryptsetup close $mapper
fi
done
}
open_crypt() {
local device="$1"
local mapper=$(find_dm_by_device $device)
if [ -z "$mapper" ]; then
read -p "Enter LUKS device mapper name: " mapper
fi
if ! cryptsetup status $mapper &>/dev/null; then
cryptsetup open --type=luks $device $(sanitize_name $mapper)
fi
}
create_pv() {
local mapper="$1"
local answer=
if ! is_pv $mapper; then
pvcreate $mapper
tidy_mounts $mapper
fi
}
create_vg() {
local pv="$1"
local vg=$(find_vg $pv)
local answer=
if [ -z "$vg" ]; then
read -p "Create LVM volume group on LVM PV '$pv' ? [y/N] : " answer
case $answer in
y*|Y*) until [ -n "$(find_vg $pv)" ]; do
read -p "Enter name for LVM volume group? : " vg
vgcreate $vg $pv
done;;
*) return 1;;
esac
fi
activate_vgs
}
free_vg(){
local vg=$1
vgs --noheadings $vg|awk '{ print $7 }'
}
activate_vgs() {
if lvscan 2>/dev/null |grep -w inactive 1>/dev/null; then
vgchange -a y &>/dev/null
fi
}
create_lv() {
local mapper=$1
local vg=$(find_vg $mapper)
local lv= size= answer= msgfile=/tmp/lvm.msg
if [ "$(free_vg $vg)" != "0" ]; then
read -p "Enter name for new logical volume? <'q' to quit> : " lv
if echo $lv|egrep "^q|^Q" 1>/dev/null; then break; fi
lv=$(sanitize_name $lv)
read -p "Enter size for logical volume $lv ('xxxM' or 'x.xG' or 'xxx%{VG|PVS|FREE|ORIGIN}') ? : " size
case $size in
q*|Q*) break;;
*%VG|*%PVS|*%FREE|*%ORIGIN) lvcreate -l $size $vg -n $lv 1>$msgfile;;
*m|*M|*g|*G|*t|*T|*p|*P|*e|*E) lvcreate -L $size $vg -n $lv 1>$msgfile;; #must be 2nd or 100%FREE matches on *E
esac
else
echo -en "\033[1A\033[K\033[1;31mvolume group '$vg' has no free space.\033[0m";sleep 1;echo -en "\n\033[1A"
fi
}
delete_resize_lv() {
local mapper=$1
local vg=$(find_vg $mapper)
local size= lv= config=/tmp/mnt_list.in
local answer=$2 lvmmsg=/tmp/lvm.msg # rows to columns
local lv_list=$(lvs --noheadings $vg|awk '{ print $1 }'|tr "\n" " ")
if [ -n "$lv_list" ]; then
echo -en "\n\033[4m$question on '$vg' ? [ q to Quit ]\033[0m\n"
until echo "$lv_list" | grep -w "$lv" 1>/dev/null; do
echo -en "\033[1;32m>>>\033[37m[ $lv_list]: \033[0m"; read lv
echo -en "\033[1A\033[K" #move the cursor & clear the line
if echo $lv|egrep "^q|^Q" 1>/dev/null; then answer=cancel; break; fi
done
case $answer in
r*|R*) read -p "Enter new size for '$lv' ( +/-size || xxxM or x.xG || xxx%{VG|LV|PVS|FREE|ORIGIN} ) ? : " size
case $size in
q*|Q*) break;;
*%VG|*%LV|*%PVS|*%FREE|*%ORIGIN) if echo "$(find_fs $mapper)" | egrep -q -w "ext2|ext3|ext4|btrfs"; then
lvresize --resizefs -l $size /dev/$vg/$lv 1>$lvmmsg
else
lvresize -l $size /dev/$vg/$lv 1>$lvmmsg
fi;;
#must be 2nd or 100%FREE matches *E
*m|*M|*g|*G|*t|*T|*p|*P|*e|*E) if echo "$(find_fs $mapper)" | egrep -q -w "ext2|ext3|ext4|btrfs"; then
lvresize --resizefs -L $size /dev/$vg/$lv 1>$lvmmsg
else
lvresize -L $size /dev/$vg/$lv 1>/$lvmmsg
fi;;
esac
;;
d*|D*) lvremove $vg/$lv 1>$lvmmsg;
tidy_mounts "/dev/mapper/$vg-$lv";;
esac
else
echo -en "\033[1A\033[K\033[1;31mvolume group '$vg' has no logical volumes.\033[0m";sleep 1;echo -en "\n\033[1A"
fi
}
manage_pv() {
local pv="$1"
local action="$2"
local vg=$(find_vg $pv)
local total_pv=$(find_pvs_in_vg $vg |wc -w)
case "$action" in
remove) if [ $total_pv -gt 1 ]; then
if [ $? -eq 0 ]; then vgreduce $vg $pv; fi
if [ $? -eq 0 ]; then pvremove $pv; else return 1; fi
elif [ $total_pv -eq 1 ]; then
vgremove $vg
if [ $? -eq 0 ]; then pvremove $pv; else return 1; fi
fi;;
esac
}
manage_lv() {
local mapper=$1
local vg=$(find_vg $mapper)
local answer= lvmmsg=/tmp/lvm.msg
while :
do
clear;
cat $lvmmsg 2>/dev/null; rm -f $lvmmsg
echo -e "\n\033[1;4mConfiguring volume group '$vg' on device '$mapper':\033[0m"; lvm_table
echo -en "\033[4mCreate | Delete | Resize | Mount logical volumes? < 'x' to exit >\033[0m\n"
echo -en "\033[1;32m>>>\033[37m[ C | D | R | M | X ]: \033[0m"; read answer
echo -en "\033[1A\033[K"
case $answer in
c*|C*) create_lv $mapper;;
d*|D*) question="DELETE which logical volume"; delete_resize_lv $mapper $answer;;
r*|R*) question="RESIZE which logical volume"; delete_resize_lv $mapper $answer;;
m*|M*) return 0;;
x*|X*) return 1;;
esac
done
}
custom_install_lvm_luks() {
local diskdev=$1
local device=$ACTIVE_DEV type=$ACTIVE_TYPE
local fstype=$(find_fs $device) lvmmsg=/tmp/lvm.msg
local answer= mnt= vg= lv= mount_list=
if [ ! "$QUIT" ]; then
while true; do #break out if create_crypt fails
case "$type" in
part) if [ "$fstype" != "crypto_LUKS" ]; then
create_crypt $device
if [ $? -ne 0 ]; then break; fi
fstype=$(find_fs $device)
fi
if [ "$fstype" = "crypto_LUKS" ]; then
open_crypt $device;
device=$(find_dm_by_device $device)
fi
activate_vgs
if ! is_pv "$device"; then
read -p "Configure LVM on '$device' ? [ y/N ] : " answer
case "$answer" in
y*|Y*) create_pv $device;
if [ $? -eq 0 ]; then create_vg $device; fi;
if [ $? -eq 0 ]; then manage_lv $device; fi;;
*) mount_list=$device;;
esac
else
read -p "Manage LVM volumes on '$device' ? [ Y ] | r <remove> : " answer
case "$answer" in
r*|R*) read -p "Continuing will destroy all logical volumes on $device [ y/N ] : " answer
case "$answer" in
y*|Y*) manage_pv $device remove
esac;;
*) create_vg $device;;
esac
if [ $? -eq 0 ] && is_pv $device; then
manage_lv $device;
fi
fi;;
lvm) mount_list=$device;;
crypt) cryptsetup status $device
local cryptdisk=$(find_mapper_disk $device)
read -p "Wipe LUKS header & device || Reconfigure LUKS on '$device' ? [ w | r ]: " answer
case "$answer" in
w*|W*) wipe_crypt $device; confirm_wipe $cryptdisk;;
r*|R*) wipe_crypt $device; create_crypt $cryptdisk;;
esac
;;
esac
#manage_lv was not exited || crypt created successfully
if [ $? -eq 0 ]; then
if is_pv $device; then
vg=$(find_vg $device);
if [ -n "$vg" ]; then
local lv_list=$(lvs --noheadings $vg|awk '{ print $1 }'|tr "\n" " ")
for lv in $lv_list; do
mount_list="$mount_list /dev/mapper/$vg-$lv"
done
fi
fi
for mapper in $mount_list; do
mnt=$(find_mount_point $mapper)
if [ -z "$mnt" ]; then
choose_mount $mapper
else
read -p "Change Mount point? [$mnt] [y/N]: " answer
case "$answer" in
y*|Y*) choose_mount $mapper;;
esac
fi
choose_fs $mapper
write_mounts $mapper
done
fi
break
done
fi
}
choose_mount() {
local device=$1
local mnt=${mnt:-/mnt} answer=
local get_requests=$(requested_mnt_fs $device)
local requested_mnt=$(echo $get_requests |awk '{ print $1 }')
if [ -z "$requested_mnt" ]; then # if mnt is not already set
local type=$(find_dev_type $device)
local part_type=$(find_part_type $device)
local fs=$(find_fs $device)
local lv= no_mount_list="swap GPT-BIOS"
case "$type" in
lvm) lv="$(find_lv_by_device $device)"
if [ "$fs" = "swap" ] || [ "$lv" = "swap" ]; then
mnt=swap
fi;;
part) lv="$(basename $device)"
if [ "$fs" = "swap" ]; then
mnt=swap
elif [ "$device" = "$(find_boot_partition)" ]; then
lv="boot"
fi;;
crypt) lv="$(basename $device)"
if [ "$fs" = "swap" ] || [ "$lv" = "swap" ]; then
mnt=swap
fi;;
esac
case "$part_type" in
EF02) mnt="GPT-BIOS";;
esac
if ! echo $lv |grep -qw "root" && ! echo "$no_mount_list" |grep -w "$mnt" 1>/dev/null; then
mnt="$mnt/$lv"
fi
else
mnt=$requested_mnt
fi
echo -en "\nEnter Mount point for $device? [ $mnt ] : "; read answer
if echo $answer |grep "^/" 1>/dev/null || [ "$answer" = "swap" ]; then
mnt=$(sanitize_path $answer)
fi
ACTIVE_MNT=$mnt
}
choose_fs() {
local device=$1
local answer= fs= default_fs=$ROOTFS
local fs_list="ext2 ext3 ext4 swap btrfs none"
local get_requests=$(requested_mnt_fs $device)
local requested_fs=$(echo $get_requests |awk '{ print $2 }')
if [ -z "$requested_fs" ]; then # if fs is not already set
local fs_type=$(find_fs $device)
local lv="$(find_lv_by_device $device)"
local part_type="$(find_part_type $device)"
case "$part_type" in
EF02) default_fs="ext3";;
EF00) default_fs="ext2";;
8200) default_fs="swap";;
*) if [ "$fs_type" = "swap" ] || [ "$lv" = "swap" ]; then
default_fs="swap"
elif [ "$device" = "$(find_boot_partition)" ]; then
default_fs=$BOOTFS
fi;;
esac
else
default_fs=$requested_fs
fi
echo -en "\n\033[4mSelect filesystem to create? [ $default_fs ]: \033[0m\n"
until echo $fs_list | grep -w "$fs" &>/dev/null; do
echo -en "\033[1;32m>>>\033[37m[ $fs_list = n ]: \033[0m"; read answer
echo -en "\033[1A\033[K"
case "$answer" in
n*|N*) fs="none";;
*) if [ -z "$answer" ]; then
fs=$default_fs
else
fs=$answer
fi
;;
esac
done
ACTIVE_FS=$fs
}
detect_disks() { # rows to columns
local disk_list=$(lsblk -no NAME,SIZE,TYPE 2>/dev/null |awk '$3 = /disk/ { print $1 }' |tr "\n" " ")
local disk= default_disk=$(echo $disk_list |awk '{print $1}')
echo -e "\033[4mNAME SIZE VENDOR LABEL\033[0m" |awk '{ printf "%-14s %-10s %-10s %-10s\n", $1, $2, $3, $4}'
lsblk -no NAME,SIZE,TYPE,VENDOR,LABEL 2>/dev/null |awk '$3 = /disk/ { printf "%-10s %-10s %-10s %-10s\n", $1, $2, $4, $5}'
echo -en "\n\033[4mChoose Disk [ $default_disk ]\033[0m\n\n"
until echo $disk_list | grep -w $disk &> /dev/null; do
echo -en "\033[1A\033[K"
echo -en "\033[1;32m>>>\033[37m[ $disk_list]: \033[0m"; read disk
case "$disk" in
*) if [ -z "$disk" ]; then disk=$default_disk; fi
esac
done
ACTIVE_DISK=/dev/$disk
}
choose_scheme() {
local default_scheme= default_layout="none"
local scheme_list="gpt mbr" layout_list="lvm boot none"
local scheme= layout= answer=
local msgfile=/tmp/part.msg
case "$(check_scheme $ACTIVE_DISK)" in
dos) default_scheme="mbr";;
gpt) default_scheme="gpt";;
esac
echo -en "\n\033[4mChoose Partition Scheme [ $default_scheme ]\033[0m\n\n"
until echo $scheme_list | grep -w $scheme &> /dev/null; do
echo -en "\033[1A\033[K"
echo -en "\033[1;32m>>>\033[37m[ m(br) | g(pt) ]: \033[0m"; read answer
case "$answer" in
m*|M*) scheme="mbr";;
g*|G*) scheme="gpt";;
*) scheme=$default_scheme;;
esac
done
echo -en "\n\033[4mChoose Disk Layout [ none ]\033[0m\n\n"
until echo $layout_list | grep -w $layout &> /dev/null; do
echo -en "\033[1A\033[K"
echo -en "\033[1;32m>>>\033[37m[ l(vm) | b(oot) | none ]: \033[0m"; read answer
case "$answer" in
l*|L*) layout="lvm";
destroy_disks "an LVM Scheme";;
b*|B*) layout="boot";
destroy_disks "a Boot Scheme";;
*) layout=$default_layout;
scheme="manual";;
esac
done
case "$scheme" in
mbr) mbr_create $layout;
if [ $? = 1 ]; then
echo -e "** unhandled error **" >> $msgfile
fi;;
gpt) gpt_create $layout;
if [ $? = 1 ]; then
echo -e "** unhandled gpt error **" >> $msgfile
fi;;
manual) case "$(check_scheme $ACTIVE_DISK)" in
dos) cfdisk $ACTIVE_DISK;;
gpt) cgdisk $ACTIVE_DISK;;
esac
esac
tidy_mounts
}
destroy_crypts() {
local answer= device= crypt_list=$@
for device in $crypt_list; do
lvm_table
read -p "Wipe LUKS header (destroy) '$device' ? [ w ] : " answer
case "$answer" in
w*|W*) wipe_crypt $device;;
*) ;;
esac
done
}
destroy_disks() {
local new_scheme=$1
local lvm_device=$(find_lvm_partition $ACTIVE_DISK)
local crypt_list=$(find_crypts)
if [ -n "$crypt_list" ]; then
destroy_crypts $crypt_list
fi
if is_pv $lvm_device; then
lvm_table
read -p "Destroy LVM PV '$lvm_device' & replace with $new_scheme ? [ y/N ] : " answer
case "$answer" in
y*|Y*) manage_pv $lvm_device remove;;
esac
else
if lsblk -o TYPE $ACTIVE_DISK |grep "part" &>/dev/null; then
echo -e; lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL,UUID $ACTIVE_DISK 2>/dev/null; echo -e;
read -p "Destroy current Disk Layout & replace with $new_scheme ? [ y/N ] : " answer
case "$answer" in
y*|Y*) ;;
*) scheme="manual";;
esac
fi
fi
}
mbr_create() {
local bootline="2048,204800,83,*"
local config=/tmp/mnt_list.in
if [ "$(check_scheme $ACTIVE_DISK)" = "gpt" ]; then
sgdisk --zap $ACTIVE_DISK &>/dev/null
if [ -f $config ]; then rm -f $config; fi
fi
case "$1" in
lvm) local lvmline="206848,,8e";
echo -e "$bootline\n$lvmline" | sfdisk -q -uS $ACTIVE_DISK &>/dev/null || return 1;;
*) echo -e "$bootline\n$lvmline" | sfdisk -q -uS $ACTIVE_DISK &>/dev/null || return 1;
cfdisk $ACTIVE_DISK;;
esac
cat "$MBR" > $ACTIVE_DISK
echo "DOS MBR written to $ACTIVE_DISK" >> $msgfile
}
gpt_create() {
local biosboot=2 bootspace=100 config=/tmp/mnt_list.in msgfile=/tmp/part.msg
local diskspace=$(( $(grep ${ACTIVE_DISK#/dev/}$ /proc/partitions | awk '{print $3}') * 2 / 2048 - 1 ))
local sys_space=$(( $diskspace - $biosboot - $bootspace - 1 ));
if [ "$(check_scheme $ACTIVE_DISK)" = "dos" ]; then
if [ -f $config ]; then rm -f $config; fi
fi
case "$1" in
lvm) local lvmstring="--new 3:0:+${sys_space}MiB --typecode 3:8e00";