79 lines
1.7 KiB
Bash
79 lines
1.7 KiB
Bash
#!/bin/bash -e
|
|
DIR="$(dirname "${BASH_SOURCE[0]}")"
|
|
|
|
BRIDGE=virbr0
|
|
|
|
NORMAL=$'\e[0m'
|
|
BOLD=$'\e[1m'
|
|
RED=$'\e[31m'
|
|
|
|
function ask_yn() {
|
|
local ans
|
|
|
|
printf '%s (Y/n) ' "$1" >&2
|
|
read -r ans
|
|
case "${ans,,}" in
|
|
y*|'') return 0 ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
declare -A ISO
|
|
declare -a OPTS
|
|
function download_isos() {
|
|
local file
|
|
|
|
for iso in "${!ISO[@]}"; do
|
|
file="$iso.iso"
|
|
if ! [ -f "$file" ] && ask_yn "download $file?"; then
|
|
curl -LC- -o "$file" "${ISO[$iso]}"
|
|
fi
|
|
if [ -f "$file" ]; then
|
|
OPTS+=(-drive "file=$file,media=cdrom,readonly=on")
|
|
fi
|
|
done
|
|
}
|
|
|
|
function _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"
|
|
}
|
|
|
|
function _getnet() {
|
|
local net=user
|
|
|
|
if ! ip link show "$BRIDGE" | grep -q UP; then
|
|
ask_yn 'create bridge?' && sudo "$DIR/startnat.sh" "$BRIDGE" > /dev/null
|
|
fi
|
|
if ip link show "$BRIDGE" | grep -q UP; then
|
|
net="bridge,br=$BRIDGE"
|
|
fi
|
|
echo "$net"
|
|
}
|
|
|
|
function qemu() {
|
|
local ram=2G
|
|
local maxram; maxram="$(_getmaxram)"
|
|
local net; net="$(_getnet)"
|
|
|
|
(set -x
|
|
qemu-system-x86_64 -accel kvm \
|
|
-monitor stdio \
|
|
-M q35 \
|
|
-cpu host \
|
|
-m "$ram,maxmem=$maxram" \
|
|
-vga virtio \
|
|
-audio pipewire,model=hda \
|
|
-drive if=virtio,file=hda.qcow2 \
|
|
-nic "model=virtio-net-pci,type=$net" \
|
|
-device qemu-xhci \
|
|
-object "memory-backend-ram,id=mem,size=$ram,share=on" \
|
|
-numa node,memdev=mem \
|
|
"${OPTS[@]}" \
|
|
"$@"
|
|
)
|
|
}
|