add qemu start gestion

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2025-05-10 11:49:20 +02:00
commit 1cdb89b0e6
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
2 changed files with 43 additions and 2 deletions

View file

@ -10,6 +10,7 @@
. ./lib/vpc.sh
. ./lib/subnet.sh
. ./lib/colors.sh
. ./lib/qemu.sh
DRY_RUN="1"
SIMULATION="1"
@ -58,8 +59,7 @@ function start_vm {
create_vpc "${FLAGS_vpc_name}"
create_subnet "${FLAGS_vpc_name}" "${FLAGS_subnet_name}" "${FLAGS_vxlan_id}" "${local_ip}" "${gateway_ip}" "${subnet}"
# create volume
# create qemu
qemu_start_vm "${FLAGS_vm_ip}" "${FLAGS_subnet_name}" "${FLAGS_vpc_name}" "${FLAGS_vm_name}" "${FLAGS_volume_id}"
}
function stop_vm {

41
lib/qemu.sh Normal file
View file

@ -0,0 +1,41 @@
#!/bin/bash
. ./lib/numbers.sh
. ./lib/colors.sh
function create_tap {
tap_id="${1}"
bridge_name="${2}"
ip tuntap add dev "tap${tap_id}" mode tap
brctl addif "${bridge_name}" "tap${tap_id}"
ip 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 tap_id="$(generate_random_number 10)"
local subnet_id="$(echo "${subnet_name}" | cut -d\- -f2)"
local mac="$(find_mac "${vpc_name}_br-${subnet_id}" "${ip}")"
print_in_color "${COLOR_GREEN}" "Create tap tap${tap_id}"
create_tap "${tap_id}" "br-${subnet_id}"
print_in_color "${COLOR_GREEN}" "Start vm ${vm_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" \
-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
}