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