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