From 9dbfe12bc718091565a2c211d0d818f6233c4d9d Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Tue, 19 Nov 2019 17:53:43 +0100 Subject: make code better --- .../slx-dmsetup/scripts/dmsetup-slx-device | 64 +++++++++++++--------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/builder/modules.d/slx-dmsetup/scripts/dmsetup-slx-device b/builder/modules.d/slx-dmsetup/scripts/dmsetup-slx-device index d65c0dbb..71b82b9c 100755 --- a/builder/modules.d/slx-dmsetup/scripts/dmsetup-slx-device +++ b/builder/modules.d/slx-dmsetup/scripts/dmsetup-slx-device @@ -95,7 +95,7 @@ parse_config() { dmsetup_create_noudevsync() { ( set -o errexit - dmsetup create "$1" --noudevsync --table "$2" + printf "%s\n" "$2" | dmsetup create "$1" --noudevsync dmsetup mknodes --noudevsync "$1" ) local ret=$? @@ -129,12 +129,38 @@ create_snapshot() { return 0 } -# Call this to fallback to a RAMdisk stored under /run/openslx -# This will call terminate the whole script by calling finish_setup, if successful +# This function is called if no ID44 partition could be found or anoother kind +# of critical error occurs during the CoW layer setup. It will combine the +# the read-only device with a DM zero device to increase its virtual size +# by half the RAM size. A sparse file of that size will then be created and +# placed on a dedicated tmpfs. ramdisk_fallback() { echo "$0: Falling back to regular dm-snapshot on a RAMdisk." - # first we create a dedicated tmpfs to control the maximum size - local cow_tmpfs="/run/openslx/dnbd-cow" + + # 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 )}' /proc/meminfo)" + + # try to prepare the zero extension device + local extended_device="/dev/mapper/${read_only_device##*/}-extended" + ( + set -x + lsmod | grep -q dm-zero || modprobe dm-zero + dmsetup_create_noudevsync zero "0 $ram_size_in_kb 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" + ) + if [ "$?" -eq 0 ]; then + read_only_device="$extended_device" + read_only_device_size="$(( read_only_device_size + ram_size_in_kb ))" + else + echo "$0: Failed to setup the fake larger '$read_only_device'." + echo "$0: Continuing with its original size." + fi + + # prepare dedicated tmpfs mount point + local cow_tmpfs="/run/openslx/cow" if ! mkdir -p "$cow_tmpfs"; then cow_tmpfs="${cow_tmpfs}.$$" if ! mkdir -p "$cow_tmpfs"; then @@ -142,38 +168,24 @@ ramdisk_fallback() { cow_tmpfs="/run/openslx" fi fi - - local qcow_size_in_kb="$(( read_only_device_size / 2 ))" - local ram_size_in_kb="$(awk '/MemTotal/ {printf("%d\n", $2 )}' /proc/meminfo)" - modprobe dm-zero - # fill the qcow2 device with a zero device to make it larger than it is - dmsetup_create_noudevsync filler "0 $ram_size_in_kb zero" - echo "0 $read_only_device_size linear $read_only_device 0 - $read_only_device_size $ram_size_in_kb linear /dev/mapper/filler 0" | dmsetup create bigger --noudevsync - dmsetup mknodes --noudevsync bigger - - # Use the qcow size + ***************half************* the RAM as tmpfs size. - local cow_size="$(( qcow_size_in_kb + ( ram_size_in_kb / 2 ) ))" - if ! mount -t tmpfs tmpfs -o size="${cow_size}k" "$cow_tmpfs"; then - emergency_shell "Failed to prepare dedicated CoW tmpfs in $cow_tmpfs of size $cow_size." + 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 ))'." fi + # create sparse file there local file="$(mktemp -u -p "$cow_tmpfs" dnbd_cow.XXX)" - if ! dd of="$file" seek="$cow_size" bs=1k count=0 &> /dev/null; then + if ! dd of="$file" seek="$(( read_only_device_size / 2 ))" bs=1k count=0 &> /dev/null; then emergency_shell "Failed to allocate CoW file $file." fi - writable_device="$(losetup --show --find "$file")" - read_only_device=/dev/mapper/bigger - read_only_device_size="$(( read_only_device_size + ram_size_in_kb ))" - cow_device_candidate="root" + declare -rg writable_device="$(losetup --show --find "$file")" + local cow_device_candidate="root" while [ -b "/dev/mapper/$cow_device_candidate" ]; do cow_device_candidate="root.$RANDOM" done if [ -z "$writable_device" ] || ! create_snapshot "$cow_device_candidate N"; then emergency_shell "CRITICAL: failed to setup RAMdisk fallback." - exit 1 fi - finish_setup "$cow_device_candidate" "0" "$size" + finish_setup "$cow_device_candidate" "0" "$read_only_device_size" } # finish_setup [] -- cgit v1.2.3-55-g7522