two_with_bash/lib/qemu.sh
GnomeZworc e22f3d4e67
add public_ip
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2025-05-24 14:16:51 +02:00

104 lines
No EOL
3.7 KiB
Bash

#!/bin/bash
. ./lib/numbers.sh
. ./lib/colors.sh
. ./lib/db.sh
. ./lib/metadata.sh
. ./lib/public_ip.sh
function check_qemu_exist {
local vm_name="${1}"
print_in_color "${COLOR_GREY}" "Check in db if ${vm_name} exist"
check_in_db "vm" "${vm_name}"
[ "$?" -eq "0" ] || return 1
ps aux | grep -v grep | grep "${vm_name}" > /dev/null || return 1
return 0
}
function create_tap {
local tap_id="${1}"
local bridge_name="${2}"
local vpc_name="${3}"
ip -n "${vpc_name}" tuntap add dev "tap${tap_id}" mode tap
ip netns exec "${vpc_name}" brctl addif "${bridge_name}" "tap${tap_id}"
ip -n "${vpc_name}" link set up dev "tap${tap_id}"
}
function qemu_start_vm {
local ip="${1}"
local subnet_name="${2}"
local vpc_name="${3}"
local vm_name="${4}"
local volume_path="${5}"
local ssh_key="${6}"
local gateway_ip="${7}"
local local_ip="${8}"
local public_ip="${9}"
local tap_id="$(generate_random_id 10)"
local metadata_port="$(generate_random_number)"
local subnet_id="$(echo "${subnet_name}" | cut -d\- -f2)"
local mac="$(find_mac "${vpc_name}_br-${subnet_id}" "${ip}")"
check_qemu_exist "${vm_name}"
[ "$?" -eq "0" ] && \
{
print_in_color "${COLOR_ORANGE}" "Would have create ${vm_name}"
} || \
{
print_in_color "${COLOR_GREEN}" "Create tap tap${tap_id}"
add_in_db "vm" "${vm_name}" "${subnet_name}" "${tap_id}" "${metadata_port}" "${ip}" "${public_ip}"
create_tap "${tap_id}" "br-${subnet_id}" "${vpc_name}"
metadata_start "${vpc_name}" "${gateway_ip}" "${metadata_port}" "${vm_name}" "${ssh_key}"
ip netns exec "${vpc_name}" iptables -t nat -A PREROUTING -s "${ip}/32" -d "169.254.169.254/32" -p tcp -m tcp --dport 80 -j DNAT --to-destination "${gateway_ip}:${metadata_port}"
print_in_color "${COLOR_GREEN}" "Start vm ${vm_name}"
ip netns exec "${vpc_name}" qemu-system-x86_64 \
-enable-kvm \
-cpu host \
-m 512 \
-smp 1 \
-serial "unix:/tmp/${vm_name}.sock,server,nowait" \
-monitor "unix:/tmp/${vm_name}.mon-sock,server,nowait" \
-qmp "unix:/tmp/${vm_name}.qmp-sock,server,nowait" \
-display "none" \
-drive "file=${volume_path},if=virtio" \
-netdev "tap,id=net0,ifname=tap${tap_id},script=no,downscript=no" \
-device "virtio-net-pci,netdev=net0,mac=${mac}" \
-daemonize
add_public_ip "${vpc_name}" "${public_ip}" "${ip}" "${local_ip}"
}
}
function qemu_stop_vm {
local vm_name="${1}"
local vm_def=$(get_from_db "vm" "${vm_name}")
local subnet_name="$(echo "${vm_def}" | cut -d\; -f 2)"
local tap_id="$(echo "${vm_def}" | cut -d\; -f 3)"
local metadata_port="$(echo "${vm_def}" | cut -d\; -f 4)"
local ip="$(echo "${vm_def}" | cut -d\; -f 5)"
local public_ip="$(echo "${vm_def}" | cut -d\; -f 6)"
local subnet_def=$(get_from_db "subnet" "${subnet_name}")
local vpc_name="$(echo "${subnet_def}" | cut -d\; -f 2)"
local gateway_ip="$(echo "${subnet_def}" | cut -d\; -f 5 | cut -d\/ -f 1)"
local unix_path="/tmp/${vm_name}.qmp-sock"
print_in_color "${COLOR_RED}" "Stop ${vm_name}"
echo -e '{ "execute": "qmp_capabilities" }\n{ "execute": "system_powerdown" }' | socat - UNIX-CONNECT:"${unix_path}" > /dev/null
print_in_color "${COLOR_RED}" "Delete tap${tap_id}"
ip -n "${vpc_name}" link del dev "tap${tap_id}"
metadata_stop "${vpc_name}" "${gateway_ip}" "${metadata_port}"
delete_public_ip "${vpc_name}" "${public_ip}" "${ip}"
delete_in_db "vm" "${vm_name}"
print_in_color "${COLOR_GREY}" "Try to delete ${subnet_name}"
count_in_db "vm" "${subnet_name}"
[ "$?" -eq "0" ] && delete_subnet "${subnet_name}"
}