Unverified Commit a22ebd20 authored by Kroese's avatar Kroese Committed by GitHub
Browse files

feat: Improve memory allocation logic (#1026)

parent 2234fb31
Loading
Loading
Loading
Loading
+29 −39
Original line number Diff line number Diff line
@@ -2,17 +2,19 @@
set -Eeuo pipefail

msg="Checking memory..."

html "$msg"
[[ "$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
@@ -20,8 +22,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
@@ -34,57 +36,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_WANTED" < 1 )); then
  if (( RAM_AVAIL < (RAM_SPARE * 2) )); then

    RAM_WANTED=$(( RAM_AVAIL - (RAM_SPARE * 2) ))
    RAM_WANTED=$(( RAM_WANTED / 1073741825 ))
    wanted=$(( RAM_AVAIL / 2 ))

    if (( "$RAM_WANTED" < 1 )); then
  else

      RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE ))
      RAM_WANTED=$(( RAM_WANTED / 1073741825 ))
    wanted=$(( RAM_AVAIL - (RAM_SPARE * 3) ))

      if (( "$RAM_WANTED" < 1 )); then
    if (( wanted < (RAM_SPARE * 6) )); then
      wanted=$(( RAM_AVAIL - RAM_SPARE ))
    fi

        RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE ))
        RAM_WANTED=$(( RAM_WANTED / 1048577 ))
  fi

        if (( "$RAM_WANTED" < 1 )); then
  wanted=$(( wanted / 1048577 ))
  RAM_SIZE="${wanted}M"

          RAM_WANTED=$(( RAM_AVAIL ))
          RAM_WANTED=$(( RAM_WANTED / 1048577 ))
  info "Allocated $wanted MB of RAM for the virtual machine."

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"
  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
+5 −4
Original line number Diff line number Diff line
@@ -120,12 +120,13 @@ if [ ! -w "$STORAGE" ]; 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

@@ -135,8 +136,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