diff --git a/agent.sh b/agent.sh index c782b4a..5575097 100644 --- a/agent.sh +++ b/agent.sh @@ -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 { diff --git a/lib/qemu.sh b/lib/qemu.sh new file mode 100644 index 0000000..a90ef97 --- /dev/null +++ b/lib/qemu.sh @@ -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 +} \ No newline at end of file