-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbootstrap-library.sh
1442 lines (1259 loc) · 47.9 KB
/
bootstrap-library.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
# ### encryption options
# How long is a secure passphrase ?
# https://gitlab.com/cryptsetup/cryptsetup/-/wikis/FrequentlyAskedQuestions#5-security-aspects
# LUKS1 and LUKS2: Use > 65 bit. That is e.g. 14 random chars from a-z
# or a random English sentence of > 108 characters length.
luks_encryption_options() {
echo "-c aes-xts-plain64 -s 512 -h sha256"
# LUKS chiper recommendation:
# For LUKS, the key size chosen is 512 bits.
# However, XTS mode requires two keys, so the LUKS key is split in half.
# Thus, -s 512 means AES-256
}
zfs_encryption_options() {
echo "-O encryption=aes-256-gcm -O keyformat=passphrase"
# ZFS chiper recommendation:
# ZFS native encryption defaults to aes-256-ccm, but the default has
# changed upstream to aes-256-gcm. AES-GCM seems to be generally preferred
# over AES-CCM, is faster now, and will be even faster in the future.
# https://crypto.stackexchange.com/questions/6842/how-to-choose-between-aes-ccm-and-aes-gcm-for-storage-volume-encryption
}
# ### default packages
get_default_packages() {
if which apt-get &> /dev/null; then
echo "cryptsetup gdisk mdadm lvm2 grub-pc grub-pc-bin grub-efi-amd64-bin grub-efi-amd64-signed efibootmgr squashfs-tools openssh-server curl gnupg gpgv ca-certificates bzip2 libc-bin rsync tmux haveged debootstrap"
elif which pamac &> /dev/null; then
echo "cryptsetup gptfdisk mdadm lvm2 grub openssh curl gnupg ca-certificates bzip2 rsync tmux manjaro-tools-iso git"
else
echo "Error: unknown platform, add list for other platforms in get_default_packages"
exit 1
fi
}
get_zfs_packages() {
if which apt-get &> /dev/null; then
echo "zfsutils-linux"
elif which pamac &> /dev/null; then
echo "zfs-utils"
else
echo "Error: unknown platform, add cmds for other platforms in get_zfs_packages"
exit 1
fi
}
# ### tool functions
# mk_partlabel, by_partlabel, x_of, first_of, is_substr, substr_fstype, substr_vgname
# dev_fs_uuid, dev_part_uuid, is_enczfs, is_zfs, is_lvm, is_mdadm, is_luks
# get_efi1_mountpath, get_efi2_mountpath
mk_partlabel() { # diskcount crypt=true/false/native lvm=vgname/""/false fs=""/ext4/xfs/zfs partname
local diskcount crypt lvm fs partname label
diskcount=$1; crypt=$2; lvm=$3; fs=$4; partname=$5; label=""
if test "$diskcount" != "1" -a "$fs" != "zfs"; then
label="raid_"
fi
if test "$crypt" = "true" -o "$crypt" = "native"; then
if test "$crypt" = "true" -o "$fs" != "zfs"; then
label="${label}luks_"
else
label="enc${label}"
fi
fi
if test "$lvm" != "" -a "$lvm" != "false"; then
label="${label}lvm.${lvm}_"
fi
if test "$fs" != ""; then
label="${label}${fs}_"
fi
label="${label}$partname"
echo "$label"
}
by_partlabel() { # partname_postfix [default_value=""]
# eg. "ROOT" /invalid
# may return 0, 1 or 2 entries
local partname default devlist
partname="$1"
default="$2"
devlist=$(find /dev/disk/by-partlabel/ -type l | sort | grep -E "${partname}[12]?$")
if test "$devlist" = ""; then
devlist="$default"
fi
echo "$devlist"
}
x_of() { # x | x_of 2
tr "\n" " " | awk '{print $'$1';}'
}
first_of() {
x_of 1
}
is_substr() { # string substr
(echo "$1" | grep -q "$2")
}
substr_fstype() { # devpathlist
# take first entry of list, parse and also return swap for swap
local entry partname fstype
entry=$(basename "$(echo "$@" | first_of)")
partname=$(echo "$entry" | sed -r "s/(^|.+_)([A-Z]+)([12]?)$/\2/g")
if test "$partname" = "SWAP"; then
fstype="swap"
elif test "$partname" = "EFI"; then
fstype="vfat"
else
fstype=$(echo "$entry" | sed -r "s/.*(ext4|xfs|zfs|other)_([A-Z]+)([12]?)$/\1/g")
if test "$fstype" = "$entry"; then
fstype="unknown"
fi
fi
echo "$fstype"
}
substr_vgname() { # devpathlist
# take first entry of list, parse and return lvm volume group name
local entry vgname
entry=$(basename "$(echo "$@" | first_of)")
vgname=$(echo "$entry" | sed -r "s/.*lvm\.([^_]+)_.*([A-Z]+[12]?)$/\1/g")
echo "$vgname"
}
is_enczfs() { # devpathlist
is_substr "$(basename "$(echo "$@" | first_of)")" "enczfs_"
}
is_zfs() { # devpathlist
is_substr "$(basename "$(echo "$@" | first_of)")" "zfs_"
}
is_lvm() { # devpathlist
is_substr "$(basename "$(echo "$@" | first_of)")" "lvm."
}
is_mdadm() { # devpathlist
is_substr "$(basename "$(echo "$@" | first_of)")" "raid_"
}
is_luks() { # devpathlist
is_substr "$(basename "$(echo "$@" | first_of)")" "luks_"
}
dev_fs_uuid() { # devpath
blkid -s UUID -o value "$1"
}
dev_part_uuid() { # devpath
blkid -s PARTUUID -o value "$1"
}
get_efi1_mountpath() {
local mountpath bootcount
mountpath=/boot
bootcount="$(by_partlabel BOOT | wc -w)"
if test "$bootcount" = 1 -o "$bootcount" = 2; then mountpath=/efi; fi
echo "$mountpath"
}
get_efi2_mountpath() {
echo "/efi2"
}
# ### Create and format partitions
# create_efi, create_swap, create_boot, create_boot_zpool
# create_and_mount_root, create_root_zpool, create_data, create_data_zpool
# create_lvm_volume, create_zfs_childfs, create_homedir, create_file_swap
# create_fstab, create_crypttab, create_zpool_cachefile
create_efi() {
local devlist devcount
devlist=$(by_partlabel EFI)
devcount=$(echo "$devlist" | wc -w)
for disk in $devlist; do
echo "format $disk with fat"
mkfs.fat -F 32 "$disk"
done
}
create_swap() { # diskpassword
local diskpassword devlist devcount targetdev
diskpassword="$1"
devlist=$(by_partlabel SWAP)
devcount=$(echo "$devlist" | wc -w)
if test "$devcount" = "1" -o "$devcount" = "2"; then
if test "$devcount" != "1"; then
echo "create mdadm swap partition $devlist"
echo "y" | mdadm --create /dev/md/$(hostname):mdadm-swap -v \
--symlinks=yes --assume-clean \
--level=mirror "--raid-disks=${devcount}" \
$devlist
targetdev="/dev/md/$(hostname):mdadm-swap"
else
targetdev="$devlist"
fi
echo "create luks swap partition $targetdev"
echo "$diskpassword" \
| cryptsetup luksFormat $(luks_encryption_options) "$targetdev"
echo "$diskpassword" \
| cryptsetup open --type luks "$targetdev" luks-swap
udevadm settle --exit-if-exists=/dev/mapper/luks-swap
echo "format swap"
mkswap -L swap /dev/mapper/luks-swap
fi
}
create_boot() { # basedir distrib_id
local basedir distrib_id devlist devcount
basedir="$1"
distrib_id="$2"
devlist=$(by_partlabel BOOT)
devcount=$(echo "$devlist" | wc -w)
if test "$devcount" = "1" -o "$devcount" = "2"; then
if is_zfs "$devlist"; then
echo "create zfs boot pool (bpool for $distrib_id) $devlist"
create_boot_zpool "$basedir" "$distrib_id" \
"$(if test "$devcount" != 1; then echo "mirror"; fi)" $devlist
else
if test "$devcount" != 1; then
echo "create mdadm-boot $devlist"
echo "y" | mdadm --create /dev/md/$(hostname):mdadm-boot -v \
--symlinks=yes --assume-clean \
--level=mirror "--raid-disks=${devcount}" \
$devlist
actlist="/dev/md/$(hostname):mdadm-boot"
else
actlist="$devlist"
fi
echo "format boot"
"mkfs.$(substr_fstype "$devlist")" -q -L boot "$actlist"
fi
fi
}
create_boot_zpool() { # basedir distrib_id zpool-create-parameter (eg. mirror sda1 sda2)
local basedir distrib_id
basedir="$1"
distrib_id="$2"
shift 2
# -d = no default options, because boot pool must be accessable from bootloader
zpool create \
-d \
-o ashift=12 \
-o autotrim=on \
-o feature@async_destroy=enabled \
-o feature@bookmarks=enabled \
-o feature@embedded_data=enabled \
-o feature@empty_bpobj=enabled \
-o feature@enabled_txg=enabled \
-o feature@extensible_dataset=enabled \
-o feature@filesystem_limits=enabled \
-o feature@hole_birth=enabled \
-o feature@large_blocks=enabled \
-o feature@lz4_compress=enabled \
-o feature@spacemap_histogram=enabled \
-O normalization=formD \
-O xattr=sa \
-O acltype=posixacl \
-O relatime=on \
-O compression=lz4 \
-O devices=off \
-O canmount=off \
-O mountpoint=/boot \
-R "$basedir" \
bpool "$@"
zfs create \
-o canmount=off \
-o mountpoint=none \
bpool/BOOT
zfs create \
-o canmount=noauto \
-o mountpoint=/boot \
bpool/BOOT/$distrib_id
# Set the bootfs property so the boot loader knows where to find the operating system
zpool set bootfs=bpool/BOOT/$distrib_id bpool
}
create_and_mount_root() { # basedir distrib_id diskpassword root_lvm_vol_size
local basedir distrib_id diskpassword root_lvm_vol_size
local devlist devcount devindex devtarget actlist templist vgname luksdev
basedir="$1"; distrib_id="$2"; diskpassword="$3"; root_lvm_vol_size="$4"
devlist=$(by_partlabel ROOT)
devcount=$(echo "$devlist" | wc -w)
if test "$devcount" != "1" -a "$devcount" != "2"; then
echo "error: 0 or > 2 ($devcount) devices found. only 1 or 2 are Supported"
exit 1
fi
actlist=$devlist
if (test "$devcount" != "1" && ! is_zfs "$devlist"); then
echo "create mdadm-root $actlist"
echo "y" | mdadm --create /dev/md/$(hostname):mdadm-root -v \
--symlinks=yes --assume-clean \
--level=mirror "--raid-disks=${devcount}" \
$actlist
actlist=/dev/md/$(hostname):mdadm-root
fi
if is_luks "$devlist"; then
devindex=1
templist=$actlist
actlist=""
for luksdev in $templist; do
devtarget=luks-root$(if (test "$devcount" != "1" && is_zfs "${luksdev}"); then echo "${i##*ROOT}"; fi)
echo "setup luks-root $devtarget $luksdev"
echo "$diskpassword" \
| cryptsetup luksFormat $(luks_encryption_options) ${luksdev}
echo "$diskpassword" \
| cryptsetup open --type luks ${luksdev} "$devtarget"
actlist="$(if test -n $actlist; then echo "$actlist "; fi)/dev/mapper/$devtarget"
dmsetup info
dmsetup mknodes
flock -s /dev/mapper/$devtarget partprobe /dev/mapper/$devtarget
devindex=$((devindex+1))
done
sleep 5
dmsetup info
fi
if is_lvm "$devlist"; then
if is_zfs "$devlist"; then
echo "error: device(s) $devlist are configured for ROOT on lvm *and* zfs which is not supported"
exit 1
fi
vgname="$(substr_vgname "$devlist")"
echo "setup lvm pv $actlist and vg $vgname"
lvm pvcreate -f -y $actlist
lvm vgcreate -y "$vgname" $actlist
echo "create format and mount lv lvm-root"
lvm lvcreate -y --size "${root_lvm_vol_size}" "$vgname" --name lvm-root
"mkfs.$(substr_fstype "$devlist")" -q -L root "/dev/$vgname/lvm-root"
mount "/dev/$vgname/lvm-root" "$basedir"
elif is_zfs "$devlist"; then
if is_enczfs "$devlist"; then
echo "create native encrypted zfs root pool (rpool for $distrib_id) $actlist"
create_root_zpool --password "$diskpassword" "$basedir" "$distrib_id" \
"$(if test "$devcount" != 1; then echo "mirror"; fi)" $actlist
else
echo "create zfs root pool (rpool for $distrib_id) $actlist"
create_root_zpool "$basedir" "$distrib_id" \
"$(if test "$devcount" != 1; then echo "mirror"; fi)" $actlist
fi
else
echo "format and mount root $actlist"
"mkfs.$(substr_fstype "$devlist")" -q -L root "$actlist"
mount "$actlist" "$basedir"
fi
}
create_root_zpool() { # [--password password] basedir distrib_id zpool-create-args* (eg. mirror sda1 sda2)
local basedir distrib_id diskpassword option_encrypt diskpassword_file
diskpassword=""; option_encrypt=""; diskpassword_file="/dev/shm/diskpassword"
if test "$1" = "--password"; then
diskpassword=$2; shift 2
printf "$diskpassword" > ${diskpassword_file}
option_encrypt="$(zfs_encryption_options) -O keylocation=file://${diskpassword_file}"
fi
basedir="$1"
distrib_id="$2"
shift 2
# XXX ashift 12 or 13 (4096/8192 byte sectors) depending disk
# -O compression=on|off|gzip|gzip-N|lz4|lzjb|zle|zstd|zstd-N|zstd-fast|zstd-fast-N
zpool create \
-o ashift=12 \
-o autotrim=on \
-O acltype=posixacl \
-O xattr=sa \
-O dnodesize=auto \
-O normalization=formD \
-O relatime=on \
-O compression=lz4 \
$option_encrypt \
-O canmount=off \
-O mountpoint=/ \
-R "$basedir" \
rpool "$@"
if test -e "${diskpassword_file}"; then
# remove temporary password file from ram
rm "${diskpassword_file}"
# set default keylocation back to prompt
zfs set keylocation=prompt rpool
fi
zfs create \
-o canmount=off \
-o mountpoint=none \
-o devices=off \
rpool/ROOT
# "/" from ROOT/$distrib_id
zfs create \
-o canmount=noauto \
-o mountpoint=/ \
-o com.sun:auto-snapshot:frequent=true \
-o com.sun:auto-snapshot:hourly=false \
-o com.sun:auto-snapshot:daily=true \
-o com.sun:auto-snapshot:weekly=false \
-o com.sun:auto-snapshot:monthly=false \
rpool/ROOT/$distrib_id
# Set the bootfs property on the descendant root filesystem
# so the boot loader knows where to find the operating system
zpool set bootfs=rpool/ROOT/$distrib_id rpool
# mount future root ("/") to $basedir/
zfs mount rpool/ROOT/$distrib_id
# ephemeral parts of /var
zfs create \
-o canmount=off \
-o com.sun:auto-snapshot:frequent=true \
-o com.sun:auto-snapshot:hourly=false \
-o com.sun:auto-snapshot:daily=false \
-o com.sun:auto-snapshot:weekly=false \
-o com.sun:auto-snapshot:monthly=false \
-o local.custom:auto-backup=false \
-o logbias=throughput \
rpool/var
# make /var/lib an unmountable container, so subcontainer have automatic mountpoint
zfs create \
-o canmount=off \
rpool/var/lib
# create /var/lib (which is part of /var) and other needed base directories
mkdir -p "$basedir/var/lib"
# important data to keep: rpool/data
zfs create \
-o setuid=off \
-o exec=off \
-o canmount=off \
-o mountpoint=none \
-o com.sun:auto-snapshot:frequent=true \
-o com.sun:auto-snapshot:hourly=true \
-o com.sun:auto-snapshot:daily=true \
-o com.sun:auto-snapshot:weekly=true \
-o com.sun:auto-snapshot:monthly=true \
rpool/data
# keep in sync with salt-shared/zfs/defaults.jinja/zfs_rpool_defaults, see README.md for snippet
zfs create -o "mountpoint=/home" -o "setuid=off" -o "exec=on" rpool/data/home
zfs create -o "mountpoint=/root" rpool/data/home/root
zfs create -o "mountpoint=/tmp" rpool/var/basedir-tmp
zfs create rpool/var/tmp
zfs create rpool/var/spool
zfs create -o "exec=off" rpool/var/log
zfs create -o "exec=off" rpool/var/cache
zfs create -o "recordsize=16K" -o "logbias=throughput" -o "primarycache=metadata" rpool/data/postgresql
zfs create -o "mountpoint=/var/lib/postgresql" rpool/data/postgresql/localhost
zfs create -o "mountpoint=/var/lib/mail" rpool/data/mail
if test "$distrib_id" = "ubuntu" -o "$distrib_id" = "debian"; then
# for apt based systems
zfs create rpool/var/backups
mkdir -p "$basedir/var/lib/apt"
zfs create -o "exec=off" -o "mountpoint=/var/lib/apt/lists" rpool/var/lib/apt-lists
# for pbuilder
zfs create -o "exec=on" -o "devices=on" rpool/var/cache/pbuilder
# for snaps
zfs create rpool/var/lib/snapd
fi
# for GNOME
#- name: var/lib/AccountsService
# for Docker
# - name: var/lib/docker
# for NFS
# - name: var/lib/nfs
# for LXC
# - name: var/lib/lxc
# for LibVirt
# - name: var/lib/libvirt
# for podman
# - name: var/lib/containers
# for containerd
# - name: var/lib/containerd
# keep in sync end
# correct filepermission for temp directories
chmod 1777 "$basedir/tmp"
chmod 1777 "$basedir/var/tmp"
}
create_data() { # diskpassword data_lvm_vol_size
local diskpassword root_lvm_vol_size
local devlist devcount devindex devtarget actlist templist vgname luksdev
diskpassword="$1"; data_lvm_vol_size="$2"
devlist=$(by_partlabel DATA)
devcount=$(echo "$devlist" | wc -w)
if test "$devcount" = "1" -o "$devcount" = "2"; then
actlist=$devlist
if (test "$devcount" != "1" && ! is_zfs "$devlist"); then
echo "create mdadm-data $actlist"
echo "y" | mdadm --create /dev/md/$(hostname):mdadm-data -v \
--symlinks=yes --assume-clean \
--level=mirror "--raid-disks=${devcount}" \
$actlist
actlist=/dev/md/$(hostname):mdadm-data
fi
if is_luks "$devlist"; then
devindex=1
templist=$actlist
actlist=""
for luksdev in $templist; do
devtarget=luks-data$(if (test "$devcount" != "1" && is_zfs "${luksdev}"); then echo "${i##*DATA}"; fi)
echo "setup luks $devtarget ${luksdev}"
echo "$diskpassword" \
| cryptsetup luksFormat $(luks_encryption_options) ${luksdev}
echo "$diskpassword" \
| cryptsetup open --type luks ${luksdev} "$devtarget"
actlist="$actlist /dev/mapper/$devtarget"
while test ! -L /dev/mapper/$devtarget; do
udevadm settle --exit-if-exists=/dev/mapper/$devtarget
sleep 1
done
devindex=$((devindex+1))
done
sleep 2
fi
if is_lvm "$devlist"; then
if is_zfs "$devlist"; then
echo "error: device(s) $devlist are configured for DATA using lvm *and* zfs which is not supported"
exit 1
fi
vgname="$(substr_vgname "$devlist")"
echo "setup lvm pv $actlist and vg $vgname"
lvm pvcreate -f -y $actlist
lvm vgcreate -y "$vgname" $actlist
echo "format lv lvm_data"
lvm lvcreate -y --size "${data_lvm_vol_size}" "$vgname" --name lvm_data
"mkfs.$(substr_fstype "$devlist")" -q -L data "/dev/$vgname/lvm_data"
elif is_zfs "$devlist"; then
if is_enczfs "$devlist"; then
echo "create native encrypted data zpool $actlist"
create_data_zpool --password "$diskpassword" "$basedir" \
"$(if test "$devcount" != 1; then echo "mirror"; fi)" $actlist
else
echo "create data zpool $actlist"
create_data_zpool "$basedir" \
"$(if test "$devcount" != 1; then echo "mirror"; fi)" $actlist
fi
else
if test "$(substr_fstype "$devlist")" = "other"; then
echo "not touching $actlist as fstype=other"
else
echo "format data $actlist"
"mkfs.$(substr_fstype "$devlist")" -q -L data "$actlist"
fi
fi
fi
}
create_data_zpool() { # [--password password] basedir zpool-create-args* (eg. mirror sda1 sda2)
local basedir diskpassword option_encrypt diskpassword_file
diskpassword=""; option_encrypt=""; diskpassword_file="/dev/shm/diskpassword"
if test "$1" = "--password"; then
diskpassword=$2; shift 2
printf "$diskpassword" > ${diskpassword_file}
option_encrypt="$(zfs_encryption_options) -O keylocation=file://${diskpassword_file}"
fi
basedir="$1"
shift
# XXX ashift 12 or 13 (4096/8192 byte sectors) depending disk
zpool create \
-o ashift=12 \
-o autotrim=on \
-O acltype=posixacl \
-O xattr=sa \
-O dnodesize=auto \
-O normalization=formD \
-O relatime=on \
-O compression=lz4 \
"$option_encrypt" \
-O canmount=off \
-O mountpoint=/ \
-R "$basedir" \
dpool "$@"
if test -e "${diskpassword_file}"; then
# remove temporary password file from ram
rm "${diskpassword_file}"
# set default keylocation back to prompt
zfs set keylocation=prompt dpool
fi
zfs create \
-o setuid=off \
-o exec=off \
-o mountpoint=/data \
-o com.sun:auto-snapshot:frequent=true \
"dpool/data"
}
create_lvm_volume() { # volpath volname volsize
# eg. vgroot/data username 20gb
local volpath volname volsize
volpath="$1"; volname="$2"; volsize="$3"
echo "create lvm volume $volpath/$volname"
lvcreate -y --size "$volsize" "$volpath" --name "$volname"
}
create_zfs_childfs() { # volpath volname
# eg. rpool/data/home username
local volpath volname mountpath
volpath=$1; volname=$2; mountpath="";
shift 2
if test "$1" = "--mount" -a "$2" != ""; then
mountpath="$2"
shift 2
fi
echo "create zfs volume $volpath/$volname"
zfs create "$volpath/$volname"
}
create_homedir() { # relativbase username
if is_zfs "$(by_partlabel ROOT)"; then
create_zfs_childfs "rpool/data/$1" "$2"
fi
}
create_file_swap() { # <swap_size-in-mb:default=1024>
local swap_size
swap_size="$1"
if "$swap_size" = ""; then swap_size="1024"; fi
swap_size="${swap_size}M"
if is_zfs "$(by_partlabel ROOT)"; then
zfs create \
-V $swap_size \
-b "$(getconf PAGESIZE)" \
-o compression=lz4 \
-o logbias=throughput \
-o sync=always \
-o primarycache=metadata \
-o secondarycache=none \
-o com.sun:auto-snapshot=false \
-o local.custom:auto-backup=false \
"$(get_zfs_pool ROOT)/swap"
mkswap -y "/dev/zvol/$(get_zfs_pool ROOT)/swap"
else
echo "ERROR: swap file generation on ext4/xfs is requested but not implemented!"
exit 1
fi
}
create_fstab() { # distrib_id
local devlist devcount devtarget devpath hasboot distrib_id efi_mount_opts
if test -e /etc/fstab; then rm /etc/fstab; fi
devlist=$(by_partlabel BOOT)
devcount=$(echo "$devlist" | wc -w)
hasboot="false"
distrib_id="ubuntu"
efi_mount_opts="x-systemd.idle-timeout=1min,x-systemd.automount,noauto,umask=0022,fmask=0022,dmask=0022 0 1"
if test "$1" != ""; then distrib_id="$1"; shift; fi
if test "$devcount" = "1" -o "$devcount" = "2"; then
hasboot="true"
if is_zfs "$devlist"; then
cat >> /etc/fstab << EOF
bpool/BOOT/$distrib_id /boot zfs defaults 0 0
EOF
else
devtarget="$devlist"
if is_mdadm "$devlist"; then devtarget="/dev/md/$(hostname):mdadm-boot"; fi
cat >> /etc/fstab << EOF
UUID=$(dev_fs_uuid "$devtarget") /boot $(substr_fstype "$devlist") defaults,nofail 0 1
EOF
fi
fi
devlist=$(by_partlabel EFI)
devcount=$(echo "$devlist" | wc -w)
if test "$hasboot" = "true"; then devpath=/efi; else devpath=/boot; fi
if test "$devcount" = "1"; then
cat >> /etc/fstab << EOF
PARTUUID=$(dev_part_uuid "$devlist") $devpath vfat $efi_mount_opts
EOF
else
cat >> /etc/fstab << EOF
PARTUUID=$(dev_part_uuid "$(echo "$devlist" | x_of 1)") $devpath vfat $efi_mount_opts
PARTUUID=$(dev_part_uuid "$(echo "$devlist" | x_of 2)") /efi2 vfat $efi_mount_opts
EOF
fi
devlist=$(by_partlabel SWAP)
devcount=$(echo "$devlist" | wc -w)
if test "$devcount" = "1" -o "$devcount" = "2"; then
devtarget="$devlist"
if is_mdadm "$devlist"; then devtarget="/dev/md/$(hostname):mdadm-swap"; fi
if is_luks "$devlist"; then devtarget="/dev/mapper/luks-swap"; fi
cat >> /etc/fstab << EOF
$devtarget swap swap defaults
EOF
fi
devlist=$(by_partlabel ROOT)
devcount=$(echo "$devlist" | wc -w)
if test "$devcount" = "1" -o "$devcount" = "2"; then
if is_zfs "$devlist"; then
cat >> /etc/fstab << EOF
rpool/ROOT/$distrib_id / zfs defaults 0 0
EOF
else
devtarget="$devlist"
if is_mdadm "$devlist"; then devtarget="/dev/md/$(hostname):mdadm-root"; fi
if is_luks "$devlist"; then devtarget="/dev/mapper/luks-root"; fi
if is_lvm "$devlist"; then
devtarget="/dev/$(substr_vgname "$devlist")/lvm-root"
fi
cat >> /etc/fstab << EOF
UUID=$(dev_fs_uuid "$devtarget") / $(substr_fstype "$devlist") defaults 0 1
EOF
fi
fi
devlist=$(by_partlabel DATA)
devcount=$(echo "$devlist" | wc -w)
if test "$devcount" = "1" -o "$devcount" = "2"; then
if is_zfs "$devlist"; then
cat >> /etc/fstab << EOF
dpool/data /data zfs defaults 0 0
EOF
else
devtarget="$devlist"
if is_mdadm "$devlist"; then devtarget="/dev/md/$(hostname):mdadm-data"; fi
if is_luks "$devlist"; then devtarget="/dev/mapper/luks-data"; fi
if is_lvm "$devlist"; then
devtarget="/dev/$(substr_vgname "$devlist")/lvm_data"
fi
cat >> /etc/fstab << EOF
UUID=$(dev_fs_uuid "$devtarget") /data $(substr_fstype "$devlist") defaults 0 1
EOF
fi
fi
}
create_crypttab() {
local devlist devcount thisdev p
if test -e /etc/crypttab; then rm /etc/crypttab; fi
for p in root data swap; do
devlist=$(by_partlabel "${p^^}")
devcount=$(echo "$devlist" | wc -w)
if is_luks "$devlist"; then
echo "configure crypttab for $p"
if (test "$devcount" = "2" && is_zfs "$devlist"); then
cat >> /etc/crypttab << EOF
luks-${p}1 /dev/disk/by-partuuid/$(dev_part_uuid "$(echo "$devlist" | x_of 1)") none luks,discard
luks-${p}2 /dev/disk/by-partuuid/$(dev_part_uuid "$(echo "$devlist" | x_of 2)") none luks,discard
EOF
else
if test "$devcount" = "1"; then
thisdev="/dev/disk/by-partuuid/$(dev_part_uuid "$devlist")"
else
thisdev="/dev/disk/by-uuid/$(dev_fs_uuid /dev/md/$(hostname):mdadm-${p})"
fi
cat >> /etc/crypttab << EOF
luks-${p} $thisdev none luks,discard
EOF
fi
fi
done
}
create_zpool_cachefile() {
local p poolname
for p in ROOT DATA BOOT; do
if is_zfs "$(by_partlabel $p)"; then
poolname=$(printf "${p:0:1}" | tr "[[:upper:]]" "[[:lower:]]"; printf "pool")
zpool set cachefile=/etc/zfs/zpool.cache $poolname
fi
done
}
# ### Activate/Deactivate mdadm, luks, lvm, zfs_pools
activate_mdadm() {
local p all_mdadm this_md
all_mdadm=$(mdadm --examine --brief --scan --config=partitions)
for p in BOOT SWAP ROOT DATA; do
if is_mdadm "$(by_partlabel $p)"; then
if test ! -e /dev/md/$(hostname):mdadm-${p,,}; then
this_md=$(echo "$all_mdadm" \
| grep -E "ARRAY.+name=[^:]+:mdadm-${p,,}" \
| sed -r "s/ARRAY ([^ ]+) .+/\1/g")
if test "$this_md" != "" -a "$this_md" != "/dev/md/$(hostname):mdadm-${p,,}"; then
echo "deactivate mdadm raid on $this_md because name is different"
mdadm --manage --stop $this_md
fi
echo "activate mdadm raid on $p"
mdadm --assemble /dev/md/$(hostname):mdadm-${p,,} $(by_partlabel $p)
else
echo "warning: mdadm raid on ${p,,} already activated"
fi
fi
done
}
deactivate_mdadm() {
local p
for p in BOOT SWAP ROOT DATA; do
if is_mdadm "$(by_partlabel $p)"; then
if test -e /dev/md/$(hostname):mdadm-${p,,}; then
echo "deactivate mdadm raid on $p"
mdadm --manage --stop /dev/md/$(hostname):mdadm-${p,,} || echo "failed!"
fi
fi
done
}
luks_start_one() { # luksname passphrase
local luksname passphrase device
luksname="$1"
passphrase="$2"
device="$(cat /etc/crypttab | grep "^$1" | sed -r "s/$1 ([^ ]+)( .+)/\1/g")"
if test -e /dev/mapper/$luksname; then
echo "warning: luks mapper $luksname already activated"
else
if test "$passphrase" = ""; then
cryptsetup open --type luks "$device" "$luksname"
else
printf "%s" "$passphrase" \
| cryptsetup -q open --type luks "$device" "$luksname" --key-file=-
fi
fi
}
activate_luks() { # passphrase
local devlist devcount actlist passphrase p
passphrase="$1"
for p in SWAP ROOT DATA; do
if is_luks "$(by_partlabel $p)"; then
devlist=$(by_partlabel "$p")
devcount=$(echo "$devlist" | wc -w)
actlist="luks-${p,,}"
echo "activate luks on $actlist"
if (test "$devcount" = "2" && is_zfs "$devlist"); then
luks_start_one ${actlist}1 "$passphrase"
luks_start_one ${actlist}2 "$passphrase"
else
luks_start_one ${actlist} "$passphrase"
fi
fi
done
}
deactivate_luks() {
local p
for p in SWAP ROOT DATA; do
if is_luks "$(by_partlabel $p)"; then
if test -e /dev/mapper/luks-${p,,}; then
echo "deactivate luks on luks-${p,,}"
cryptdisks_stop "luks-${p,,}" || echo "failed!"
fi
fi
done
}
activate_lvm() {
local p
if (is_lvm "$(by_partlabel ROOT)" || is_lvm "$(by_partlabel DATA)"); then
lvm vgscan -v
fi
for p in ROOT DATA; do
if is_lvm "$(by_partlabel $p)"; then
lvm vgchange --activate y "$(substr_vgname "$(by_partlabel $p)")"
fi
done
}
deactivate_lvm() {
local lv vg
for lv in $(lvm lvs -o vg_name,lv_name,lv_device_open --no-headings \
| grep -E ".+open[[:space:]]*$" \
| sed -r "s/[[:space:]]+([^[:space:]]+)[[:space:]]+([^[:space:]]+)[[:space:]]+.+/\1\/\2/g"); do
lvchange -a n $lv || echo "deactivate of lv $lv failed!"
done
for vg in $(lvm vgs -o vg_name --no-headings); do
vgchange -a n $vg || echo "deactivate of vg $vg failed!"
done
}
activate_zfs_pools() { # basedir passphrase force:true|false
local basedir passphrase p poolname import_opts altroot
basedir="$1"
passphrase="$2"
if test "$3" = "true"; then import_opts="-f"; else import_opts=""; fi
for p in ROOT DATA BOOT; do
if is_zfs "$(by_partlabel $p)"; then
altroot="$basedir"
if test "$p" != "ROOT"; then
altroot="$basedir/$(echo $p | tr '[[:upper:]]' '[[:lower:]]')"
fi
poolname=$(printf "${p:0:1}" | tr "[[:upper:]]" "[[:lower:]]"; printf "pool")
echo "zpool import $import_opts -N -R $altroot $poolname"
zpool import $import_opts -N -R "$altroot" "$poolname"
if is_enczfs "$(by_partlabel $p)"; then
echo "zfs load-key -r $poolname"
printf "%s" "$passphrase" | zfs load-key -r $poolname
fi
fi
done
}
deactivate_zfs_pools() {
local p poolname
for p in BOOT DATA ROOT; do
if is_zfs "$(by_partlabel $p)"; then
poolname=$(printf "${p:0:1}" | tr "[[:upper:]]" "[[:lower:]]"; printf "pool")
if is_enczfs "$(by_partlabel $p)"; then
echo "zfs unload-key -r $poolname"
zfs unload-key -r $poolname || echo "Warning, zfs unload-key -r $poolname exited with error"
fi
echo "zpool export $poolname"
zpool export $poolname || echo "Warning, zpool export $poolname exited with error"
fi
done
}
# ### Mounting / Unmounting root, boot, data, efi, bindmounts
mount_root() { # basedir force:true|false
local basedir devlist devcount import_opts bootfs
basedir=$1
devlist=$(by_partlabel ROOT)
devcount=$(echo "$devlist" | wc -w)
if test "$2" = "true"; then import_opts="-f"; else import_opts=""; fi
mkdir -p "$basedir"
if is_zfs "$devlist"; then
bootfs=$(zpool get -H -o value bootfs rpool)
echo "mount root ($bootfs) at $basedir"
zfs mount $bootfs
zfs mount -a || echo "Error: could not mount all zfs volumes!"
elif is_lvm "$devlist"; then
echo "mount $(substr_vgname "$devlist")/lvm-root at $basedir"
mount "/dev/$(substr_vgname "$devlist")/lvm-root" "$basedir"
else
echo "mount root at $basedir"
mount /dev/disk/by-label/root "$basedir"
fi
}