1 ##############################################################################
2 # Copyright (c) 2017 Tim Rozet (trozet@redhat.com) 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 ##############################################################################
16 MAX_NUM_MACS = math.trunc(0xff / 2)
19 def generate_baremetal_macs(count=1):
20 """Generate an Ethernet MAC address suitable for baremetal testing."""
21 # NOTE(dprince): We generate our own bare metal MAC address's here
22 # instead of relying on libvirt so that we can ensure the
23 # locally administered bit is set low. (The libvirt default is
24 # to set the 2nd MSB high.) This effectively allows our
25 # fake baremetal VMs to more accurately behave like real hardware
26 # and fixes issues with bridge/DHCP configurations which rely
27 # on the fact that bridges assume the MAC address of the lowest
29 # MACs generated for a given machine will also be in sequential
30 # order, which matches how most BM machines are laid out as well.
31 # Additionally we increment each MAC by two places.
34 if count > MAX_NUM_MACS:
35 raise ValueError("The MAX num of MACS supported is %i." % MAX_NUM_MACS)
38 random.randint(0x00, 0xff),
39 random.randint(0x00, 0xff),
40 random.randint(0x00, 0xff),
41 random.randint(0x00, 0xff)]
42 base_mac = ':'.join(map(lambda x: "%02x" % x, base_nums))
44 start = random.randint(0x00, 0xff)
45 if (start + (count * 2)) > 0xff:
46 # leave room to generate macs in sequence
47 start = 0xff - count * 2
48 for num in range(0, count * 2, 2):
50 macs.append(base_mac + ":" + ("%02x" % mac))
54 def create_vm_storage(domain, vol_path='/var/lib/libvirt/images'):
55 volume_name = domain + '.qcow2'
59 <allocation>0</allocation>
60 <capacity unit="G">41</capacity>
62 <format type='qcow2'/>
68 <label>virt_image_t</label>
71 </volume>""".format(volume_name, os.path.join(vol_path, volume_name))
73 conn = libvirt.open('qemu:///system')
74 pool = conn.storagePoolLookupByName('default')
76 raise Exception("Default libvirt storage pool missing")
77 # TODO(trozet) create default storage pool
79 if pool.isActive() == 0:
82 vol = pool.storageVolLookupByName(volume_name)
85 except libvirt.libvirtError as e:
86 if e.get_error_code() != libvirt.VIR_ERR_NO_STORAGE_VOL:
88 new_vol = pool.createXML(stgvol_xml)
90 raise Exception("Unable to create new volume")
91 logging.debug("Created new storage volume: {}".format(volume_name))
94 def create_vm(name, image, diskbus='sata', baremetal_interfaces=['admin'],
95 arch='x86_64', engine='kvm', memory=8192, bootdev='network',
96 cpus=4, nic_driver='virtio', macs=[], direct_boot=None,
97 kernel_args=None, default_network=False,
98 template_dir='/usr/share/opnfv-apex'):
99 # TODO(trozet): fix name here to be image since it is full path of qcow2
100 create_vm_storage(name)
101 with open(os.path.join(template_dir, 'domain.xml'), 'r') as f:
102 source_template = f.read()
103 imagefile = os.path.realpath(image)
104 memory = int(memory) * 1024
107 'imagefile': imagefile,
110 'memory': str(memory),
114 'enable_serial_console': '',
117 'user_interface': '',
120 # Configure the bus type for the target disk device
121 params['diskbus'] = diskbus
123 'nicdriver': nic_driver,
126 params['network'] = """
127 <!-- regular natted network, for access to the vm -->
128 <interface type='network'>
129 <source network='default'/>
130 <model type='%(nicdriver)s'/>
131 </interface>""" % nicparams
133 params['network'] = ''
134 while len(macs) < len(baremetal_interfaces):
135 macs += generate_baremetal_macs(1)
137 params['bm_network'] = ""
138 for bm_interface, mac in zip(baremetal_interfaces, macs):
139 bm_interface_params = {
140 'bminterface': bm_interface,
142 'nicdriver': nic_driver,
144 params['bm_network'] += """
145 <!-- bridged 'bare metal' network on %(bminterface)s -->
146 <interface type='network'>
147 <mac address='%(bmmacaddress)s'/>
148 <source network='%(bminterface)s'/>
149 <model type='%(nicdriver)s'/>
150 </interface>""" % bm_interface_params
152 params['enable_serial_console'] = """
157 <target type='serial' port='0'/>
161 params['direct_boot'] = """
162 <kernel>/var/lib/libvirt/images/%(direct_boot)s.vmlinuz</kernel>
163 <initrd>/var/lib/libvirt/images/%(direct_boot)s.initrd</initrd>
164 """ % {'direct_boot': direct_boot}
166 params['kernel_args'] = """
167 <cmdline>%s</cmdline>
168 """ % ' '.join(kernel_args)
170 if arch == 'aarch64':
172 params['direct_boot'] += """
173 <loader readonly='yes' \
174 type='pflash'>/usr/share/AAVMF/AAVMF_CODE.fd</loader>
175 <nvram>/var/lib/libvirt/qemu/nvram/centos7.0_VARS.fd</nvram>
177 params['user_interface'] = """
178 <controller type='virtio-serial' index='0'>
179 <address type='virtio-mmio'/>
185 <target type='serial' port='0'/>
187 <channel type='unix'>
188 <target type='virtio' name='org.qemu.guest_agent.0'/>
189 <address type='virtio-serial' controller='0' bus='0' port='1'/>
193 params['user_interface'] = """
194 <input type='mouse' bus='ps2'/>
195 <graphics type='vnc' port='-1' autoport='yes'/>
197 <model type='cirrus' vram='9216' heads='1'/>
201 libvirt_template = source_template % params
202 logging.debug("libvirt template is {}".format(libvirt_template))
203 conn = libvirt.open('qemu:///system')
204 vm = conn.defineXML(libvirt_template)
205 logging.info("Created machine %s with UUID %s" % (name, vm.UUIDString()))