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

feat: Check the amount of RAM available (#482)

parent 44091ace
Loading
Loading
Loading
Loading
+34 −37
Original line number Diff line number Diff line
@@ -62,18 +62,17 @@ docker run -it --rm --name qemu -e "BOOT=http://example.com/image.iso" -p 8006:8

  Enjoy your brand new machine, and don't forget to star this repo!

* ### How do I increase the amount of CPU or RAM?

  By default, a single CPU core and 1 GB of RAM are allocated to the container.
* ### How do I change the storage location?

  To increase this, add the following environment variables:
  To change the storage location, include the following bind mount in your compose file:

  ```yaml
  environment:
    RAM_SIZE: "4G"
    CPU_CORES: "4"
  volumes:
    - /var/qemu:/storage
  ```

  Replace the example path `/var/qemu` with the desired storage folder.

* ### How do I change the size of the disk?

  To expand the default size of 16 GB, add the `DISK_SIZE` setting to your compose file and set it to your preferred capacity:
@@ -85,20 +84,9 @@ docker run -it --rm --name qemu -e "BOOT=http://example.com/image.iso" -p 8006:8
  
  This can also be used to resize the existing disk to a larger capacity without any data loss.

* ### How do I change the storage location?

  To change the storage location, include the following bind mount in your compose file:

  ```yaml
  volumes:
    - /var/qemu:/storage
  ```

  Replace the example path `/var/qemu` with the desired storage folder.

* ### How do I boot a local image?

  You can use a local file directly, and skip the download, by binding it in your compose file in this way:
  You can use a local file directly, and skip the download altogether, by binding it in your compose file in this way:
  
  ```yaml
  volumes:
@@ -131,6 +119,29 @@ docker run -it --rm --name qemu -e "BOOT=http://example.com/image.iso" -p 8006:8

  You can use [qemu-arm](https://github.com/qemus/qemu-arm/) to run ARM64 images.

* ### How do I verify if my system supports KVM?

  To verify if your system supports KVM, run the following commands:

  ```bash
  sudo apt install cpu-checker
  sudo kvm-ok
  ```

  If you receive an error from `kvm-ok` indicating that KVM acceleration can't be used, check the virtualization settings in the BIOS.

* ### How do I increase the amount of CPU or RAM?

  By default, a single CPU core and 1 GB of RAM are allocated to the container.

  If there arises a need to increase this, add the following environment variables:

  ```yaml
  environment:
    RAM_SIZE: "4G"
    CPU_CORES: "4"
  ```

* ### How do I assign an individual IP address to the container?

  By default, the container uses bridge networking, which shares the IP address with the host. 
@@ -189,15 +200,12 @@ docker run -it --rm --name qemu -e "BOOT=http://example.com/image.iso" -p 8006:8
  It is possible to pass-through disk devices directly by adding them to your compose file in this way:

  ```yaml
  environment:
    DEVICE: "/dev/sda"
    DEVICE2: "/dev/sdb"
  devices:
    - /dev/sda
    - /dev/sdb
    - /dev/sdb:/dev/disk1
    - /dev/sdc:/dev/disk2
  ```

  Use `DEVICE` if you want it to become your main drive, and use `DEVICE2` and higher to add them as secondary drives.
  Use `/dev/disk1` if you want it to become your main drive, and use `/dev/disk2` and higher to add them as secondary drives.

* ### How do I pass-through a USB device?

@@ -210,18 +218,7 @@ docker run -it --rm --name qemu -e "BOOT=http://example.com/image.iso" -p 8006:8
    - /dev/bus/usb
  ```

* ### How do I verify if my system supports KVM?

  To verify if your system supports KVM, run the following commands:

  ```bash
  sudo apt install cpu-checker
  sudo kvm-ok
  ```

  If you receive an error from `kvm-ok` indicating that KVM acceleration can't be used, check the virtualization settings in the BIOS.

* ### How do I provide custom arguments to QEMU?
* ### How can I provide custom arguments to QEMU?

  You can create the `ARGUMENTS` environment variable to provide additional arguments to QEMU at runtime:

+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ set -Eeuo pipefail
DEF_OPTS="-nodefaults"
SERIAL_OPTS="-serial $SERIAL"
USB_OPTS="-device $USB -device usb-tablet"
RAM_OPTS=$(echo "-m $RAM_SIZE" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
RAM_OPTS=$(echo "-m ${RAM_SIZE^^}" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
CPU_OPTS="-cpu $CPU_FLAGS -smp $CPU_CORES,sockets=1,dies=1,cores=$CPU_CORES,threads=1"
MON_OPTS="-monitor $MONITOR -name $PROCESS,process=$PROCESS,debug-threads=on"
MAC_OPTS="-machine type=${MACHINE}${SECURE},graphics=off,vmport=off,dump-guest-core=off,hpet=off${KVM_OPTS}"
+5 −0
Original line number Diff line number Diff line
@@ -512,6 +512,11 @@ fi
: "${DEVICE3:=""}"
: "${DEVICE4:=""}"

[ -z "$DEVICE" ] && [ -b "/dev/disk1" ] && DEVICE="/dev/disk1"
[ -z "$DEVICE2" ] && [ -b "/dev/disk2" ] && DEVICE2="/dev/disk2"
[ -z "$DEVICE3" ] && [ -b "/dev/disk3" ] && DEVICE3="/dev/disk3"
[ -z "$DEVICE4" ] && [ -b "/dev/disk4" ] && DEVICE4="/dev/disk4"

if [ -n "$DEVICE" ]; then
  addDevice "$DEVICE" "device" "3" "0xa" || exit $?
else
+2 −1
Original line number Diff line number Diff line
@@ -9,7 +9,8 @@ set -Eeuo pipefail
: "${CPU_MODEL:=""}"
: "${DEF_MODEL:="qemu64"}"

[ "$ARCH" != "amd64" ] && KVM="N"
[[ "${ARCH,,}" != "amd64" ]] && KVM="N"
[[ "${MACHINE,,}" == "pc-q35-2"* ]] && HV="N"

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

+24 −5
Original line number Diff line number Diff line
#!/usr/bin/env bash
set -Eeuo pipefail

info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n"; }
error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; }
warn () { printf "%b%s%b" "\E[1;31m❯ " "Warning: $1" "\E[0m\n" >&2; }
info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "${1:-}" "\E[0m\n"; }
error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: ${1:-}" "\E[0m\n" >&2; }
warn () { printf "%b%s%b" "\E[1;31m❯ " "Warning: ${1:-}" "\E[0m\n" >&2; }

trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR

@@ -43,7 +43,6 @@ HOST=$(hostname -s)
KERNEL=$(echo "$SYS" | cut -b 1)
MINOR=$(echo "$SYS" | cut -d '.' -f2)
ARCH=$(dpkg --print-architecture)
RAM="$(free -g | grep Mem: | awk '{print $7}')/$(free -g | grep Mem: | awk '{print $2}') GB"
CPU=$(lscpu | grep -m 1 'Model name' | cut -f 2 -d ":" | awk '{$1=$1}1' | sed 's# @.*##g' | sed s/"(R)"//g | sed 's/[^[:alnum:] ]\+/ /g' | sed 's/  */ /g')

# Check system
@@ -60,6 +59,15 @@ if [ ! -d "$STORAGE" ]; then
  error "Storage folder ($STORAGE) not found!" && exit 13
fi

# Read memory
RAM_AVAIL=$(free -b | grep -m 1 Mem: | awk '{print $7}')
RAM_TOTAL=$(free -b | grep -m 1 Mem: | awk '{print $2}')
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")
AVAIL_GB=$(( (RAM_AVAIL + 1073741823)/1073741824 ))
TOTAL_GB=$(( (RAM_TOTAL + 1073741823)/1073741824 ))
WANTED_GB=$(( (RAM_WANTED + 1073741823)/1073741824 ))

# Print system info
SYS="${SYS/-generic/}"
FS=$(stat -f -c %T "$STORAGE")
@@ -67,9 +75,20 @@ FS="${FS/ext2\/ext3/ext4}"
SPACE=$(df --output=avail -B 1 "$STORAGE" | tail -n 1)
SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))

echo "❯ CPU: ${CPU} | RAM: ${RAM} | DISK: $SPACE_GB GB (${FS}) | HOST: ${SYS}..."
echo "❯ CPU: ${CPU} | RAM: $AVAIL_GB/$TOTAL_GB GB | DISK: $SPACE_GB GB (${FS}) | HOST: ${SYS}..."
echo

# Check memory

if (( RAM_WANTED > RAM_AVAIL )); then
  error "Your configured RAM_SIZE of $WANTED_GB GB is higher than the $AVAIL_GB GB of memory available."
  exit 15
fi

if (( (RAM_WANTED + 1950000000) > RAM_AVAIL )); then
  warn "your configured RAM_SIZE of $WANTED_GB GB is much too close to the $AVAIL_GB GB of memory available."
fi

# Helper functions

isAlive() {