+
+ # fai would do this:
+ #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
+
+ # note: swaplist seems to do nothing.
+ cat >/tmp/fai/disk_var.sh <<EOF
+BOOT_DEVICE="${short_devs[@]}"
+BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
+# ROOT_PARTITIONS is added by me, used in arch setup.
+ROOT_PARTITIONS="${root_devs[@]}"
+ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_dev}
+SWAPLIST=\${SWAPLIST:-"${swap_devs[@]}"}
+EOF
+
+ if [[ $HOSTNAME == frodo ]]; then
+ big_disks=(
+ ata-Hitachi_HDS722020ALA330_JK1121YAG7SXWS-part1
+ ata-Hitachi_HDS722020ALA330_JK1121YAG7SY4S-part1
+ ata-Hitachi_HDS723030ALA640_MK0311YHG2WUSA-part1
+ ata-ST4000DM000-1F2168_Z300AZ6K-part1
+ ata-ST6000DM001-1XY17Z_Z4D2WMZK-part1
+ ata-TOSHIBA_MD04ACA500_8539K4TQFS9A-part1
+ ata-TOSHIBA_MD04ACA500_85NAK4T2FS9A-part1
+ ata-TOSHIBA_MD04ACA500_9551K615FS9A-part1
+ ata-TOSHIBA_MD04ACA500_Y5IFK6IJFS9A-part1
+ )
+ for d in ${big_disks[@]}; do
+ cat >>/tmp/fai/crypttab <<EOF
+crypt_dev_$d /dev/disk/by-id/$d /mnt/root/q/root/luks/iank discard,luks
+EOF
+ done
+ cat >> /tmp/fai/fstab <<EOF
+/dev/mapper/crypt_dev_${big_disks[0]} /mnt/i btrfs nofail,$fstabstd,noatime,subvolid=0 0 0
+EOF
+ fi
+ if [[ $HOSTNAME == kd ]]; then
+ # note, having these with keyscript and initramfs causes a luks error in fai.log,
+ # but it is safely ignorable and gets us the ability to just type our password
+ # in once at boot. A downside is that they are probably needed to be plugged in to boot.
+ cat >>/tmp/fai/crypttab <<EOF
+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
+crypt_dev_ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1 /dev/disk/by-id/ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1 /mnt/root/q/root/luks/iank discard,luks
+crypt_dev_ata-ST6000DM001-1XY17Z_Z4D29EBL-part1 /dev/disk/by-id/ata-ST6000DM001-1XY17Z_Z4D29EBL-part1 /mnt/root/q/root/luks/iank discard,luks
+EOF
+ cat >> /tmp/fai/fstab <<EOF
+# r7 = root partition7. it isnt actually #7 anymore, not a great name, but whatever
+/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
+/dev/mapper/crypt_dev_ata-TOSHIBA_MD04ACA500_84R2K773FS9A-part1 /mnt/rust1 btrfs nofail,$fstabstd,noatime,compress=zstd,subvolid=0 0 0
+/dev/mapper/crypt_dev_ata-ST6000DM001-1XY17Z_Z4D29EBL-part1 /mnt/rust2 btrfs nofail,$fstabstd,noatime,compress=zstd,subvolid=0 0 0
+EOF
+ fi
+ fi
+}
+
+
+
+#### root2 non-fai run
+
+# todo: update for lvm
+doroot2() {
+
+
+ # We write to these files instead of just /etc/fstab, /etc/crypttab,
+ # because these are filesystems created after our current root, and so
+ # this allows us to update other root filesystems too.
+ rm -f /mnt/root/root2-{fs,crypt}tab
+ if $partition; then
+ echo $0: error: found partition=true but have mkroot2 arg
+ exit 1
+ fi
+
+
+ root2_devs=()
+ for vg in ${vgs[@]}; do
+
+
+ root2_devs+=(/dev/mapper/crypt-$vg-root2)
+ if $mkroot2; then
+ lvcreate -y -L $root2_part_mib $vg -n root2
+ lvcreate -y -L $boot2_part_mib $vg -n boot2
+ luks-setup /dev/$vg/root2 crypt-$vg-root2
+ fi
+ cat >>/mnt/root/root2-crypttab <<EOF
+crypt-$vg-root2 /dev/$vg/root2 $luks_file discard,luks,initramfs
+EOF
+ done
+ if $mkroot2; then
+ bpart ${root2_devs[@]}
+ bpart ${boot2_devs[@]}
+ fi
+ mkdir -p /mnt/root2 /mnt/boot2
+ cat >>/mnt/root/root2-fstab <<EOF
+${root2_devs[0]} /mnt/root2 btrfs nofail,x-systemd.device-timeout=30s,x-systemd.mount-timeout=30s,noatime,subvolid=0$mopts 0 0
+${boot2_devs[0]} /mnt/boot2 btrfs nofail,x-systemd.device-timeout=30s,x-systemd.mount-timeout=30s,noatime,subvolid=0 0 0
+EOF
+ exit 0
+}
+
+
+##### end function defs
+
+
+##### begin variable setup
+
+
+partition=false
+if ifclass REPARTITION; then
+ partition=true # force a full wipe
+fi
+wipe=true
+if ifclass NOWIPE; then
+ wipe=false
+fi
+
+rerootfs=false
+if ifclass REROOTFS; then
+ rerootfs=true
+fi
+
+if (($(nproc) > 2)); then
+ mopts=,compress=zstd
+fi
+
+declare -A disk_excludes
+if ! $mkroot2 && ! $mkroot2tab && ! $mktab ! ifclass USE_MOUNTED; then
+ ## ignore disks that are mounted, eg when running from fai-cd
+ while read -r l; do
+ eval "$l"
+ if [[ ! $PKNAME ]]; then
+ # shellcheck disable=SC2153 # not a misspelling
+ PKNAME="$KNAME"
+ fi
+ if [[ $MOUNTPOINT ]]; then
+ disk_excludes[$PKNAME]=true
+ fi
+ done < <(lsblk -nP -o KNAME,MOUNTPOINT,PKNAME)
+fi
+
+hdds=()
+ssds=()
+# this excludes "usb". vda disk has empty tran (transport). This may need adjustment
+# for some new type we come across. cdrom has type "rom"
+for disk in $(lsblk -ndo name,type,tran | awk '$3 ~ "^(sata|nvme|)$" && $2 == "disk" { print $1 }'); do
+ if [[ ${disk_excludes[$disk]} ]]; then
+ continue
+ fi
+ case $disk in
+ # cdrom
+ sr*) continue ;;
+ esac
+ case $(cat /sys/block/$disk/queue/rotational) in
+ 0)
+ ssds+=("/dev/$disk")
+ echo $0: found ssd /dev/$disk
+ ;;
+ 1)
+ hdds+=("/dev/$disk")
+ echo $0: found hdd /dev/$disk
+ ;;
+ *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
+$(cat $disk/queue/rotational)"; exit 1 ;;
+ esac
+done
+
+# install all ssds, or if there are none, all hdds.
+# Note, usb flash disks are seen as rotational, which is
+# very odd, but convenient for ignoring them here.
+if ! ifclass ROTATIONAL && (( ${#ssds[@]} > 0 )); then
+ read -ra short_devs<<<"${ssds[@]}"
+else
+ read -ra short_devs<<<"${hdds[@]}"
+fi
+
+pvn=1
+
+bootn=2
+
+# rootn=1
+# root2n=2
+# swapn=3
+# bootn=4
+# boot2n=5
+
+efin=3
+# ext partition so grub can write persistent variables,
+# so it can do a one time boot. grub can't write to
+# btrfs or any cow fs because it's more
+# more complicated to do and they don't want to.
+grub_extn=4
+# bios boot partition,
+# https://wiki.archlinux.org/index.php/GRUB
+bios_grubn=5
+# for an even raid (raid 1), when one disk is bigger, this partition goes on the big disk
+even_bign=6
+# even_bign only exists in some cases
+lastn=$bios_grubn
+# check if the partitions exist have the right filesystems
+#blkid="$(blkid -s TYPE)"
+for dev in ${short_devs[@]}; do
+ if $partition; then break; fi
+ y=$(readlink -f $dev)
+ # shellcheck disable=SC2206 # globbing is intended
+ arr=($y?*)
+ if (( ${#arr[@]} < lastn )); then
+ partition=true
+ fi
+ # On one system, blkid is missing some partitions.
+ # maybe we need a flag, like FUZZY_BLKID or something, so we
+ # can check that at least some exist.
+ # for x in "`rootdev`: TYPE=\"crypto_LUKS\"" "`bootdev`: TYPE=\"btrfs\""; do
+ # echo "$blkid" | grep -Fx "$x" &>/dev/null || partition=true
+ # done
+done
+
+if $partition && ifclass PARTITION_PROMPT; then
+ echo "Press any key except ctrl-c to continue and partition these drives:"
+ if [[ $SPECIAL_DISK ]]; then
+ echo $SPECIAL_DISK
+ else
+ echo " ${short_devs[*]}"
+ fi
+ read -r
+fi
+
+devs=()
+vgs=()
+root_devs=()
+o_devs=()
+swap_devs=()
+shopt -s extglob
+partsuffix=-part
+for short_dev in ${short_devs[@]}; do
+ dev="$(devbyid $short_dev)"
+ if [[ $dev != */by-id/* ]]; then
+ # no by-id link, assume we are in a vm and this is true for all devs.
+ partsuffix=
+ fi
+ # for vms, cant name a vg the same as the short device name, they
+ # conflict: /dev/$vg is already taken
+
+ dname=${dev##*/}
+ vg=vg$dname
+ vg=${vg//:/}
+ vgs+=("$vg")
+ devs+=("$dev")
+ if fsf; then
+ root_devs+=(/dev/$vg/root)
+ swap_devs+=(/dev/$vg/swap)
+ else
+ o_devs+=(/dev/mapper/crypt-$vg-o)
+ root_devs+=(/dev/mapper/crypt-$vg-root)
+ swap_devs+=(/dev/mapper/crypt-$vg-swap)
+ fi