1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 from vstf.common.utils import check_and_kill, randomMAC, my_mkdir, check_call, check_output, my_sleep
14 from vstf.agent.env.basic.vm9pfs import VMConfigBy9pfs
16 LOG = logging.getLogger(__name__)
19 class VMControlOperation(object):
21 a libivrt virsh wrapper for creating virtual machine.
26 all tmp files will be created under '/tmp/atf_vm_manager'
29 work_dir = '/tmp/atf_vm_manager'
30 shutil.rmtree(work_dir, ignore_errors=True)
32 self.work_dir = work_dir
36 self.vm_9p_controllers = {}
41 def composite_xml(context):
43 composit a libvirt xml configuration for creating vm from context.
45 :param context: a dict containing all necessary options for creating a vm.
46 :return: libvirt xml configuration string
48 from vm_xml_help import xml_head, xml_disk, xml_ovs, xml_pci, xml_9p, xml_tail, xml_ctrl_br, xml_br
50 tmp = xml_head.replace('VM_NAME', context['vm_name'])
51 tmp = tmp.replace('VM_MEMORY', str(context['vm_memory']))
52 tmp = tmp.replace('CPU_NUM', str(context['vm_cpu']))
54 tmp = xml_disk.replace('IMAGE_TYPE', context['image_type'])
55 tmp = tmp.replace('IMAGE_PATH', context['image_path'])
58 if context['9p_path']:
59 tmp = xml_9p.replace('9P_PATH', context['9p_path'])
62 if context['eth_pci']:
63 for pci in context['eth_pci']:
67 tmp = xml_pci.replace('BUS', bus)
68 tmp = tmp.replace('SLOT', slot)
69 tmp = tmp.replace('FUNCTION', func)
72 if context['ctrl_br']:
73 tmp = xml_ctrl_br.replace('CTRL_BR', context['ctrl_br'])
74 tmp = tmp.replace('CTRL_MAC', context['ctrl_mac'])
75 tmp = tmp.replace('CTRL_MODEL', context['ctrl_model'])
78 for tap_cfg in context['taps']:
79 if tap_cfg['br_type'] == "ovs":
80 br_type = "openvswitch"
82 br_type = tap_cfg['br_type']
83 if br_type == 'bridge':
85 tmp = xml_ovs.replace('BR_TYPE', br_type)
86 tmp = tmp.replace('TAP_MAC', tap_cfg['tap_mac'])
87 tmp = tmp.replace('TAP_NAME', tap_cfg['tap_name'])
88 tmp = tmp.replace('BR_NAME', tap_cfg['br_name'])
95 def check_required_options(context):
96 for key in ('vm_name', 'vm_memory', 'vm_cpu', 'image_path', 'image_type', 'taps'):
97 if not context.has_key(key):
98 raise Exception("vm config error, must set %s option" % key)
100 def set_vm_defaults(self, context):
101 vm_9p_path = '%s/%s' % (self.work_dir, context['vm_name'])
102 shutil.rmtree(vm_9p_path, ignore_errors=True)
104 default = {'vm_memory': 4194304,
106 'image_type': 'qcow2',
108 '9p_path': vm_9p_path,
111 'ctrl_mac': randomMAC(),
112 'ctrl_model': 'virtio',
113 'ctrl_ip_setting': '192.168.100.100/24',
114 'ctrl_gw': '192.168.100.1'
116 for k, v in default.items():
117 context.setdefault(k, v)
119 def _shutdown_vm(self):
120 out = check_output("virsh list | sed 1,2d | awk '{print $2}'", shell=True)
121 vm_set = set(out.split())
123 check_call("virsh shutdown %s" % vm, shell=True)
125 # wait for gracefully shutdown
127 out = check_output("virsh list | sed 1,2d | awk '{print $2}'", shell=True)
128 vm_set = set(out.split())
133 LOG.info("waiting for vms:%s to shutdown gracefully", vm_set)
136 check_call("virsh destroy %s" % vm, shell=True)
138 out = check_output("virsh list --all | sed 1,2d | awk '{print $2}'", shell=True)
139 vm_set = set(out.split())
141 check_call("virsh undefine %s" % vm, shell=True)
143 check_and_kill('qemu-system-x86_64')
145 def clean_all_vms(self):
147 for _, ctrl in self.vm_9p_controllers.items():
148 LOG.debug("remove vm9pfs dir:%s", ctrl.vm_9p_path)
149 shutil.rmtree(ctrl.vm_9p_path, ignore_errors=True)
150 self.vm_9p_controllers = {}
152 # shutil.rmtree(self.work_dir, ignore_errors=True)
159 def create_vm(self, context):
160 self.set_vm_defaults(context)
161 self.check_required_options(context)
162 xml = self.composite_xml(context)
163 vm_name = context['vm_name']
164 file_name = os.path.join(self.work_dir, vm_name + '.xml')
165 with open(file_name, 'w') as f:
167 check_call('virsh define %s' % file_name, shell=True)
168 check_call('virsh start %s' % vm_name, shell=True)
169 vm_name = context['vm_name']
170 vm_9pfs = context['9p_path']
171 self.vm_9p_controllers[vm_name] = VMConfigBy9pfs(vm_9pfs)
172 self.vm_configs[vm_name] = context
173 LOG.debug("%s's vm_9pfs path:%s", vm_name, vm_9pfs)
176 def wait_vm(self, vm_name):
177 vm9pctrl = self.vm_9p_controllers[vm_name]
178 ret = vm9pctrl.wait_up()
179 if ret not in (True,):
180 raise Exception('vm running but stuck in boot process, please manully check.')
181 LOG.debug('waitVM %s up ok, ret:%s', vm_name, ret)
184 def init_config_vm(self, vm_name):
186 using libvirt 9pfs to config boot up options like network ip/gw.
188 :param vm_name: the vm to be config with.
189 :return: True if succeed, Exception if fail.
191 vm_cfg = self.vm_configs[vm_name]
192 vm9pctrl = self.vm_9p_controllers[vm_name]
193 # print self.vm_9p_controllers
194 init_cfg = vm_cfg['init_config']
195 if "ctrl_ip_setting" in init_cfg:
196 ret = vm9pctrl.config_ip(vm_cfg['ctrl_mac'], init_cfg['ctrl_ip_setting'])
198 LOG.info('initConfigVM config ip ok')
199 if 'ctrl_gw' in init_cfg:
200 ret = vm9pctrl.config_gw(init_cfg['ctrl_gw'])
202 LOG.info('initConfigVM ctrl_gw ok')
203 if "ctrl_ip_setting" in init_cfg and "amqp_server" in init_cfg:
204 identity = init_cfg['ctrl_ip_setting'].split('/')[0]
205 if init_cfg['amqp_id'].strip():
206 identity = init_cfg['amqp_id'].strip()
207 server = init_cfg['amqp_server']
208 port = init_cfg['amqp_port']
209 user = init_cfg['amqp_user']
210 passwd = init_cfg['amqp_passwd']
211 ret = vm9pctrl.config_amqp(identity, server, port, user, passwd)
213 LOG.info('initConfigVM config_amqp ok')
214 if 'tap_pktloop_config' in init_cfg:
215 taps = vm_cfg['taps']
218 macs.append(tap['tap_mac'])
219 ret = vm9pctrl.set_pktloop_dpdk(macs)
221 LOG.info('initConfigVM set_pktloop_dpdk ok')