def create(self, uuid_start=None, get_mac=False): """Create initial UUID object; return True.""" if uuid_start: self.next_uuid_start = uuid_start if not common.is_number(self.next_uuid_start): msg = 'next_uuid_start must be an integer' raise error.InputError(msg) # Now we know it's an integer, make it a string for LDAP's sake self.next_uuid_start = str(self.next_uuid_start) dn = self.next_uuid_dn dn_info = [] if not self.next_uuid_class in self.next_uuid_classes: dn_info.append((0, 'objectClass', self.next_uuid_class)) if not self.next_uuid_attr in self.next_uuid_attrs: dn_info.append((0, self.next_uuid_attr, self.next_uuid_start)) if dn_info == []: msg = 'UUID entry already set on %s.' % dn raise error.AlreadyExists(msg) self.log.debug('Adding %s to %s ' % (dn_info, dn)) result = self._create_object(dn, dn_info) uuid = int(result['data'][0][1][self.next_uuid_attr][0]) mac = common.mac_from_uuid(uuid, 0) if get_mac: result['data'] = (uuid, mac) else: result['data'] = [uuid] result['msg'] = 'Created UUID:' self.log.debug('Result: %s' % result) return result
def modify(self, increment=1, get_mac=False): """Increment initial UUID object; return list of UUIDs.""" if not common.is_number(increment): msg = 'increment input must be an Integer' raise error.InputError, msg dn = self.next_uuid_dn filter = '%s=*' % self.next_uuid_attr #MMB changed attrs_only to unique result = self._get_object(dn, 0, filter, self.retrieve_attr, unique=True) if result['data'] == []: msg = "Cannot locate a UUID; maybe you need to run create?" raise error.NotFound(msg) uuid = int(result['data'][0][1][self.next_uuid_attr][0]) new_uuid = int(uuid) + int(increment) old_attrs = result['data'][0][1] new_attrs = old_attrs.copy() new_attrs[self.next_uuid_attr] = [str(new_uuid)] self.log.debug('Modifying %s from %s to %s' % (dn, old_attrs, new_attrs)) try: result = self._modify_attributes(dn, new_attrs, old_attrs) except: msg = 'Update of next free UUID to %d failed' % new_uuid raise error.IncrementError, msg mac = [] for item in range(uuid, new_uuid): mac.append(common.mac_from_uuid(item,0)) if get_mac: result['data'] = (range(uuid, (new_uuid)), mac) else: result['data'] = range(uuid, (new_uuid)) result['msg'] = "Reserved UUIDs: " + str(result['data']) self.log.debug('Result: %s' % result) return result
def create(self, uuid_start=None, get_mac=False): """Create initial UUID object; return True.""" self.log.debug('Running UUID create with get_mac set to %s' % get_mac) if uuid_start: self.next_uuid_start = uuid_start if not common.is_number(self.next_uuid_start): msg = 'next_uuid_start must be an integer' raise error.InputError(msg) # Now we know it's an integer, make it a string for LDAP's sake self.next_uuid_start = str(self.next_uuid_start) dn = self.next_uuid_dn dn_info = [] if not self.next_uuid_class in self.next_uuid_classes: dn_info.append((0, 'objectClass', self.next_uuid_class)) if not self.next_uuid_attr in self.next_uuid_attrs: dn_info.append((0, self.next_uuid_attr, self.next_uuid_start)) if dn_info == []: msg = 'UUID entry already set on %s.' % dn raise error.AlreadyExists(msg) self.log.debug('Adding %s to %s ' % (dn_info, dn)) result = self._create_object(dn, dn_info) uuid = int(result['data'][0][1][self.next_uuid_attr][0]) mac = common.mac_from_uuid(uuid, 0) if get_mac: result['data'] = (uuid, mac) else: result['data'] = [uuid] result['msg'] = 'Created UUID:' self.log.debug('Result: %s' % result) return result
def get(self, get_mac=False): """Retrieve the next free UUID object; return UUID as integer.""" dn = self.next_uuid_dn filter = '%s=*' % self.next_uuid_attr msg = 'Searching at %s with scope %s and filter %s' % \ (dn, self.search_scope, filter) self.log.debug(msg) result = self._get_object(dn, self.search_scope, filter, self.retrieve_attr) if result['data'] == []: msg = "Cannot locate a UUID; maybe you need to run create?" raise error.NotFound(msg) uuid = int(result['data'][0][1][self.next_uuid_attr][0]) if get_mac is True: mac = common.mac_from_uuid(uuid, 0) result['data'] = (uuid, mac) else: result['data'] = [uuid] result['msg'] = 'Found UUID:' self.log.debug('Result: %s' % result) return result
def modify(self, increment=1, get_mac=False): """Increment initial UUID object; return list of UUIDs.""" if not common.is_number(increment): msg = 'increment input must be an Integer' raise error.InputError, msg dn = self.next_uuid_dn filter = '%s=*' % self.next_uuid_attr #MMB changed attrs_only to unique result = self._get_object(dn, 0, filter, self.retrieve_attr, unique=True) if result['data'] == []: msg = "Cannot locate a UUID; maybe you need to run create?" raise error.NotFound(msg) uuid = int(result['data'][0][1][self.next_uuid_attr][0]) new_uuid = int(uuid) + int(increment) old_attrs = result['data'][0][1] new_attrs = old_attrs.copy() new_attrs[self.next_uuid_attr] = [str(new_uuid)] self.log.debug('Modifying %s from %s to %s' % (dn, old_attrs, new_attrs)) try: result = self._modify_attributes(dn, new_attrs, old_attrs) except: msg = 'Update of next free UUID to %d failed' % new_uuid raise error.IncrementError, msg mac = [] for item in range(uuid, new_uuid): mac.append(common.mac_from_uuid(item, 0)) if get_mac: result['data'] = (range(uuid, (new_uuid)), mac) else: result['data'] = range(uuid, (new_uuid)) result['msg'] = "Reserved UUIDs: " + str(result['data']) self.log.debug('Result: %s' % result) return result
def create(self, vm_name, vm_uuid, vm_mem, vm_cpu, vm_family, vm_storage_layout, vm_network_layout, vm_install=False, vm_disks=None, vm_interfaces=None): """Define a new VM and add to hypervisor's store (does not start).""" try: vm_name = common.validate_hostname(vm_name) vm_cpu = common.validate_cpu(vm_cpu) vm_mem = common.validate_mem(vm_mem) #vm_type = common.validate_host_type(vm_type) vm_family = common.validate_host_family(vm_family) #vm_extra_opts = common.is_shell_safe(vm_extra_opts) vm_uuid = common.validate_uuid(vm_uuid) except error.InputError as e: self.log.error(e) raise e try: self.conn.lookupByName(vm_name) except libvirt.libvirtError: pass else: msg = "Domain %s already exists, cannot create." % vm_name raise error.AlreadyExists(msg) # Create a UUID in hypervisor format formatted_uuid = self._format_uuid(vm_uuid) #-1 means XEN will give the right XenID when it starts vm_id=-1 #Initial xml structure doc = xml.createDoc("doc") domain = xml.createElement(doc, "domain", {"type": vm_family}) #Variable config options #xml.createChild(doc, domain, "arch name", None, 'i686') xml.createChild(doc, domain, "name", None, vm_name) xml.createChild(doc, domain, "memory", None, vm_mem) xml.createChild(doc, domain, "currentMemory", None, vm_mem) xml.createChild(doc, domain, "vcpu", None, vm_cpu) xml.createChild(doc, domain, "uuid", None, formatted_uuid) #fixed(ish) config options os = xml.createChild(doc, domain, "os", None, None) #ks - the below is going to have to change for 64bit xml.createChild(doc, os, "type", {"arch": "i686"}, "hvm") xml.createChild(doc, domain, "clock", {"offset": "utc"}, None) xml.createChild(doc, domain, "on_poweroff", None, "destroy") xml.createChild(doc, domain, "on_reboot", None, "restart") xml.createChild(doc, domain, "on_crash", None, "restart") devices = xml.createChild(doc, domain, "devices", None, None) console = xml.createChild(doc, devices, "console", {"type": "pty"}, None) xml.createChild(doc, console, "target", {"type": "xen", "port": "0"}, None) #ks #xml.createChild(doc, devices, "input", {"type": "mouse", "bus": "xen"}, None) # TODO Change the port such that it is associated with the UUID and change listen to internal interface only xml.createChild(doc, devices, "graphics", {"type": "vnc", "port": "-1", "autoport": "yes", "listen": "0.0.0.0"}, None) xml.createChild(doc, devices, "emulator", None, "/usr/lib/xen/bin/qemu-dm") # #parse disk info # for item in vm_disks: # #local1 means hda and vg01 # if item[0] == "local1": # disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None) # xml.createChild(doc, disk, "driver", {"name": "phy"}, None) # xml.createChild(doc, disk, "source", {"dev": "/dev/vg01/%s" % vm_name}, None) # xml.createChild(doc, disk, "target", {"dev": "hda", "bus": "ide"}, None) # #local2 means hdb and vg02 # if item[0] == "local2": # disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None) # xml.createChild(doc, disk, "driver", {"name": "phy"}, None) # xml.createChild(doc, disk, "source", {"dev": "/dev/vg01/ko-test-02"}, None) # xml.createChild(doc, disk, "target", {"dev": "hdb", "bus": "ide"}, None) if vm_disks is not None: for item in vm_disks: if item[0] == "local1": disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None) xml.createChild(doc, disk, "driver", {"name": "phy"}, None) xml.createChild(doc, disk, "source", {"dev": "/dev/vg01/%s" % vm_name}, None) xml.createChild(doc, disk, "target", {"dev": "hda", "bus": "ide"}, None) #local2 means hdb and vg02 if item[0] == "local2": disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None) xml.createChild(doc, disk, "driver", {"name": "phy"}, None) xml.createChild(doc, disk, "source", {"dev": "/dev/vg02/%s" % vm_name}, None) xml.createChild(doc, disk, "target", {"dev": "hdb", "bus": "ide"}, None) elif vm_storage_layout is not None: try: disks = self.dtypes[vm_storage_layout] except KeyError as e: msg = "The disk type %s is not present in config file." % e raise error.InputError, msg for item in disks: item = common.validate_disks_in_conf(self.dnames[item]) hv_dev = item[0] + "/" + vm_name dom_dev = item[1] disk = xml.createChild(doc, devices, "disk", {"type": "block", "device": "disk"}, None) xml.createChild(doc, disk, "driver", {"name": "phy"}, None) xml.createChild(doc, disk, "source", {"dev": hv_dev}, None) xml.createChild(doc, disk, "target", {"dev": dom_dev, "bus": "ide"}, None) #parse interface info if vm_interfaces is not None: for interface in vm_interfaces: #get input from interface list bridge = int( interface[0].lstrip('breth') ) mac = interface[1] source_interface = interface[2] interface = xml.createChild(doc, devices, "interface", {"type": "bridge"}, None) xml.createChild(doc, interface, "mac", {"address": mac}, None) xml.createChild(doc, interface, "source", {"bridge": source_interface}, None) xml.createChild(doc, interface, "script", {"path": "vif-bridge"}, None) xml.createChild(doc, interface, "target", {"dev": "vif%i.%i" % (vm_id, bridge)}, None) elif vm_network_layout is not None: try: interfaces = self.ifacedef[vm_network_layout] except KeyError: msg = "The interface type %s is not present in config file." % vm_network_layout raise error.InputError(msg) # Ensure that br0,x is done first as xen cares about order in xml. interfaces = sorted(interfaces, key=itemgetter(0)) for interface in interfaces: interface = common.validate_interfaces_in_conf(interface) iface_number = int( interface[0].lstrip('breth') ) if iface_number == 0: boot_mac = common.mac_from_uuid(vm_uuid, iface_number) boot_int = interface[1] mac = common.mac_from_uuid(vm_uuid, iface_number) source_interface = interface[1] # KS enumerate avail interfaces via facter, not remote socket op #if not source_interface in self._all_interfaces(): # msg = "%s does not exist on this machine so we cant bridge to it!" % source_interface # raise error.InsufficientResource, msg interface = xml.createChild(doc, devices, "interface", {"type": "bridge"}, None) xml.createChild(doc, interface, "mac", {"address": mac}, None) xml.createChild(doc, interface, "source", {"bridge": source_interface}, None) xml.createChild(doc, interface, "script", {"path": "vif-bridge"}, None) xml.createChild(doc, interface, "target", {"dev": "vif%i.%i" % (vm_id, iface_number)}, None) if vm_install: # Add network boot lines xml.createChild(doc, domain, "bootloader", None, "/usr/sbin/pypxeboot" ) try: xml.createChild(doc, domain, "bootloader_args", None, "--udhcpc=/usr/local/pkg/udhcp/sbin/udhcpc --interface=%s mac=%s --label=install-aethernet" % (boot_int, boot_mac) ) except UnboundLocalError: msg = "In config there must be an interface br0 as the provisioning interface!" raise error.ConfigError(msg) else: xml.createChild(doc, domain, "bootloader", None, "/usr/bin/pygrub" ) try: out = self.conn.defineXML(xml.out(doc)) except Exception, e: trace = traceback.format_exc() raise error.LibvirtError(e, trace)