From 63de3e9917c9cb3601727b0f50885cd31df20be0 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 9 Aug 2022 15:38:42 +0200 Subject: [slx-dmsetup] Add linear swap space if slx.swap is in command line .... and if we don't seem to have an existing swap partition --- modules.d/slx-dmsetup/scripts/dmsetup-slx-device | 52 ++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/modules.d/slx-dmsetup/scripts/dmsetup-slx-device b/modules.d/slx-dmsetup/scripts/dmsetup-slx-device index 4b087130..2dcd7eb3 100755 --- a/modules.d/slx-dmsetup/scripts/dmsetup-slx-device +++ b/modules.d/slx-dmsetup/scripts/dmsetup-slx-device @@ -61,7 +61,7 @@ handle_unit() { declare -ag linear snapshot thin_snapshot thin_volume parse_config() { [ -z "$1" ] && return 1 - while IFS= read -r line; do + while IFS= read -r line || [ -n "$line" ]; do [ -z "$line" ] && continue read -r type name range crypt ignore <<< "$line" type=${type//-/_} # to use the type as variable in eval @@ -428,6 +428,7 @@ find_ntfs_partitions() { return fi local part sum + ntfs_extra_space_sz=0 for part in /dev/disk/by-partuuid/*; do # Only count ranges >= 256MB, sum will be in number of 512b blocks sum="$( ntfsfree --block-size 512 --min-size "$(( 256 * 1024 * 1024 ))" "$part" \ @@ -435,14 +436,22 @@ find_ntfs_partitions() { # Only consider volume if sum of these ranges > 1GB (this is BLOCKS, not bytes) (( "$sum" > 2 * 1024 * 1024 )) || continue echo "$sum $part" # only thing in loop going to stdout + (( ntfs_extra_space_sz += sum )) done | sort -nr > "$ntfs_list" } +ntfs_extra_space_sz=0 ### ## MAIN ### . /etc/openslx + +. slx-tools +# "Preload" functions by executing them NOT in a subshell +dev_find_partitions &> /dev/null +dev_swap_version &> /dev/null + # This is the main variable driving this script declare -g writable_device= if [ -z "$SLX_WRITABLE_DEVICE_IDENTIFIER" ]; then @@ -455,7 +464,7 @@ fi if [ -n "$SLX_WRITABLE_DEVICE_IDENTIFIER" ]; then # only first one for now TODO create linear devices of all ID44s declare -a writable_devices - writable_devices=( $( slx-tools dev_find_partitions "${SLX_WRITABLE_DEVICE_IDENTIFIER[@]}" ) ) + writable_devices=( $( dev_find_partitions "${SLX_WRITABLE_DEVICE_IDENTIFIER[@]}" ) ) if [[ "${#writable_devices[@]}" -eq 0 && "$SLX_NTFSFREE" != "never" ]] || [ "$SLX_NTFSFREE" = "always" ]; then find_ntfs_partitions fi @@ -526,9 +535,44 @@ declare -rg writable_device_sz="$( blockdev --getsz "$writable_device" )" # If SLX_WRITABLE_DEVICE_PARTITION_TABLE is not set, just do # regular thin-snapshot for the CoW layer, else parse it. -if [ -n "$SLX_WRITABLE_DEVICE_PARTITION_TABLE" ]; then - parse_config "$SLX_WRITABLE_DEVICE_PARTITION_TABLE" +if [ -z "$SLX_WRITABLE_DEVICE_PARTITION_TABLE" ]; then + SLX_WRITABLE_DEVICE_PARTITION_TABLE="thin-snapshot root 100% 0" fi + +# extra swap? +if grep -qFw 'slx.swap' "/proc/cmdline"; then + # Only if our basic writable_device is large enough, or we have ntfs backup + do_swap_sz=0 + if (( writable_device_sz > 80078125 )); then + # more than ~40GB, go ahead + do_swap_sz="$(( ( writable_device_sz - 70312500 ) / 2 ))" + # cap to 6GB + (( do_swap_sz > 11718750 )) && do_swap_sz=11718750 + elif [ "$SLX_NTFSFREE" = "backup" ] \ + && (( ntfs_extra_space_sz > 70312500 )) && (( writable_device_sz > 11718750 )); then + # more than 40GB NTFS backup space, more than 6GB ID44, make 4GB swap + do_swap_sz=7812500 + fi + # Check how many we have and if they're regular, unencrypted ones. + # If it's plenty, don't cut out swap from our backing device + swap_sz=0 + for part in $( dev_find_partitions "82" "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f" ); do + dev_swap_version "$part" &> /dev/null || continue + this_sz="$( blockdev --getsz "$part" )" + (( this_sz > 0 )) && (( swap_sz += this_sz )) + done + echo "Have existing swap of $swap_sz blocks" + # Go ahead with swap? Only if existing swap < 4GB. If so, add line to table. + if (( do_swap_sz > 0 )) && (( swap_sz < 7812500 )); then + echo "Adding $do_swap_sz blocks of additional swap on backing dev" + skb="$(( do_swap_sz / 2 ))" + SLX_WRITABLE_DEVICE_PARTITION_TABLE="$( printf "%s\n%s" "linear slx-swap ${skb}K 0" \ + "$SLX_WRITABLE_DEVICE_PARTITION_TABLE" )" + fi +fi + +parse_config "$SLX_WRITABLE_DEVICE_PARTITION_TABLE" + # Default to thin-snapshot, if none were configured if [ "${#snapshot[@]}" = 0 ] && [ "${#thin_snapshot[@]}" = 0 ]; then parse_config "thin-snapshot root 100% 0" -- cgit v1.2.3-55-g7522