def _prepare_disks(vm_settings, target_dir): """ Prepare VM disks for OVF appliance creation. File based disks will be copied to VM creation directory. LVM and block-device based disks will be converted into file based images and copied to creation directory. @param target_dir: directory where disks will be copied """ disk_list_dom = get_libvirt_conf_xml(vm_settings["vm_name"])\ .getElementsByTagName("domain")[0].getElementsByTagName("disk") disk_num, disk_list = 0, [] for disk_dom in disk_list_dom: if disk_dom.getAttribute("device") == "disk": disk_num += 1 source_dom = disk_dom.getElementsByTagName("source")[0] filename = "%s%d.img" % (vm_settings["template_name"], disk_num) new_path = path.join(target_dir, filename) if disk_dom.getAttribute("type") == "file": disk_path = source_dom.getAttribute("file") shutil.copy2(disk_path, new_path) elif disk_dom.getAttribute("type") == "block": source_dev = source_dom.getAttribute("dev") execute("qemu-img convert -f raw -O qcow2 %s %s" % (source_dev, new_path)) disk_dict = { "file_size": str(get_file_size_bytes(new_path)), "filename": filename, "new_path": new_path, "file_id": "diskfile%d" % (disk_num), "disk_id": "vmdisk%d.img" % (disk_num), "disk_capacity": str(get_kvm_disk_capacity_bytes(new_path)) } disk_list.append(disk_dict) return disk_list
def _generate_ovf_file(vm_settings, ct_archive_fnm): ovf = OvfFile() # Workaround for broken OvfFile.__init__ ovf.files = [] ovf.createEnvelope() instanceId = 0 virtualSystem = ovf.createVirtualSystem(ident=vm_settings["template_name"], info="OpenVZ OpenNode template") # add OS section ovf.createOperatingSystem(node=virtualSystem, ident='operating_system', info='Operating system type deployed in a template', description=vm_settings.get('ostemplate', 'linux')) hardwareSection = ovf.createVirtualHardwareSection(node=virtualSystem, ident="virtual_hardware", info="Virtual hardware requirements for a virtual machine") ovf.createSystem(hardwareSection, "Virtual Hardware Family", str(instanceId), {"VirtualSystemType": "openvz"}) instanceId += 1 # add cpu section for bound, cpu in zip(["normal", "min", "max"], [vm_settings.get("vcpu%s" % pfx) for pfx in ["", "_min", "_max"]]): if cpu: ovf.addResourceItem(hardwareSection, { "Caption": "%s virtual CPU" % cpu, "Description": "Number of virtual CPUs", "ElementName": "%s virtual CPU" % cpu, "InstanceID": str(instanceId), "ResourceType": "3", "VirtualQuantity": cpu }, bound=bound) instanceId += 1 # add memory section for bound, memory in zip(["normal", "min", "max"], [vm_settings.get("memory%s" % pfx) for pfx in ["", "_min", "_max"]]): if memory: ovf.addResourceItem(hardwareSection, { "AllocationUnits": "GigaBytes", "Caption": "%s GB of memory" % memory, "Description": "Memory Size", "ElementName": "%s GB of memory" % memory, "InstanceID": str(instanceId), "ResourceType": "4", "VirtualQuantity": memory }, bound=bound) instanceId += 1 def get_checksum(fnm): # calculate checksum for the file chunk_size = 1024 ** 2 # 1Mb sha = sha1() with open(fnm) as chkfile: while 1: data = chkfile.read(chunk_size) if not data: break sha.update(data) return sha.hexdigest() # add reference a file (see http://gitorious.org/open-ovf/mainline/blobs/master/py/ovf/OvfReferencedFile.py) ref_file = OvfReferencedFile(path.dirname(ct_archive_fnm), path.basename("%s.tar.gz" % vm_settings["template_name"]), file_id="diskfile1", size=str(get_file_size_bytes(ct_archive_fnm)), compression="gz", checksum=get_checksum(ct_archive_fnm)) ovf.addReferencedFile(ref_file) ovf.createReferences() def get_ct_disk_usage_bytes(ctid): return str(int(execute("du -s /vz/private/%s/" % ctid).split()[0]) * 1024) # add disk section ovf.createDiskSection([{ "diskId": "vmdisk1", "capacity": str(round(float(vm_settings["disk"]) * 1024 ** 3)), # in bytes "capacityAllocUnits": None, # bytes default "populatedSize": get_ct_disk_usage_bytes(vm_settings["vm_name"]), "fileRef": "diskfile1", "parentRef": None, "format": "tar.gz"}], "OpenVZ CT template disks") return ovf
def _generate_ovf_file(vm_settings, ct_archive_fnm): ovf = OvfFile() # Workaround for broken OvfFile.__init__ ovf.files = [] ovf.createEnvelope() instanceId = 0 virtualSystem = ovf.createVirtualSystem(ident=vm_settings["template_name"], info="OpenVZ OpenNode template") # add OS section ovf.createOperatingSystem( node=virtualSystem, ident='operating_system', info='Operating system type deployed in a template', description=vm_settings.get('ostemplate', 'linux')) hardwareSection = ovf.createVirtualHardwareSection( node=virtualSystem, ident="virtual_hardware", info="Virtual hardware requirements for a virtual machine") ovf.createSystem(hardwareSection, "Virtual Hardware Family", str(instanceId), {"VirtualSystemType": "openvz"}) instanceId += 1 # add cpu section for bound, cpu in zip( ["normal", "min", "max"], [vm_settings.get("vcpu%s" % pfx) for pfx in ["", "_min", "_max"]]): if cpu: ovf.addResourceItem(hardwareSection, { "Caption": "%s virtual CPU" % cpu, "Description": "Number of virtual CPUs", "ElementName": "%s virtual CPU" % cpu, "InstanceID": str(instanceId), "ResourceType": "3", "VirtualQuantity": cpu }, bound=bound) instanceId += 1 # add memory section for bound, memory in zip( ["normal", "min", "max"], [vm_settings.get("memory%s" % pfx) for pfx in ["", "_min", "_max"]]): if memory: ovf.addResourceItem(hardwareSection, { "AllocationUnits": "GigaBytes", "Caption": "%s GB of memory" % memory, "Description": "Memory Size", "ElementName": "%s GB of memory" % memory, "InstanceID": str(instanceId), "ResourceType": "4", "VirtualQuantity": memory }, bound=bound) instanceId += 1 def get_checksum(fnm): # calculate checksum for the file chunk_size = 1024**2 # 1Mb sha = sha1() with open(fnm) as chkfile: while 1: data = chkfile.read(chunk_size) if not data: break sha.update(data) return sha.hexdigest() # add reference a file (see http://gitorious.org/open-ovf/mainline/blobs/master/py/ovf/OvfReferencedFile.py) ref_file = OvfReferencedFile(path.dirname(ct_archive_fnm), path.basename("%s.tar.gz" % vm_settings["template_name"]), file_id="diskfile1", size=str(get_file_size_bytes(ct_archive_fnm)), compression="gz", checksum=get_checksum(ct_archive_fnm)) ovf.addReferencedFile(ref_file) ovf.createReferences() def get_ct_disk_usage_bytes(ctid): return str( int(execute("du -s /vz/private/%s/" % ctid).split()[0]) * 1024) # add disk section ovf.createDiskSection( [{ "diskId": "vmdisk1", "capacity": str(round( float(vm_settings["disk"]) * 1024**3)), # in bytes "capacityAllocUnits": None, # bytes default "populatedSize": get_ct_disk_usage_bytes(vm_settings["vm_name"]), "fileRef": "diskfile1", "parentRef": None, "format": "tar.gz" }], "OpenVZ CT template disks") return ovf