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