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

feat: Move memory check to own module (#1072)

parent dac574d9
Loading
Loading
Loading
Loading
+0 −88
Original line number Diff line number Diff line
@@ -12,92 +12,4 @@ DEV_OPTS+=" -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pcie.0,addr=0x1c"
ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $DISPLAY_OPTS $MON_OPTS $SERIAL_OPTS $NET_OPTS $DISK_OPTS $DEV_OPTS $ARGUMENTS"
ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ')

# Check available memory as the very last step

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,"
    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,"
      if [[ "${FS,,}" == "zfs" ]]; then
        info "$msg but since ZFS is active this will be ignored."
      else
        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

return 0
+2 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ cd /run
. serial.sh     # Initialize serialport
. power.sh      # Configure shutdown
. config.sh     # Configure arguments
. memory.sh     # Check available memory
. finish.sh     # Finish initialization

trap - ERR

src/finish.sh

0 → 100644
+8 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -Eeuo pipefail

if [[ "$DEBUG" == [Yy1]* ]]; then
  printf "QEMU arguments:\n\n%s\n\n" "${ARGS// -/$'\n-'}"
fi

return 0

src/memory.sh

0 → 100644
+86 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -Eeuo pipefail

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,"
    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,"
      if [[ "${FS,,}" == "zfs" ]]; then
        info "$msg but since ZFS is active this will be ignored."
      else
        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

return 0