fix: available cpu/ram

This commit is contained in:
ange 2025-07-21 12:50:10 +00:00
parent fd88530dc4
commit 3b271807a3
Signed by: ange
GPG Key ID: 9E0C4157BB7BEB1D
3 changed files with 15 additions and 26 deletions

18
lib.sh
View File

@ -28,14 +28,6 @@ download_isos() {
"${curl[@]}"
}
_getmaxram() {
local ram; ram="$(free -g | awk '/^Mem:/{print $2 - 2}')"
if [ "$ram" -le 2 ]; then ram=2
elif [ "$ram" -gt 32 ]; then ram=32; fi
echo "${ram}G"
}
_getnet() {
local net=user
@ -49,18 +41,18 @@ _getnet() {
}
qemu() {
local ram=2G
local maxram; maxram="$(_getmaxram)"
local net; net="$(_getnet)"
local ram net
ram="$(free -g | awk '/^Mem:/{t=$2 / 2; print(t < 2 ? 2 : t)"G"}')"
net="$(_getnet)"
(set -x
qemu-system-x86_64 \
-accel kvm \
-M q35 \
-bios /usr/share/edk2/x64/OVMF.4m.fd \
-monitor stdio \
-cpu host \
-m "$ram,maxmem=$maxram" \
-smp "$(nproc --ignore 4)" \
-m "$ram" \
-vga virtio \
-audio pipewire,model=hda \
-nic "model=virtio-net-pci,type=$net" \

View File

@ -1,15 +1,14 @@
#!/bin/bash -eu
DIR="$(dirname "${BASH_SOURCE[0]}")"
fix_nft_drops() {
local j s='"add chain \(.family) \(.table) \(.name) { policy \(.policy); }"'
if [ -f "$DIR/restore-nft.conf" ]; then
nft -f "$DIR/restore-nft.conf"
if [ -f /tmp/restore-nft.conf ]; then
nft -f /tmp/restore-nft.conf
fi
j="$(nft -j list chains | jq '.[][].chain | select(.policy == "drop")')"
jq -r ".policy=\"accept\" | $s" <<< "$j"
jq -r "$s" <<< "$j" > "$DIR/restore-nft.conf"
jq -r "$s" <<< "$j" > /tmp/restore-nft.conf
}
command -V dnsmasq > /dev/null
@ -29,7 +28,6 @@ if ! ip link show "$BRIDGE" 2> /dev/null; then
ip link add "$BRIDGE" type bridge
fi
ip link set dev "$BRIDGE" up
ip address flush dev "$BRIDGE"
ip address add 192.168.123.1/24 dev "$BRIDGE"
nft -f- <<EOF
@ -50,7 +48,7 @@ destroy table ip qemu; table ip qemu {
}
EOF
if ! pidof dnsmasq | grep -q "$(cat /var/run/dnsmasq-virbr0.pid)"; then
if ! [ -f /var/run/dnsmasq-virbr0.pid ]; then
dnsmasq -z \
-i "$BRIDGE" \
-F 192.168.123.2,192.168.123.254,255.255.255.0 \

View File

@ -5,14 +5,13 @@ if [ "$EUID" != 0 ]; then
exit
fi
BRIDGE="${1-virbr0}"
ip link del dev "$BRIDGE"
ip link del dev "${1-virbr0}"
nft destroy table qemu
if [ -f restore-nft.conf ]; then
nft -f ./restore-nft.conf
rm restore-nft.conf
if [ -f /tmp/restore-nft.conf ]; then
nft -f /tmp/restore-nft.conf
rm /tmp/restore-nft.conf
fi
kill -TERM "$(cat /var/run/dnsmasq-virbr0.pid)"
kill -TERM "$(< /var/run/dnsmasq-virbr0.pid)"
rm -f /var/run/dnsmasq-virbr0.pid