def getMacAddress(network, name): global mac_dict if (network == PUBLIC_BRIDGE and os.path.exists(PUBLIC_MAC_FILE) and name in mac_dict): mac = mac_dict[name] print 'Use %s for nic connected to %s' % (mac, network) else: mac = randomMAC() return mac
def __init__(self, type, net_name, mac_address=None): if type not in Network.valid_types: raise domain.IllegalArgumentError(Network.valid_types, type) self.type = type self.net_name = net_name if mac_address is None: self.mac_address = randomMAC() else: self.mac_address = mac_address
def _generate_random_mac(self): if self.conn and not self._random_mac: found = False for ignore in range(256): self._random_mac = util.randomMAC(self.conn.getType().lower(), conn=self.conn) ret = self.is_conflict_net(self.conn, self._random_mac) if ret[1] is not None: continue found = True break if not found: logging.debug("Failed to generate non-conflicting MAC") return self._random_mac
def clone_vm(base_name, vm_name): image = VM_IMG % vm_name mac = randomMAC() cmd = ["virt-clone", "--connect", "qemu:///system", "-o", base_name, "-n", vm_name, "-f", image, "--preserve-data", "--mac", mac] run_cmd(cmd, sudo=True) libvirt_conn = libvirt.open("qemu:///system") base_image = get_vm_disks(base_name, libvirt_conn)['vda'] create_qcow2_image(image, base_image) guest_ip = update_network_map(vm_name, mac) update_guest(vm_name, mac) time.sleep(10) start_vm(vm_name, libvirt_conn) if 'engine' in base_name: time.sleep(20) setup_engine(vm_name, guest_ip)
#!/usr/bin/env python #author by limin from virtinst.util import randomMAC import commands for i in range(20): print randomMAC(type='qemu'),commands.getoutput('uuidgen')
def randomMAC(): return virtutils.randomMAC("qemu")
def createVm(self, name, cpu=1, memory=1048576, nics=[], hdd_format="qcow2", hdd_size="10G", type="kvm", vmCreateTmplFile=None, cdromIso="/dev/null"): logging.info("Creating domain with name: %s, cpu: %s, mem: %s, nics: %s, hdd_format: %s, hdd_size: %s, cdrom_iso: %s, tmplFile: %s", name, cpu, memory, nics, hdd_format, hdd_size, cdromIso, vmCreateTmplFile) if not vmCreateTmplFile: vmCreateTmplFile = self._vmCreateTmplFile params = {'name':name, 'cpu':cpu, 'memory':memory, 'nics':[], 'format':hdd_format, 'type':type, 'hdd_size':hdd_size, 'cdrom_iso': cdromIso} # Add extra parms to nics list for i, nic in enumerate(nics): mac = virtutils.randomMAC("qemu") targetdev = "vnet%s" % i slot = '0x%02x' % self.getNextSlot() param = {'network': nic, 'mac': mac, 'slot': slot, 'targetdev': targetdev} params['nics'].append(param) # Load template file with open(vmCreateTmplFile) as f: tmpl = Template(f.read()) libVirtTmpXmlFile = "%s_%s.xml" % (self._tmpLibVirtXmlFilePrefix, name) with open(libVirtTmpXmlFile, "w+") as f: f.write(tmpl.render(params)) # Delete any existing domain defs try: domain = self._connection.lookupByName(name) if domain: try: if domain.info()[0] == VIR_DOMAIN_RUNNING: domain.destroy() except Exception: pass domain.undefine() except Exception: pass # Define machine domain = None with open(libVirtTmpXmlFile) as f: domain = self._connection.defineXML(f.read()) # Create vhd call(['qemu-img', 'create', "/var/lib/libvirt/images/%s.%s" % (name, hdd_format), "-f", hdd_format, hdd_size]) # Start machine if domain: domain.create() logging.info("Successfully created domain: %s" % name) else: logging.error("Unable to create domain: %s" % name) # Return information about machine (eg: mac addresses) return params