diff --git a/src/memory.sh b/src/memory.sh index 246405d..04a084f 100644 --- a/src/memory.sh +++ b/src/memory.sh @@ -1,14 +1,18 @@ #!/usr/bin/env bash set -Eeuo pipefail +msg="Checking memory..." +[[ "$DEBUG" == [Yy1]* ]] && echo "$msg" + RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}') +AVAIL_MEM=$(formatBytes "$RAM_AVAIL") if [[ "$RAM_CHECK" != [Nn]* && "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "half" ]]; then - AVAIL_MEM=$(formatBytes "$RAM_AVAIL") + wanted=$(numfmt --from=iec "$RAM_SIZE") - if (( (RAM_WANTED + RAM_SPARE) > RAM_AVAIL )); then - msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of memory available," + if (( (wanted + RAM_SPARE) > RAM_AVAIL )); then + msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of free memory available," if [[ "${FS,,}" == "zfs" ]]; then info "$msg but since ZFS is active this will be ignored." else @@ -16,8 +20,8 @@ if [[ "$RAM_CHECK" != [Nn]* && "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "h warn "$msg it will automatically be adjusted to a lower amount." fi else - if (( (RAM_WANTED + (RAM_SPARE * 3)) > RAM_AVAIL )); then - msg="your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is very close to the $AVAIL_MEM of memory available," + if (( (wanted + (RAM_SPARE * 3)) > RAM_AVAIL )); then + msg="your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is very close to the $AVAIL_MEM of free memory available," if [[ "${FS,,}" == "zfs" ]]; then info "$msg but since ZFS is active this will be ignored." else @@ -30,57 +34,45 @@ fi if [[ "${RAM_SIZE,,}" == "half" ]]; then - RAM_WANTED=$(( RAM_AVAIL / 2 )) - RAM_WANTED=$(( RAM_WANTED / 1073741825 )) - - if (( "$RAM_WANTED" < 1 )); then - RAM_WANTED=$(( RAM_AVAIL / 2 )) - RAM_WANTED=$(( RAM_WANTED / 1048577 )) - RAM_SIZE="${RAM_WANTED}M" + if (( (RAM_AVAIL / 2) > RAM_SPARE )); then + wanted=$(( (RAM_AVAIL / 2) / 1048577 )) + RAM_SIZE="${wanted}M" + info "Allocated $wanted MB of RAM for the virtual machine." else - RAM_SIZE="${RAM_WANTED}G" + RAM_SIZE="max" fi fi if [[ "${RAM_SIZE,,}" == "max" ]]; then - RAM_WANTED=$(( RAM_AVAIL - (RAM_SPARE * 3) )) - RAM_WANTED=$(( RAM_WANTED / 1073741825 )) + if (( RAM_AVAIL < (RAM_SPARE * 2) )); then - if (( "$RAM_WANTED" < 1 )); then + wanted=$(( RAM_AVAIL / 2 )) - RAM_WANTED=$(( RAM_AVAIL - (RAM_SPARE * 2) )) - RAM_WANTED=$(( RAM_WANTED / 1073741825 )) - - if (( "$RAM_WANTED" < 1 )); then - - RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE )) - RAM_WANTED=$(( RAM_WANTED / 1073741825 )) - - if (( "$RAM_WANTED" < 1 )); then - - RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE )) - RAM_WANTED=$(( RAM_WANTED / 1048577 )) - - if (( "$RAM_WANTED" < 1 )); then - - RAM_WANTED=$(( RAM_AVAIL )) - RAM_WANTED=$(( RAM_WANTED / 1048577 )) - - fi - - RAM_SIZE="${RAM_WANTED}M" - else - RAM_SIZE="${RAM_WANTED}G" - fi - else - RAM_SIZE="${RAM_WANTED}G" - fi else - RAM_SIZE="${RAM_WANTED}G" + + wanted=$(( RAM_AVAIL - (RAM_SPARE * 3) )) + + if (( wanted < (RAM_SPARE * 6) )); then + wanted=$(( RAM_AVAIL - RAM_SPARE )) + fi + fi + wanted=$(( wanted / 1048577 )) + RAM_SIZE="${wanted}M" + + info "Allocated $wanted MB of RAM for the virtual machine." + +fi + +wanted=$(numfmt --from=iec "$RAM_SIZE") + +if [ "$wanted" -lt "$RAM_MINIMUM" ]; then + wanted=$(( wanted / 1048577 )) + error "Not enough memory available, there is only $wanted MB left!" + exit 16 fi return 0 diff --git a/src/reset.sh b/src/reset.sh index fbf5cc3..2a81850 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -127,12 +127,13 @@ if [[ "${FS,,}" == "ecryptfs" || "${FS,,}" == "tmpfs" ]]; then fi # Read memory -RAM_SPARE=500000000 RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}') RAM_TOTAL=$(free -b | grep -m 1 Mem: | awk '{print $2}') +RAM_SPARE=500000000 +RAM_MINIMUM=136314880 RAM_SIZE="${RAM_SIZE// /}" -[ -z "$RAM_SIZE" ] && error "RAM_SIZE not specified!" && exit 16 +[ -z "$RAM_SIZE" ] && RAM_SIZE="2G" if [[ "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "half" ]]; then @@ -142,8 +143,8 @@ if [[ "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "half" ]]; then RAM_SIZE=$(echo "${RAM_SIZE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g') ! numfmt --from=iec "$RAM_SIZE" &>/dev/null && error "Invalid RAM_SIZE: $RAM_SIZE" && exit 16 - RAM_WANTED=$(numfmt --from=iec "$RAM_SIZE") - [ "$RAM_WANTED" -lt "136314880 " ] && error "RAM_SIZE is too low: $RAM_SIZE" && exit 16 + wanted=$(numfmt --from=iec "$RAM_SIZE") + [ "$wanted" -lt "$RAM_MINIMUM " ] && error "RAM_SIZE is too low: $RAM_SIZE" && exit 16 fi