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

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

parent 9ef5f5eb
Loading
Loading
Loading
Loading
+0 −101
Original line number Diff line number Diff line
@@ -42,105 +42,4 @@ fi
ARGS="$DEF_OPTS $CPU_OPTS $RAM_OPTS $MAC_OPTS $DISPLAY_OPTS $MON_OPTS $SERIAL_OPTS ${USB_OPTS:-} $NET_OPTS $DISK_OPTS $BOOT_OPTS $DEV_OPTS $ARGUMENTS"
ARGS=$(echo "$ARGS" | sed 's/\t/ /g' | tr -s ' ')

if [[ "${DISPLAY,,}" == "web" ]]; then
  [ ! -f "$INFO" ] && error "File $INFO not found?!"
  rm -f "$INFO"
  [ ! -f "$PAGE" ] && error "File $PAGE not found?!"
  rm -f "$PAGE"
else
  if [[ "${DISPLAY,,}" == "vnc" ]]; then
    html "You can now connect to VNC on port $VNC_PORT." "0"
  else
    html "The virtual machine was booted successfully." "0"
  fi
fi

# 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
. boot.sh       # Configure boot
. proc.sh       # Initialize processor
. config.sh     # Configure arguments
. memory.sh     # Check available memory
. finish.sh     # Finish initialization

trap - ERR

src/finish.sh

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

if [[ "${DISPLAY,,}" == "web" ]]; then
  [ ! -f "$INFO" ] && error "File $INFO not found?!"
  rm -f "$INFO"
  [ ! -f "$PAGE" ] && error "File $PAGE not found?!"
  rm -f "$PAGE"
else
  if [[ "${DISPLAY,,}" == "vnc" ]]; then
    html "You can now connect to VNC on port $VNC_PORT." "0"
  else
    html "The virtual machine was booted successfully." "0"
  fi
fi

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

return 0

src/memory.sh

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

msg="Checking memory..."
html "$msg"
[[ "$DEBUG" == [Yy1]* ]] && echo "$msg"

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