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-11 18:42:57 +00:00
|
|
|
import subprocess as sp
|
2019-12-26 07:24:19 +00:00
|
|
|
import ipaddress
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-11-15 16:11:45 +00:00
|
|
|
from string import Template
|
2019-11-28 08:04:53 +00:00
|
|
|
from os.path import join as join_path
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-03 10:40:41 +00:00
|
|
|
from ucloud.common.request import RequestEntry, RequestType
|
2019-12-28 10:39:11 +00:00
|
|
|
from ucloud.common.vm import VMStatus, declare_stopped
|
|
|
|
from ucloud.common.network import create_dev, delete_network_interface
|
|
|
|
from ucloud.common.schemas import VMSchema, NetworkSchema
|
2019-12-03 11:49:10 +00:00
|
|
|
from ucloud.host import logger
|
2019-12-22 07:26:48 +00:00
|
|
|
from ucloud.shared import shared
|
|
|
|
from ucloud.settings import settings
|
2019-12-28 10:39:11 +00:00
|
|
|
from ucloud.vmm import VMM
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
from marshmallow import ValidationError
|
2019-11-25 06:52:36 +00:00
|
|
|
|
2019-10-25 06:42:40 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
def maintenance():
|
|
|
|
pass
|
2019-10-25 06:42:40 +00:00
|
|
|
|
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
class VM:
|
|
|
|
def __init__(self, vm_entry):
|
|
|
|
self.schema = VMSchema()
|
|
|
|
self.vmm = VMM()
|
|
|
|
self.key = vm_entry.key
|
2019-12-22 07:26:48 +00:00
|
|
|
try:
|
2019-12-28 10:39:11 +00:00
|
|
|
self.vm = self.schema.loads(vm_entry.value)
|
|
|
|
except ValidationError:
|
|
|
|
logger.exception('Couldn\'t validate VM Entry', vm_entry.value)
|
|
|
|
self.vm = None
|
|
|
|
else:
|
|
|
|
self.uuid = vm_entry.key.split('/')[-1]
|
|
|
|
self.host_key = self.vm['hostname']
|
|
|
|
|
|
|
|
def get_qemu_args(self):
|
|
|
|
command = (
|
|
|
|
'-name {owner}_{name}'
|
|
|
|
' -drive file={file},format=raw,if=virtio,cache=none'
|
|
|
|
' -device virtio-rng-pci'
|
|
|
|
' -m {memory} -smp cores={cores},threads={threads}'
|
|
|
|
).format(owner=self.vm['owner'], name=self.vm['name'],
|
|
|
|
memory=int(self.vm['specs']['ram'].to_MB()), cores=self.vm['specs']['cpu'],
|
|
|
|
threads=1, file=shared.storage_handler.qemu_path_string(self.uuid))
|
|
|
|
|
|
|
|
return command.split(' ')
|
|
|
|
|
|
|
|
def start(self, destination_host_key=None):
|
|
|
|
migration = False
|
|
|
|
if destination_host_key:
|
|
|
|
migration = True
|
|
|
|
|
|
|
|
self.create()
|
|
|
|
try:
|
|
|
|
network_args = self.create_network_dev()
|
|
|
|
except Exception as err:
|
|
|
|
declare_stopped(self.vm)
|
|
|
|
self.vm['log'].append('Cannot Setup Network Properly')
|
|
|
|
logger.error('Cannot Setup Network Properly for vm %s', self.uuid, exc_info=err)
|
|
|
|
else:
|
|
|
|
self.vmm.start(uuid=self.uuid, migration=migration,
|
|
|
|
*self.get_qemu_args(), *network_args)
|
|
|
|
|
|
|
|
status = self.vmm.get_status(self.uuid)
|
|
|
|
if status == 'running':
|
|
|
|
self.vm['status'] = VMStatus.running
|
|
|
|
self.vm['vnc_socket'] = self.vmm.get_vnc(self.uuid)
|
|
|
|
elif status == 'inmigrate':
|
|
|
|
r = RequestEntry.from_scratch(
|
|
|
|
type=RequestType.TransferVM, # Transfer VM
|
|
|
|
hostname=self.host_key, # Which VM should get this request. It is source host
|
|
|
|
uuid=self.uuid, # uuid of VM
|
2019-12-29 18:48:04 +00:00
|
|
|
destination_sock_path=join_path(self.vmm.socket_dir, self.uuid),
|
2019-12-28 10:39:11 +00:00
|
|
|
destination_host_key=destination_host_key, # Where source host transfer VM
|
|
|
|
request_prefix=settings['etcd']['request_prefix']
|
|
|
|
)
|
|
|
|
shared.request_pool.put(r)
|
|
|
|
else:
|
|
|
|
self.stop()
|
|
|
|
declare_stopped(self.vm)
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
self.sync()
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
def stop(self):
|
|
|
|
self.vmm.stop(self.uuid)
|
|
|
|
self.delete_network_dev()
|
|
|
|
declare_stopped(self.vm)
|
|
|
|
self.sync()
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-29 18:48:04 +00:00
|
|
|
def migrate(self, destination_host, destination_sock_path):
|
|
|
|
self.vmm.transfer(src_uuid=self.uuid, destination_sock_path=destination_sock_path,
|
|
|
|
host=destination_host)
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
def create_network_dev(self):
|
|
|
|
command = ''
|
|
|
|
for network_mac_and_tap in self.vm['network']:
|
2019-12-22 07:26:48 +00:00
|
|
|
network_name, mac, tap = network_mac_and_tap
|
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
_key = os.path.join(settings['etcd']['network_prefix'], self.vm['owner'], network_name)
|
|
|
|
network = shared.etcd_client.get(_key, value_in_json=True)
|
|
|
|
network_schema = NetworkSchema()
|
|
|
|
try:
|
|
|
|
network = network_schema.load(network.value)
|
|
|
|
except ValidationError:
|
|
|
|
continue
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
if network['type'] == "vxlan":
|
|
|
|
tap = create_vxlan_br_tap(_id=network['id'],
|
2019-12-22 07:26:48 +00:00
|
|
|
_dev=settings['network']['vxlan_phy_dev'],
|
|
|
|
tap_id=tap,
|
2019-12-28 10:39:11 +00:00
|
|
|
ip=network['ipv6'])
|
|
|
|
|
|
|
|
all_networks = shared.etcd_client.get_prefix(settings['etcd']['network_prefix'],
|
|
|
|
value_in_json=True)
|
2019-12-26 07:24:19 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
if ipaddress.ip_network(network['ipv6']).is_global:
|
2019-12-26 07:24:19 +00:00
|
|
|
update_radvd_conf(all_networks)
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +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-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
return command.split(' ')
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
def delete_network_dev(self):
|
|
|
|
try:
|
|
|
|
for network in self.vm['network']:
|
|
|
|
network_name = network[0]
|
|
|
|
_ = network[1] # tap_mac
|
|
|
|
tap_id = network[2]
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
delete_network_interface('tap{}'.format(tap_id))
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
owners_vms = shared.vm_pool.by_owner(self.vm['owner'])
|
|
|
|
owners_running_vms = shared.vm_pool.by_status(VMStatus.running,
|
|
|
|
_vms=owners_vms)
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
networks = map(
|
|
|
|
lambda n: n[0], map(lambda vm: vm.network, owners_running_vms)
|
|
|
|
)
|
|
|
|
networks_in_use_by_user_vms = [vm[0] for vm in networks]
|
|
|
|
if network_name not in networks_in_use_by_user_vms:
|
|
|
|
network_entry = resolve_network(network[0], self.vm['owner'])
|
|
|
|
if network_entry:
|
|
|
|
network_type = network_entry.value["type"]
|
|
|
|
network_id = network_entry.value["id"]
|
|
|
|
if network_type == "vxlan":
|
|
|
|
delete_network_interface('br{}'.format(network_id))
|
|
|
|
delete_network_interface('vxlan{}'.format(network_id))
|
|
|
|
except Exception:
|
|
|
|
logger.exception("Exception in network interface deletion")
|
|
|
|
|
|
|
|
def create(self):
|
|
|
|
if shared.storage_handler.is_vm_image_exists(self.uuid):
|
2019-12-22 07:26:48 +00:00
|
|
|
# File Already exists. No Problem Continue
|
2019-12-28 10:39:11 +00:00
|
|
|
logger.debug("Image for vm %s exists", self.uuid)
|
2019-12-22 07:26:48 +00:00
|
|
|
else:
|
2019-12-28 10:39:11 +00:00
|
|
|
if shared.storage_handler.make_vm_image(src=self.vm['image_uuid'], dest=self.uuid):
|
|
|
|
if not shared.storage_handler.resize_vm_image(path=self.uuid,
|
|
|
|
size=int(self.vm['specs']['os-ssd'].to_MB())):
|
|
|
|
self.vm['status'] = VMStatus.error
|
2019-12-22 07:26:48 +00:00
|
|
|
else:
|
|
|
|
logger.info("New VM Created")
|
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
def sync(self):
|
|
|
|
shared.etcd_client.put(self.key, self.schema.dump(self.vm), value_in_json=True)
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
def delete(self):
|
|
|
|
self.stop()
|
2019-12-22 07:26:48 +00:00
|
|
|
|
2019-12-28 10:39:11 +00:00
|
|
|
if shared.storage_handler.is_vm_image_exists(self.uuid):
|
|
|
|
r_status = shared.storage_handler.delete_vm_image(self.uuid)
|
|
|
|
if r_status:
|
|
|
|
shared.etcd_client.client.delete(self.key)
|
2019-12-22 07:26:48 +00:00
|
|
|
else:
|
2019-12-28 10:39:11 +00:00
|
|
|
shared.etcd_client.client.delete(self.key)
|
2019-12-05 13:30:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def resolve_network(network_name, network_owner):
|
2019-12-28 10:39:11 +00:00
|
|
|
network = shared.etcd_client.get(
|
|
|
|
join_path(settings['etcd']['network_prefix'], network_owner, network_name), value_in_json=True
|
|
|
|
)
|
2019-12-05 13:30:41 +00:00
|
|
|
return network
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
def update_radvd_conf(all_networks):
|
2019-11-15 16:11:45 +00:00
|
|
|
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']
|
2019-12-22 07:26:48 +00:00
|
|
|
for net in all_networks
|
2019-12-26 07:24:19 +00:00
|
|
|
if net.value.get('ipv6') and ipaddress.ip_network(net.value.get('ipv6')).is_global
|
2019-11-18 17:39:57 +00:00
|
|
|
}
|
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)
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
content = [
|
|
|
|
radvd_template.safe_substitute(
|
|
|
|
bridge='br{}'.format(networks[net]),
|
|
|
|
prefix=net
|
|
|
|
)
|
|
|
|
for net in networks if networks.get(net)
|
|
|
|
]
|
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'])
|
2019-12-28 10:39:11 +00:00
|
|
|
except sp.CalledProcessError:
|
|
|
|
try:
|
|
|
|
sp.check_output(['service', 'radvd', 'restart'])
|
|
|
|
except sp.CalledProcessError as err:
|
|
|
|
raise err.__class__('Cannot start/restart radvd service', err.cmd) from err
|