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

View File

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

View File

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