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

feat: Automatically adjust RAM size when too high (#916)

parent e419610d
Loading
Loading
Loading
Loading
+69 −9
Original line number Diff line number Diff line
@@ -57,28 +57,88 @@ fi

# Check available memory as the very last step

if [[ "$RAM_CHECK" != [Nn]* ]]; then

RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}')
  
if [[ "$RAM_CHECK" != [Nn]* && "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "half" ]]; then

  AVAIL_MEM=$(formatBytes "$RAM_AVAIL")

  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, please set a lower value."
    [[ "${FS,,}" != "zfs" ]] && error "$msg" && exit 17
    info "$msg"
    msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of memory available,"
    if [[ "${FS,,}" == "zfs" ]]; then
      info "$msg but since ZFS is active this will be ignored."
    else
      RAM_SIZE="max"
      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, please consider a lower value."
      if [[ "${FS,,}" != "zfs" ]]; then
        warn "$msg"
      msg="your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is very close to the $AVAIL_MEM of memory available,"
      if [[ "${FS,,}" == "zfs" ]]; then
        info "$msg but since ZFS is active this will be ignored."
      else
        info "$msg"
        warn "$msg please consider a lower amount."
      fi
    fi
  fi

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"
  else
    RAM_SIZE="${RAM_WANTED}G"
  fi

fi

if [[ "${RAM_SIZE,,}" == "max" ]]; then

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

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

    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"
  fi
  
fi

if [[ "$DEBUG" == [Yy1]* ]]; then
  printf "QEMU arguments:\n\n%s\n\n" "${ARGS// -/$'\n-'}"
fi
+10 −21
Original line number Diff line number Diff line
@@ -98,11 +98,7 @@ RAM_TOTAL=$(free -b | grep -m 1 Mem: | awk '{print $2}')
RAM_SIZE="${RAM_SIZE// /}"
[ -z "$RAM_SIZE" ] && error "RAM_SIZE not specified!" && exit 16

if [[ "${RAM_SIZE,,}" == "max" ]]; then
  RAM_WANTED=$(( RAM_AVAIL - RAM_SPARE - RAM_SPARE ))
  RAM_WANTED=$(( RAM_WANTED / 1073741825 ))
  RAM_SIZE="${RAM_WANTED}G"
fi
if [[ "${RAM_SIZE,,}" != "max" && "${RAM_SIZE,,}" != "half" ]]; then

  if [ -z "${RAM_SIZE//[0-9. ]}" ]; then
    [ "${RAM_SIZE%%.*}" -lt "130" ] && RAM_SIZE="${RAM_SIZE}G" || RAM_SIZE="${RAM_SIZE}M"
@@ -113,6 +109,8 @@ RAM_SIZE=$(echo "${RAM_SIZE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
  RAM_WANTED=$(numfmt --from=iec "$RAM_SIZE")
  [ "$RAM_WANTED" -lt "136314880 " ] && error "RAM_SIZE is too low: $RAM_SIZE" && exit 16

fi

# Print system info
SYS="${SYS/-generic/}"
FS=$(stat -f -c %T "$STORAGE")
@@ -140,15 +138,6 @@ if [[ "${BOOT_MODE:-}" == "windows"* ]]; then
  fi
fi

# Check available memory

if [[ "$RAM_CHECK" != [Nn]* ]] && (( (RAM_WANTED + RAM_SPARE) > RAM_AVAIL )); then
  AVAIL_MEM=$(formatBytes "$RAM_AVAIL")
  msg="Your configured RAM_SIZE of ${RAM_SIZE/G/ GB} is too high for the $AVAIL_MEM of memory available, please set a lower value."
  [[ "${FS,,}" != "zfs" ]] && error "$msg" && exit 17
  info "$msg"
fi

# Check KVM support

if [[ "${PLATFORM,,}" == "x64" ]]; then