fix bugs in resize script, support multi disk
[automated-distro-installer] / fai / config / hooks / partition.DEFAULT
index 5d4684c6bf79af7c3bbe6a00dc196885b7505380..adb7b629e6b8bc243a323106b6c25247c35e9258 100755 (executable)
@@ -10,11 +10,11 @@ skiptask partition ||: # for running out of fai
 
 #### begin configuration
 
-bootn=1
+bootn=3
+rootn=1
 swapn=2
-rootn=3
 bios_grubn=4
-boot_end=804
+boot_mib=750
 lastn=$bios_grubn
 
 if ifclass VM; then
@@ -48,7 +48,7 @@ devs=(${letters[@]/#//dev/${d}})
 crypt_devs=(${letters[@]/#//dev/mapper/crypt_dev_${d}})
 first_boot_dev=${devs[0]}$bootn
 
-partition=true
+partition=true # hardcoded for now
 
 # somewhat crude detection of whether to partition
 for dev in ${devs[@]}; do
@@ -80,14 +80,17 @@ fi
 
 crypt=/dev/mapper/crypt_dev_${d##/dev/}a$rootn
 
-
-if ifclass frodo; then
-    # next upgrade is prolly 16 gigs of memory, across ~8 devices
-    swap_end=$((3500 + boot_end))
-else
-    # 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_end=$(( $(grep ^MemTotal: /proc/meminfo| awk '{print $2}') * 3/(${#devs[@]} * 2 ) / 1000 + boot_end ))
-fi
+bios_grub_end=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 ))
+# parted will round up the disk size. Do -1 so we can have
+# fully 1MiB unit partitions for easy resizing of the last partition.
+# Otherwise we would pass in -0 for the end argument for the last partition.
+disk_mib=$(( $(parted -m ${devs[0]} unit MiB print | \
+                  sed -nr "s#^${devs[0]}:([0-9]+).*#\1#p") - 1))
+root_end=$(( disk_mib - swap_mib - boot_mib ))
+swap_end=$(( root_end + swap_mib))
 
 mkdir -p /tmp/fai
 shopt -s nullglob
@@ -97,19 +100,20 @@ if $partition; then
     done
     for dev in ${devs[@]}; do
         parted -s $dev mklabel gpt
-        # gpt ubuntu cloud image uses ~4. fai uses 1 MiB. ehh, i'll do 4.
-        # also, using MB instead of MiB causes complains about alignment.
+        # gpt ubuntu cloud image uses ~4. fai uses 1 MiB.
+        # I read something in the parted manual saying cheap flash media
+        # likes to start at 4.
+        # MiB because parted complains about alignment otherwise.
         pcmd="parted -a optimal -s -- $dev"
-        $pcmd mkpart primary "ext3" 4MB ${boot_end}MiB
-        $pcmd mkpart primary "linux-swap" ${boot_end}MiB ${swap_end}MiB
-        $pcmd mkpart primary "" ${swap_end}MiB -0
+        $pcmd mkpart primary "ext3" 4MiB ${root_end}MiB
+        $pcmd mkpart primary "linux-swap" ${root_end}MiB ${swap_end}MiB
+        $pcmd mkpart primary "" ${swap_end}MiB ${disk_mib}MiB
         $pcmd mkpart primary "" 1MiB 4MiB
         $pcmd set $bios_grubn bios_grub on
         $pcmd set $bootn boot on # generally not needed on modern systems
         # the mkfs failed randomly on a vm, so I threw a sleep in here.
         sleep .1
 
-
         luks_dev=$dev$rootn
         yes YES | cryptsetup luksFormat $luks_dev $luks_dir/host-$HOSTNAME \
                              -c aes-cbc-essiv:sha256 -s 256 || [[ $? == 141 ]]