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