From 1703d6f8cfae4973ed5d84e60f3151b707303530 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sun, 12 May 2019 12:16:28 -0300 Subject: Calculate partition alignment --- share/hydractl/provision | 59 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/share/hydractl/provision b/share/hydractl/provision index 24fd614..ac78699 100755 --- a/share/hydractl/provision +++ b/share/hydractl/provision @@ -82,6 +82,20 @@ function hydra_provision_create_volume { fi } +# Determine optimal partition start taking into account disk alignment +# This function needs to know where the last partition ends +function partition_sector_start { + local sector_start="$1" + local last_partition_end="$2" + local optimal_sector_size="$3" + + while (( $sector_start <= $last_partition_end + 1)); do + sector_start="$(($sector_start + $optimal_sector_size))" + done + + echo $sector_start +} + # Make sure there is provision config. function hydra_provision_config { local base_arch="`uname -m`" @@ -186,21 +200,54 @@ if [ "$num_devices" != "1" ]; then boot_device="$device" syst_device="$device" else + # Partition alignment + megabyte="$((1024*1024))" + block="`echo $device | sed -e 's|^/dev/||'`" + optimal_size="`cat /sys/block/$block/queue/optimal_io_size`" + alignment_offset="`cat /sys/block/$block/alignment_offset`" + block_size="`cat /sys/block/$block/queue/physical_block_size`" + start="$((($optimal_size + $alignment_offset) / $block_size))" + optimal_sector_size="$(($optimal_size / $block_size))" + + #start="`awk -v x=$(cat /sys/block/$block/queue/optimal_io_size) \ + # -v y=$(cat /sys/block/$block/alignment_offset) \ + # -v z=$(cat /sys/block/$block/queue/physical_block_size) 'BEGIN { print ( x + y ) / z }'`" + + # Sector size for a 1MB partition + bios_grub_size="$(($megabyte/$block_size))" + bios_grub_end="$(($start + $bios_grub_size - 1))" + # Regular disk partitioning. hydra_sudo_run parted -s -- $device mklabel gpt - hydra_sudo_run parted -s -- $device unit MB mkpart non-fs 2 3 + #hydra_sudo_run parted -s -- $device unit MB mkpart non-fs 2 3 + hydra_sudo_run parted -s -- $device mkpart non-fs ${start}s ${bios_grub_end}s hydra_sudo_run parted -s -- $device set 1 bios_grub on if [ "$encrypt" == "y" ]; then - hydra_sudo_run parted -s -- $device unit MB mkpart ext2 3 -1 - hydra_sudo_run parted -s -- $device set 2 lvm on + # Second partition must also be aligned by a multiple of $optimal_sector_size + # So we find the minimum optimal start sector which is after the grub partition + #lvm_start="$(($bios_grub_end + 1))" + lvm_start="`partition_sector_start $start $bios_grub_end $optimal_sector_size`" + + #hydra_sudo_run parted -s -- $device unit MB mkpart ext2 3 -1 + hydra_sudo_run parted -s -- $device mkpart ext2 ${lvm_start}s -1 + hydra_sudo_run parted -s -- $device set 2 lvm on boot_device="$device"2 syst_device="$device"2 else - hydra_sudo_run parted -s -- $device unit MB mkpart ext2 3 200 - hydra_sudo_run parted -s -- $device unit MB mkpart ext2 200 -1 - hydra_sudo_run parted -s -- $device set 3 lvm on + # Make a 200MB boot partition + #boot_start="$(($bios_grub_end + 1))" + boot_start="`partition_sector_start $start $bios_grub_end $optimal_sector_size`" + boot_size="$((200*megabyte/$block_size))" + boot_end="$(($boot_start + $boot_size -1))" + #lvm_start="$($boot_end + 1))" + lvm_start="`partition_sector_start $start $boot_end $optimal_sector_size`" + + #hydra_sudo_run parted -s -- $device unit MB mkpart ext2 3 200 + hydra_sudo_run parted -s -- $device mkpart ext2 ${boot_start}s ${boot_end}s + hydra_sudo_run parted -s -- $device mkpart ext2 ${lvm_start}s -1 + hydra_sudo_run parted -s -- $device set 3 lvm on boot_device="$device"2 syst_device="$device"3 -- cgit v1.2.3