2019-10-25 06:42:40 +00:00
|
|
|
# QEMU Manual
|
|
|
|
# https://qemu.weilnetz.de/doc/qemu-doc.html
|
|
|
|
|
|
|
|
# For QEMU Monitor Protocol Commands Information, See
|
|
|
|
# https://qemu.weilnetz.de/doc/qemu-doc.html#pcsys_005fmonitor
|
|
|
|
|
|
|
|
import os
|
2019-11-18 17:39:57 +00:00
|
|
|
import random
|
2019-11-11 18:42:57 +00:00
|
|
|
import subprocess as sp
|
2019-10-25 06:42:40 +00:00
|
|
|
import tempfile
|
|
|
|
import time
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
from functools import wraps
|
2019-11-15 16:11:45 +00:00
|
|
|
from string import Template
|
2019-11-18 17:39:57 +00:00
|
|
|
from typing import Union
|
2019-11-28 08:04:53 +00:00
|
|
|
from os.path import join as join_path
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
import bitmath
|
|
|
|
import sshtunnel
|
2019-11-15 16:11:45 +00:00
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
from common.helpers import get_ipv6_address
|
2019-11-18 17:39:57 +00:00
|
|
|
from common.request import RequestEntry, RequestType
|
|
|
|
from common.vm import VMEntry, VMStatus
|
2019-11-25 06:52:36 +00:00
|
|
|
from config import etcd_client, request_pool, running_vms, vm_pool, env_vars, image_storage_handler
|
2019-11-18 17:39:57 +00:00
|
|
|
from . import qmp
|
|
|
|
from host import logger
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
class VM:
|
|
|
|
def __init__(self, key, handle, vnc_socket_file):
|
|
|
|
self.key = key # type: str
|
|
|
|
self.handle = handle # type: qmp.QEMUMachine
|
|
|
|
self.vnc_socket_file = vnc_socket_file # type: tempfile.NamedTemporaryFile
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "VM({})".format(self.key)
|
|
|
|
|
|
|
|
|
2019-11-15 16:11:45 +00:00
|
|
|
def create_dev(script, _id, dev, ip=None):
|
|
|
|
command = [script, _id, dev]
|
2019-11-18 17:39:57 +00:00
|
|
|
if ip:
|
2019-11-15 16:11:45 +00:00
|
|
|
command.append(ip)
|
2019-11-11 18:42:57 +00:00
|
|
|
try:
|
2019-11-15 16:11:45 +00:00
|
|
|
output = sp.check_output(command, stderr=sp.PIPE)
|
2019-11-11 18:42:57 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e.stderr)
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return output.decode("utf-8").strip()
|
|
|
|
|
|
|
|
|
2019-11-28 08:04:53 +00:00
|
|
|
def create_vxlan_br_tap(_id, _dev, tap_id, ip=None):
|
2019-11-11 18:42:57 +00:00
|
|
|
network_script_base = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'network')
|
|
|
|
vxlan = create_dev(script=os.path.join(network_script_base, 'create-vxlan.sh'),
|
2019-11-18 17:39:57 +00:00
|
|
|
_id=_id, dev=_dev)
|
2019-11-11 18:42:57 +00:00
|
|
|
if vxlan:
|
|
|
|
bridge = create_dev(script=os.path.join(network_script_base, 'create-bridge.sh'),
|
2019-11-15 16:11:45 +00:00
|
|
|
_id=_id, dev=vxlan, ip=ip)
|
2019-11-11 18:42:57 +00:00
|
|
|
if bridge:
|
|
|
|
tap = create_dev(script=os.path.join(network_script_base, 'create-tap.sh'),
|
2019-11-28 08:04:53 +00:00
|
|
|
_id=str(tap_id), dev=bridge)
|
2019-11-11 18:42:57 +00:00
|
|
|
if tap:
|
|
|
|
return tap
|
|
|
|
|
|
|
|
|
|
|
|
def random_bytes(num=6):
|
|
|
|
return [random.randrange(256) for _ in range(num)]
|
|
|
|
|
|
|
|
|
|
|
|
def generate_mac(uaa=False, multicast=False, oui=None, separator=':', byte_fmt='%02x'):
|
|
|
|
mac = random_bytes()
|
|
|
|
if oui:
|
|
|
|
if type(oui) == str:
|
|
|
|
oui = [int(chunk) for chunk in oui.split(separator)]
|
2019-11-18 17:39:57 +00:00
|
|
|
mac = oui + random_bytes(num=6 - len(oui))
|
2019-11-11 18:42:57 +00:00
|
|
|
else:
|
|
|
|
if multicast:
|
2019-11-18 17:39:57 +00:00
|
|
|
mac[0] |= 1 # set bit 0
|
2019-11-11 18:42:57 +00:00
|
|
|
else:
|
2019-11-18 17:39:57 +00:00
|
|
|
mac[0] &= ~1 # clear bit 0
|
2019-11-11 18:42:57 +00:00
|
|
|
if uaa:
|
2019-11-18 17:39:57 +00:00
|
|
|
mac[0] &= ~(1 << 1) # clear bit 1
|
2019-11-11 18:42:57 +00:00
|
|
|
else:
|
2019-11-18 17:39:57 +00:00
|
|
|
mac[0] |= 1 << 1 # set bit 1
|
2019-11-11 18:42:57 +00:00
|
|
|
return separator.join(byte_fmt % b for b in mac)
|
|
|
|
|
|
|
|
|
2019-11-15 16:11:45 +00:00
|
|
|
def update_radvd_conf(etcd_client):
|
|
|
|
network_script_base = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'network')
|
|
|
|
|
2019-11-18 17:39:57 +00:00
|
|
|
networks = {
|
|
|
|
net.value['ipv6']: net.value['id']
|
|
|
|
for net in etcd_client.get_prefix('/v1/network/', value_in_json=True)
|
|
|
|
if net.value.get('ipv6')
|
|
|
|
}
|
2019-11-15 16:11:45 +00:00
|
|
|
radvd_template = open(os.path.join(network_script_base,
|
|
|
|
'radvd-template.conf'), 'r').read()
|
|
|
|
radvd_template = Template(radvd_template)
|
|
|
|
|
|
|
|
content = [radvd_template.safe_substitute(bridge='br{}'.format(networks[net]),
|
2019-11-18 17:39:57 +00:00
|
|
|
prefix=net)
|
2019-11-15 16:11:45 +00:00
|
|
|
for net in networks if networks.get(net)]
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-11-15 16:11:45 +00:00
|
|
|
with open('/etc/radvd.conf', 'w') as radvd_conf:
|
|
|
|
radvd_conf.writelines(content)
|
2019-11-27 18:48:38 +00:00
|
|
|
try:
|
|
|
|
sp.check_output(['systemctl', 'restart', 'radvd'])
|
|
|
|
except Exception:
|
|
|
|
sp.check_output(['service', 'radvd', 'restart'])
|
2019-11-15 16:11:45 +00:00
|
|
|
|
2019-11-28 08:04:53 +00:00
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
def get_start_command_args(vm_entry, vnc_sock_filename: str, migration=False, migration_port=None):
|
2019-10-25 06:42:40 +00:00
|
|
|
threads_per_core = 1
|
2019-11-25 06:52:36 +00:00
|
|
|
vm_memory = int(bitmath.parse_string_unsafe(vm_entry.specs["ram"]).to_MB())
|
2019-10-25 06:42:40 +00:00
|
|
|
vm_cpus = int(vm_entry.specs["cpu"])
|
|
|
|
vm_uuid = vm_entry.uuid
|
2019-11-11 18:42:57 +00:00
|
|
|
vm_networks = vm_entry.network
|
|
|
|
|
2019-11-27 07:12:29 +00:00
|
|
|
command = "-name {}_{}".format(vm_entry.owner, vm_entry.name)
|
|
|
|
|
|
|
|
command += " -drive file={},format=raw,if=virtio,cache=none".format(
|
2019-11-25 06:52:36 +00:00
|
|
|
image_storage_handler.qemu_path_string(vm_uuid)
|
|
|
|
)
|
2019-10-25 06:42:40 +00:00
|
|
|
command += " -device virtio-rng-pci -vnc unix:{}".format(vnc_sock_filename)
|
|
|
|
command += " -m {} -smp cores={},threads={}".format(
|
|
|
|
vm_memory, vm_cpus, threads_per_core
|
|
|
|
)
|
|
|
|
|
|
|
|
if migration:
|
2019-11-25 06:52:36 +00:00
|
|
|
command += " -incoming tcp:[::]:{}".format(migration_port)
|
2019-11-18 17:39:57 +00:00
|
|
|
|
2019-11-11 18:42:57 +00:00
|
|
|
tap = None
|
2019-11-28 08:04:53 +00:00
|
|
|
for network_mac_and_tap in vm_networks:
|
|
|
|
network_name, mac, tap = network_mac_and_tap
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
_key = os.path.join(env_vars.get('NETWORK_PREFIX'), vm_entry.owner, network_name)
|
2019-11-11 18:42:57 +00:00
|
|
|
network = etcd_client.get(_key, value_in_json=True)
|
|
|
|
network_type = network.value["type"]
|
|
|
|
network_id = str(network.value["id"])
|
2019-11-15 16:11:45 +00:00
|
|
|
network_ipv6 = network.value["ipv6"]
|
2019-11-11 18:42:57 +00:00
|
|
|
|
|
|
|
if network_type == "vxlan":
|
2019-11-28 08:04:53 +00:00
|
|
|
tap = create_vxlan_br_tap(_id=network_id,
|
|
|
|
_dev=env_vars.get("VXLAN_PHY_DEV"),
|
|
|
|
tap_id=tap,
|
|
|
|
ip=network_ipv6)
|
2019-11-15 16:11:45 +00:00
|
|
|
update_radvd_conf(etcd_client)
|
2019-11-18 17:39:57 +00:00
|
|
|
|
|
|
|
command += " -netdev tap,id=vmnet{net_id},ifname={tap},script=no,downscript=no" \
|
|
|
|
" -device virtio-net-pci,netdev=vmnet{net_id},mac={mac}" \
|
|
|
|
.format(tap=tap, net_id=network_id, mac=mac)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
return command.split(" ")
|
|
|
|
|
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
def create_vm_object(vm_entry, migration=False, migration_port=None):
|
2019-10-25 06:42:40 +00:00
|
|
|
# NOTE: If migration suddenly stop working, having different
|
|
|
|
# VNC unix filename on source and destination host can
|
|
|
|
# be a possible cause of it.
|
|
|
|
|
|
|
|
# REQUIREMENT: Use Unix Socket instead of TCP Port for VNC
|
|
|
|
vnc_sock_file = tempfile.NamedTemporaryFile()
|
|
|
|
|
|
|
|
qemu_args = get_start_command_args(
|
|
|
|
vm_entry=vm_entry,
|
|
|
|
vnc_sock_filename=vnc_sock_file.name,
|
|
|
|
migration=migration,
|
|
|
|
migration_port=migration_port,
|
|
|
|
)
|
|
|
|
qemu_machine = qmp.QEMUMachine("/usr/bin/qemu-system-x86_64", args=qemu_args)
|
|
|
|
return VM(vm_entry.key, qemu_machine, vnc_sock_file)
|
|
|
|
|
|
|
|
|
|
|
|
def get_vm(vm_list: list, vm_key) -> Union[VM, None]:
|
|
|
|
return next((vm for vm in vm_list if vm.key == vm_key), None)
|
|
|
|
|
|
|
|
|
|
|
|
def need_running_vm(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(e):
|
|
|
|
vm = get_vm(running_vms, e.key)
|
|
|
|
if vm:
|
|
|
|
try:
|
|
|
|
status = vm.handle.command("query-status")
|
2019-11-18 17:39:57 +00:00
|
|
|
logger.debug("VM Status Check - %s", status)
|
2019-10-25 06:42:40 +00:00
|
|
|
except Exception as exception:
|
2019-11-18 17:39:57 +00:00
|
|
|
logger.info("%s failed - VM %s %s", func.__name__, e, exception)
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
return func(e)
|
|
|
|
|
|
|
|
return None
|
|
|
|
else:
|
2019-11-18 17:39:57 +00:00
|
|
|
logger.info("%s failed because VM %s is not running", func.__name__, e.key)
|
2019-10-25 06:42:40 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def create(vm_entry: VMEntry):
|
2019-11-25 06:52:36 +00:00
|
|
|
if image_storage_handler.is_vm_image_exists(vm_entry.uuid):
|
|
|
|
# File Already exists. No Problem Continue
|
|
|
|
logger.debug("Image for vm %s exists", vm_entry.uuid)
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
2019-11-25 06:52:36 +00:00
|
|
|
vm_hdd = int(bitmath.parse_string_unsafe(vm_entry.specs["os-ssd"]).to_MB())
|
|
|
|
if image_storage_handler.make_vm_image(src=vm_entry.image_uuid, dest=vm_entry.uuid):
|
|
|
|
if not image_storage_handler.resize_vm_image(path=vm_entry.uuid, size=vm_hdd):
|
2019-11-27 07:12:29 +00:00
|
|
|
vm_entry.status = VMStatus.error
|
2019-11-25 06:52:36 +00:00
|
|
|
else:
|
|
|
|
logger.info("New VM Created")
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
|
2019-11-25 06:52:36 +00:00
|
|
|
def start(vm_entry: VMEntry, destination_host_key=None, migration_port=None):
|
2019-10-25 06:42:40 +00:00
|
|
|
_vm = get_vm(running_vms, vm_entry.key)
|
|
|
|
|
|
|
|
# VM already running. No need to proceed further.
|
|
|
|
if _vm:
|
2019-11-27 07:12:29 +00:00
|
|
|
logger.info("VM %s already running" % vm_entry.uuid)
|
2019-10-25 06:42:40 +00:00
|
|
|
return
|
|
|
|
else:
|
2019-11-27 07:12:29 +00:00
|
|
|
logger.info("Trying to start %s" % vm_entry.uuid)
|
2019-11-25 06:52:36 +00:00
|
|
|
if destination_host_key:
|
|
|
|
launch_vm(vm_entry, migration=True, migration_port=migration_port,
|
|
|
|
destination_host_key=destination_host_key)
|
|
|
|
else:
|
|
|
|
create(vm_entry)
|
|
|
|
launch_vm(vm_entry)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@need_running_vm
|
|
|
|
def stop(vm_entry):
|
|
|
|
vm = get_vm(running_vms, vm_entry.key)
|
|
|
|
vm.handle.shutdown()
|
|
|
|
if not vm.handle.is_running():
|
|
|
|
vm_entry.add_log("Shutdown successfully")
|
|
|
|
vm_entry.declare_stopped()
|
|
|
|
vm_pool.put(vm_entry)
|
|
|
|
running_vms.remove(vm)
|
|
|
|
|
|
|
|
|
|
|
|
def delete(vm_entry):
|
2019-11-18 17:39:57 +00:00
|
|
|
logger.info("Deleting VM | %s", vm_entry)
|
2019-10-25 06:42:40 +00:00
|
|
|
stop(vm_entry)
|
|
|
|
|
2019-11-28 08:04:53 +00:00
|
|
|
if image_storage_handler.is_vm_image_exists(vm_entry.uuid):
|
|
|
|
r_status = image_storage_handler.delete_vm_image(vm_entry.uuid)
|
|
|
|
if r_status:
|
|
|
|
etcd_client.client.delete(vm_entry.key)
|
|
|
|
else:
|
2019-10-25 06:42:40 +00:00
|
|
|
etcd_client.client.delete(vm_entry.key)
|
|
|
|
|
|
|
|
def transfer(request_event):
|
|
|
|
# This function would run on source host i.e host on which the vm
|
|
|
|
# is running initially. This host would be responsible for transferring
|
|
|
|
# vm state to destination host.
|
|
|
|
|
|
|
|
_host, _port = request_event.parameters["host"], request_event.parameters["port"]
|
|
|
|
_uuid = request_event.uuid
|
|
|
|
_destination = request_event.destination_host_key
|
2019-11-25 06:52:36 +00:00
|
|
|
vm = get_vm(running_vms, join_path(env_vars.get('VM_PREFIX'), _uuid))
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
if vm:
|
|
|
|
tunnel = sshtunnel.SSHTunnelForwarder(
|
2019-11-25 06:52:36 +00:00
|
|
|
_host,
|
2019-11-18 17:39:57 +00:00
|
|
|
ssh_username=env_vars.get("ssh_username"),
|
|
|
|
ssh_pkey=env_vars.get("ssh_pkey"),
|
2019-10-25 06:42:40 +00:00
|
|
|
remote_bind_address=("127.0.0.1", _port),
|
2019-11-25 06:52:36 +00:00
|
|
|
ssh_proxy_enabled=True,
|
|
|
|
ssh_proxy=(_host, 22)
|
2019-10-25 06:42:40 +00:00
|
|
|
)
|
|
|
|
try:
|
|
|
|
tunnel.start()
|
|
|
|
except sshtunnel.BaseSSHTunnelForwarderError:
|
2019-11-18 17:39:57 +00:00
|
|
|
logger.exception("Couldn't establish connection to (%s, 22)", _host)
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
vm.handle.command(
|
2019-11-25 06:52:36 +00:00
|
|
|
"migrate", uri="tcp:0.0.0.0:{}".format(tunnel.local_bind_port)
|
2019-10-25 06:42:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
status = vm.handle.command("query-migrate")["status"]
|
|
|
|
while status not in ["failed", "completed"]:
|
|
|
|
time.sleep(2)
|
|
|
|
status = vm.handle.command("query-migrate")["status"]
|
|
|
|
|
|
|
|
with vm_pool.get_put(request_event.uuid) as source_vm:
|
|
|
|
if status == "failed":
|
|
|
|
source_vm.add_log("Migration Failed")
|
|
|
|
elif status == "completed":
|
|
|
|
# If VM is successfully migrated then shutdown the VM
|
|
|
|
# on this host and update hostname to destination host key
|
|
|
|
source_vm.add_log("Successfully migrated")
|
|
|
|
source_vm.hostname = _destination
|
|
|
|
running_vms.remove(vm)
|
|
|
|
vm.handle.shutdown()
|
|
|
|
source_vm.in_migration = False # VM transfer finished
|
|
|
|
finally:
|
|
|
|
tunnel.close()
|
|
|
|
|
|
|
|
|
|
|
|
def launch_vm(vm_entry, migration=False, migration_port=None, destination_host_key=None):
|
2019-11-27 07:12:29 +00:00
|
|
|
logger.info("Starting %s" % vm_entry.key)
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
vm = create_vm_object(vm_entry, migration=migration, migration_port=migration_port)
|
|
|
|
try:
|
|
|
|
vm.handle.launch()
|
2019-11-25 06:52:36 +00:00
|
|
|
except Exception:
|
|
|
|
logger.exception("Error Occured while starting VM")
|
|
|
|
vm.handle.shutdown()
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
if migration:
|
|
|
|
# We don't care whether MachineError or any other error occurred
|
2019-11-25 06:52:36 +00:00
|
|
|
pass
|
2019-10-25 06:42:40 +00:00
|
|
|
else:
|
|
|
|
# Error during typical launch of a vm
|
2019-11-25 06:52:36 +00:00
|
|
|
vm.handle.shutdown()
|
2019-10-25 06:42:40 +00:00
|
|
|
vm_entry.declare_killed()
|
|
|
|
vm_pool.put(vm_entry)
|
|
|
|
else:
|
|
|
|
vm_entry.vnc_socket = vm.vnc_socket_file.name
|
|
|
|
running_vms.append(vm)
|
|
|
|
|
|
|
|
if migration:
|
|
|
|
vm_entry.in_migration = True
|
|
|
|
r = RequestEntry.from_scratch(
|
|
|
|
type=RequestType.TransferVM,
|
|
|
|
hostname=vm_entry.hostname,
|
2019-11-25 06:52:36 +00:00
|
|
|
parameters={"host": get_ipv6_address(), "port": migration_port},
|
2019-10-25 06:42:40 +00:00
|
|
|
uuid=vm_entry.uuid,
|
|
|
|
destination_host_key=destination_host_key,
|
2019-11-18 17:39:57 +00:00
|
|
|
request_prefix=env_vars.get("REQUEST_PREFIX")
|
2019-10-25 06:42:40 +00:00
|
|
|
)
|
|
|
|
request_pool.put(r)
|
|
|
|
else:
|
|
|
|
# Typical launching of a vm
|
|
|
|
vm_entry.status = VMStatus.running
|
|
|
|
vm_entry.add_log("Started successfully")
|
|
|
|
|
|
|
|
vm_pool.put(vm_entry)
|