def revert_volume_from_snapshot(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = RevertVolumeFromSnapshotRsp() snapshot_abs_path = translate_absolute_path_from_install_path( cmd.snapshotInstallPath) qcow2_options = self.calc_qcow2_option(self, cmd.qcow2Options, True, cmd.provisioning) new_volume_path = cmd.installPath if new_volume_path is None or new_volume_path == "": new_volume_path = "/dev/%s/%s" % (cmd.vgUuid, uuidhelper.uuid()) else: new_volume_path = translate_absolute_path_from_install_path( new_volume_path) with lvm.RecursiveOperateLv(snapshot_abs_path, shared=True): size = linux.qcow2_virtualsize(snapshot_abs_path) lvm.create_lv_from_cmd( new_volume_path, size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(new_volume_path, shared=False, delete_when_exception=True): linux.qcow2_clone_with_option(snapshot_abs_path, new_volume_path, qcow2_options) size = linux.qcow2_virtualsize(new_volume_path) rsp.newVolumeInstallPath = new_volume_path rsp.size = size return jsonobject.dumps(rsp)
def create_empty_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() install_abs_path = translate_absolute_path_from_install_path(cmd.installPath) if cmd.backingFile: qcow2_options = self.calc_qcow2_option(self, cmd.qcow2Options, True, cmd.provisioning) backing_abs_path = translate_absolute_path_from_install_path(cmd.backingFile) with lvm.RecursiveOperateLv(backing_abs_path, shared=True): virtual_size = linux.qcow2_virtualsize(backing_abs_path) if not lvm.lv_exists(install_abs_path): lvm.create_lv_from_cmd(install_abs_path, virtual_size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(install_abs_path, shared=False, delete_when_exception=True): linux.qcow2_create_with_backing_file_and_option(backing_abs_path, install_abs_path, qcow2_options) elif not lvm.lv_exists(install_abs_path): lvm.create_lv_from_cmd(install_abs_path, cmd.size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) if cmd.volumeFormat != 'raw': qcow2_options = self.calc_qcow2_option(self, cmd.qcow2Options, False, cmd.provisioning) with lvm.OperateLv(install_abs_path, shared=False, delete_when_exception=True): linux.qcow2_create_with_option(install_abs_path, cmd.size, qcow2_options) linux.qcow2_fill(0, 1048576, install_abs_path) logger.debug('successfully create empty volume[uuid:%s, size:%s] at %s' % (cmd.volumeUuid, cmd.size, cmd.installPath)) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def download_image(self, req): #TODO: report percentage to mgmt server def percentage_callback(percent, url): logger.debug('Downloading %s ... %s%%' % (url, percent)) def use_wget(url, name, workdir, timeout): return linux.wget(url, workdir=workdir, rename=name, timeout=timeout, interval=2, callback=percentage_callback, callback_data=url) cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = DownloadResponse() supported_schemes = [self.URL_HTTP, self.URL_HTTPS, self.URL_FILE] if cmd.urlScheme not in supported_schemes: rsp.success = False rsp.error = 'unsupported url scheme[%s], SimpleSftpBackupStorage only supports %s' % (cmd.urlScheme, supported_schemes) return jsonobject.dumps(rsp) path = os.path.dirname(cmd.installPath) if not os.path.exists(path): os.makedirs(path, 0777) image_name = os.path.basename(cmd.installPath) install_path = cmd.installPath timeout = cmd.timeout if cmd.timeout else 7200 if cmd.urlScheme in [self.URL_HTTP, self.URL_HTTPS]: try: ret = use_wget(cmd.url, image_name, path, timeout) if ret != 0: rsp.success = False rsp.error = 'http/https download failed, [wget -O %s %s] returns value %s' % (image_name, cmd.url, ret) return jsonobject.dumps(rsp) except linux.LinuxError as e: traceback.format_exc() rsp.success = False rsp.error = str(e) return jsonobject.dumps(rsp) elif cmd.urlScheme == self.URL_FILE: src_path = cmd.url.lstrip('file:') src_path = os.path.normpath(src_path) if not os.path.isfile(src_path): raise Exception('cannot find the file[%s]' % src_path) logger.debug("src_path is: %s" % src_path) shell.call('yes | cp %s %s' % (src_path, install_path)) os.chmod(cmd.installPath, stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH) image_format = bash_o("qemu-img info %s | grep -w '^file format' | awk '{print $3}'" % install_path).strip('\n') size = os.path.getsize(install_path) md5sum = 'not calculated' logger.debug('successfully downloaded %s to %s' % (cmd.url, install_path)) (total, avail) = self.get_capacity() rsp.md5Sum = md5sum rsp.actualSize = size rsp.size = linux.qcow2_virtualsize(install_path) rsp.totalCapacity = total rsp.availableCapacity = avail rsp.format = image_format 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 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_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, virtual_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_empty_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() install_abs_path = translate_absolute_path_from_install_path(cmd.installPath) if cmd.backingFile: qcow2_options = self.calc_qcow2_option(self, cmd.qcow2Options, True, cmd.provisioning) backing_abs_path = translate_absolute_path_from_install_path(cmd.backingFile) with lvm.RecursiveOperateLv(backing_abs_path, shared=True): virtual_size = linux.qcow2_virtualsize(backing_abs_path) if not lvm.lv_exists(install_abs_path): lvm.create_lv_from_cmd(install_abs_path, virtual_size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(install_abs_path, shared=False, delete_when_exception=True): linux.qcow2_create_with_backing_file_and_option(backing_abs_path, install_abs_path, qcow2_options) elif not lvm.lv_exists(install_abs_path): qcow2_options = self.calc_qcow2_option(self, cmd.qcow2Options, False, cmd.provisioning) lvm.create_lv_from_cmd(install_abs_path, cmd.size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(install_abs_path, shared=False, delete_when_exception=True): linux.qcow2_create_with_option(install_abs_path, cmd.size, qcow2_options) linux.qcow2_fill(0, 1048576, install_abs_path) logger.debug('successfully create empty volume[uuid:%s, size:%s] at %s' % (cmd.volumeUuid, cmd.size, cmd.installPath)) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def create_root_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() template_abs_path_cache = translate_absolute_path_from_install_path( cmd.templatePathInCache) install_abs_path = translate_absolute_path_from_install_path( cmd.installPath) qcow2_options = self.calc_qcow2_option(self, cmd.qcow2Options, True, cmd.provisioning) with lvm.RecursiveOperateLv(template_abs_path_cache, shared=True, skip_deactivate_tags=[IMAGE_TAG]): virtual_size = linux.qcow2_virtualsize(template_abs_path_cache) if not lvm.lv_exists(install_abs_path): lvm.create_lv_from_cmd( install_abs_path, virtual_size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(install_abs_path, shared=False, delete_when_exception=True): linux.qcow2_clone_with_option(template_abs_path_cache, install_abs_path, qcow2_options) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def offline_merge_snapshots(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = OfflineMergeSnapshotRsp() src_abs_path = translate_absolute_path_from_install_path(cmd.srcPath) dst_abs_path = translate_absolute_path_from_install_path(cmd.destPath) with lvm.RecursiveOperateLv(src_abs_path, shared=True): virtual_size = linux.qcow2_virtualsize(src_abs_path) if not lvm.lv_exists(dst_abs_path): lvm.create_lv_from_absolute_path( dst_abs_path, virtual_size, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.RecursiveOperateLv(dst_abs_path, shared=False): if not cmd.fullRebase: linux.qcow2_rebase(src_abs_path, dst_abs_path) else: tmp_lv = 'tmp_%s' % uuidhelper.uuid() tmp_abs_path = os.path.join(os.path.dirname(dst_abs_path), tmp_lv) tmp_abs_path = os.path.join(os.path.dirname(dst_abs_path), tmp_lv) logger.debug("creating temp lv %s" % tmp_abs_path) lvm.create_lv_from_absolute_path( tmp_abs_path, virtual_size, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(tmp_abs_path, shared=False, delete_when_exception=True): linux.create_template(dst_abs_path, tmp_abs_path) lvm.lv_rename(tmp_abs_path, dst_abs_path, overwrite=True) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def offline_merge_snapshots(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = OfflineMergeSnapshotRsp() src_abs_path = translate_absolute_path_from_install_path(cmd.srcPath) dst_abs_path = translate_absolute_path_from_install_path(cmd.destPath) with lvm.RecursiveOperateLv(src_abs_path, shared=True): virtual_size = linux.qcow2_virtualsize(src_abs_path) if not lvm.lv_exists(dst_abs_path): lvm.create_lv_from_absolute_path(dst_abs_path, virtual_size, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.RecursiveOperateLv(dst_abs_path, shared=False): if not cmd.fullRebase: linux.qcow2_rebase(src_abs_path, dst_abs_path) else: tmp_lv = 'tmp_%s' % uuidhelper.uuid() tmp_abs_path = os.path.join(os.path.dirname(dst_abs_path), tmp_lv) tmp_abs_path = os.path.join(os.path.dirname(dst_abs_path), tmp_lv) logger.debug("creating temp lv %s" % tmp_abs_path) lvm.create_lv_from_absolute_path(tmp_abs_path, virtual_size, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(tmp_abs_path, shared=False, delete_when_exception=True): linux.create_template(dst_abs_path, tmp_abs_path) lvm.lv_rename(tmp_abs_path, dst_abs_path, overwrite=True) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def resize(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) install_path = cmd.installPath rsp = ResizeVolumeRsp() shell.call("qemu-img resize %s %s" % (install_path, cmd.size)) ret = linux.qcow2_virtualsize(install_path) rsp.size = ret return jsonobject.dumps(rsp)
def resize_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) install_path = cmd.installPath rsp = ResizeVolumeRsp() shell.call("qemu-img resize %s %s" % (install_path, cmd.size)) ret = linux.qcow2_virtualsize(install_path) rsp.size = ret 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 revertvolume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = RevertVolumeFromSnapshotRsp() install_path = cmd.snapshotInstallPath new_volume_path = os.path.join(os.path.dirname(install_path), '{0}.qcow2'.format(uuidhelper.uuid())) linux.qcow2_clone_with_cmd(install_path, new_volume_path, cmd) size = linux.qcow2_virtualsize(new_volume_path) rsp.newVolumeInstallPath = new_volume_path rsp.size = size return jsonobject.dumps(rsp)
def create_empty_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = VolumeRsp() install_abs_path = get_absolute_path_from_install_path(cmd.installPath) drbdResource = drbd.DrbdResource( self.get_name_from_installPath(cmd.installPath), False) drbdResource.config.local_host.hostname = cmd.local_host_name drbdResource.config.local_host.disk = install_abs_path drbdResource.config.local_host.minor = cmd.local_host_port - DRBD_START_PORT drbdResource.config.local_host.address = "%s:%s" % ( cmd.local_address, cmd.local_host_port) drbdResource.config.remote_host.hostname = cmd.remote_host_name drbdResource.config.remote_host.disk = install_abs_path drbdResource.config.remote_host.minor = cmd.remote_host_port - DRBD_START_PORT drbdResource.config.remote_host.address = "%s:%s" % ( cmd.remote_address, cmd.remote_host_port) drbdResource.config.write_config() try: if cmd.backingFile: backing_abs_path = get_absolute_path_from_install_path( cmd.backingFile) virtual_size = linux.qcow2_virtualsize(backing_abs_path) lvm.create_lv_from_cmd( install_abs_path, virtual_size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time()), False) lvm.active_lv(install_abs_path) drbdResource.initialize(cmd.init, cmd, backing_abs_path) elif not lvm.lv_exists(install_abs_path): lvm.create_lv_from_cmd( install_abs_path, cmd.size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time()), False) lvm.active_lv(install_abs_path) drbdResource.initialize(cmd.init, cmd) except Exception as e: drbdResource.destroy() lvm.delete_lv(install_abs_path) logger.debug( 'failed to create empty volume[uuid:%s, size:%s] at %s' % (cmd.volumeUuid, cmd.size, cmd.installPath)) raise e logger.debug( 'successfully create empty volume[uuid:%s, size:%s] at %s' % (cmd.volumeUuid, cmd.size, cmd.installPath)) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp._init_from_drbd(drbdResource) return jsonobject.dumps(rsp)
def revert_volume_from_snapshot(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = RevertVolumeFromSnapshotRsp() install_path = cmd.snapshotInstallPath new_volume_path = os.path.join(os.path.dirname(install_path), '{0}.qcow2'.format(uuidhelper.uuid())) linux.qcow2_clone_with_cmd(install_path, new_volume_path, cmd) size = linux.qcow2_virtualsize(new_volume_path) rsp.newVolumeInstallPath = new_volume_path rsp.size = size 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 revert_volume_from_snapshot(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = RevertVolumeFromSnapshotResponse() install_path = cmd.snapshotInstallPath new_volume_path = os.path.join(os.path.dirname(install_path), '{0}.qcow2'.format(uuidhelper.uuid())) linux.qcow2_clone_with_cmd(install_path, new_volume_path, cmd) rsp.newVolumeInstallPath = new_volume_path size = linux.qcow2_virtualsize(new_volume_path) rsp.size = size self._set_capacity_to_response(cmd.uuid, rsp) return jsonobject.dumps(rsp)
def revert_volume_from_snapshot(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = RevertVolumeFromSnapshotResponse() install_path = cmd.snapshotInstallPath new_volume_path = os.path.join(os.path.dirname(install_path), '{0}.qcow2'.format(uuidhelper.uuid())) linux.qcow2_clone(install_path, new_volume_path) rsp.newVolumeInstallPath = new_volume_path size = linux.qcow2_virtualsize(new_volume_path) rsp.size = size self._set_capacity_to_response(cmd.uuid, rsp) return jsonobject.dumps(rsp)
def resize_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) install_abs_path = translate_absolute_path_from_install_path(cmd.installPath) with lvm.RecursiveOperateLv(install_abs_path, shared=False): lvm.resize_lv_from_cmd(install_abs_path, cmd.size, cmd) if not cmd.live: shell.call("qemu-img resize %s %s" % (install_abs_path, cmd.size)) ret = linux.qcow2_virtualsize(install_abs_path) rsp = ResizeVolumeRsp() rsp.size = ret return jsonobject.dumps(rsp)
def resize_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) install_abs_path = translate_absolute_path_from_install_path( cmd.installPath) with lvm.RecursiveOperateLv(install_abs_path, False): lvm.resize_lv(install_abs_path, cmd.size) shell.call("qemu-img resize %s %s" % (install_abs_path, cmd.size)) ret = linux.qcow2_virtualsize(install_abs_path) rsp = ResizeVolumeRsp() rsp.size = ret return jsonobject.dumps(rsp)
def revert_volume_from_snapshot(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = RevertVolumeFromSnapshotRsp() snapshot_abs_path = translate_absolute_path_from_install_path(cmd.snapshotInstallPath) qcow2_options = self.calc_qcow2_option(self, cmd.qcow2Options, True, cmd.provisioning) new_volume_path = cmd.installPath if new_volume_path is None or new_volume_path == "": new_volume_path = "/dev/%s/%s" % (cmd.vgUuid, uuidhelper.uuid()) else: new_volume_path = translate_absolute_path_from_install_path(new_volume_path) with lvm.RecursiveOperateLv(snapshot_abs_path, shared=True): size = linux.qcow2_virtualsize(snapshot_abs_path) lvm.create_lv_from_cmd(new_volume_path, size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(new_volume_path, shared=False, delete_when_exception=True): linux.qcow2_clone_with_option(snapshot_abs_path, new_volume_path, qcow2_options) size = linux.qcow2_virtualsize(new_volume_path) rsp.newVolumeInstallPath = new_volume_path rsp.size = size return jsonobject.dumps(rsp)
def create_root_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = AgentRsp() template_abs_path_cache = translate_absolute_path_from_install_path(cmd.templatePathInCache) install_abs_path = translate_absolute_path_from_install_path(cmd.installPath) qcow2_options = self.calc_qcow2_option(self, cmd.qcow2Options, True, cmd.provisioning) with lvm.RecursiveOperateLv(template_abs_path_cache, shared=True, skip_deactivate_tags=[IMAGE_TAG]): virtual_size = linux.qcow2_virtualsize(template_abs_path_cache) if not lvm.lv_exists(install_abs_path): lvm.create_lv_from_cmd(install_abs_path, virtual_size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(install_abs_path, shared=False, delete_when_exception=True): linux.qcow2_clone_with_option(template_abs_path_cache, install_abs_path, qcow2_options) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def merge_snapshot(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = MergeSnapshotRsp() snapshot_abs_path = translate_absolute_path_from_install_path(cmd.snapshotInstallPath) workspace_abs_path = translate_absolute_path_from_install_path(cmd.workspaceInstallPath) with lvm.RecursiveOperateLv(snapshot_abs_path, shared=True): virtual_size = linux.qcow2_virtualsize(snapshot_abs_path) if not lvm.lv_exists(workspace_abs_path): lvm.create_lv_from_absolute_path(workspace_abs_path, virtual_size, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.OperateLv(workspace_abs_path, shared=False, delete_when_exception=True): linux.create_template(snapshot_abs_path, workspace_abs_path) rsp.size, rsp.actualSize = linux.qcow2_size_and_actual_size(workspace_abs_path) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp.actualSize = rsp.size return jsonobject.dumps(rsp)
def create_root_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = VolumeRsp() template_abs_path_cache = get_absolute_path_from_install_path( cmd.templatePathInCache) install_abs_path = get_absolute_path_from_install_path(cmd.installPath) drbdResource = drbd.DrbdResource( self.get_name_from_installPath(cmd.installPath), False) drbdResource.config.local_host.hostname = cmd.local_host_name drbdResource.config.local_host.disk = install_abs_path drbdResource.config.local_host.minor = cmd.local_host_port - DRBD_START_PORT drbdResource.config.local_host.address = "%s:%s" % ( cmd.local_address, cmd.local_host_port) drbdResource.config.remote_host.hostname = cmd.remote_host_name drbdResource.config.remote_host.disk = install_abs_path drbdResource.config.remote_host.minor = cmd.remote_host_port - DRBD_START_PORT drbdResource.config.remote_host.address = "%s:%s" % ( cmd.remote_address, cmd.remote_host_port) drbdResource.config.write_config() virtual_size = linux.qcow2_virtualsize(template_abs_path_cache) try: lvm.qcow2_lv_recursive_active(template_abs_path_cache, lvm.LvmlockdLockType.SHARE) if not lvm.lv_exists(install_abs_path): lvm.create_lv_from_cmd( install_abs_path, virtual_size, cmd, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time()), False) lvm.active_lv(install_abs_path) drbdResource.initialize(cmd.init, cmd, template_abs_path_cache) except Exception as e: drbdResource.destroy() lvm.delete_lv(install_abs_path) raise e rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) rsp._init_from_drbd(drbdResource) return jsonobject.dumps(rsp)
def offline_merge_snapshots(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = OfflineMergeSnapshotRsp() src_abs_path = translate_absolute_path_from_install_path(cmd.srcPath) dst_abs_path = translate_absolute_path_from_install_path(cmd.destPath) with lvm.RecursiveOperateLv(src_abs_path, shared=True): virtual_size = linux.qcow2_virtualsize(src_abs_path) if not lvm.lv_exists(dst_abs_path): lvm.create_lv_from_absolute_path( dst_abs_path, virtual_size, "%s::%s::%s" % (VOLUME_TAG, cmd.hostUuid, time.time())) with lvm.RecursiveOperateLv(dst_abs_path, shared=False): if not cmd.fullRebase: linux.qcow2_rebase(src_abs_path, dst_abs_path) else: # TODO(weiw): add tmp disk and then rename is better linux.create_template(src_abs_path, dst_abs_path) rsp.totalCapacity, rsp.availableCapacity = lvm.get_vg_size(cmd.vgUuid) return jsonobject.dumps(rsp)
def resize_volume(self, req): cmd = jsonobject.loads(req[http.REQUEST_BODY]) install_abs_path = get_absolute_path_from_install_path(cmd.installPath) rsp = ResizeVolumeRsp() if not cmd.drbd: lvm.resize_lv_from_cmd(install_abs_path, cmd.size, cmd) return jsonobject.dumps(rsp) r = drbd.DrbdResource(cmd.installPath.split("/")[-1]) r._init_from_disk(install_abs_path) with drbd.OperateDrbd(r): r.resize() with drbd.OperateDrbd(r): if not cmd.live: shell.call("qemu-img resize %s %s" % (r.get_dev_path(), cmd.size)) ret = linux.qcow2_virtualsize(r.get_dev_path()) rsp.size = ret rsp._init_from_drbd(r) 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) with lvm.RecursiveOperateLv(volume_abs_path, shared=False): 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, virtual_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)) 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 download_image(self, req): #TODO: report percentage to mgmt server def percentage_callback(percent, url): logger.debug('Downloading %s ... %s%%' % (url, percent)) def use_wget(url, name, workdir, timeout): return linux.wget(url, workdir=workdir, rename=name, timeout=timeout, interval=2, callback=percentage_callback, callback_data=url) cmd = jsonobject.loads(req[http.REQUEST_BODY]) rsp = DownloadResponse() supported_schemes = [self.URL_HTTP, self.URL_HTTPS, self.URL_FILE] if cmd.urlScheme not in supported_schemes: rsp.success = False rsp.error = 'unsupported url scheme[%s], SimpleSftpBackupStorage only supports %s' % ( cmd.urlScheme, supported_schemes) return jsonobject.dumps(rsp) path = os.path.dirname(cmd.installPath) if not os.path.exists(path): os.makedirs(path, 0777) image_name = os.path.basename(cmd.installPath) install_path = cmd.installPath timeout = cmd.timeout if cmd.timeout else 7200 if cmd.urlScheme in [self.URL_HTTP, self.URL_HTTPS]: try: image_name = linux.shellquote(image_name) cmd.url = linux.shellquote(cmd.url) ret = use_wget(cmd.url, image_name, path, timeout) if ret != 0: rsp.success = False rsp.error = 'http/https download failed, [wget -O %s %s] returns value %s' % ( image_name, cmd.url, ret) return jsonobject.dumps(rsp) except linux.LinuxError as e: traceback.format_exc() rsp.success = False rsp.error = str(e) return jsonobject.dumps(rsp) elif cmd.urlScheme == self.URL_FILE: src_path = cmd.url.lstrip('file:') src_path = os.path.normpath(src_path) if not os.path.isfile(src_path): raise Exception('cannot find the file[%s]' % src_path) logger.debug("src_path is: %s" % src_path) shell.call('yes | cp %s %s' % (src_path, linux.shellquote(install_path))) os.chmod(cmd.installPath, stat.S_IRUSR + stat.S_IRGRP + stat.S_IROTH) image_format = bash_o( "qemu-img info %s | grep -w '^file format' | awk '{print $3}'" % linux.shellquote(install_path)).strip('\n') size = os.path.getsize(install_path) md5sum = 'not calculated' logger.debug('successfully downloaded %s to %s' % (cmd.url, install_path)) (total, avail) = self.get_capacity() rsp.md5Sum = md5sum rsp.actualSize = size rsp.size = linux.qcow2_virtualsize(install_path) rsp.totalCapacity = total rsp.availableCapacity = avail rsp.format = image_format return jsonobject.dumps(rsp)