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