small bug fixes and improvements
[automated-distro-installer] / fai / config / hooks / partition.DEFAULT
index b0d1c9cb2ade103c480056f20aca35527798d7d5..aa7440968b8eaca3cde4c43672a69ad5825441c0 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash -x
 
 set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
 # # fai's setup-storage won't do btrfs on luks,
 # # so we do it ourself :)
@@ -10,9 +10,9 @@ skiptask partition || ! type skiptask # for running not in fai
 
 #### begin configuration
 
-bootn=3
 rootn=1
 swapn=2
+bootn=3
 bios_grubn=4
 boot_mib=1500
 
@@ -61,13 +61,13 @@ fi
 lastn=$bios_grubn
 
 
-hds=()
+hdds=()
 ssds=()
 cd /sys/block
 for disk in [sv]d[a-z]; do
     case $(cat $disk/queue/rotational) in
         0) ssds+=(/dev/$disk) ;;
-        1) hds+=(/dev/$disk) ;;
+        1) hdds+=(/dev/$disk) ;;
         *) echo "$0: error: unknown /sys/block/$disk/queue/rotational: \
 $(cat $disk/queue/rotational)"; exit 1 ;;
     esac
@@ -77,7 +77,7 @@ done
 if (( ${#ssds[@]} > 0 )); then
     short_devs=( ${ssds[@]} )
 else
-    short_devs=( ${hds[@]} )
+    short_devs=( ${hdds[@]} )
 fi
 
 # check if the partitions exist have the right filesystems
@@ -168,11 +168,17 @@ bpart() { # btrfs a partition
     esac
 }
 
-first_boot_dev=$(bootdev ${devs[0]})
+first_boot_dev=${boot_devs[0]}
 
 # keyfiles generated like:
 # head -c 2048 /dev/urandom | od | s dd of=/q/root/luks/host-demohost
 luks_dir=${LUKS_DIR:-/var/lib/fai/config/distro-install-common/luks}
+
+if [[ ! -e $luks_dir/host-$HOSTNAME ]]; then
+    echo "$0: error: no key for hostname at $luks_dir/host-$HOSTNAME" >&2
+    exit 1
+fi
+
 if ifclass tp; then
     lukspw=$(cat $luks_dir/traci)
 else
@@ -188,9 +194,13 @@ first_root_crypt=$(root-cryptdev ${devs[0]})
 bios_grubn=4
 # 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
 swap_mib=$(( $(grep ^MemTotal: /proc/meminfo | \
-                      awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
+                   awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1024 ))
 
 mkdir -p /tmp/fai
+root_devs=()
+for dev in ${devs[@]}; do
+    root_devs+=(`rootdev`)
+done
 shopt -s nullglob
 if $partition; then
     for dev in ${devs[@]}; do
@@ -212,7 +222,7 @@ if $partition; then
         # parted print error output is expected. example:
         # Error: /dev/vda: unrecognised disk label
         disk_mib=$(( $(parted -m $dev unit MiB print | \
-                              sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
+                           sed -nr "s#^/dev/[^:]+:([0-9]+).*#\1#p") - 1))
         root_end=$(( disk_mib - swap_mib - boot_mib /  ${#boot_devs[@]} ))
         swap_end=$(( root_end + swap_mib))
 
@@ -272,20 +282,19 @@ fi
 mount -o subvolid=0 $first_root_crypt /mnt
 # systemd creates subvolumes we want to delete.
 s=($(btrfs subvolume list --sort=-path /mnt |
-            sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
+         sed -rn "s#^.*path\s*(root_$DISTRO/\S+)\s*\$#\1#p"))
 for subvol in ${s[@]}; do btrfs subvolume delete /mnt/$subvol; done
 btrfs subvolume set-default 0 /mnt
 [[ ! -e /mnt/root_$DISTRO ]] || btrfs subvolume delete /mnt/root_$DISTRO
 
-
 ## create subvols ##
 cd /mnt
-for x in q home_$DISTRO root_$DISTRO; do
-    btrfs subvolume list . | grep "$x$" >/dev/null || btrfs subvolume create $x
-    chown root:1000 q
-done
+btrfs subvolume create root_$DISTRO
+[[ -e q ]] || btrfs subvolume create q
+chown root:1000 q
+
 mkdir -p /mnt/root_$DISTRO/boot
-for x in root/a q/a; do
+for x in q/a q/i; do
     mkdir -p $x
     chown 1000:1000 $x
     chmod 755 $x
@@ -306,15 +315,27 @@ umount /mnt
 ## end create subvols ##
 
 
-
+# note, the mount point /a seems to get automatically created somewhere
 cat > /tmp/fai/fstab <<EOF
 $first_root_crypt  /  btrfs  noatime,subvol=root_$DISTRO  0 0
 $first_root_crypt  /q  btrfs  noatime,subvol=q  0 0
 /q/a  /a  none  bind  0 0
-$first_root_crypt  /home  btrfs  noatime,subvol=home_$DISTRO  0 0
 $first_boot_dev  /boot  btrfs  noatime,subvol=boot_$DISTRO  0 0
 EOF
 
+
+if ifclass treetowl; then
+    cat >> /tmp/fai/fstab <<'EOF'
+UUID=3f7b31cd-f299-40b4-a86b-7604282e2715 /i btrfs  noatime  0 2
+EOF
+fi
+
+if ifclass frodo; then
+    cat >> /tmp/fai/fstab <<'EOF'
+/q/i  /i  none  bind  0 0
+EOF
+fi
+
 swaps=()
 for dev in ${devs[@]}; do
     swaps+=(`swap-cryptname`)
@@ -331,7 +352,10 @@ done
 #BOOT_DEVICE=\${BOOT_DEVICE:-"${devs[0]}"}
 
 # swaplist seems to do nothing.
+
 cat >/tmp/fai/disk_var.sh <<EOF
+# ROOT_PARTITIONS is added by me, used in arch setup.
+ROOT_PARTITIONS="${root_devs[@]}"
 ROOT_PARTITION=\${ROOT_PARTITION:-$first_root_crypt}
 BOOT_PARTITION=\${BOOT_PARTITION:-$first_boot_dev}
 BOOT_DEVICE="${short_devs[@]}"