# Copyright 2015-2016 Intel Corporation. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Automation of QEMU hypervisor for launching guests. """ import os import logging import locale import re import subprocess import time import pexpect from conf import settings as S from conf import get_test_param from vnfs.vnf.vnf import IVnf class IVnfQemu(IVnf): """ Abstract class for controling an instance of QEMU """ _cmd = None _expect = None _proc_name = 'qemu' class GuestCommandFilter(logging.Filter): """ Filter out strings beginning with 'guestcmd :'. """ def filter(self, record): return record.getMessage().startswith(self.prefix) def __init__(self): """ Initialisation function. """ super(IVnfQemu, self).__init__() self._expect = S.getValue('GUEST_PROMPT_LOGIN')[self._number] self._logger = logging.getLogger(__name__) self._logfile = os.path.join( S.getValue('LOG_DIR'), S.getValue('LOG_FILE_QEMU')) + str(self._number) self._timeout = S.getValue('GUEST_TIMEOUT')[self._number] self._monitor = '%s/vm%dmonitor' % ('/tmp', self._number) # read GUEST NICs configuration and use only defined NR of NICS nics_nr = S.getValue('GUEST_NICS_NR')[self._number] # and inform user about missconfiguration if nics_nr < 1: raise RuntimeError('At least one VM NIC is mandotory, but {} ' 'NICs are configured'.format(nics_nr)) elif nics_nr > 1 and nics_nr % 2: nics_nr = int(nics_nr / 2) * 2 self._logger.warning('Odd number of NICs is configured, only ' '%s NICs will be used', nics_nr) self._nics = S.getValue('GUEST_NICS')[self._number][:nics_nr] # set guest loopback application based on VNF configuration # cli option take precedence to config file values self._guest_loopback = S.getValue('GUEST_LOOPBACK')[self._number] self._testpmd_fwd_mode = S.getValue('GUEST_TESTPMD_FWD_MODE')[self._number] # in case of SRIOV we must ensure, that MAC addresses are not swapped if S.getValue('SRIOV_ENABLED') and self._testpmd_fwd_mode.startswith('mac') and \ not S.getValue('VNF').endswith('PciPassthrough'): self._logger.info("SRIOV detected, forwarding mode of testpmd was changed from '%s' to '%s'", self._testpmd_fwd_mode, 'io') self._testpmd_fwd_mode = 'io' guest_smp = int(get_test_param('guest_smp', 0)) if guest_smp: override_list = [guest_smp] * (self._number + 1) S.setValue('GUEST_SMP', override_list) name = 'Client%d' % self._number vnc = ':%d' % self._number # NOTE: affinization of main qemu process can cause hangup of 2nd VM # in case of DPDK usage. It can also slow down VM response time. cpumask = ",".join(S.getValue('GUEST_CORE_BINDING')[self._number]) self._cmd = ['sudo', '-E', 'taskset', '-c', cpumask, S.getValue('TOOLS')['qemu-system'], '-m', S.getValue('GUEST_MEMORY')[self._number], '-smp', str(S.getValue('GUEST_SMP')[self._number]), '-cpu', 'host,migratable=off', '-drive', 'if={},file='.format(S.getValue( 'GUEST_BOOT_DRIVE_TYPE')[self._number]) + S.getValue('GUEST_IMAGE')[self._number], '-boot', 'c', '--enable-kvm', '-monitor', 'unix:%s,server,nowait' % self._monitor, '-object', 'memory-backend-file,id=mem,size=' + str(S.getValue('GUEST_MEMORY')[self._number]) + 'M,' + 'mem-path=' + S.getValue('HUGEPAGE_DIR') + ',share=on', '-numa', 'node,memdev=mem -mem-prealloc', '-nographic', '-vnc', str(vnc), '-name', name, '-snapshot', '-net none', '-no-reboot', '-drive', 'if=%s,format=raw,file=fat:rw:%s,snapshot=off' % (S.getValue('GUEST_SHARED_DRIVE_TYPE')[self._number], S.getValue('GUEST_SHARE_DIR')[self._number]), ] self._configure_logging() def _configure_logging(self): """ Configure logging. """ self.GuestCommandFilter.prefix = self._log_prefix logger = logging.getLogger() cmd_logger = logging.FileHandler( filename=os.path.join(S.getValue('LOG_DIR'), S.getValue('LOG_FILE_GUEST_CMDS')) + str(self._number)) cmd_logger.setLevel(logging.DEBUG) cmd_logger.addFilter(self.GuestCommandFilter()) logger.addHandler(cmd_logger) # startup/Shutdown def start(self): """ Start QEMU instance, login and prepare for commands. """ super(IVnfQemu, self).start() if S.getValue('VNF_AFFINITIZATION_ON'): self._affinitize() if S.getValue('VSWITCH_VHOST_NET_AFFINITIZATION') and S.getValue( 'VNF') == 'QemuVirtioNet': self._affinitize_vhost_net() if self._timeout: self._config_guest_loopback() def stop(self): """ Stops VNF instance gracefully first. """ if self.is_running(): try: # exit testpmd if needed if self._guest_loopback == 'testpmd': self.execute_and_wait('stop', 120, "Done") self.execute_and_wait('quit', 120, "[bB]ye") # turn off VM self.execute_and_wait('poweroff', 120, "Power down") except pexpect.TIMEOUT: self.kill() # wait until qemu shutdowns self._logger.debug('Wait for QEMU to terminate') for dummy in range(30): time.sleep(1) if not self.is_running(): break # just for case that graceful shutdown failed super(IVnfQemu, self).stop() # helper functions def _login(self, timeout=120): """ Login to QEMU instance. This can be used immediately after booting the machine, provided a sufficiently long ``timeout`` is given. :param timeout: Timeout to wait for login to complete. :returns: None """ # if no timeout was set, we likely started QEMU without waiting for it # to boot. This being the case, we best check that it has finished # first. if not self._timeout: self._expect_process(timeout=timeout) self._child.sendline(S.getValue('GUEST_USERNAME')[self._number]) self._child.expect(S.getValue('GUEST_PROMPT_PASSWORD')[self._number], timeout=5) self._child.sendline(S.getValue('GUEST_PASSWORD')[self._number]) self._expect_process(S.getValue('GUEST_PROMPT')[self._number], timeout=5) def send_and_pass(self, cmd, timeout=30): """ Send ``cmd`` and wait ``timeout`` seconds for it to pass. :param cmd: Command to send to guest. :param timeout: Time to wait for prompt before checking return code. :returns: None """ self.execute(cmd) self.wait(S.getValue('GUEST_PROMPT')[self._number], timeout=timeout) self.execute('echo $?') self._child.expect('^0$', timeout=1) # expect a 0 self.wait(S.getValue('GUEST_PROMPT')[self._number], timeout=timeout) def _affinitize(self): """ Affinitize the SMP cores of a QEMU instance. This is a bit of a hack. The 'socat' utility is used to interact with the QEM
#!/bin/bash
##############################################################################
# Copyright (c) 2016 HUAWEI TECHNOLOGIES CO.,LTD and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
compass_vm_dir=$WORK_DIR/vm/compass
rsa_file=$compass_vm_dir/boot.rsa
ssh_args="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $rsa_file"
function tear_down_compass() {
    sudo virsh destroy compass > /dev/null 2>&1
    sudo virsh undefine compass > /dev/null 2>&1

    sudo umount $compass_vm_dir/old > /dev/null 2>&1
    sudo umount $compass_vm_dir/new > /dev/null 2>&1

    sudo rm -rf $compass_vm_dir

    log_info "tear_down_compass success!!!"
}

function install_compass_core() {
    install_compass "compass_nodocker.yml"
}

function set_compass_machine() {
    local config_file=$WORK_DIR/installer/compass-install/install/group_vars/all

    sed -i -e '/test: true/d' -e '/pxe_boot_macs/d' $config_file
    echo "test: true" >> $config_file
    echo "pxe_boot_macs: [${machines}]" >> $config_file

    install_compass "compass_machine.yml"
}

function install_compass() {
    local inventory_file=$compass_vm_dir/inventory.file
    sed -i "s/mgmt_next_ip:.*/mgmt_next_ip: ${COMPASS_SERVER}/g" $WORK_DIR/installer/compass-install/install/group_vars/all
    echo "compass_nodocker ansible_ssh_host=$MGMT_IP ansible_ssh_port=22" > $inventory_file
    PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook -e pipeline=true --private-key=$rsa_file --user=root --connection=ssh --inventory-file=$inventory_file $WORK_DIR/installer/compass-install/install/$1
    exit_status=$?
    rm $inventory_file
    if [[ $exit_status != 0