From 19f34787e30111533a2f06d35845e9c50c3bc25b Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 1 Oct 2020 12:43:01 +0200 Subject: [slx-dmsetup] Use _sz for sizes expressed in 512b sectors --- modules.d/slx-dmsetup/scripts/dmsetup-slx-device | 120 ++++++++++++----------- 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/modules.d/slx-dmsetup/scripts/dmsetup-slx-device b/modules.d/slx-dmsetup/scripts/dmsetup-slx-device index 9ef4f22d..f890cd44 100755 --- a/modules.d/slx-dmsetup/scripts/dmsetup-slx-device +++ b/modules.d/slx-dmsetup/scripts/dmsetup-slx-device @@ -26,7 +26,9 @@ exec &> /run/openslx/dmsetup.log # read-only device to prepare for CoW [ -z "$1" ] && emergency_shell "Read-only device was not given!" declare -g read_only_device="$1" -declare -g read_only_device_size="$( blockdev --getsz "$1" )" +declare -g read_only_device_sz="$( blockdev --getsz "$1" )" +# Use _sz suffix for sizes expressed in number of 512b sectors, +# _size for random other crap declare -rg ntfs_list="/run/openslx/.thin-ntfs-candidates" @@ -70,8 +72,8 @@ parse_config() { echo "Ignoring invalid percentages: $min/$max" continue fi - min=$(( writable_device_size * min / 100 )) - max=$(( writable_device_size * max / 100 )) + min=$(( writable_device_sz * min / 100 )) + max=$(( writable_device_sz * max / 100 )) ;; [Kk]) potency=1 ;;& [Mm]) potency=2 ;;& @@ -131,7 +133,7 @@ create_snapshot() { modprobe dm-snapshot || echo "$0: dm-snapshot loading failed, maybe builtin?" read -r name persist ignore <<< "$1" if ! dmsetup_create_noudevsync "$name" \ - "0 $read_only_device_size snapshot $read_only_device $writable_device ${persist:-N} 8"; then + "0 $read_only_device_sz snapshot $read_only_device $writable_device ${persist:-N} 8"; then echo "$0: Failed to create snapshot on '$writable_device' for '$read_only_device'." return 1 fi @@ -149,21 +151,21 @@ ramdisk_fallback() { # RAM size in kb, note that this is equal to half # of the entire RAM in 512-bytes sectors. - local ram_size_in_kb="$(awk '/^MemTotal:/ { printf("%d\n", $2 ); exit }' /proc/meminfo)" + local ram_cow_sz="$(awk '/^MemTotal:/ { printf("%d\n", $2 ); exit }' /proc/meminfo)" # try to prepare the zero extension device local extended_device="/dev/mapper/${read_only_device##*/}-extended" ( set -e lsmod | grep -q dm-zero || modprobe dm-zero - dmsetup_create_noudevsync zero "0 $ram_size_in_kb zero" + dmsetup_create_noudevsync zero "0 $ram_cow_sz zero" dmsetup_create_noudevsync "${extended_device##*/}" \ - "0 $read_only_device_size linear $read_only_device 0 - $read_only_device_size $ram_size_in_kb linear /dev/mapper/zero 0" + "0 $read_only_device_sz linear $read_only_device 0 + $read_only_device_sz $ram_cow_sz linear /dev/mapper/zero 0" ) if [ "$?" -eq 0 ]; then read_only_device="$extended_device" - read_only_device_size="$(( read_only_device_size + ram_size_in_kb ))" + read_only_device_sz="$(( read_only_device_sz + ram_cow_sz ))" else echo "$0: Failed to setup the fake larger '$read_only_device'." echo "$0: Continuing with its original size." @@ -178,13 +180,13 @@ ramdisk_fallback() { cow_tmpfs="/run/openslx" fi fi - if ! mount -t tmpfs cow-tmpfs -o size="$(( read_only_device_size / 2 ))k" "$cow_tmpfs"; then - echo "$0: Failed to mount tmpfs in '$cow_tmpfs' of size '$(( read_only_device_size / 2 ))'." + if ! mount -t tmpfs cow-tmpfs -o size="$(( read_only_device_sz / 2 + 100 ))k" "$cow_tmpfs"; then + echo "$0: Failed to mount tmpfs in '$cow_tmpfs' of size '$(( read_only_device_sz / 2 + 100 ))KiB'." fi # create sparse file there local file="$(mktemp -u -p "$cow_tmpfs" dnbd_cow.XXX)" - if ! dd if=/dev/null of="$file" seek="$(( read_only_device_size / 2 ))" bs=1k 2> /dev/null; then + if ! dd if=/dev/null of="$file" seek="$(( read_only_device_sz ))" bs=512 2> /dev/null; then emergency_shell "Failed to allocate CoW file $file." fi declare -rg writable_device="$(losetup --show --find "$file")" @@ -196,7 +198,7 @@ ramdisk_fallback() { emergency_shell "CRITICAL: failed to setup RAMdisk fallback." exit 1 fi - finish_setup "$cow_device_candidate" "0" "$read_only_device_size" + finish_setup "$cow_device_candidate" "0" "$read_only_device_sz" } # finish_setup [] @@ -268,7 +270,7 @@ if [ -z "$writable_device" ]; then fi # NOTE: from here on out, every value related to size is in 512 bytes sectors! -declare -rg writable_device_size="$( blockdev --getsz "$writable_device" )" +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. @@ -300,40 +302,40 @@ fi ### # start allocating spaces to the configured devices -declare -g writable_device_allocated=0 +declare -g writable_device_used_sz=0 # first, reserve the space for the rootfs cow snapshot (of either type)... read -r name crypt min max ignore <<< "${thin_snapshot:-${snapshot}}" declare -g scratch_device="$writable_device" -declare -gi scratch_device_size=0 -if (( min <= writable_device_size )); then - scratch_device_size="$max" - (( scratch_device_size < min )) && scratch_device_size="$min" - (( scratch_device_size > writable_device_size )) && scratch_device_size="$writable_device_size" +declare -gi scratch_device_sz=0 +if (( min <= writable_device_sz )); then + scratch_device_sz="$max" + (( scratch_device_sz < min )) && scratch_device_sz="$min" + (( scratch_device_sz > writable_device_sz )) && scratch_device_sz="$writable_device_sz" else # minimum snapshot size is bigger than physical device size echo "$0: Minimum snapshot size is too big for the scratch partition." echo "$0: You probably need to use a more conservative value." - echo "$0: Using this client maximum scratch space ($writable_device_size sectors)." - scratch_device_size="$writable_device_size" + echo "$0: Using this client maximum scratch space ($writable_device_sz sectors)." + scratch_device_sz="$writable_device_sz" fi # encrypt the scratch device, if configured if [ "$crypt" -ne 0 ] && encrypt_device \ - "$scratch_device" "${scratch_device##*/}-crypt" "$scratch_device_size"; then + "$scratch_device" "${scratch_device##*/}-crypt" "$scratch_device_sz"; then scratch_device="/dev/mapper/${scratch_device##*/}-crypt" else echo "$0: Continuing with unencrypted scratch" fi -writable_device_allocated="$scratch_device_size" +writable_device_used_sz="$scratch_device_sz" # first setup linear slices of the writable device for line in "${linear[@]}"; do [ -z "$line" ] && continue read -r name crypt min max ignore <<< "$line" - free_space="$(( writable_device_size - writable_device_allocated ))" + free_space="$(( writable_device_sz - writable_device_used_sz ))" if [ "$min" -gt "$free_space" ]; then echo "$0: Not enough space left for linear devices: '$line'" break @@ -342,7 +344,7 @@ for line in "${linear[@]}"; do to_allocate="$max" [ "$to_allocate" -gt "$free_space" ] && to_allocate="$free_space" - if ! dmsetup_create_noudevsync "$name" "0 $to_allocate linear $writable_device $writable_device_allocated"; then + if ! dmsetup_create_noudevsync "$name" "0 $to_allocate linear $writable_device $writable_device_used_sz"; then echo "$0: Failed to create linear device: $line" continue fi @@ -352,30 +354,30 @@ for line in "${linear[@]}"; do ! encrypt_device "/dev/mapper/$name" "${name}-crypt" "$to_allocate"; then echo "$0: Failed to encrypt '$name'." fi - writable_device_allocated=$(( to_allocate + writable_device_allocated )) + writable_device_used_sz=$(( to_allocate + writable_device_used_sz )) done # This will create another dm-linear on top of $scratch_device in case its -# size differs from $scratch_device_size. This is useful for setups where you +# size differs from $scratch_device_sz. This is useful for setups where you # cannot explicitly configure how much space to use from the underlying device, # and the partition table says not to use the entire $writable_device for cow require_exact_scratch_size() { - local current_size="$( blockdev --getsz "$scratch_device" )" - (( current_size == scratch_device_size )) && return 0 # Everything fine - if (( current_size < scratch_device_size )); then - echo "$0: WARNING: scratch_device_size is larger than actual device." + local current_sz="$( blockdev --getsz "$scratch_device" )" + (( current_sz == scratch_device_sz )) && return 0 # Everything fine + if (( current_sz < scratch_device_sz )); then + echo "$0: WARNING: scratch_device_sz is larger than actual device." echo "$0: This should never happen." - scratch_device_size="$current_size" + scratch_device_sz="$current_sz" return 0 fi # We could check if $scratch_device already is a dm target, and just adjust its # size, but I think that scenario isn't possible, currently. - if ! dmsetup_create_noudevsync "scratch" "0 $scratch_device_size linear $scratch_device 0"; then + if ! dmsetup_create_noudevsync "scratch" "0 $scratch_device_sz linear $scratch_device 0"; then echo "$0: Failed to create scratch space for the CoW layer." return 1 fi scratch_device="/dev/mapper/scratch" - save_partition_info "scratch" "*" "1" "$scratch_device_size" + save_partition_info "scratch" "*" "1" "$scratch_device_sz" return 0 } @@ -385,7 +387,7 @@ require_exact_scratch_size() { declare -rg pool_dev="/dev/mapper/pool" declare -gi root_ntfs_extra=0 # Extra blocks to provision to root fs for later expansion create_pool() { - declare -r data_block_size=256 # Desired Block size (number of 512byte sectors) + declare -r data_block_sz=256 # Desired Block size (number of 512byte sectors) declare -r wanted_low_mb=100 # Free space below this will trigger a dm event # create external snapshot for read-only device # create remaining thin volumes @@ -393,12 +395,12 @@ create_pool() { # create temporary metadata device # calculate number of sectors needed and check boundaries: # XXX Formula from thin-pool.txt calculates size in *bytes*, we want 512b blocks - metadata_dev_size="$(( 48 * scratch_device_size / data_block_size / 512 ))" + metadata_dev_sz="$(( 48 * scratch_device_sz / data_block_sz / 512 ))" # If we want NTFS as a backup plan to extend the pool, check if the current size # is less than 100GB, and only then consider this feature. # Maybe make that thresold configurable one day, but the the desktop client # use case this is sensible for now. - if [ "$SLX_NTFSFREE" = "backup" ] && (( scratch_device_size < 209715200 )) \ + if [ "$SLX_NTFSFREE" = "backup" ] && (( scratch_device_sz < 209715200 )) \ && [ -z "$metadata_persistent" ]; then find_ntfs_partitions if [ -s "$ntfs_list" ]; then @@ -409,28 +411,28 @@ create_pool() { if (( sum > 0 )); then (( sum > 209715200 )) && sum=209715200 # Max 100GB # Account for this potential growth in the metadata device size for future expansion - metadata_dev_size="$(( metadata_dev_size + 48 * sum / data_block_size / 512 ))" + metadata_dev_sz="$(( metadata_dev_sz + 48 * sum / data_block_sz / 512 ))" echo "$sum" > "/run/openslx/.thin-ntfs-growsize" root_ntfs_extra="$sum" fi fi fi # Min 2MB -> 4096 sectors, max 16GB -> 33554432 sectors - [ "$metadata_dev_size" -lt 4096 ] && metadata_dev_size="4096" + [ "$metadata_dev_sz" -lt 4096 ] && metadata_dev_sz="4096" # TODO handle the exotic case of a too large metadata device to fit within RAM. - [ "$metadata_dev_size" -gt 33554432 ] && metadata_dev_size="33554432" + [ "$metadata_dev_sz" -gt 33554432 ] && metadata_dev_sz="33554432" local scratch_device_offset=0 local metadata_dev= local metadata_persistent= if [ -n "$metadata_persistent" ]; then # create persistent slice of the writable device for the pool metadata if ! dmsetup_create_noudevsync "pool-metadata" \ - "0 $metadata_dev_size linear $scratch_device $scratch_device_offset"; then + "0 $metadata_dev_sz linear $scratch_device $scratch_device_offset"; then echo "$0: Failed to create linear device for pool metadata device." else # Adjust size for pool-data down accordingly - scratch_device_offset="$metadata_dev_size" - scratch_device_size=$(( scratch_device_size - metadata_dev_size )) + scratch_device_offset="$metadata_dev_sz" + scratch_device_sz=$(( scratch_device_sz - metadata_dev_sz )) declare -r metadata_dev="/dev/mapper/pool-metadata" # TODO configurable wipe: dd if=/dev/zero of="$metadata_dev" count=1 bs=4096 # TODO: If we fail later on in this function, we would actually have to destroy @@ -442,7 +444,7 @@ create_pool() { # create RAMdisk in /run for metadata device metadata_dev="$(mktemp -p /run/openslx .pool-metadata.XXX)" # Create sparse file of required size - dd if=/dev/null of="$metadata_dev" bs=512 seek="$metadata_dev_size" 2> /dev/null + dd if=/dev/null of="$metadata_dev" bs=512 seek="$metadata_dev_sz" 2> /dev/null declare -r metadata_dev="$( losetup --show --find "$metadata_dev" )" fi if [ -z "$metadata_dev" ]; then @@ -459,16 +461,16 @@ create_pool() { # Create linear device of the writable device, in case we have an offset from # the on-disk meta data. Also this way we can easily extend it later. if ! dmsetup_create_noudevsync "${pool_data_dev##*/}" \ - "0 $scratch_device_size linear $scratch_device $scratch_device_offset"; then + "0 $scratch_device_sz linear $scratch_device $scratch_device_offset"; then echo "$0: Failed to create pool data device on '$scratch_device'." return 1 fi fi local low_water_mark # Convert MB to blocks - low_water_mark=$(( wanted_low_mb * 2048 / data_block_size )) + low_water_mark=$(( wanted_low_mb * 2048 / data_block_sz )) if ! dmsetup_create_noudevsync "${pool_dev##*/}" \ - "0 $scratch_device_size thin-pool $metadata_dev $pool_data_dev $data_block_size $low_water_mark 1 skip_block_zeroing"; then + "0 $scratch_device_sz thin-pool $metadata_dev $pool_data_dev $data_block_sz $low_water_mark 1 skip_block_zeroing"; then echo "$0: Failed to create thin-pool device (meta: $metadata_dev, data: $pool_data_dev)" return 1 fi @@ -553,7 +555,7 @@ if [ -n "$thin_snapshot" ] || [ -n "$thin_volume" ]; then if ! create_volume "$name" "$(( volume_id++ ))" "$max"; then echo "Failed to create thin volume '$name'." fi - save_partition_info "$name" "*" "1" "${scratch_device_size}-${max}" + save_partition_info "$name" "*" "1" "${scratch_device_sz}-${max}" if [ "$crypt" -ne 0 ] && ! encrypt_device \ "/dev/mapper/$name" "$name-crypt" "$max"; then echo "Failed to encrypt thin volume '$name'." @@ -566,24 +568,24 @@ if [ -n "$thin_snapshot" ] || [ -n "$thin_volume" ]; then # min/max was used for the pool data device, ignore it here! # NOTE: the filesystem will most likely malfunction if the size of the # thin-snapshot is smaller than what it was upon creation. - # As such, the size of the thin-snapshot can only be $scratch_device_size - # if it is larger than $read_only_device_size, otherwise we should only - # use $read_only_device_size. While live-shrinking the filesystem might be + # As such, the size of the thin-snapshot can only be $scratch_device_sz + # if it is larger than $read_only_device_sz, otherwise we should only + # use $read_only_device_sz. While live-shrinking the filesystem might be # an option, it is not supported throughout all fileystems (xfs can't). - if (( scratch_device_size >= read_only_device_size )); then - thin_snapshot_size="$scratch_device_size" + if (( scratch_device_sz >= read_only_device_sz )); then + thin_snapshot_sz="$scratch_device_sz" else - thin_snapshot_size="$read_only_device_size" + thin_snapshot_sz="$read_only_device_sz" fi # For later on-demand growing if (( root_ntfs_extra > 0 )); then - thin_snapshot_size="$(( thin_snapshot_size + root_ntfs_extra ))" + thin_snapshot_sz="$(( thin_snapshot_sz + root_ntfs_extra ))" fi - if ! create_volume "$name" 1 "$thin_snapshot_size" "$read_only_device"; then + if ! create_volume "$name" 1 "$thin_snapshot_sz" "$read_only_device"; then echo "Failed to create external snapshot for '$read_only_device'." ramdisk_fallback fi - finish_setup "$name" "1" "$thin_snapshot_size" + finish_setup "$name" "1" "$thin_snapshot_sz" fi echo "$0: Thin volumes defined, but no snapshot. Using tmpfs." ramdisk_fallback @@ -598,7 +600,7 @@ if [ -n "$snapshot" ] && require_exact_scratch_size; then if ! create_snapshot "$name $persist"; then echo "Failed to create regular snapshot for '$read_only_device' on '$scratch_device'." else - finish_setup "$name" "1" "$scratch_device_size" + finish_setup "$name" "1" "$scratch_device_sz" fi fi -- cgit v1.2.3-55-g7522