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