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

This commit is contained in:
Kroese 2024-05-02 20:10:38 +02:00 committed by GitHub
parent aad52572c3
commit 18e68a3f21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 73 additions and 52 deletions

View file

@ -1,4 +1,4 @@
<h1 align="center">Qemu ARM<br /> <h1 align="center">QEMU ARM64<br />
<div align="center"> <div align="center">
<a href="https://github.com/qemus/qemu-arm"><img src="https://github.com/qemus/qemu-arm/raw/master/.github/logo.png" title="Logo" style="max-width:100%;" width="128" /></a> <a href="https://github.com/qemus/qemu-arm"><img src="https://github.com/qemus/qemu-arm/raw/master/.github/logo.png" title="Logo" style="max-width:100%;" width="128" /></a>
</div> </div>
@ -65,18 +65,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! Enjoy your brand new machine, and don't forget to star this repo!
* ### How do I increase the amount of CPU or RAM? * ### How do I change the storage location?
By default, a single CPU core and 1 GB of RAM are allocated to the container. To change the storage location, include the following bind mount in your compose file:
To increase this, add the following environment variables:
```yaml ```yaml
environment: volumes:
RAM_SIZE: "4G" - /var/qemu:/storage
CPU_CORES: "4"
``` ```
Replace the example path `/var/qemu` with the desired storage folder.
* ### How do I change the size of the disk? * ### 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: To expand the default size of 16 GB, add the `DISK_SIZE` setting to your compose file and set it to your preferred capacity:
@ -88,24 +87,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. 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 x86 image?
You can use [qemu-docker](https://github.com/qemus/qemu-docker/) to run x86 and x64 images on ARM.
* ### How do I boot a local image? * ### 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 ```yaml
volumes: volumes:
@ -114,6 +98,33 @@ docker run -it --rm --name qemu -e "BOOT=http://example.com/image.iso" -p 8006:8
Replace the example path `/home/user/example.iso` with the filename of the desired ISO file. Replace the example path `/home/user/example.iso` with the filename of the desired ISO file.
* ### How do I boot a x86 image?
You can use [qemu-docker](https://github.com/qemus/qemu-docker/) to run x86 and x64 images on ARM.
* ### 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? * ### 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. By default, the container uses bridge networking, which shares the IP address with the host.
@ -172,15 +183,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: It is possible to pass-through disk devices directly by adding them to your compose file in this way:
```yaml ```yaml
environment:
DEVICE: "/dev/sda"
DEVICE2: "/dev/sdb"
devices: devices:
- /dev/sda - /dev/sdb:/dev/disk1
- /dev/sdb - /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? * ### How do I pass-through a USB device?
@ -193,18 +201,7 @@ docker run -it --rm --name qemu -e "BOOT=http://example.com/image.iso" -p 8006:8
- /dev/bus/usb - /dev/bus/usb
``` ```
* ### How do I verify if my system supports KVM? * ### How can I provide custom arguments to QEMU?
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?
You can create the `ARGUMENTS` environment variable to provide additional arguments to QEMU at runtime: You can create the `ARGUMENTS` environment variable to provide additional arguments to QEMU at runtime:

View file

@ -8,7 +8,7 @@ set -Eeuo pipefail
DEF_OPTS="-nodefaults" DEF_OPTS="-nodefaults"
SERIAL_OPTS="-serial $SERIAL" SERIAL_OPTS="-serial $SERIAL"
USB_OPTS="-device $USB -device usb-kbd -device usb-tablet" USB_OPTS="-device $USB -device usb-kbd -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" 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" MON_OPTS="-monitor $MONITOR -name $PROCESS,process=$PROCESS,debug-threads=on"
MAC_OPTS="-machine type=${MACHINE}${SECURE},dump-guest-core=off${KVM_OPTS}" MAC_OPTS="-machine type=${MACHINE}${SECURE},dump-guest-core=off${KVM_OPTS}"

View file

@ -498,6 +498,11 @@ fi
: "${DEVICE3:=""}" : "${DEVICE3:=""}"
: "${DEVICE4:=""}" : "${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 if [ -n "$DEVICE" ]; then
addDevice "$DEVICE" "device" "3" "0xa" || exit $? addDevice "$DEVICE" "device" "3" "0xa" || exit $?
else else

View file

@ -8,7 +8,7 @@ set -Eeuo pipefail
: "${CPU_MODEL:=""}" : "${CPU_MODEL:=""}"
: "${DEF_MODEL:="neoverse-n1"}" : "${DEF_MODEL:="neoverse-n1"}"
[[ "$ARCH" != "arm"* ]] && KVM="N" [[ "${ARCH,,}" != "arm"* ]] && KVM="N"
if [[ "$KVM" != [Nn]* ]]; then if [[ "$KVM" != [Nn]* ]]; then

View file

@ -1,9 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -Eeuo pipefail set -Eeuo pipefail
info () { printf "%b%s%b" "\E[1;34m \E[1;36m" "$1" "\E[0m\n"; } 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; } 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; } 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 trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR
@ -44,7 +44,6 @@ HOST=$(hostname -s)
KERNEL=$(echo "$SYS" | cut -b 1) KERNEL=$(echo "$SYS" | cut -b 1)
MINOR=$(echo "$SYS" | cut -d '.' -f2) MINOR=$(echo "$SYS" | cut -d '.' -f2)
ARCH=$(dpkg --print-architecture) 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') 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 # Check system
@ -61,6 +60,15 @@ if [ ! -d "$STORAGE" ]; then
error "Storage folder ($STORAGE) not found!" && exit 13 error "Storage folder ($STORAGE) not found!" && exit 13
fi 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 # Print system info
SYS="${SYS/-generic/}" SYS="${SYS/-generic/}"
FS=$(stat -f -c %T "$STORAGE") FS=$(stat -f -c %T "$STORAGE")
@ -68,9 +76,20 @@ FS="${FS/ext2\/ext3/ext4}"
SPACE=$(df --output=avail -B 1 "$STORAGE" | tail -n 1) SPACE=$(df --output=avail -B 1 "$STORAGE" | tail -n 1)
SPACE_GB=$(( (SPACE + 1073741823)/1073741824 )) 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 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 # Helper functions
isAlive() { isAlive() {