f3df8652b9532ab4f38f97ed70ad30ecd6c01e0d
[automated-distro-installer] / fai / config / hooks / partition.DEFAULT
1 #!/bin/bash -x
2 # Copyright (C) 2016 Ian Kelling
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 PS4='+ $LINENO '
19 set -eE -o pipefail
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
21
22 if [[ $EUID != 0 ]]; then
23 echo "$0: error: need to run as root" >&2
24 exit 1
25 fi
26
27 # for calling outside of FAI:
28 # fai-redep
29 #
30 # source /b/fai/fai-wrapper
31 # - set any appropriate classes with: fai-setclass OPT1... which sets CLASS_OPT1=true...
32 # or run eval-fai-classfile FILE.
33 # - Set a VOL_DISTROVER (if not doing mkroot2) eg:
34 # fai-setclass VOL_NABIA
35 #
36 # OPTIONS:
37 #
38 # mkroot2: for running outside of fai and setting up the root2/boot2 luks and btrfs and tab files
39 #
40 # mkroot2tab: for running outside of fai and setting up the root2/boot2 tab files, in case luks and btrfs
41 # happen to already be setup.
42 #
43 #
44 # environment variables:
45 #
46 # HOSTNAME: if frodo, we exclude 2 devices from the /boot array, which
47 # the bios does not see. if demohost, we set the luks password to just
48 # 'x'.
49 #
50 # SPECIAL_DISK: For use outside of fai. A base disk name like
51 # /dev/sdk. If set, we just cryptsetup and partition this one disk then
52 # exit. This is useful for partitioning a disk in preparation to replace
53 # a failed or failing disk from a raid10 array.
54 #
55 # classes:
56 #
57 # REPARTITION: forces repartitioning even if we detect the proper amount
58 # of partitions already exist.
59 #
60 # NOWIPE: use existing subvolumes if they exist
61 #
62 # ROTATIONAL: forces to install onto hdds instead of sdds. normally sdds
63 # are chosen if they exist.
64 #
65 # PARTITION_PROMPT: command line prompt before partitioning
66 #
67 # RAID0: forces raid0 filesystem. Normally with 4+ devices, we use
68 # raid10.
69 # RAID1: forces raid1 filesystem.
70
71 mkroot2tab=false
72 mkroot2=false
73 if [[ $1 ]]; then
74 case $1 in
75 mkroot2)
76 mkroot2=true
77 ;;
78 mkroot2tab)
79 mkroot2tab=true
80 ;;
81 *)
82 echo "$0: error: unsupported arg: $1" >&2
83 exit 1
84 ;;
85 esac
86 fi
87
88
89 if [[ $SPECIAL_DISK ]]; then
90 export CLASS_REPARTITION=true
91 fi
92
93 # # fai's setup-storage won't do btrfs on luks,
94 # # so we do it ourself :)
95 # inspiration taken from files in fai-setup-storage package
96
97 # if we are not running in fai, skiptask won't be defined, so carry on.
98 skiptask partition || ! type skiptask
99
100 if ! type -p devbyid; then
101 for d in $FAI/distro-install-common \
102 /a/bin/fai/fai/config/distro-install-common $FAI $PWD; do
103 [[ -d $d ]] || continue
104 if [[ -e $d/devbyid ]]; then
105 devbyid=$d/devbyid
106 devbyid() { $devbyid "$@"; }
107 break
108 fi
109 done
110 if [[ ! $devbyid ]]; then
111 echo "$0: error: failed to find devbyid script" >&2
112 exit 1
113 fi
114 fi
115
116
117
118 #### begin configuration
119
120 # this is the ordering of the /dev/sdaX, but
121 # the ordering of the partition layout goes like this:
122 # bios_grub
123 # grub_ext
124 # efi
125 # root
126 # swap
127 # boot
128
129 rootn=1
130 root2n=2
131 swapn=3
132 bootn=4
133 boot2n=5
134 efin=6
135 # ext partition so grub can write persistent variables,
136 # so it can do a one time boot. grub can't write to
137 # btrfs or any cow fs because it's more
138 # more complicated to do and they don't want to.
139 grub_extn=7
140 # bios boot partition,
141 # https://wiki.archlinux.org/index.php/GRUB
142 bios_grubn=8
143 even_bign=9
144 lastn=$bios_grubn
145
146
147
148 ##### end configuration
149
150
151 add-part() { # add partition suffix to $dev
152 local d part
153 if [[ $# == 1 ]]; then
154 d=$dev
155 part=$1
156 else
157 d=$1
158 part=$2
159 fi
160 echo $d-part$part
161 }
162
163 rootdev() { add-part $@ $rootn; }
164 root2dev() { add-part $@ $root2n; }
165 swapdev() { add-part $@ $swapn; }
166 bootdev() { add-part $@ $bootn; }
167 boot2dev() { add-part $@ $boot2n; }
168 efidev() { add-part $@ $efin; }
169 grub_extdev() { add-part $@ $grub_extn; }
170 bios_grubdev() { add-part $@ $bios_grubn; }
171 even_bigdev() { add-part $@ $even_bign; }
172
173 crypt-dev() { echo /dev/mapper/crypt_dev_${1##*/}; }
174 crypt-name() { echo crypt_dev_${1##*/}; }
175 root-cryptdev() { crypt-dev $(rootdev $@); }
176 root2-cryptdev() { crypt-dev $(root2dev $@); }
177 swap-cryptdev() { crypt-dev $(swapdev $@); }
178 root-cryptname() { crypt-name $(rootdev $@); }
179 root2-cryptname() { crypt-name $(root2dev $@); }
180 swap-cryptname() { crypt-name $(swapdev $@); }
181
182 dev-mib() {
183 local d=${1:-$dev}
184 echo $(( $(parted -m $d unit MiB print | \
185 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
186 }
187
188 luks-setup() {
189 local luksdev="$1"
190 # when we move to newer than trisquel 9, we can remove
191 # --type luks1. We can also check on cryptsetup --help | less /compil
192 # to see about the other settings. Default in debian 9 is luks2.
193 # You can convert from luks2 to luks 1 by adding a temporary key:
194 # cryptsetup luksAddKey --pbkdf pbkdf2
195 # then remove the new format keys with cryptsetup luksRemoveKey
196 # then cryptsetup convert DEV --type luks1, then readd old keys and remove temp.
197 yes YES | cryptsetup luksFormat $luksdev $luks_file || [[ $? == 141 ]]
198 yes "$lukspw" | \
199 cryptsetup luksAddKey --key-file $luks_file \
200 $luksdev || [[ $? == 141 ]]
201 # background: Keyfile and password are treated just
202 # like 2 ways to input a passphrase, so we don't actually need to have
203 # different contents of keyfile and passphrase, but it makes some
204 # security sense to a really big randomly generated passphrase
205 # as much as possible, so we have both.
206 #
207 # This would remove the keyfile.
208 # yes 'test' | cryptsetup luksRemoveKey /dev/... \
209 # /key/file || [[ $? == 141 ]]
210 cryptsetup luksOpen $luksdev $(crypt-name $luksdev) --key-file $luks_file
211 }
212
213 ##### end function defs
214
215 if ifclass REPARTITION; then
216 partition=true # force a full wipe
217 else
218 partition=false # change to true to force a full wipe
219 fi
220 if ifclass NOWIPE; then
221 wipe=false
222 else
223 wipe=true
224 fi
225
226 if (($(nproc) > 2)); then
227 mopts=,compress=zstd
228 fi
229
230 declare -A disk_excludes
231 if ! $mkroot2 && ! $mkroot2tab ! ifclass USE_MOUNTED; then
232 ## ignore disks that are mounted, eg when running from fai-cd
233 while read -r l; do
234 eval "$l"
235 if [[ ! $PKNAME ]]; then
236 PKNAME="$KNAME"
237 fi
238 if [[ $MOUNTPOINT ]]; then
239 disk_excludes[$PKNAME]=true
240 fi
241 done < <(lsblk -nP -o KNAME,MOUNTPOINT,PKNAME)
242 fi
243
244 hdds=()
245 ssds=()
246 # this excludes usb. note: i may encounter some other type in the future.
247 for disk in $(lsblk -do name,tran -n | awk '$2 ~ "^(sata|nvme)$" { print $1 }'); do
248 if [[ ${disk_excludes[$disk]} ]]; then
249 continue
250 fi
251 case $(cat /sys/block/$disk/queue/rotational) in
252 0) ssds+=(/dev/$disk) ;;
253 1) hdds+=(/dev/$disk) ;;
254 *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
255 $(cat $disk/queue/rotational)"; exit 1 ;;
256 esac
257 done
258
259 # install all ssds, or if there are none, all hdds.
260 # Note, usb flash disks are seen as rotational, which is
261 # very odd, but convenient for ignoring them here.
262 # TODO: find a reliable way to ignore them.
263 if ! ifclass ROTATIONAL && (( ${#ssds[@]} > 0 )); then
264 short_devs=( ${ssds[@]} )
265 else
266 short_devs=( ${hdds[@]} )
267 fi
268
269 # check if the partitions exist have the right filesystems
270 #blkid="$(blkid -s TYPE)"
271 for dev in ${short_devs[@]}; do
272 if $partition; then break; fi
273 y=$(readlink -f $dev)
274 arr=($y?*)
275 if (( ${#arr[@]} < lastn )); then
276 partition=true
277 fi
278 # On one system, blkid is missing some partitions.
279 # maybe we need a flag, like FUZZY_BLKID or something, so we
280 # can check that at least some exist.
281 # for x in "`rootdev`: TYPE=\"crypto_LUKS\"" "`bootdev`: TYPE=\"btrfs\""; do
282 # echo "$blkid" | grep -Fx "$x" &>/dev/null || partition=true
283 # done
284 done
285
286 if $partition && ifclass PARTITION_PROMPT; then
287 echo "Press any key except ctrl-c to continue and partition these drives:"
288 echo " ${short_devs[*]}"
289 read -r
290 fi
291
292 devs=()
293 shopt -s extglob
294 for short_dev in ${short_devs[@]}; do
295 devs+=($(devbyid $short_dev))
296 done
297 if [[ ! ${devs[0]} ]]; then
298 echo "$0: error: failed to detect devs" >&2
299 exit 1
300 fi
301
302 boot_space=0
303 first=true
304 boot_devs=()
305 boot2_devs=()
306 for dev in ${devs[@]}; do
307 if ifclass frodo; then
308 # I ran into a machine where the bios doesn't know about some disks,
309 # so 1st stage of grub also doesn't know about them.
310 # Also, grub does not support mounting degraded btrfs as far as
311 # I can tell with some googling.
312 # From within an arch install env, I could detect them by noting
313 # their partitions were mixed with the next disk in /dev/disk/by-path,
314 # and I have mixed model disks, and I could see the 8 models which showed
315 # up in the bios, and thus see which 2 models were missing.
316 # hdparm -I /dev/sdh will give model info in linux.
317 # However, in fai on jessie, /dev/disk/by-path dir doesn't exist,
318 # and I don't see another way, so I'm hardcoding them.
319 # We still put grub on them and partition them the same, for uniformity
320 # and in case they get moved to a system that can recognize them,
321 # we just exclude them from the boot filesystem.
322 cd /dev/disk/by-id/
323 bad_disk=false
324 for id in ata-TOSHIBA_MD04ACA500_8539K4TQFS9A \
325 ata-TOSHIBA_MD04ACA500_Y5IFK6IJFS9A; do
326 if [[ $(readlink -f $id) == "$(readlink -f $dev)" ]]; then
327 bad_disk=true
328 break
329 fi
330 done
331 if ! $bad_disk; then
332 boot_devs+=($(bootdev))
333 boot2_devs+=($(boot2dev))
334 fi
335 else
336 boot_space=$(( boot_space + $(parted -m $dev unit MiB print | \
337 sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
338 boot_devs+=($(bootdev))
339 boot2_devs+=($(boot2dev))
340 fi
341 if $first && [[ $boot_devs ]]; then
342 first_efi=$(efidev)
343 first_grub_extdev=$(grub_extdev)
344 first=false
345 fi
346 done
347 first_boot_dev=${boot_devs[0]}
348
349 even_raid=false
350 if ifclass RAID0 || (( ${#boot_devs[@]} == 1 )); then
351 raid_level=0
352 elif ifclass RAID1 || (( ${#boot_devs[@]} <= 3 )); then
353 if (( ${#boot_devs[@]} == 2 )); then
354 even_raid=true
355 fi
356 raid_level=1
357 else
358 raid_level=10
359 fi
360
361 ### Begin calculate boot partition space
362 # due to raid duplication
363 case $raid_level in
364 1*) boot_space=$(( boot_space / 2 )) ;;
365 esac
366 if (( boot_space > 60000 )); then
367 # this is larger than needed for several /boot subvols,
368 # becuase I keep a minimal debian install on it for
369 # recovery needs and for doing pxe-kexec.
370 boot_mib=10000
371 root2_mib=1000000
372 boot2_mib=5000
373 elif (( boot_space > 30000 )); then
374 boot_mib=$(( 5000 + (boot_space - 30000) / 2 ))
375 root2_mib=100
376 boot2_mib=100
377 else
378 # Small vms don't have room for /boot recovery. With 3 kernels
379 # installed, i'm using 132M on t8, so this seems like plenty of
380 # room. note: rhel 8 recomments 1g for /boot. u20.04 with 3 kernels =
381 # 308 mb, so things have grown significantly
382 boot_mib=1000
383 root2_mib=100
384 boot2_mib=100
385 fi
386 case $raid_level in
387 1*)
388 boot_mib=$(( boot_mib * 2 ))
389 boot2_mib=$(( boot2_mib * 2 ))
390 root2_mib=$(( root2_mib * 2 ))
391 ;;
392 esac
393 ### end calculate boot partition space
394
395
396
397 if [[ ! $DISTRO ]]; then
398 if ifclass VOL_BULLSEYE_BOOTSTRAP; then
399 DISTRO=debianbullseye_bootstrap
400 elif ifclass VOL_STRETCH; then
401 DISTRO=debianstretch
402 elif ifclass VOL_BUSTER; then
403 DISTRO=debianbuster
404 elif ifclass VOL_BULLSEYE; then
405 DISTRO=debianbullseye
406 elif ifclass VOL_BOOKWORM; then
407 DISTRO=debianbookworm
408 elif ifclass VOL_TESTING; then
409 DISTRO=debiantesting
410 elif ifclass VOL_XENIAL; then
411 DISTRO=ubuntuxenial
412 elif ifclass VOL_BIONIC; then
413 DISTRO=ubuntubionic
414 elif ifclass VOL_FOCAL; then
415 DISTRO=ubuntufocal
416 elif ifclass VOL_FLIDAS; then
417 DISTRO=trisquelflidas
418 elif ifclass VOL_ETIONA; then
419 DISTRO=trisqueletiona
420 elif ifclass VOL_NABIA; then
421 DISTRO=trisquelnabia
422 elif $mkroot2 || $mkroot2tab; then
423 :
424 else
425 echo "PARTITIONER ERROR: no distro class/var set" >&2
426 exit 1
427 fi
428 fi
429
430
431 bpart() { # btrfs a partition
432 case $raid_level in
433 0) mkfs.btrfs -f $@ ;;
434 1) mkfs.btrfs -f -m raid1 -d raid1 $@ ;;
435 10) mkfs.btrfs -f -m raid10 -d raid10 $@ ;;
436 esac
437 }
438
439
440 if [[ ! $luks_dir ]]; then
441 # see README for docs about how to create these
442 luks_dir=$FAI/distro-install-common/luks
443 if [[ ! -d $luks_dir ]]; then
444 luks_dir=/q/root/luks
445 fi
446 if [[ ! -d $luks_dir ]]; then
447 echo "$0: error: no luks_dir found" >&2
448 exit 1
449 fi
450 fi
451
452 luks_file=$luks_dir/host-$HOSTNAME
453 if [[ ! -e $luks_file ]]; then
454 hostkeys=($luks_dir/host-*)
455 # if there is only one key, we might be deploying somewhere
456 # where dhcp doesnt give us a proper hostname, so use that.
457 if [[ ${#hostkeys[@]} == 1 && -e ${hostkeys[0]} ]]; then
458 luks_file=${hostkeys[0]}
459 else
460 echo "$0: error: no key for hostname at $luks_file" >&2
461 exit 1
462 fi
463 fi
464
465 # # note, corresponding changes in /b/ds/keyscript-{on,off}
466 if ifclass demohost; then
467 lukspw=x
468 elif [[ -e $luks_dir/$HOSTNAME ]]; then
469 lukspw=$(cat $luks_dir/$HOSTNAME)
470 else
471 lukspw=$(cat $luks_dir/iank)
472 fi
473
474
475 first_root_crypt=$(root-cryptdev ${devs[0]})
476
477 # 1.5 x based on https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Installation_Guide/sect-disk-partitioning-setup-x86.html#sect-custom-partitioning-x86
478 swap_mib=$(( $(grep ^MemTotal: /proc/meminfo | \
479 awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
480
481 mkdir -p /tmp/fai
482 root_devs=()
483 for dev in ${devs[@]}; do
484 root_devs+=($(rootdev))
485 done
486 shopt -s nullglob
487
488 # We write to these files instead of just /etc/fstab, /etc/crypttab,
489 # because these are filesystems created after our current root, and so
490 # this allows us to update other root filesystems too.
491 rm -f /mnt/root/root2-{fs,crypt}tab
492 if $mkroot2 || $mkroot2tab; then
493 if $partition; then
494 echo $0: error: found partition=true but have mkroot2 arg
495 exit 1
496 fi
497 for dev in ${devs[@]}; do
498 if $mkroot2; then
499 luks_file=$luks_dir/host-amy
500 lukspw=$(cat $luks_dir/amy)
501 luks-setup $(root2dev)
502 fi
503 cat >>/mnt/root/root2-crypttab <<EOF
504 $(root2-cryptname) $(root2dev) $luks_file discard,luks,initramfs
505 EOF
506 done
507 if $mkroot2; then
508 bpart $(for dev in ${devs[@]}; do root2-cryptdev; done)
509 bpart ${boot2_devs[@]}
510 fi
511 mkdir -p /mnt/root2 /mnt/boot2
512 cat >>/mnt/root/root2-fstab <<EOF
513 $(root2-cryptdev ${devs[0]}) /mnt/root2 btrfs nofail,x-systemd.device-timeout=30s,x-systemd.mount-timeout=30s,noatime,subvolid=0$mopts 0 0
514 ${boot2_devs[0]} /mnt/boot2 btrfs nofail,x-systemd.device-timeout=30s,x-systemd.mount-timeout=30s,noatime,subvolid=0 0 0
515 EOF
516 exit 0
517 fi
518
519
520 if $partition; then
521 ### begin wipefs
522 if [[ ! $SPECIAL_DISK ]]; then
523 for dev in ${devs[@]}; do
524 # if we repartition to the same as an old partition,
525 # we don't want any old fses hanging around.
526 for (( i=1; i <= lastn; i++ )); do
527 x=$(add-part $i)
528 [[ -e $x ]] || continue
529 count_down=10
530 # wipefs has failed, manual run works, google suggests timing issue
531 while ! wipefs -a $x; do
532 sleep 2
533 count_down=$((count_down - 1))
534 (( count_down > 0 )) || exit 1
535 done
536 done
537 done
538 fi
539 ### end wipefs
540
541
542 # When we have 2 disks of at least 100g difference in size,
543 # make an extra partition on the end of the bigger one.
544 even_big_part=false
545 even_diff_min=100000
546 if $even_raid; then
547 smalli=0
548 bigi=1
549 if (( $(dev-mib ${devs[0]}) >= $(dev-mib ${devs[1]}) )); then
550 smalli=1
551 bigi=0
552 fi
553 disk_mib=$(dev-mib ${devs[smalli]})
554 even_big_dev=${devs[bigi]}
555 even_big_mib=$(dev-mib $even_big_dev)
556 if (( even_big_mib - disk_mib > even_diff_min )); then
557 even_big_part=true
558 fi
559 fi
560
561 for dev in ${devs[@]}; do
562 if [[ $SPECIAL_DISK ]]; then
563 dev=$(devbyid $SPECIAL_DISK)
564 fi
565
566 # parted will round up the disk size. Do -1 so we can have
567 # fully 1MiB unit partitions for easy resizing of the last partition.
568 # Otherwise we would pass in -0 for the end argument for the last partition.
569 #
570 # Note: parted print error output is expected. example:
571 # Error: /dev/vda: unrecognised disk label
572 if ! $even_raid; then
573 disk_mib=$(dev-mib)
574 fi
575
576 boot_part_mib=$(( boot_mib / ${#boot_devs[@]} ))
577 boot2_part_mib=$(( boot2_mib / ${#boot_devs[@]} ))
578 root2_part_mib=$(( root2_mib / ${#root_devs[@]} ))
579 root_end=$(( disk_mib - root2_part_mib - swap_mib - boot_part_mib - boot2_part_mib ))
580 root2_end=$(( root_end + root2_part_mib ))
581 swap_end=$(( root2_end + swap_mib ))
582 boot_end=$(( swap_end + boot_part_mib ))
583
584 parted -s $dev mklabel gpt
585 # MiB because parted complains about alignment otherwise.
586 pcmd="parted -a optimal -s -- $dev"
587 # root partition, the main big one
588 $pcmd mkpart primary ext3 524MiB ${root_end}MiB
589 # without naming, systemd gives us misc errors like:
590 # dev-disk-by\x2dpartlabel-primary.device: Dev dev-disk-by\x2dpartlabel-primary.device appeared twice
591 $pcmd name $rootn root
592 # root2 partition
593 $pcmd mkpart primary ext3 ${root_end}MiB ${root2_end}MiB
594 $pcmd name $root2n root2
595 # normally a swap is type "linux-swap", but this is encrypted swap. using that
596 # label will confuse systemd.
597 # swap partition
598 $pcmd mkpart primary "" ${root2_end}MiB ${swap_end}MiB
599 $pcmd name $swapn swap
600 # boot partition
601 $pcmd mkpart primary "" ${swap_end}MiB ${boot_end}MiB
602 $pcmd name $bootn boot
603 # boot2 partition
604 $pcmd mkpart primary "" ${boot_end}MiB ${disk_mib}MiB
605 $pcmd name $boot2n boot2
606 # uefi partition. efi sucks, half a gig, rediculous.
607 $pcmd mkpart primary "fat32" 12MiB 524MiB
608 $pcmd name $efin efi
609 $pcmd set $efin esp on
610 # note, this is shown here: https://support.system76.com/articles/bootloader/
611 # but not mentioned https://wiki.archlinux.org/index.php/EFI_system_partition
612 # probably not needed
613 $pcmd set $bootn boot on
614 $pcmd set $boot2n boot on
615 # i only need a few k, but googling min size,
616 # I found someone saying that gparted required
617 # required at least 8 because of their hard drive cylinder size.
618 # And 8 is still very tiny.
619 # grub_ext partition
620 $pcmd mkpart primary "ext2" 4MiB 12MiB
621 $pcmd name $grub_extn grubext
622 # gpt ubuntu cloud image uses ~4 mb for this partition. fai uses 1 MiB.
623 # so, I use 3, whatever.
624 # note: parted manual saying cheap flash media
625 # should to start at 4.
626 # biols grub partition
627 $pcmd mkpart primary "" 1MiB 4MiB
628 $pcmd name $bios_grubn biosgrub
629 $pcmd set $bios_grubn bios_grub on
630 if $even_big_part && [[ $dev == "$even_big_dev" ]]; then
631 $pcmd mkpart primary ext3 ${disk_mib}MiB ${even_big_mib}MiB
632 $pcmd name $even_bign even_big
633 fi
634
635 # the mkfs failed before on a vm, which prompted me to add
636 # sleep .1
637 # then it failed again on a physical machine
638 # with:
639 # Device /dev/disk/by-id/foo doesn't exist or access denied,
640 # so I added a wait until it existed.
641 # Then I added the mkfs.ext2, which claimed to succeed,
642 # but then couldn't be found upon reboot. In that case we didn't
643 # wait at all. So I've added a 3 second minimum wait.
644 secs=0
645 while [[ ! -e $(bios_grubdev) ]] && (( secs < 10 )); do
646 sleep 1
647 secs=$((secs +1))
648 done
649 sleep 3
650
651 mkfs.fat -F32 $(efidev)
652
653 if $even_big_part && [[ $dev == "$even_big_dev" ]]; then
654 luks-setup $(even_bigdev)
655 mkfs.btrfs -f $(crypt-dev $(even_bigdev))
656 fi
657
658 # Holds just a single file, rarely written, so
659 # use ext2, like was often used for the /boot partition.
660 # This exists because grub can only persist data to a non-cow fs.
661 # And we use persisting a var in grub to do a one time boot.
662 # We could pass the data on the kernel command line and persist it
663 # to grubenv after booting, but that relies on the boot always succeeding.
664 # This is just a bit more robust, and it could work for booting
665 # into ipxe which can't persist data, if we ever got that working.
666 mkfs.ext2 $(grub_extdev)
667 luks-setup $(rootdev)
668
669 if [[ $SPECIAL_DISK ]]; then
670 exit 0
671 fi
672 done
673 ls -la /dev/btrfs-control # this was probably for debugging...
674 sleep 1
675 bpart $(for dev in ${devs[@]}; do root-cryptdev; done)
676 bpart ${boot_devs[@]}
677 else
678 for dev in ${devs[@]}; do
679 if [[ -e /dev/mapper/$(root-cryptname) ]]; then
680 continue
681 fi
682 cryptsetup luksOpen $(rootdev) $(root-cryptname) \
683 --key-file $luks_file
684 done
685 sleep 1
686 fi
687
688
689 if $wipe && [[ $DISTRO != debianbullseye_bootstrap ]]; then
690 # bootstrap distro doesn't use separate encrypted root.
691 mount -o subvolid=0 $first_root_crypt /mnt
692 # systemd creates subvolumes we want to delete.
693 s=($(btrfs subvolume list --sort=-path /mnt |
694 sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
695 for subvol in ${s[@]}; do btrfs subvolume delete /mnt/$subvol; done
696 btrfs subvolume set-default 0 /mnt
697 [[ ! -e /mnt/root_$DISTRO ]] || btrfs subvolume delete /mnt/root_$DISTRO
698
699 ## create subvols ##
700 cd /mnt
701
702 btrfs subvolume create root_$DISTRO
703
704 # could set default subvol like this, but no reason to.
705 # btrfs subvolume set-default \
706 # $(btrfs subvolume list . | grep "root_$DISTRO$" | awk '{print $2}') .
707
708 # For raid systems, cow allows for error correction, for non-raid systems,
709 # protects root fs from having the plug pulled. Reprovisioning a root
710 # subvol is not my favorite thing to do.
711 # # no cow on the root filesystem. it's setup is fully scripted,
712 # # if it's messed up, we will just recreated it,
713 # # and we can get better perf with this.
714 # # I can't remember exactly why, but this is preferable to mounting with
715 # # -o nodatacow, I think because subvolumes inherit that.
716 # chattr -Rf +C root_$DISTRO
717 cd /
718 umount /mnt
719 fi
720
721 mount -o subvolid=0 $first_boot_dev /mnt
722 cd /mnt
723 btrfs subvolume set-default 0 /mnt # already default, just ensuring it.
724
725 # for libreboot systems. grub2 only reads from subvolid=0
726 mkdir -p /mnt/grub2
727 cp $FAI/distro-install-common/libreboot_grub.cfg /mnt/grub2
728
729 if [[ $DISTRO == debianbullseye_bootstrap ]]; then
730 # this is just convenience for the libreboot_grub config
731 # so we can glob the other ones easier.
732 boot_vol=$DISTRO
733 else
734 boot_vol=boot_$DISTRO
735 fi
736 if $wipe && [[ -e /mnt/$boot_vol ]]; then
737 btrfs subvolume delete /mnt/$boot_vol
738 fi
739 if [[ ! -e /mnt/$boot_vol ]]; then
740 btrfs subvolume create $boot_vol
741 fi
742 cd /
743 umount /mnt
744 ## end create subvols ##
745
746 dev=${boot_devs[0]}
747 mount $first_grub_extdev /mnt
748 grub-editenv /mnt/grubenv set did_fai_check=true
749 grub-editenv /mnt/grubenv set last_boot=/$boot_vol
750 umount /mnt
751
752 fstabstd=x-systemd.device-timeout=30s,x-systemd.mount-timeout=30s
753 if [[ $DISTRO == debianbullseye_bootstrap ]]; then
754 cat > /tmp/fai/fstab <<EOF
755 $first_boot_dev / btrfs noatime,subvol=$boot_vol 0 0
756 $first_efi /boot/efi vfat nofail,$fstabstd 0 0
757 EOF
758 cat >/tmp/fai/disk_var.sh <<EOF
759 BOOT_DEVICE="${short_devs[@]}"
760 ROOT_PARTITION=$first_boot_dev
761 EOF
762 else
763 # note, fai creates the mountpoints listed here
764 cat > /tmp/fai/fstab <<EOF
765 $first_root_crypt / btrfs $fstabstdopts,noatime,subvol=root_$DISTRO$mopts 0 0
766 $first_root_crypt /mnt/root btrfs nofail,$fstabstd,noatime,subvolid=0$mopts 0 0
767 $first_boot_dev /boot btrfs nofail,$fstabstd,noatime,subvol=$boot_vol 0 0
768 $first_efi /boot/efi vfat nofail,$fstabstd 0 0
769 $first_boot_dev /mnt/boot btrfs nofail,$fstabstd,noatime,subvolid=0 0 0
770 EOF
771 swaps=()
772 rm -f /tmp/fai/crypttab
773 for dev in ${devs[@]}; do
774 swaps+=($(swap-cryptname))
775 cat >>/tmp/fai/crypttab <<EOF
776 $(root-cryptname) $(rootdev) none keyscript=/root/keyscript,discard,luks,initramfs
777 $(swap-cryptname) $(swapdev) /dev/urandom swap,cipher=aes-xts-plain64,size=256,hash=ripemd160
778 EOF
779 cat >> /tmp/fai/fstab <<EOF
780 $(swap-cryptdev) none swap nofail,$fstabstd,sw 0 0
781 EOF
782 done
783
784 # fai would do this:
785 #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
786
787 # note: swaplist seems to do nothing.
788 cat >/tmp/fai/disk_var.sh <<EOF
789 BOOT_DEVICE="${short_devs[@]}"
790 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
791 # ROOT_PARTITIONS is added by me, used in arch setup.
792 ROOT_PARTITIONS="${root_devs[@]}"
793 ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_crypt}
794 SWAPLIST=\${SWAPLIST:-"${swaps[@]}"}
795 EOF
796
797 if [[ $HOSTNAME == kd ]]; then
798 # note, having these with keyscript and initramfs causes a luks error in fai.log,
799 # but it is safely ignorable and gets us the ability to just type our password
800 # in once at boot. A downside is that they are probably needed to be plugged in to boot.
801 cat >>/tmp/fai/crypttab <<EOF
802 crypt_dev_ata-Samsung_SSD_870_QVO_8TB_S5VUNG0N900656V-part${even_bign} /dev/disk/by-id/ata-Samsung_SSD_870_QVO_8TB_S5VUNG0N900656V-part7 none keyscript=decrypt_keyctl,discard,luks,initramfs
803 crypt_dev_ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1 /dev/disk/by-id/ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1 none keyscript=decrypt_keyctl,discard,luks,initramfs
804 crypt_dev_ata-ST6000DM001-1XY17Z_Z4D29EBL-part1 /dev/disk/by-id/ata-ST6000DM001-1XY17Z_Z4D29EBL-part1 none keyscript=decrypt_keyctl,discard,luks,initramfs
805 EOF
806 cat >> /tmp/fai/fstab <<EOF
807 # r7 = root partition7. it isnt actually #7 anymore, not a great name, but whatever
808 /dev/mapper/crypt_dev_ata-Samsung_SSD_870_QVO_8TB_S5VUNG0N900656V-part${even_bign} /mnt/r7 btrfs nofail,$fstabstd,noatime,compress=zstd,subvolid=0 0 0
809 /dev/mapper/crypt_dev_ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1 /mnt/rust1 btrfs nofail,$fstabstd,noatime,compress=zstd,subvolid=0 0 0
810 /dev/mapper/crypt_dev_ata-ST6000DM001-1XY17Z_Z4D29EBL-part1 /mnt/rust2 btrfs nofail,$fstabstd,noatime,compress=zstd,subvolid=0 0 0
811 EOF
812 fi
813
814 fi
815
816 # initial setup of extra data fs, mounted,
817 # btrfs subvol create nocow
818 # chattr +C nocow
819 # chown iank.iank nocow
820