2023-06-04 13:06:22 +00:00
|
|
|
#! /bin/bash
|
2023-05-18 17:21:30 +00:00
|
|
|
#
|
|
|
|
# This collection of scripts aims to install a NOVIT cluster easily, with help of QEMU
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
## QEMU functions
|
|
|
|
#
|
|
|
|
prereqs_qemu() {
|
|
|
|
for com in qemu-system-x86_64 truncate docker iptables ; do
|
|
|
|
command -v $com 1>/dev/null || error Command $com not found, please install it. Aborting...
|
|
|
|
done
|
|
|
|
}
|
|
|
|
setup_network_qemu() {
|
2023-06-14 18:21:23 +00:00
|
|
|
if ! ip li show $QEMU_BR_NAME &>/dev/null ; then
|
2023-05-24 18:12:07 +00:00
|
|
|
ip li add name $QEMU_BR_NAME type bridge
|
|
|
|
ip li set $QEMU_BR_NAME up
|
|
|
|
fi
|
2023-05-18 17:21:30 +00:00
|
|
|
QEMU_BR_IP=$(extract_var clusters gateway)
|
|
|
|
QEMU_BR_MASK=$(extract_var clusters netmask)
|
|
|
|
if [ $(echo $QEMU_BR_IP | wc -w) -gt 1 ]; then
|
|
|
|
perror "More than one cluster is configured, not compatible with our quick-start setup, exiting"
|
|
|
|
fi
|
|
|
|
pinfo "Using detected gateway IP $QEMU_BR_IP for bridge $QEMU_BR_NAME"
|
2023-05-24 18:12:07 +00:00
|
|
|
if ! ip a show dev $QEMU_BR_NAME | grep $QEMU_BR_IP ; then
|
|
|
|
ip a add $QEMU_BR_IP/$QEMU_BR_MASK dev $QEMU_BR_NAME
|
2023-06-08 17:38:17 +00:00
|
|
|
iptables -t nat -I POSTROUTING -j MASQUERADE -s $QEMU_BR_IP/$QEMU_BR_MASK \! -o $QEMU_BR_NAME
|
|
|
|
iptables -I FORWARD -o $QEMU_BR_NAME -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
|
|
|
|
iptables -I FORWARD -j ACCEPT -i $QEMU_BR_NAME
|
2023-05-24 18:12:07 +00:00
|
|
|
fi
|
2023-06-05 08:24:24 +00:00
|
|
|
if ! test -d /etc/qemu; then
|
|
|
|
mkdir -p /etc/qemu
|
|
|
|
fi
|
2023-11-06 16:17:02 +00:00
|
|
|
if ! grep -qs "allow $QEMU_BR_NAME" /etc/qemu/bridge.conf; then
|
2023-06-08 17:38:17 +00:00
|
|
|
echo "allow $QEMU_BR_NAME" >> /etc/qemu/bridge.conf
|
2023-06-05 08:24:24 +00:00
|
|
|
fi
|
2023-05-18 17:21:30 +00:00
|
|
|
}
|
2023-06-14 21:51:59 +00:00
|
|
|
|
2023-05-18 17:21:30 +00:00
|
|
|
run_qemu() {
|
|
|
|
id=1
|
|
|
|
for host in ${!hosts[*]}; do
|
2023-05-24 18:12:07 +00:00
|
|
|
ip route show |grep "${hosts[$host]} dev $QEMU_BR_NAME" ||\
|
|
|
|
ip route add ${hosts[$host]} dev $QEMU_BR_NAME
|
|
|
|
pinfo "Starting host $host with ip ${hosts[$host]}"
|
2023-05-18 17:21:30 +00:00
|
|
|
qemu-system-x86_64 -enable-kvm -smp $QEMU_VM_CPU -m $QEMU_VM_MEM \
|
|
|
|
-nic bridge,br=$QEMU_BR_NAME,mac=42:42:42:42:42:0${id} \
|
2023-11-06 16:17:02 +00:00
|
|
|
-kernel $ctxdir/data/$host/kernel -initrd $ctxdir/data/$host/initrd -vga qxl \
|
2023-05-24 18:12:07 +00:00
|
|
|
-drive format=raw,file=$ctxdir/data/$host/disk &
|
2023-05-18 17:21:30 +00:00
|
|
|
echo $! >$ctxdir/data/$host/pid
|
|
|
|
((++id))
|
|
|
|
done
|
2023-05-24 18:12:07 +00:00
|
|
|
pinfo "$(ls $ctxdir/data/*/pid|wc -w) host(s) have been started"
|
2023-05-18 17:21:30 +00:00
|
|
|
}
|
|
|
|
# # # # # # # #
|
|
|
|
|
2023-06-14 21:51:59 +00:00
|
|
|
|
|
|
|
|
2023-05-24 18:12:07 +00:00
|
|
|
source $(dirname $0)/.common
|
|
|
|
check_root
|
2023-05-18 17:21:30 +00:00
|
|
|
prereqs
|
|
|
|
check_conf
|
2023-05-24 18:12:07 +00:00
|
|
|
#fresh_start
|
2023-05-18 17:21:30 +00:00
|
|
|
trap clean SIGINT SIGTERM SIGKILL
|
|
|
|
declare -A hosts
|
|
|
|
setup_network_qemu
|
|
|
|
get_hosts
|
|
|
|
get_parts
|
2023-06-08 17:38:17 +00:00
|
|
|
destroyvms
|
2023-05-18 17:21:30 +00:00
|
|
|
run_qemu
|
|
|
|
|