forked from tabulon-ext/zfs-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-zfs.sh
executable file
·1703 lines (1352 loc) · 58.9 KB
/
install-zfs.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
# shellcheck disable=SC2016 # single quoted strings with characters used for interpolation
set -o errexit
set -o pipefail
set -o nounset
# VARIABLES/CONSTANTS ##########################################################
# Variables set (indirectly) by the user
#
# The passphrase has a special workflow - it's sent to a named pipe (see create_passphrase_named_pipe()).
# The same strategy can possibly be used for `v_root_passwd` (the difference being that is used
# inside a jail); logging the ZFS commands is enough, for now.
#
# Note that `ZFS_PASSPHRASE` considers the unset state (see help).
v_boot_partition_size= # Integer number with `M` or `G` suffix
v_bpool_create_options= # array; see defaults below for format
v_passphrase=
v_root_password= # Debian-only
v_rpool_name=
v_rpool_create_options= # array; see defaults below for format
v_dataset_create_options= # string; see help for format
declare -A v_vdev_configs # ([0,1,2]=mirror, ...)
declare -a v_selected_disks # (/dev/by-id/disk_id, ...)
v_swap_size= # integer
v_free_tail_space= # integer
# Variables set during execution
v_linux_distribution= # Debian, Ubuntu, ... WATCH OUT: not necessarily from `lsb_release` (ie. UbuntuServer)
v_use_ppa= # 1=true, false otherwise (applies only to Ubuntu-based).
v_temp_volume_device= # /dev/zdN; scope: setup_partitions -> sync_os_temp_installation_dir_to_rpool
v_suitable_disks=() # (/dev/by-id/disk_id, ...); scope: find_suitable_disks -> select_disk
# Constants
#
# Note that Linux Mint is "Linuxmint" from v20 onwards. This actually helps, since some operations are
# specific to it.
c_hotswap_file=$PWD/install-zfs.hotswap.sh # see hotswap() for an explanation.
c_bpool_name=bpool
c_ppa=ppa:jonathonf/zfs
c_efi_system_partition_size=512 # megabytes
c_default_boot_partition_size=2048 # megabytes
c_memory_warning_limit=$((3584 - 128)) # megabytes; exclude some RAM, which can be occupied/shared
c_default_bpool_create_options=(
-o ashift=12
-o autotrim=on
-d
-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 acltype=posixacl
-O compression=lz4
-O devices=off
-O normalization=formD
-O relatime=on
-O xattr=sa
)
c_default_rpool_create_options=(
-o ashift=12
-o autotrim=on
-O acltype=posixacl
-O compression=lz4
-O dnodesize=auto
-O normalization=formD
-O relatime=on
-O xattr=sa
-O devices=off
)
c_dataset_options_help='# The defaults create a root pool similar to the Ubuntu default; see the script help for details.
# Double quotes are forbidden; lines starting with a hash (`#`) are ignored.
# Parameters and command substitutions are applied; useful variables are $c_zfs_mount_dir and $v_rpool_name.
'
# Can't include double quotes, due to the templating logic.
# KUbuntu has a /home/.directory, which must not be a separate dataset (it's a symlink).
#
c_default_dataset_create_options='
ROOT mountpoint=/ com.ubuntu.zsys:bootfs=yes com.ubuntu.zsys:last-used=$(date +%s)
ROOT/srv com.ubuntu.zsys:bootfs=no
ROOT/usr canmount=off com.ubuntu.zsys:bootfs=no
ROOT/usr/local
ROOT/var canmount=off com.ubuntu.zsys:bootfs=no
ROOT/var/games
ROOT/var/lib
ROOT/var/lib/AccountsService
ROOT/var/lib/apt
ROOT/var/lib/dpkg
ROOT/var/lib/NetworkManager
ROOT/var/log
ROOT/var/mail
ROOT/var/snap
ROOT/var/spool
ROOT/var/www
ROOT/tmp com.ubuntu.zsys:bootfs=no
USERDATA mountpoint=/ canmount=off
USERDATA/root mountpoint=/root canmount=on com.ubuntu.zsys:bootfs-datasets=$v_rpool_name/ROOT
$(find $c_installed_os_mount_dir/home -mindepth 1 -maxdepth 1 -not -name '\''.*'\'' -printf '\''
USERDATA/%P mountpoint=/home/%P canmount=on com.ubuntu.zsys:bootfs-datasets=$v_rpool_name/%P
'\'')
'
c_zfs_mount_dir=/mnt
c_installed_os_mount_dir=/target
declare -A c_supported_linux_distributions=([Debian]="10 11" [Ubuntu]="18.04 20.04" [UbuntuServer]="18.04 20.04" [LinuxMint]="19.1 19.2 19.3" [Linuxmint]="20 20.1" [elementary]=5.1)
c_temporary_volume_size=12 # gigabytes; large enough - Debian, for example, takes ~8 GiB.
c_passphrase_named_pipe=$(dirname "$(mktemp)")/zfs-installer.pp.fifo
c_dns=8.8.8.8
c_log_dir=$(dirname "$(mktemp)")/zfs-installer
c_install_log=$c_log_dir/install.log
c_os_information_log=$c_log_dir/os_information.log
c_running_processes_log=$c_log_dir/running_processes.log
c_disks_log=$c_log_dir/disks.log
c_zfs_module_version_log=$c_log_dir/updated_module_versions.log
# On a system, while installing Ubuntu 18.04(.4), all the `udevadm settle` invocations timed out.
#
# It's not clear why this happens, so we set a large enough timeout. On systems without this issue,
# the timeout won't matter, while on systems with the issue, the timeout will be enough to ensure
# that the devices are created.
#
# Note that the strategy of continuing in any case (`|| true`) is not the best, however, the exit
# codes are not documented.
#
c_udevadm_settle_timeout=10 # seconds
# HELPER FUNCTIONS #############################################################
# Invoke a function, with a primitive dynamic dispatch based on the distribution.
#
# Format: `invoke "function" [--optional]`.
#
# A target function must exist, otherwise a error is raised, unless `--optional` is specified.
# `--optional` is useful when a step is specific to a single distribution, e.g. Debian's root password.
#
# WATCH OUT! Don't forget *not* to call this from an ovverridden function, otherwise, it will call itself
# endlessly!
#
# Examples:
#
# $ function install_jail_zfs_packages { :; }
# $ function install_jail_zfs_packages_Debian { :; }
# $ invoke "install_jail_zfs_packages"
#
# If the distribution is `Debian`, the second will be invoked, otherwise, the first.
#
# $ function update_zed_cache_Ubuntu { :; }
# $ invoke "update_zed_cache" --optional
#
# If the distribution is `Debian`, nothing will happen.
#
# $ function update_zed_cache_Ubuntu { :; }
# $ invoke "update_zed_cache"
#
# If the distribution is `Debian`, an error will be raised.
#
function invoke {
local base_fx_name=$1
local distro_specific_fx_name=$1_$v_linux_distribution
local invoke_option=${2:-}
if [[ ! $invoke_option =~ ^(|--optional)$ ]]; then
>&2 echo "Invalid invoke() option: $invoke_option"
exit 1
fi
hot_swap_script
# Invoke it regardless when it's not optional.
if declare -f "$distro_specific_fx_name" > /dev/null; then
print_step_info_header "$distro_specific_fx_name"
"$distro_specific_fx_name"
elif declare -f "$base_fx_name" > /dev/null || [[ ! $invoke_option == "--optional" ]]; then
print_step_info_header "$base_fx_name"
"$base_fx_name"
fi
}
# Tee-hee-hee!!
#
# This is extremely useful for debugging long procedures. Since bash scripts can't be modified while
# running, this allows the dev to create a snapshot, and if the script fails after that, resume and
# add the hotswap script, so that the new code will be loaded automatically.
#
function hot_swap_script {
if [[ -f $c_hotswap_file ]]; then
# shellcheck disable=1090 # can't follow; the file might not exist anyway.
source "$c_hotswap_file"
fi
}
function print_step_info_header {
local function_name=$1
echo -n "
###############################################################################
# $function_name
###############################################################################
"
}
function print_variables {
for variable_name in "$@"; do
declare -n variable_reference="$variable_name"
echo -n "$variable_name:"
case "$(declare -p "$variable_name")" in
"declare -a"* )
for entry in "${variable_reference[@]}"; do
echo -n " \"$entry\""
done
;;
"declare -A"* )
for key in "${!variable_reference[@]}"; do
echo -n " $key=\"${variable_reference[$key]}\""
done
;;
* )
echo -n " $variable_reference"
;;
esac
echo
done
echo
}
# Very annoyingly, some distros (e.g. Linux Mint) have a "reduced" version of add-apt-repository
#
function checked_add_apt_repository {
local repository=$1
local option=${2:-} # optional: `--chroot`
local add_repo_command=(add-apt-repository --yes "$repository")
if add-apt-repository --help | grep -q "\--no-update"; then
# Assume that when this option isn't available, no update is performed. The fragmentation is a PITA.
#
add_repo_command+=(--no-update)
fi
case $option in
'')
"${add_repo_command[@]}"
;;
--chroot)
chroot_execute "${add_repo_command[*]}"
;;
*)
>&2 echo "Unexpected checked_add_apt_repository option: $2"
exit 1
esac
}
# Prints the zpool create vdev options, for the current v_vdev_configs; for example:
#
# mirror vdev1-part3 vdev2-part3 mirror vdev3-part3 vdev4-part3
#
# Input: $1 = `rpool`, `bpool`, `-` (the last doesn't append anything).
#
function compose_pool_create_vdev_options {
case $1 in
rpool)
local partition_suffix=-part3;;
bpool)
local partition_suffix=-part2;;
-)
local partition_suffix=;;
*)
>&2 echo "Wrong compose_pool_create_vdev_options() parameter: \`$1\`"
exit 1
esac
local result=
# First, we must put all the striping vdevs (which have blank vdev type).
#
for device_indexes in "${!v_vdev_configs[@]}"; do
local vdev_type=${v_vdev_configs[$device_indexes]}
if [[ -z $vdev_type ]]; then
mapfile -d, -t device_indexes < <(echo -n "$device_indexes")
for device_index in "${device_indexes[@]}"; do
result+=" ${v_selected_disks[$device_index]}$partition_suffix"
done
fi
done
# Then, all the other types.
#
for device_indexes in "${!v_vdev_configs[@]}"; do
local vdev_type=${v_vdev_configs[$device_indexes]}
if [[ -n $vdev_type ]]; then
result+=" $vdev_type"
mapfile -d, -t device_indexes < <(echo -n "$device_indexes")
for device_index in "${device_indexes[@]}"; do
result+=" ${v_selected_disks[$device_index]}$partition_suffix"
done
fi
done
echo -n "$result" | sed -e 's/^ //'
}
function chroot_execute {
chroot $c_zfs_mount_dir bash -c "$1"
}
# PROCEDURE STEP FUNCTIONS #####################################################
function display_help_and_exit {
local help
help='Usage: install-zfs.sh [-h|--help]
Sets up and install a ZFS Ubuntu installation.
This script needs to be run with admin permissions, from a Live CD.
The procedure can be entirely automated via environment variables:
- ZFS_OS_INSTALLATION_SCRIPT : path of a script to execute instead of Ubiquity (see dedicated section below)
- ZFS_USE_PPA : set to 1 to use packages from `'"$c_ppa"'` (automatically set to true if the O/S version doesn'\''t ship at least v0.8)
- ZFS_SELECTED_DISKS : full path of the devices to create the pool on, comma-separated
- ZFS_BOOT_PARTITION_SIZE : integer number with `M` or `G` suffix (defaults to `'${c_default_boot_partition_size}M'`)
- ZFS_PASSPHRASE : set non-blank to encrypt the pool, and blank not to. if unset, it will be asked.
- ZFS_DEBIAN_ROOT_PASSWORD
- ZFS_RPOOL_NAME
- ZFS_BPOOL_CREATE_OPTIONS : boot pool options to set on creation (see defaults below)
- ZFS_RPOOL_CREATE_OPTIONS : root pool options to set on creation (see defaults below)
- ZFS_VDEV_CONFIGS : example: `[0,1]=mirror, [2,3]=mirror`; the (0-based) indexes refer to the selected disks
- ZFS_DATASET_CREATE_OPTIONS : see explanation below
- ZFS_NO_INFO_MESSAGES : set 1 to skip informational messages
- ZFS_SWAP_SIZE : swap size (integer); set 0 for no swap
- ZFS_FREE_TAIL_SPACE : leave free space at the end of each disk (integer), for example, for a swap partition
- ZFS_SKIP_LIVE_ZFS_MODULE_INSTALL : (debug) set 1 to skip installing the ZFS package on the live system; speeds up installation on preset machines
When installing the O/S via $ZFS_OS_INSTALLATION_SCRIPT, the root pool is mounted as `'$c_zfs_mount_dir'`; the requisites are:
1. the virtual filesystems must be mounted in `'$c_zfs_mount_dir'` (ie. `for vfs in proc sys dev; do mount --rbind /$vfs '$c_zfs_mount_dir'/$vfs; done`)
2. internet must be accessible while chrooting in `'$c_zfs_mount_dir'` (ie. `echo nameserver '$c_dns' >> '$c_zfs_mount_dir'/etc/resolv.conf`)
3. `'$c_zfs_mount_dir'` must be left in a dismountable state (e.g. no file locks, no swap etc.);
Boot pool default create options: '"${c_default_bpool_create_options[*]/#-/$'\n' -}"'
Root pool default create options: '"${c_default_rpool_create_options[*]/#-/$'\n' -}"'
The root pool dataset creation options can be specified by passing a string of whom each line has:
- the dataset name (without the pool)
- the options (without `-o`)
The defaults, which create a root pool similar to the Ubuntu default, are:
'"$(echo -n "$c_default_dataset_create_options" | sed 's/^/ /')"'
Double quotes are forbidden. Parameters and command substitutions are applied; useful variables are $c_zfs_mount_dir and $v_rpool_name.
Datasets are created after the operating system is installed; at that stage, it'\'' mounted in the directory specified by $c_zfs_mount_dir.
'
echo "$help"
exit 0
}
function activate_debug {
mkdir -p "$c_log_dir"
exec 5> "$c_install_log"
BASH_XTRACEFD="5"
set -x
}
function set_distribution_data {
v_linux_distribution="$(lsb_release --id --short)"
if [[ "$v_linux_distribution" == "Ubuntu" ]] && grep -q '^Status: install ok installed$' < <(dpkg -s ubuntu-server 2> /dev/null); then
v_linux_distribution="UbuntuServer"
fi
v_linux_version="$(lsb_release --release --short)"
}
function store_os_distro_information {
lsb_release --all > "$c_os_information_log"
# Madness, in order not to force the user to invoke "sudo -E".
# Assumes that the user runs exactly `sudo bash`; it's not a (current) concern if the user runs off specification.
# Not found when running via SSH - inspect the processes for finding this information.
#
perl -lne 'BEGIN { $/ = "\0" } print if /^XDG_CURRENT_DESKTOP=/' /proc/"$PPID"/environ >> "$c_os_information_log"
}
function store_os_distro_information_Debian {
store_os_distro_information
echo "DEBIAN_VERSION=$(cat /etc/debian_version)" >> "$c_os_information_log"
}
# Simplest and most solid way to gather the desktop environment (!).
# See note in store_os_distro_information().
#
function store_running_processes {
ps ax --forest > "$c_running_processes_log"
}
function check_prerequisites {
local distro_version_regex=\\b${v_linux_version//./\\.}\\b
if [[ ! -d /sys/firmware/efi ]]; then
echo 'System firmware directory not found; make sure to boot in EFI mode!'
exit 1
elif [[ $(id -u) -ne 0 ]]; then
echo 'This script must be run with administrative privileges!'
exit 1
elif [[ -n ${ZFS_OS_INSTALLATION_SCRIPT:-} && ! -x $ZFS_OS_INSTALLATION_SCRIPT ]]; then
echo "The custom O/S installation script provided doesn't exist or is not executable!"
exit 1
elif [[ ! -v c_supported_linux_distributions["$v_linux_distribution"] ]]; then
echo "This Linux distribution ($v_linux_distribution) is not supported!"
exit 1
elif [[ ! ${c_supported_linux_distributions["$v_linux_distribution"]} =~ $distro_version_regex ]]; then
echo "This Linux distribution version ($v_linux_version) is not supported; supported versions: ${c_supported_linux_distributions["$v_linux_distribution"]}"
exit 1
elif [[ ${ZFS_USE_PPA:-} == "1" && $v_linux_distribution == "UbuntuServer" ]]; then
# As of Jun/2021, it breaks the installation.
#
echo "The PPA is not (currently) supported on Ubuntu Server! It's still possible to add it after the installation, but there are no guarantees."
exit 1
elif ! ping -c 1 "$c_dns" > /dev/null; then
echo "Can't contact the DNS ($c_dns)!"
exit 1
fi
set +x
if [[ -v ZFS_PASSPHRASE && -n $ZFS_PASSPHRASE && ${#ZFS_PASSPHRASE} -lt 8 ]]; then
echo "The passphase provided is too short; at least 8 chars required."
exit 1
fi
set -x
}
function display_intro_banner {
local dialog_message='Hello!
This script will prepare the ZFS pools on the system, install Ubuntu, and configure the boot.
In order to stop the procedure, hit Esc twice during dialogs (excluding yes/no ones), or Ctrl+C while any operation is running.
'
if [[ -z ${ZFS_NO_INFO_MESSAGES:-} ]]; then
whiptail --msgbox "$dialog_message" 30 100
fi
}
function check_system_memory {
local system_memory
system_memory=$(free -m | perl -lane 'print @F[1] if $. == 2')
if [[ $system_memory -lt $c_memory_warning_limit && -z ${ZFS_NO_INFO_MESSAGES:-} ]]; then
# A workaround for these cases is to use the swap generate, but this can potentially cause troubles
# (severe compilation slowdowns) if a user tries to compensate too little memory with a large swapfile.
#
local dialog_message='WARNING! In some cases, the ZFS modules require compilation.
On systems with relatively little RAM and many hardware threads, the procedure may crash during the compilation (e.g. 3 GB/16 threads).
In such cases, the module building may fail abruptly, either without visible errors (leaving "process killed" messages in the syslog), or with package installation errors (leaving odd errors in the module'\''s `make.log`).'
whiptail --msgbox "$dialog_message" 30 100
fi
}
function save_disks_log {
# shellcheck disable=SC2012 # `ls` may clean the output, but in this case, it doesn't matter
ls -l /dev/disk/by-id | tail -n +2 | perl -lane 'print "@F[8..10]"' > "$c_disks_log"
all_disk_ids=$(find /dev/disk/by-id -mindepth 1 -regextype awk -not -regex '.+-part[0-9]+$' | sort)
while read -r disk_id || [[ -n $disk_id ]]; do
cat >> "$c_disks_log" << LOG
## DEVICE: $disk_id ################################
$(udevadm info --query=property "$(readlink -f "$disk_id")")
LOG
done < <(echo -n "$all_disk_ids")
}
function find_suitable_disks {
# In some freaky cases, `/dev/disk/by-id` is not up to date, so we refresh. One case is after
# starting a VirtualBox VM that is a full clone of a suspended VM with snapshots.
#
udevadm trigger
local candidate_disk_ids
local mounted_devices
# Iterating via here-string generates an empty line when no devices are found. The options are
# either using this strategy, or adding a conditional.
#
candidate_disk_ids=$(find /dev/disk/by-id -regextype awk -regex '.+/(ata|nvme|scsi|mmc)-.+' -not -regex '.+-part[0-9]+$' | sort)
mounted_devices="$(df | awk 'BEGIN {getline} {print $1}' | xargs -n 1 lsblk -no pkname 2> /dev/null | sort -u || true)"
while read -r disk_id || [[ -n $disk_id ]]; do
local device_info
local block_device_basename
device_info="$(udevadm info --query=property "$(readlink -f "$disk_id")")"
block_device_basename="$(basename "$(readlink -f "$disk_id")")"
# It's unclear if it's possible to establish with certainty what is an internal disk:
#
# - there is no (obvious) spec around
# - pretty much everything has `DEVTYPE=disk`, e.g. LUKS devices
# - ID_TYPE is optional
#
# Therefore, it's probably best to rely on the id name, and just filter out optical devices.
#
if ! grep -q '^ID_TYPE=cd$' <<< "$device_info"; then
if ! grep -q "^$block_device_basename\$" <<< "$mounted_devices"; then
v_suitable_disks+=("$disk_id")
fi
fi
done < <(echo -n "$candidate_disk_ids")
if [[ ${#v_suitable_disks[@]} -eq 0 ]]; then
local dialog_message='No suitable disks have been found!
If you'\''re running inside a VMWare virtual machine, you need to add set `disk.EnableUUID = "TRUE"` in the .vmx configuration file.
If you think this is a bug, please open an issue on https://github.com/64kramsystem/zfs-installer/issues, and attach the file `'"$c_disks_log"'`.
'
whiptail --msgbox "$dialog_message" 30 100
exit 1
fi
print_variables v_suitable_disks
}
# By using a FIFO, we avoid having to hide statements like `echo $v_passphrase | zpoool create ...`
# from the logs.
#
function create_passphrase_named_pipe {
mkfifo "$c_passphrase_named_pipe"
}
function register_exit_hook {
function _exit_hook {
rm -f "$c_passphrase_named_pipe"
set +x
# Only the meaningful variable(s) are printed.
# In order to print the password, the store strategy should be changed, as the pipes may be empty.
#
echo "
Currently set exports, for performing an unattended (as possible) installation with the same configuration:
export ZFS_USE_PPA=$v_use_ppa
export ZFS_SELECTED_DISKS=$(IFS=,; echo -n "${v_selected_disks[*]}")
export ZFS_VDEV_CONFIGS='$(declare -p v_vdev_configs | perl -pe 's/.*?\((.+)\)/\1/')'
export ZFS_BOOT_PARTITION_SIZE=$v_boot_partition_size
export ZFS_PASSPHRASE=$(printf %q "$v_passphrase")
export ZFS_DEBIAN_ROOT_PASSWORD=$(printf %q "$v_root_password")
export ZFS_RPOOL_NAME=$v_rpool_name
export ZFS_BPOOL_CREATE_OPTIONS=\"${v_bpool_create_options[*]}\"
export ZFS_RPOOL_CREATE_OPTIONS=\"${v_rpool_create_options[*]}\"
export ZFS_NO_INFO_MESSAGES=1
export ZFS_SWAP_SIZE=$v_swap_size
export ZFS_FREE_TAIL_SPACE=$v_free_tail_space"
set -x
}
trap _exit_hook EXIT
}
function prepare_standard_repositories {
# Make sure it's enabled. Ubuntu MATE has it, while the standard Ubuntu doesn't.
# The program exits with success if the repository is already enabled.
#
checked_add_apt_repository universe
}
# Mint 20 has the CDROM repository enabled, but apt fails when updating due to it (or possibly due
# to it being incorrectly setup).
#
function prepare_standard_repositories_Linuxmint {
perl -i -pe 's/^(deb cdrom)/# $1/' /etc/apt/sources.list
# The universe repository may be already enabled, but it's more solid to ensure it.
#
prepare_standard_repositories
}
function prepare_standard_repositories_Debian {
# Debian doesn't require universe (for dialog).
:
}
function update_apt_index {
apt update
}
function set_use_zfs_ppa {
local zfs_package_version
zfs_package_version=$(apt show zfsutils-linux 2> /dev/null | perl -ne 'print /^Version: (\d+\.\d+)/')
# Test returns true if $zfs_package_version is blank.
#
if [[ ${ZFS_USE_PPA:-} == "1" ]] || dpkg --compare-versions "$zfs_package_version" lt 0.8; then
v_use_ppa=1
fi
}
function set_use_zfs_ppa_Debian {
# Only update apt; in this case, ZFS packages are handled in a specific way.
:
}
# Whiptail's lack of multiline editing is quite painful.
#
function install_host_base_packages {
# `efibootmgr` needs installation on all the systems.
# the other packages are each required by different distros, so for simplicity, they're all packed
# together.
#
apt install -y efibootmgr dialog software-properties-common
}
function select_disks {
if [[ -n ${ZFS_SELECTED_DISKS:-} ]]; then
mapfile -d, -t v_selected_disks < <(echo -n "$ZFS_SELECTED_DISKS")
else
while true; do
local menu_entries_option=()
local block_device_basename
if [[ ${#v_suitable_disks[@]} -eq 1 ]]; then
local disk_selection_status=ON
else
local disk_selection_status=OFF
fi
# St00pid simple way of sorting by block device name. Relies on the tokens not including whitespace.
for disk_id in "${v_suitable_disks[@]}"; do
block_device_basename="$(basename "$(readlink -f "$disk_id")")"
menu_entries_option+=("$disk_id ($block_device_basename) $disk_selection_status")
done
# shellcheck disable=2207 # cheating here, for simplicity (alternative: add tr and mapfile).
menu_entries_option=($(printf $'%s\n' "${menu_entries_option[@]}" | sort -k 2))
local dialog_message="Select the ZFS devices.
Devices with mounted partitions, cdroms, and removable devices are not displayed!
"
mapfile -t v_selected_disks < <(whiptail --checklist --separate-output "$dialog_message" 30 100 $((${#menu_entries_option[@]} / 3)) "${menu_entries_option[@]}" 3>&1 1>&2 2>&3)
if [[ ${#v_selected_disks[@]} -gt 0 ]]; then
break
fi
done
fi
print_variables v_selected_disks
}
function select_vdev_configs {
if [[ -n ${ZFS_VDEV_CONFIGS:-} ]]; then
eval declare -gA v_vdev_configs=\("$ZFS_VDEV_CONFIGS"\)
elif [[ ${#v_selected_disks[@]} -eq 1 ]]; then
# Formally, this is striping.
#
v_vdev_configs[0]=
else
while true; do
# Since the values (group disks) are represented by strings, there's no direct way of counting
# all of them.
# Remember that in $v_vdev_configs, keys are the indexes!
#
local all_vdev_disks_count
all_vdev_disks_count=$(echo "${!v_vdev_configs[@]}" | perl -pe 's/[, ]/\n/g' | wc -l)
if [[ $all_vdev_disks_count -eq ${#v_selected_disks[@]} ]]; then
break
fi
local current_setup
current_setup=$(compose_pool_create_vdev_options -)
local dialog_message
dialog_message="Choose the disk group type.
Disks for this group will be selected in the next dialog.
This and the next dialog will be repeated until all the groups are defined, so that it's possible to configure complex setups.
WARNING! The installer allows creating pools with different replication levels (eg. stripe + mirror), and it also doesn't verify their correctness.
Current pool creation setup: ${current_setup:-(none)}"
local vdev_types_option=(
"" Striping OFF
mirror Mirroring OFF
raidz RAIDZ1 OFF
raidz2 RAIDZ2 OFF
raidz3 RAIDZ3 OFF
)
local current_vdev_type
current_vdev_type=$(whiptail --radiolist "$dialog_message" 30 100 $((${#vdev_types_option[@]} / 3)) "${vdev_types_option[@]}" 3>&1 1>&2 2>&3)
local dialog_message="Choose the disks for the current disk group.
If any disks are remaining, they will be made available for another group."
local current_vdev_disks_option=()
for (( i = 0; i < ${#v_selected_disks[@]}; i++ )); do
if ! echo "${!v_vdev_configs[@]}" | perl -pe 's/[, ]/\n/g' | grep "^$i$"; then
local disk_basename
disk_basename=$(basename "${v_selected_disks[i]}")
current_vdev_disks_option+=("$i" "$disk_basename" OFF)
fi
done
local current_vdev_indexes
current_vdev_indexes=$(whiptail --checklist --separate-output "$dialog_message" 30 100 $((${#current_vdev_disks_option[@]} / 3)) "${current_vdev_disks_option[@]}" 3>&1 1>&2 2>&3)
if [[ -n $current_vdev_indexes ]]; then
current_vdev_indexes=${current_vdev_indexes//$'\n'/,}
current_vdev_indexes=${current_vdev_indexes%,}
v_vdev_configs[$current_vdev_indexes]=$current_vdev_type
fi
done
fi
}
function ask_root_password_Debian {
set +x
if [[ -n ${ZFS_DEBIAN_ROOT_PASSWORD:-} ]]; then
v_root_password="$ZFS_DEBIAN_ROOT_PASSWORD"
else
local password_invalid_message=
local password_repeat=-
while [[ $v_root_password != "$password_repeat" || -z $v_root_password ]]; do
v_root_password=$(whiptail --passwordbox "${password_invalid_message}Please enter the root account password (can't be empty):" 30 100 3>&1 1>&2 2>&3)
password_repeat=$(whiptail --passwordbox "Please repeat the password:" 30 100 3>&1 1>&2 2>&3)
password_invalid_message="Passphrase empty, or not matching! "
done
fi
set -x
}
function ask_encryption {
set +x
if [[ -v ZFS_PASSPHRASE ]]; then
v_passphrase=$ZFS_PASSPHRASE
else
local passphrase_repeat=_
local passphrase_invalid_message=
while [[ $v_passphrase != "$passphrase_repeat" || ${#v_passphrase} -lt 8 ]]; do
local dialog_message="${passphrase_invalid_message}Please enter the passphrase (8 chars min.):
Leave blank to keep encryption disabled.
"
v_passphrase=$(whiptail --passwordbox "$dialog_message" 30 100 3>&1 1>&2 2>&3)
if [[ -z $v_passphrase ]]; then
break
fi
passphrase_repeat=$(whiptail --passwordbox "Please repeat the passphrase:" 30 100 3>&1 1>&2 2>&3)
passphrase_invalid_message="Passphrase too short, or not matching! "
done
fi
set -x
}
function ask_boot_partition_size {
if [[ -n ${ZFS_BOOT_PARTITION_SIZE:-} ]]; then
v_boot_partition_size=$ZFS_BOOT_PARTITION_SIZE
else
local boot_partition_size_invalid_message=
while [[ ! $v_boot_partition_size =~ ^[0-9]+[MGmg]$ ]]; do
v_boot_partition_size=$(whiptail --inputbox "${boot_partition_size_invalid_message}Enter the boot partition size.
Supported formats: '512M', '3G'" 30 100 ${c_default_boot_partition_size}M 3>&1 1>&2 2>&3)
boot_partition_size_invalid_message="Invalid boot partition size! "
done
fi
print_variables v_boot_partition_size
}
function ask_swap_size {
if [[ -n ${ZFS_SWAP_SIZE:-} ]]; then
v_swap_size=$ZFS_SWAP_SIZE
else
local swap_size_invalid_message=
while [[ ! $v_swap_size =~ ^[0-9]+$ ]]; do
v_swap_size=$(whiptail --inputbox "${swap_size_invalid_message}Enter the swap size in GiB (0 for no swap):" 30 100 2 3>&1 1>&2 2>&3)
swap_size_invalid_message="Invalid swap size! "
done
fi
print_variables v_swap_size
}
function ask_free_tail_space {
if [[ -n ${ZFS_FREE_TAIL_SPACE:-} ]]; then
v_free_tail_space=$ZFS_FREE_TAIL_SPACE
else
local tail_space_invalid_message=
local tail_space_message="${tail_space_invalid_message}Enter the space in GiB to leave at the end of each disk (0 for none).
If the tail space is less than the space required for the temporary O/S installation, it will be reclaimed after it.
WATCH OUT! In rare cases, the reclamation may cause an error; if this happens, set the tail space to ${c_temporary_volume_size} gigabytes. It's still possible to reclaim the space after the ZFS installation is over.
For detailed informations, see the wiki page: https://github.com/64kramsystem/zfs-installer/wiki/Tail-space-reclamation-issue.
"
while [[ ! $v_free_tail_space =~ ^[0-9]+$ ]]; do
v_free_tail_space=$(whiptail --inputbox "$tail_space_message" 30 100 0 3>&1 1>&2 2>&3)
tail_space_invalid_message="Invalid size! "
done
fi
print_variables v_free_tail_space
}
function ask_rpool_name {
if [[ -n ${ZFS_RPOOL_NAME:-} ]]; then
v_rpool_name=$ZFS_RPOOL_NAME
else
local rpool_name_invalid_message=
while [[ ! $v_rpool_name =~ ^[a-z][a-zA-Z_:.-]+$ ]]; do
v_rpool_name=$(whiptail --inputbox "${rpool_name_invalid_message}Insert the name for the root pool" 30 100 rpool 3>&1 1>&2 2>&3)
rpool_name_invalid_message="Invalid pool name! "
done
fi
print_variables v_rpool_name
}
function ask_pool_create_options {
local bpool_create_options_message='Insert the create options for the boot pool
The mount-related options are automatically added, and must not be specified.'
local raw_bpool_create_options=${ZFS_BPOOL_CREATE_OPTIONS:-$(whiptail --inputbox "$bpool_create_options_message" 30 100 -- "${c_default_bpool_create_options[*]}" 3>&1 1>&2 2>&3)}
mapfile -d' ' -t v_bpool_create_options < <(echo -n "$raw_bpool_create_options")
local rpool_create_options_message='Insert the create options for the root pool
The encryption/mount-related options are automatically added, and must not be specified.'
local raw_rpool_create_options=${ZFS_RPOOL_CREATE_OPTIONS:-$(whiptail --inputbox "$rpool_create_options_message" 30 100 -- "${c_default_rpool_create_options[*]}" 3>&1 1>&2 2>&3)}
mapfile -d' ' -t v_rpool_create_options < <(echo -n "$raw_rpool_create_options")
print_variables v_bpool_create_options v_rpool_create_options
}
function ask_dataset_create_options {
if [[ -n ${ZFS_DATASET_CREATE_OPTIONS:-} ]]; then
v_dataset_create_options=$ZFS_DATASET_CREATE_OPTIONS
else
while true; do
local tempfile
tempfile=$(mktemp)
echo "$c_dataset_options_help$c_default_dataset_create_options" > "$tempfile"
local user_value
user_value=$(dialog --editbox "$tempfile" 30 120 3>&1 1>&2 2>&3)
if [[ -n $user_value && $user_value != *\"* ]]; then
v_dataset_create_options=$(echo "$user_value" | perl -ne 'print unless /^\s*#/')
break
fi
done
fi
print_variables v_dataset_create_options
}
function install_host_zfs_packages {
if [[ $v_use_ppa == "1" ]]; then
if [[ ${ZFS_SKIP_LIVE_ZFS_MODULE_INSTALL:-} != "1" ]]; then
checked_add_apt_repository "$c_ppa"
apt update
# Libelf-dev allows `CONFIG_STACK_VALIDATION` to be set - it's optional, but good to have.
# Module compilation log: `/var/lib/dkms/zfs/**/*/make.log` (adjust according to version).
#
echo "zfs-dkms zfs-dkms/note-incompatible-licenses note true" | debconf-set-selections
apt install --yes libelf-dev zfs-dkms
systemctl stop zfs-zed
modprobe -r zfs
modprobe zfs
systemctl start zfs-zed
fi
fi
# Required only by some distros.
#
apt install --yes zfsutils-linux
zfs --version > "$c_zfs_module_version_log" 2>&1
}
function install_host_zfs_packages_Debian {
if [[ ${ZFS_SKIP_LIVE_ZFS_MODULE_INSTALL:-} != "1" ]]; then
echo "zfs-dkms zfs-dkms/note-incompatible-licenses note true" | debconf-set-selections
echo "deb http://deb.debian.org/debian buster contrib" >> /etc/apt/sources.list
echo "deb http://deb.debian.org/debian buster-backports main contrib" >> /etc/apt/sources.list
apt update
apt install --yes -t buster-backports zfs-dkms
modprobe zfs
fi
zfs --version > "$c_zfs_module_version_log" 2>&1
}
function install_host_zfs_packages_UbuntuServer {
if [[ $v_use_ppa != "1" ]]; then
apt install --yes zfsutils-linux efibootmgr
zfs --version > "$c_zfs_module_version_log" 2>&1
elif [[ ${ZFS_SKIP_LIVE_ZFS_MODULE_INSTALL:-} != "1" ]]; then
# This is not needed on UBS 20.04, which has the modules built-in - incidentally, if attempted,
# it will cause /dev/disk/by-id changes not to be recognized.
#
# On Ubuntu Server, `/lib/modules` is a SquashFS mount, which is read-only.
#
cp -R /lib/modules /tmp/
systemctl stop 'systemd-udevd*'
umount /lib/modules
rm -r /lib/modules
ln -s /tmp/modules /lib
systemctl start --all 'systemd-udevd*'
# Additionally, the linux packages for the running kernel are not installed, at least when
# the standard installation is performed. Didn't test on the HWE option; if it's not required,
# this will be a no-op.
#
apt update
apt install --yes "linux-headers-$(uname -r)"