def do_convert(args): volume_abs_path = args[1] raw_volume_abs_path = volume_abs_path + RAW_SUFFIX qcow2_volume_abs_path = volume_abs_path + QCOW2_SUFFIX check(volume_abs_path, raw_volume_abs_path) volume_size = lvm.get_lv_size(volume_abs_path) lvm.create_lv_from_absolute_path(raw_volume_abs_path, volume_size) with lvm.RecursiveOperateLv(volume_abs_path, shared=False, delete_when_exception=False): with lvm.OperateLv(raw_volume_abs_path, shared=False, delete_when_exception=False): lvm.add_lv_tag(volume_abs_path, START_TAG) r, o, e = qcow2_convert_to_raw(volume_abs_path, raw_volume_abs_path) if r != 0: logger.warn("convert failed: %s, removing tag and raw volume") lvm.clean_lv_tag(volume_abs_path, START_TAG) lvm.delete_lv(raw_volume_abs_path) return lvm.add_lv_tag(volume_abs_path, DONE_TAG) lvm.clean_lv_tag(volume_abs_path, START_TAG) lvm.lv_rename(volume_abs_path, qcow2_volume_abs_path, overwrite=False) lvm.lv_rename(raw_volume_abs_path, volume_abs_path, overwrite=False)
def create_template_from_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() volume_abs_path = translate_absolute_path_from_install_path(cmd.volumePath) install_abs_path = translate_absolute_path_from_install_path(cmd.installPath) if cmd.sharedVolume: lvm.do_active_lv(volume_abs_path, lvm.LvmlockdLockType.SHARE, True) lv_size = lvm.get_lv_size(volume_abs_path) with lvm.RecursiveOperateLv(volume_abs_path, shared=cmd.sharedVolume, skip_deactivate_tag=IMAGE_TAG): virtual_size = linux.qcow2_virtualsize(volume_abs_path) if not lvm.lv_exists(install_abs_path): lvm.create_lv_from_absolute_path(install_abs_path, lvm.getOriginalSize(lv_size), "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(install_abs_path, shared=False, delete_when_exception=True): linux.create_template(volume_abs_path, install_abs_path) logger.debug('successfully created template[%s] from volume[%s]' % (cmd.installPath, cmd.volumePath)) if cmd.compareQcow2 is True: logger.debug("comparing qcow2 between %s and %s") bash.bash_errorout("time qemu-img compare %s %s" % (volume_abs_path, install_abs_path)) logger.debug("confirmed qcow2 %s and %s are identical" % (volume_abs_path, install_abs_path)) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def convert_volume_provisioning(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = ConvertVolumeProvisioningRsp() if cmd.provisioningStrategy != "ThinProvisioning": raise NotImplementedError abs_path = translate_absolute_path_from_install_path(cmd.installPath) with lvm.RecursiveOperateLv(abs_path, shared=False): image_offest = long( bash.bash_o( "qemu-img check %s | grep 'Image end offset' | awk -F ': ' '{print $2}'" % abs_path).strip()) current_size = long(lvm.get_lv_size(abs_path)) virtual_size = linux.qcow2_virtualsize(abs_path) size = image_offest + cmd.addons[ lvm.thinProvisioningInitializeSize] if size > current_size: size = current_size if size > virtual_size: size = virtual_size lvm.resize_lv(abs_path, size, True) rsp.actualSize = size rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def create_template_from_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() volume_abs_path = translate_absolute_path_from_install_path(cmd.volumePath) install_abs_path = translate_absolute_path_from_install_path(cmd.installPath) if cmd.sharedVolume: lvm.do_active_lv(volume_abs_path, lvm.LvmlockdLockType.SHARE, True) with lvm.RecursiveOperateLv(volume_abs_path, shared=cmd.sharedVolume, skip_deactivate_tags=[IMAGE_TAG]): virtual_size = linux.qcow2_virtualsize(volume_abs_path) total_size = 0 for qcow2 in linux.qcow2_get_file_chain(volume_abs_path): total_size += int(lvm.get_lv_size(qcow2)) if total_size > virtual_size: total_size = virtual_size if not lvm.lv_exists(install_abs_path): lvm.create_lv_from_absolute_path(install_abs_path, total_size, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(install_abs_path, shared=False, delete_when_exception=True): linux.create_template(volume_abs_path, install_abs_path) logger.debug('successfully created template[%s] from volume[%s]' % (cmd.installPath, cmd.volumePath)) if cmd.compareQcow2 is True: logger.debug("comparing qcow2 between %s and %s") bash.bash_errorout("time qemu-img compare %s %s" % (volume_abs_path, install_abs_path)) logger.debug("confirmed qcow2 %s and %s are identical" % (volume_abs_path, install_abs_path)) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def migrate_volumes(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) current_abs_path = translate_absolute_path_from_install_path(struct.currentInstallPath) with lvm.OperateLv(current_abs_path, shared=True): virtual_size = lvm.get_lv_size(current_abs_path) if lvm.lv_exists(target_abs_path): target_ps_uuid = get_primary_storage_uuid_from_install_path(struct.targetInstallPath) raise Exception("found %s already exists on ps %s" % (target_abs_path, target_ps_uuid)) lvm.create_lv_from_absolute_path(target_abs_path, virtual_size, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) lvm.active_lv(target_abs_path, lvm.LvmlockdLockType.SHARE) try: for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) current_abs_path = translate_absolute_path_from_install_path(struct.currentInstallPath) with lvm.OperateLv(current_abs_path, shared=True): bash.bash_errorout("cp %s %s" % (current_abs_path, target_abs_path)) for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) current_abs_path = translate_absolute_path_from_install_path(struct.currentInstallPath) with lvm.RecursiveOperateLv(current_abs_path, shared=True): previous_ps_uuid = get_primary_storage_uuid_from_install_path(struct.currentInstallPath) target_ps_uuid = get_primary_storage_uuid_from_install_path(struct.targetInstallPath) current_backing_file = linux.qcow2_get_backing_file(current_abs_path) # type: str target_backing_file = current_backing_file.replace(previous_ps_uuid, target_ps_uuid) if current_backing_file is not None and current_backing_file != "": lvm.do_active_lv(target_backing_file, lvm.LvmlockdLockType.SHARE, False) logger.debug("rebase %s to %s" % (target_abs_path, target_backing_file)) linux.qcow2_rebase_no_check(target_backing_file, target_abs_path) if struct.compareQcow2: bash.bash_errorout("time qemu-img compare %s %s" % (current_abs_path, target_abs_path)) except Exception as e: for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) if struct.currentInstallPath == struct.targetInstallPath: logger.debug("current install path %s equals target %s, skip to delete" % (struct.currentInstallPath, struct.targetInstallPath)) else: logger.debug("error happened, delete lv %s" % target_abs_path) lvm.delete_lv(target_abs_path, False) raise e finally: for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) lvm.deactive_lv(target_abs_path) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def get_volume_size(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = GetVolumeSizeRsp() install_abs_path = translate_absolute_path_from_install_path(cmd.installPath) rsp.size = lvm.get_lv_size(install_abs_path) rsp.actualSize = rsp.size rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def get_volume_size(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = GetVolumeSizeRsp() install_abs_path = translate_absolute_path_from_install_path(cmd.installPath) with lvm.OperateLv(install_abs_path, shared=True): rsp.size = linux.qcow2_virtualsize(install_abs_path) rsp.actualSize = lvm.get_lv_size(install_abs_path) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def get_volume_size(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = GetVolumeSizeRsp() install_abs_path = get_absolute_path_from_install_path(cmd.installPath) r = drbd.DrbdResource(cmd.installPath.split("/")[-1]) with drbd.OperateDrbd(r): rsp.size = linux.qcow2_virtualsize(r.get_dev_path()) rsp.actualSize = lvm.get_lv_size(install_abs_path) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp._init_from_drbd(r) return jsonobject.dumps(rsp)
def _init_from_drbd(self, r): """ :type r: drbd.DrbdResource """ if not r.minor_allocated(): self.localNetworkStatus = drbd.DrbdNetState.Unconfigured return self.actualSize = lvm.get_lv_size(r.config.local_host.disk) self.resourceUuid = r.name self.localRole = r.get_role() self.localDiskStatus = r.get_dstate() self.remoteRole = r.get_remote_role() self.remoteDiskStatus = r.get_remote_dstate() self.localNetworkStatus = r.get_cstate()
def convert_volume_provisioning(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = ConvertVolumeProvisioningRsp() if cmd.provisioningStrategy != "ThinProvisioning": raise NotImplementedError abs_path = translate_absolute_path_from_install_path(cmd.installPath) with lvm.RecursiveOperateLv(abs_path, shared=False): image_offest = long( bash.bash_o("qemu-img check %s | grep 'Image end offset' | awk -F ': ' '{print $2}'" % abs_path).strip()) current_size = long(lvm.get_lv_size(abs_path)) virtual_size = linux.qcow2_virtualsize(abs_path) size = image_offest + cmd.addons[lvm.thinProvisioningInitializeSize] if size > current_size: size = current_size if size > virtual_size: size = virtual_size lvm.resize_lv(abs_path, size, True) rsp.actualSize = size rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def migrate_volumes(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) current_abs_path = translate_absolute_path_from_install_path(struct.currentInstallPath) with lvm.OperateLv(current_abs_path, shared=True): lv_size = lvm.get_lv_size(current_abs_path) if lvm.lv_exists(target_abs_path): target_ps_uuid = get_primary_storage_uuid_from_install_path(struct.targetInstallPath) raise Exception("found %s already exists on ps %s" % (target_abs_path, target_ps_uuid)) lvm.create_lv_from_absolute_path(target_abs_path, lvm.getOriginalSize(lv_size), "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) lvm.active_lv(target_abs_path, lvm.LvmlockdLockType.SHARE) try: for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) current_abs_path = translate_absolute_path_from_install_path(struct.currentInstallPath) with lvm.OperateLv(current_abs_path, shared=True): bash.bash_errorout("cp %s %s" % (current_abs_path, target_abs_path)) for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) current_abs_path = translate_absolute_path_from_install_path(struct.currentInstallPath) with lvm.RecursiveOperateLv(current_abs_path, shared=True): previous_ps_uuid = get_primary_storage_uuid_from_install_path(struct.currentInstallPath) target_ps_uuid = get_primary_storage_uuid_from_install_path(struct.targetInstallPath) current_backing_file = linux.qcow2_get_backing_file(current_abs_path) # type: str target_backing_file = current_backing_file.replace(previous_ps_uuid, target_ps_uuid) if struct.compareQcow2: logger.debug("comparing qcow2 between %s and %s" % (current_abs_path, target_abs_path)) if not self.compare(current_abs_path, target_abs_path): raise Exception("qcow2 %s and %s are not identical" % (current_abs_path, target_abs_path)) logger.debug("confirmed qcow2 %s and %s are identical" % (current_abs_path, target_abs_path)) if current_backing_file is not None and current_backing_file != "": lvm.do_active_lv(target_backing_file, lvm.LvmlockdLockType.SHARE, False) logger.debug("rebase %s to %s" % (target_abs_path, target_backing_file)) linux.qcow2_rebase_no_check(target_backing_file, target_abs_path) except Exception as e: for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) if struct.currentInstallPath == struct.targetInstallPath: logger.debug("current install path %s equals target %s, skip to delete" % (struct.currentInstallPath, struct.targetInstallPath)) else: logger.debug("error happened, delete lv %s" % target_abs_path) lvm.delete_lv(target_abs_path, False) raise e finally: for struct in cmd.migrateVolumeStructs: target_abs_path = translate_absolute_path_from_install_path(struct.targetInstallPath) lvm.deactive_lv(target_abs_path) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)