-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-zfs.sh
1474 lines (1131 loc) · 48.3 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=SC2015,SC2016
# Shellcheck issue descriptions:
#
# - SC2015: <condition> && <operation> || true
# - SC2016: annoying warning about using single quoted strings with characters used for interpolation
set -o errexit
set -o pipefail
set -o nounset
# VARIABLES/CONSTANTS ##########################################################
# Variables set by the script
v_linux_distribution= # Debian, Ubuntu, ... WATCH OUT: not necessarily from `lsb_release` (ie. UbuntuServer)
v_zfs_08_in_repository= # 1=true, false otherwise (applies only to Ubuntu-based)
# 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` and `ZFS_POOLS_RAID_TYPE` consider the unset state (see help).
v_boot_partition_size= # Integer number with `M` or `G` suffix
v_bpool_name=
v_bpool_tweaks= # array; see defaults below for format
v_root_password= # Debian-only
v_rpool_name=
v_rpool_tweaks= # array; see defaults below for format
v_pools_raid_type=
declare -a v_selected_disks # (/dev/by-id/disk_id, ...)
v_swap_size= # integer
v_free_tail_space= # integer
# Variables set during execution
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_efi_system_partition_size=512 # megabytes
c_default_boot_partition_size=2048 # megabytes
c_default_bpool_tweaks="-o ashift=12"
c_default_rpool_tweaks="-o ashift=12 -O acltype=posixacl -O compression=lz4 -O dnodesize=auto -O relatime=on -O xattr=sa -O normalization=formD"
c_zfs_mount_dir=/mnt
c_installed_os_data_mount_dir=/target
declare -A c_supported_linux_distributions=([Debian]=10 [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_passphrase_named_pipe_2=$(dirname "$(mktemp)")/zfs-installer.pp.2.fifo
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 #############################################################
# Chooses a function and invokes it depending on the O/S distribution.
#
# Example:
#
# $ function install_jail_zfs_packages { :; }
# $ function install_jail_zfs_packages_Debian { :; }
# $ distro_dependent_invoke "install_jail_zfs_packages"
#
# If the distribution is `Debian`, the second will be invoked, otherwise, the
# first.
#
# If the function is invoked with `--noforce` as second parameter, and there is
# no matching function:
#
# $ function update_zed_cache_Ubuntu { :; }
# $ distro_dependent_invoke "install_jail_zfs_packages" --noforce
#
# then nothing happens. Without `--noforce`, this invocation will cause an
# error.
#
function distro_dependent_invoke {
local distro_specific_fx_name="$1_$v_linux_distribution"
if declare -f "$distro_specific_fx_name" > /dev/null; then
"$distro_specific_fx_name"
else
if ! declare -f "$1" > /dev/null && [[ "${2:-}" == "--noforce" ]]; then
: # do nothing
else
"$1"
fi
fi
}
# shellcheck disable=SC2120 # allow parameters passing even if no calls pass any
function print_step_info_header {
echo -n "
###############################################################################
# ${FUNCNAME[1]}"
[[ "${1:-}" != "" ]] && echo -n " $1" || true
echo "
###############################################################################
"
}
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
}
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_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_ENCRYPT_RPOOL : set 1 to encrypt the pool
- ZFS_PASSPHRASE : set non-blank to encrypt the pool, and blank not to. if unset, it will be asked.
- ZFS_DEBIAN_ROOT_PASSWORD
- ZFS_BPOOL_NAME
- ZFS_RPOOL_NAME
- ZFS_BPOOL_TWEAKS : boot pool options to set on creation (defaults to `'$c_default_bpool_tweaks'`)
- ZFS_RPOOL_TWEAKS : root pool options to set on creation (defaults to `'$c_default_rpool_tweaks'`)
- ZFS_POOLS_RAID_TYPE : options: blank (striping), `mirror`, `raidz`, `raidz2`, `raidz3`; if unset, it will be asked.
- 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 8.8.8.8 >> '$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.);
'
echo "$help"
exit 0
}
function activate_debug {
print_step_info_header
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 {
print_step_info_header
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 {
print_step_info_header
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 [[ "${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
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 {
print_step_info_header
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 [[ ${ZFS_NO_INFO_MESSAGES:-} == "" ]]; then
whiptail --msgbox "$dialog_message" 30 100
fi
}
function find_suitable_disks {
print_step_info_header
# 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
# 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"
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
cat >> "$c_disks_log" << LOG
## DEVICE: $disk_id ################################
$(udevadm info --query=property "$(readlink -f "$disk_id")")
LOG
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/saveriomiroddi/zfs-installer/issues, and attach the file `'"$c_disks_log"'`.
'
whiptail --msgbox "$dialog_message" 30 100
exit 1
fi
print_variables v_suitable_disks
}
# REQUIREMENT: it must be ensured that, for any distro, `apt update` is invoked at this step, as
# subsequent steps rely on it.
#
# There are three parameters:
#
# 1. the tools are preinstalled (ie. Ubuntu Desktop based);
# 2. the default repository supports ZFS 0.8 (ie. Ubuntu 20.04+ based);
# 3. the distro provides the precompiled ZFS module (i.e. Ubuntu based, not Debian)
#
# Fortunately, with Debian-specific logic isolated, we need conditionals based only on #2 - see
# install_host_packages() and install_host_packages_UbuntuServer().
#
function find_zfs_package_requirements {
print_step_info_header
apt update
local zfs_package_version
zfs_package_version=$(apt show zfsutils-linux 2> /dev/null | perl -ne 'print $1 if /^Version: (\d+\.\d+)\./')
if [[ -n $zfs_package_version ]]; then
if [[ ! $zfs_package_version =~ ^0\. ]]; then
>&2 echo "Unsupported ZFS version!: $zfs_package_version"
exit 1
elif (( $(echo "$zfs_package_version" | cut -d. -f2) >= 8 )); then
v_zfs_08_in_repository=1
fi
fi
}
function find_zfs_package_requirements_Debian {
# Only update apt; in this case, ZFS packages are handled in a specific way.
apt update
}
# Mint 20 has the CDROM repository enabled, but apt fails when updating due to it (or possibly due
# to it being incorrectly setup).
#
function find_zfs_package_requirements_Linuxmint {
print_step_info_header
perl -i -pe 's/^(deb cdrom)/# $1/' /etc/apt/sources.list
find_zfs_package_requirements
}
# By using a FIFO, we avoid having to hide statements like `echo $v_passphrase | zpoool create ...`
# from the logs.
#
# The FIFO file is left in the filesystem after the script exits. It's not worth taking care of
# removing it, since the environment is entirely ephemeral.
#
function create_passphrase_named_pipe {
rm -f "$c_passphrase_named_pipe" "$c_passphrase_named_pipe_2"
mkfifo "$c_passphrase_named_pipe" "$c_passphrase_named_pipe_2"
}
function select_disks {
print_step_info_header
if [[ "${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
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
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_pools_raid_type {
print_step_info_header
if [[ -v ZFS_POOLS_RAID_TYPE ]]; then
v_pools_raid_type=$ZFS_POOLS_RAID_TYPE
elif [[ ${#v_selected_disks[@]} -ge 2 ]]; then
# Entries preparation.
local menu_entries_option=(
"" "Striping array" OFF
mirror Mirroring OFF
raidz RAIDZ1 OFF
)
if [[ ${#v_selected_disks[@]} -ge 3 ]]; then
menu_entries_option+=(raidz2 RAIDZ2 OFF)
fi
if [[ ${#v_selected_disks[@]} -ge 4 ]]; then
menu_entries_option+=(raidz3 RAIDZ3 OFF)
fi
# Defaults (ultimately, arbitrary). Based on https://calomel.org/zfs_raid_speed_capacity.html.
if [[ ${#v_selected_disks[@]} -ge 11 ]]; then
menu_entries_option[14]=ON
elif [[ ${#v_selected_disks[@]} -ge 6 ]]; then
menu_entries_option[11]=ON
elif [[ ${#v_selected_disks[@]} -ge 5 ]]; then
menu_entries_option[8]=ON
else
menu_entries_option[5]=ON
fi
local dialog_message="Select the pools RAID type."
v_pools_raid_type=$(whiptail --radiolist "$dialog_message" 30 100 $((${#menu_entries_option[@]} / 3)) "${menu_entries_option[@]}" 3>&1 1>&2 2>&3)
fi
}
function ask_root_password_Debian {
print_step_info_header
set +x
if [[ ${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" || "$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 {
print_step_info_header
local passphrase=
set +x
if [[ -v ZFS_PASSPHRASE ]]; then
passphrase=$ZFS_PASSPHRASE
else
local passphrase_repeat=_
local passphrase_invalid_message=
while [[ $passphrase != "$passphrase_repeat" || ${#passphrase} -lt 8 ]]; do
local dialog_message="${passphrase_invalid_message}Please enter the passphrase (8 chars min.):
Leave blank to keep encryption disabled.
"
passphrase=$(whiptail --passwordbox "$dialog_message" 30 100 3>&1 1>&2 2>&3)
if [[ -z $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
echo -n "$passphrase" > "$c_passphrase_named_pipe" &
echo -n "$passphrase" > "$c_passphrase_named_pipe_2" &
set -x
}
function ask_boot_partition_size {
print_step_info_header
if [[ ${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 {
print_step_info_header
if [[ ${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 {
print_step_info_header
if [[ ${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/saveriomiroddi/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_pool_names {
print_step_info_header
if [[ ${ZFS_BPOOL_NAME:-} != "" ]]; then
v_bpool_name=$ZFS_BPOOL_NAME
else
local bpool_name_invalid_message=
while [[ ! $v_bpool_name =~ ^[a-z][a-zA-Z_:.-]+$ ]]; do
v_bpool_name=$(whiptail --inputbox "${bpool_name_invalid_message}Insert the name for the boot pool" 30 100 bpool 3>&1 1>&2 2>&3)
bpool_name_invalid_message="Invalid pool name! "
done
fi
if [[ ${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_bpool_name v_rpool_name
}
function ask_pool_tweaks {
print_step_info_header
local bpool_tweaks_message='Insert the tweaks for the boot pool
The option `-O devices=off` is already set, and must not be specified.'
local raw_bpool_tweaks=${ZFS_BPOOL_TWEAKS:-$(whiptail --inputbox "$bpool_tweaks_message" 30 100 -- "$c_default_bpool_tweaks" 3>&1 1>&2 2>&3)}
mapfile -d' ' -t v_bpool_tweaks < <(echo -n "$raw_bpool_tweaks")
local rpool_tweaks_message='Insert the tweaks for the root pool
The option `-O devices=off` is already set, and must not be specified.'
local raw_rpool_tweaks=${ZFS_RPOOL_TWEAKS:-$(whiptail --inputbox "$rpool_tweaks_message" 30 100 -- "$c_default_rpool_tweaks" 3>&1 1>&2 2>&3)}
mapfile -d' ' -t v_rpool_tweaks < <(echo -n "$raw_rpool_tweaks")
print_variables v_bpool_tweaks v_rpool_tweaks
}
function install_host_packages {
print_step_info_header
if [[ $v_zfs_08_in_repository != "1" ]]; then
if [[ ${ZFS_SKIP_LIVE_ZFS_MODULE_INSTALL:-} != "1" ]]; then
add-apt-repository --yes ppa:jonathonf/zfs
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/0.8.2/build/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
apt install --yes efibootmgr
zfs --version > "$c_zfs_module_version_log" 2>&1
}
function install_host_packages_Debian {
print_step_info_header
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
apt install --yes efibootmgr
zfs --version > "$c_zfs_module_version_log" 2>&1
}
# Differently from Ubuntu, Mint doesn't have the package installed in the live version.
#
function install_host_packages_Linuxmint {
print_step_info_header
apt install --yes zfsutils-linux
install_host_packages
}
function install_host_packages_elementary {
print_step_info_header
if [[ ${ZFS_SKIP_LIVE_ZFS_MODULE_INSTALL:-} != "1" ]]; then
apt update
apt install -y software-properties-common
fi
install_host_packages
}
function install_host_packages_UbuntuServer {
print_step_info_header
if [[ $v_zfs_08_in_repository == "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 '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 -y "linux-headers-$(uname -r)"
install_host_packages
else
apt install --yes efibootmgr
fi
}
function setup_partitions {
print_step_info_header
local required_tail_space=$((v_free_tail_space > c_temporary_volume_size ? v_free_tail_space : c_temporary_volume_size))
for selected_disk in "${v_selected_disks[@]}"; do
# wipefs doesn't fully wipe ZFS labels.
#
find "$(dirname "$selected_disk")" -name "$(basename "$selected_disk")-part*" -exec bash -c '
zpool labelclear -f "$1" 2> /dev/null || true
' _ {} \;
# More thorough than `sgdisk --zap-all`.
#
wipefs --all "$selected_disk"
sgdisk -n1:1M:+"${c_efi_system_partition_size}M" -t1:EF00 "$selected_disk" # EFI boot
sgdisk -n2:0:+"$v_boot_partition_size" -t2:BF01 "$selected_disk" # Boot pool
sgdisk -n3:0:"-${required_tail_space}G" -t3:BF01 "$selected_disk" # Root pool
sgdisk -n4:0:0 -t4:8300 "$selected_disk" # Temporary partition
done
# The partition symlinks are not immediately created, so we wait.
#
# There is still a hard to reproduce issue where `zpool create rpool` fails with:
#
# cannot resolve path '/dev/disk/by-id/<disk_id>-part2'
#
# It's a race condition (waiting more solves the problem), but it's not clear which exact event
# to wait on.
# There's no relation to the missing symlinks - the issue also happened for partitions that
# didn't need a `sleep`.
#
# Using `partprobe` doesn't solve the problem.
#
# Replacing the `-L` test with `-e` is a potential solution, but couldn't check on the
# destination files, due to the nondeterministic nature of the problem.
#
# Current attempt: `udevadm`, which should be the cleanest approach.
#
udevadm settle --timeout "$c_udevadm_settle_timeout" || true
# for disk in "${v_selected_disks[@]}"; do
# part_indexes=(1 2 3)
#
# for part_i in "${part_indexes[@]}"; do
# while [[ ! -L "${disk}-part${part_i}" ]]; do sleep 0.25; done
# done
# done
for selected_disk in "${v_selected_disks[@]}"; do
mkfs.fat -F 32 -n EFI "${selected_disk}-part1"
done
v_temp_volume_device=$(readlink -f "${v_selected_disks[0]}-part4")
}
function install_operating_system {
print_step_info_header
local dialog_message='The Ubuntu GUI installer will now be launched.
Proceed with the configuration as usual, then, at the partitioning stage:
- check `Something Else` -> `Continue`
- select `'"$v_temp_volume_device"'` -> `Change`
- set `Use as:` to `Ext4`
- check `Format the partition:`
- set `Mount point` to `/` -> `OK` -> `Continue`
- `Install Now` -> `Continue`
- at the end, choose `Continue Testing`
'
if [[ ${ZFS_NO_INFO_MESSAGES:-} == "" ]]; then
whiptail --msgbox "$dialog_message" 30 100
fi
# The display is restricted only to the owner (`user`), so we need to allow any user to access
# it.
#
sudo -u "$SUDO_USER" env DISPLAY=:0 xhost +
DISPLAY=:0 ubiquity --no-bootloader
swapoff -a
# /target is not always unmounted; the reason is unclear. A possibility is that if there is an
# active swapfile under `/target` and ubiquity fails to unmount /target, it fails silently,
# leaving `/target` mounted.
# For this reason, if it's not mounted, we remount it.
#
# Note that we assume that the user created only one partition on the temp volume, as expected.
#
if ! mountpoint -q "$c_installed_os_data_mount_dir"; then
mount "$v_temp_volume_device" "$c_installed_os_data_mount_dir"
fi
rm -f "$c_installed_os_data_mount_dir/swapfile"
}
function install_operating_system_Debian {
print_step_info_header
# The temporary volume size displayed is an approximation of the format used by the installer,
# but it's acceptable - the complexity required is not worth (eg. converting hypothetical units,
# etc.).
#
local dialog_message='The Debian GUI installer will now be launched.
Proceed with the configuration as usual, then, at the partitioning stage:
- check `Manual partitioning` -> `Next`
- click on `'"${v_temp_volume_device}"'` in the filesystems panel -> `Edit`
- click on `Format`
- set `Mount Point` to `/` -> `OK`
- `Next`
- follow through the installation (ignore the EFI partition warning)
- at the end, uncheck `Restart now`, and click `Done`
'
if [[ ${ZFS_NO_INFO_MESSAGES:-} == "" ]]; then
whiptail --msgbox "$dialog_message" 30 100
fi
# See install_operating_system().
#
sudo -u "$SUDO_USER" env DISPLAY=:0 xhost +
DISPLAY=:0 calamares
mkdir -p "$c_installed_os_data_mount_dir"
# Note how in Debian, for reasons currenly unclear, the mount fails if the partition is passed;
# it requires the device to be passed.
#
mount "${v_temp_volume_device}" "$c_installed_os_data_mount_dir"
# We don't use chroot()_execute here, as it works on $c_zfs_mount_dir (which is synced on a
# later stage).
#
set +x
chroot "$c_installed_os_data_mount_dir" bash -c "echo root:$(printf "%q" "$v_root_password") | chpasswd"
set -x
# The installer doesn't set the network interfaces, so, for convenience, we do it.
#
for interface in $(ip addr show | perl -lne '/^\d+: (?!lo:)(\w+)/ && print $1' ); do
cat > "$c_installed_os_data_mount_dir/etc/network/interfaces.d/$interface" <<CONF
auto $interface
iface $interface inet dhcp
CONF
done
}
function install_operating_system_UbuntuServer {
print_step_info_header
# O/S Installation
#
# Subiquity is designed to prevent the user from opening a terminal, which is (to say the least)
# incongruent with the audience.
local dialog_message='You'\''ll now need to run the Ubuntu Server installer (Subiquity).
Switch back to the original terminal (Ctrl+Alt+F1), then proceed with the configuration as usual.
When the update option is presented, choose to update Subiquity to the latest version.
At the partitioning stage:
- select `Custom storage layout` -> `Done`
- select `'"$v_temp_volume_device"'` -> `Edit`
- set `Format:` to `ext4` (mountpoint will be automatically selected)
- click `Save`
- click `Done` -> `Continue` (ignore warning)
- follow through the installation, until the end (after the updates are applied)
- switch back to this terminal (Ctrl+Alt+F2), and continue (tap Enter)
Do not continue in this terminal (tap Enter) now!
You can switch anytime to this terminal, and back, in order to read the instructions.
'
whiptail --msgbox "$dialog_message" 30 100
swapoff -a
# See note in install_operating_system(). It's not clear whether this is required on Ubuntu
# Server, but it's better not to take risks.
#
if ! mountpoint -q "$c_installed_os_data_mount_dir"; then
mount "${v_temp_volume_device}p2" "$c_installed_os_data_mount_dir"
fi
rm -f "$c_installed_os_data_mount_dir"/swap.img
}
function custom_install_operating_system {
print_step_info_header
sudo "$ZFS_OS_INSTALLATION_SCRIPT"
}
function create_pools {
# POOL OPTIONS #######################
local passphrase
local encryption_options=()
local rpool_disks_partitions=()
local bpool_disks_partitions=()
set +x
passphrase=$(cat "$c_passphrase_named_pipe")
if [[ -n $passphrase ]]; then
encryption_options=(-O "encryption=on" -O "keylocation=prompt" -O "keyformat=passphrase")
fi
# Push back for unlogged reuse. Minor inconvenience, but worth :-)
#
echo -n "$passphrase" > "$c_passphrase_named_pipe" &
set -x
for selected_disk in "${v_selected_disks[@]}"; do
rpool_disks_partitions+=("${selected_disk}-part3")
bpool_disks_partitions+=("${selected_disk}-part2")
done
# POOLS CREATION #####################
# See https://github.com/zfsonlinux/zfs/wiki/Ubuntu-18.04-Root-on-ZFS for the details.
#
# `-R` creates an "Alternate Root Point", which is lost on unmount; it's just a convenience for a temporary mountpoint;
# `-f` force overwrite partitions is existing - in some cases, even after wipefs, a filesystem is mistakenly recognized
# `-O` set filesystem properties on a pool (pools and filesystems are distincted entities, however, a pool includes an FS by default).