Calculate memory and number of CPUs for the VM.

This commit is contained in:
Andreas B. Mundt 2024-01-25 16:36:15 +01:00
parent 138c4f7d7e
commit 1622106e3c

View file

@ -1,24 +1,30 @@
#!/usr/bin/bash
#
# Start a netboot VM
# Start a netboot VM connected to macvtap device and fraction of mem/cpus
#
set -eu
if [[ $# -eq 0 ]] ; then
arg="--memory 4096 --vcpu 4"
fi
# find macvtap interface MAC address:
MAC="$(ip link | grep -A1 "vm-macvtap" | \
mac="$(ip link | grep -A1 "vm-macvtap" | \
sed -nE "s%\s+link/ether ([[:xdigit:]:]{17}) .+%\1%p")"
if [[ $# -eq 0 ]] ; then
mem=$(sed -En "s/^MemTotal:\s+([0-9]+)\s+kB/\1/p" /proc/meminfo)
cpu=$(sed -En "0,/^cpu cores/s/^cpu cores\s+:\s+([0-9]+)/\1/p" /proc/cpuinfo)
arg="--memory $((mem/2048)) --vcpu $((cpu/2))"
echo "Set options: $arg"
fi
loader='/usr/share/OVMF/OVMF_CODE_4M.fd,\
loader.readonly=yes,loader.type=pflash,\
nvram.template=/usr/share/OVMF/OVMF_VARS_4M.fd'
type="ethernet,mac=${mac},target.dev=vm-macvtap,xpath1.set=./target/@managed=no"
XDG_CONFIG_HOME="/tmp/${UID}/.config" \
exec virt-install \
exec "virt-install \
--name bookworm \
--osinfo debiantesting \
--nodisks --import \
--pxe --boot loader=/usr/share/OVMF/OVMF_CODE_4M.fd,\
loader.readonly=yes,loader.type=pflash,\
nvram.template=/usr/share/OVMF/OVMF_VARS_4M.fd \
--network type=ethernet,mac=${MAC},\
target.dev=vm-macvtap,xpath1.set=./target/@managed=no $@ ${arg:-}
--pxe --boot loader=$loader \
--network type=$type \
$* ${arg:-}"