Exemplo n.º 1
0
Arquivo: FileSR.py Projeto: rdobson/sm
 def compose(self, sr_uuid, vdi1, vdi2):
     if self.vdi_type != vhdutil.VDI_TYPE_VHD:
         raise xs_errors.XenError("Unimplemented")
     parent_fn = vdi1 + vhdutil.FILE_EXTN[vhdutil.VDI_TYPE_VHD]
     parent_path = os.path.join(self.sr.path, parent_fn)
     assert util.pathexists(parent_path)
     vhdutil.setParent(self.path, parent_path, False)
     vhdutil.setHidden(parent_path)
     util.pread2([vhdutil.VHD_UTIL, "modify", "-p", parent_path, "-n", self.path])
     # Tell tapdisk the chain has changed
     if not blktap2.VDI.tap_refresh(self.session, sr_uuid, vdi2):
         raise util.SMException("failed to refresh VDI %s" % self.uuid)
     util.SMlog("VDI.compose: relinked %s->%s" % (vdi2, vdi1))
Exemplo n.º 2
0
 def compose(self, sr_uuid, vdi1, vdi2):
     if self.vdi_type != vhdutil.VDI_TYPE_VHD:
         raise xs_errors.XenError('Unimplemented')
     parent_fn = vdi1 + vhdutil.FILE_EXTN[vhdutil.VDI_TYPE_VHD]
     parent_path = os.path.join(self.sr.path, parent_fn)
     assert (util.pathexists(parent_path))
     vhdutil.setParent(self.path, parent_path, False)
     vhdutil.setHidden(parent_path)
     util.pread2(
         [vhdutil.VHD_UTIL, "modify", "-p", parent_path, "-n", self.path])
     # Tell tapdisk the chain has changed
     if not blktap2.VDI.tap_refresh(self.session, sr_uuid, vdi2):
         raise util.SMException("failed to refresh VDI %s" % self.uuid)
     util.SMlog("VDI.compose: relinked %s->%s" % (vdi2, vdi1))
Exemplo n.º 3
0
Arquivo: FileSR.py Projeto: falaa/sm
 def _mark_hidden(self, path):
     vhdutil.setHidden(path, True)
     self.hidden = 1
Exemplo n.º 4
0
 def _mark_hidden(self, path):
     vhdutil.setHidden(path, True)
     self.hidden = 1
Exemplo n.º 5
0
    def compose(self, sr_uuid, vdi1_uuid, vdi2_uuid):
        """
        :param sr_uuid:
        :param vdi1_uuid:
        :param vdi2_uuid:
        :return:
        """
        if VERBOSE:
            util.SMlog(
                "rbdsr_vhd.RBDVHDVDI.compose: sr_uuid=%s, vdi1_uuid=%s, vdi2_uuid=%s"
                % (sr_uuid, vdi1_uuid, vdi2_uuid))

        base_uuid = vdi1_uuid
        mirror_uuid = vdi2_uuid

        base_path = self.sr._get_path(base_uuid)
        mirror_path = self.sr._get_path(mirror_uuid)

        base_vdi_ref = self.session.xenapi.VDI.get_by_uuid(base_uuid)
        mirror_vdi_ref = self.session.xenapi.VDI.get_by_uuid(mirror_uuid)

        mirror_sm_config = self.session.xenapi.VDI.get_sm_config(
            mirror_vdi_ref)

        mirror_hostRefs = self._get_vdi_hostRefs(mirror_uuid)
        local_host_uuid = inventory.get_localhost_uuid()

        BaseVDI = self.sr.vdi(base_uuid)

        for host_uuid in mirror_hostRefs.iterkeys():
            RBDVHDVDI.attach(BaseVDI, sr_uuid, base_uuid, host_uuid=host_uuid)
        if local_host_uuid not in mirror_hostRefs:
            RBDVHDVDI.attach(BaseVDI,
                             sr_uuid,
                             base_uuid,
                             host_uuid=local_host_uuid)
            self.attach(sr_uuid, mirror_uuid, host_uuid=local_host_uuid)

        vhdutil.setParent(mirror_path, base_path, False)
        vhdutil.setHidden(base_path)
        self.sr.session.xenapi.VDI.set_managed(base_vdi_ref, False)
        RBDVHDVDI.update(
            BaseVDI, sr_uuid, base_uuid
        )  # TODO: Check if xapi invoke update after set_* op, if it's true then we can remove this line

        if 'vhd-parent' in mirror_sm_config:
            self.session.xenapi.VDI.remove_from_sm_config(
                mirror_vdi_ref, 'vhd-parent')
        self.session.xenapi.VDI.add_to_sm_config(mirror_vdi_ref, 'vhd-parent',
                                                 base_uuid)
        self.sm_config['vhd-parent'] = base_uuid
        self.update(sr_uuid, mirror_uuid)

        if local_host_uuid not in mirror_hostRefs:
            self.detach(sr_uuid, mirror_uuid, host_uuid=local_host_uuid)
            RBDVHDVDI.detach(BaseVDI,
                             sr_uuid,
                             base_uuid,
                             host_uuid=local_host_uuid)

        if not blktap2.VDI.tap_refresh(self.session, self.sr.uuid, mirror_uuid,
                                       True):
            raise util.SMException("failed to refresh VDI %s" % mirror_uuid)

        if VERBOSE:
            util.SMlog("Compose done")
Exemplo n.º 6
0
    def clone(self, sr_uuid, vdi_uuid, mode='clone'):
        """
        :param sr_uuid:
        :param vdi_uuid:
        :return:
        """
        if VERBOSE:
            util.SMlog("rbdsr_vhd.RBDVHDVDI.clone: sr_uuid=%s, vdi_uuid=%s" %
                       (sr_uuid, vdi_uuid))

        vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid)
        sm_config = self.session.xenapi.VDI.get_sm_config(vdi_ref)
        is_a_snapshot = self.session.xenapi.VDI.get_is_a_snapshot(vdi_ref)
        label = self.session.xenapi.VDI.get_name_label(vdi_ref)
        description = self.session.xenapi.VDI.get_name_description(vdi_ref)

        local_host_uuid = inventory.get_localhost_uuid()

        if mode == 'snapshot' and is_a_snapshot:
            raise util.SMException("Can not make snapshot form snapshot %s" %
                                   vdi_uuid)

        self.size = int(self.session.xenapi.VDI.get_virtual_size(vdi_ref))

        if not is_a_snapshot:
            base_uuid = util.gen_uuid()
        else:
            base_uuid = sm_config["vhd-parent"]
        clone_uuid = util.gen_uuid()

        if VERBOSE:
            util.SMlog(
                "rbdsr_vhd.RBDVHDVDI.clone: Pepare CloneVDI: sr_uuid=%s, clone_uuid=%s"
                % (sr_uuid, clone_uuid))
        cloneVDI = self.sr.vdi(clone_uuid)
        cloneVDI.label = "%s (%s)" % (label, mode)
        cloneVDI.description = description
        cloneVDI.path = self.sr._get_path(clone_uuid)
        cloneVDI.location = cloneVDI.uuid
        cloneVDI.size = self.size
        cloneVDI.utilisation = self.size
        cloneVDI.sm_config = dict()
        for key, val in sm_config.iteritems():
            if key not in ["type", "vdi_type", "vhd-parent", "paused", "attached"] and \
                    not key.startswith("host_"):
                cloneVDI.sm_config[key] = val
        if mode == 'snapshot':
            cloneVDI.is_a_snapshot = True
            cloneVDI.snapshot_of = vdi_ref

        retval_clone = RBDVHDVDI.create(cloneVDI, sr_uuid, clone_uuid,
                                        cloneVDI.size)
        clone_ref = self.session.xenapi.VDI.get_by_uuid(clone_uuid)

        if not is_a_snapshot:
            if VERBOSE:
                util.SMlog(
                    "rbdsr_vhd.RBDVHDVDI.clone: Pepare BaseVDI: sr_uuid=%s, base_uuid=%s"
                    % (sr_uuid, base_uuid))
            baseVDI = self.sr.vdi(base_uuid)
            baseVDI.label = "%s (base)" % label
            baseVDI.description = description
            baseVDI.path = self.sr._get_path(base_uuid)
            baseVDI.location = baseVDI.uuid
            baseVDI.managed = False
            baseVDI.size = self.size
            baseVDI.utilisation = self.size
            baseVDI.sm_config = dict()

            retval_base = RBDVHDVDI.create(baseVDI, sr_uuid, base_uuid,
                                           baseVDI.size)
            base_ref = self.session.xenapi.VDI.get_by_uuid(base_uuid)
        else:
            base_ref = self.session.xenapi.VDI.get_by_uuid(base_uuid)
            baseVDI = self.sr.vdi(base_uuid)
            baseVDI.path = self.sr._get_path(base_uuid)
            baseVDI.sm_config = self.session.xenapi.VDI.get_sm_config(base_ref)

        if not is_a_snapshot:
            if 'attached' in sm_config:
                if VERBOSE:
                    util.SMlog(
                        "rbdsr_vhd.RBDVHDVDI.clone: Unmap VDI as it's mapped: sr_uuid=%s, vdi_uuid=%s"
                        % (sr_uuid, vdi_uuid))

                if 'paused' not in sm_config:
                    if not blktap2.VDI.tap_pause(self.session, self.sr.uuid,
                                                 vdi_uuid):
                        raise util.SMException("failed to pause VDI %s" %
                                               vdi_uuid)
                self._unmap_rbd(vdi_uuid,
                                self.rbd_info[1]['size'],
                                devlinks=False,
                                norefcount=True)

            if VERBOSE:
                util.SMlog(
                    "rbdsr_vhd.RBDVHDVDI.clone: Swap Base and VDI: sr_uuid=%s, vdi_uuid=%s, base_uuid=%s"
                    % (sr_uuid, vdi_uuid, base_uuid))

            tmp_uuid = "temporary"  # util.gen_uuid()
            self._rename_rbd(vdi_uuid, tmp_uuid)
            self._rename_rbd(base_uuid, vdi_uuid)
            self._rename_rbd(tmp_uuid, base_uuid)

        if not is_a_snapshot:
            if 'attached' in sm_config:
                self._map_rbd(vdi_uuid,
                              self.rbd_info[1]['size'],
                              devlinks=False,
                              norefcount=True)
                base_hostRefs = self._get_vdi_hostRefs(vdi_uuid)
                if local_host_uuid not in base_hostRefs:
                    self.attach(sr_uuid, vdi_uuid, host_uuid=local_host_uuid)
            else:
                self.attach(sr_uuid, vdi_uuid, host_uuid=local_host_uuid)
                base_hostRefs = {}
        else:
            if 'attached' not in baseVDI.sm_config:
                RBDVHDVDI.attach(baseVDI,
                                 sr_uuid,
                                 base_uuid,
                                 host_uuid=local_host_uuid)
                base_hostRefs = {}
            else:
                base_hostRefs = self._get_vdi_hostRefs(base_uuid)
                if local_host_uuid not in base_hostRefs:
                    RBDVHDVDI.attach(baseVDI,
                                     sr_uuid,
                                     base_uuid,
                                     host_uuid=local_host_uuid)

        if is_a_snapshot:
            RBDVHDVDI.attach(cloneVDI,
                             sr_uuid,
                             clone_uuid,
                             host_uuid=local_host_uuid)

            vhdutil.snapshot(cloneVDI.path, baseVDI.path, False,
                             lvhdutil.MSIZE_MB)

            RBDVHDVDI.detach(cloneVDI,
                             sr_uuid,
                             clone_uuid,
                             host_uuid=local_host_uuid)
        else:
            for host_uuid in base_hostRefs.iterkeys():
                RBDVHDVDI.attach(baseVDI,
                                 sr_uuid,
                                 base_uuid,
                                 host_uuid=host_uuid)
            if local_host_uuid not in base_hostRefs:
                RBDVHDVDI.attach(baseVDI,
                                 sr_uuid,
                                 base_uuid,
                                 host_uuid=local_host_uuid)
            RBDVHDVDI.attach(cloneVDI,
                             sr_uuid,
                             clone_uuid,
                             host_uuid=local_host_uuid)

            vhdutil.snapshot(cloneVDI.path, baseVDI.path, False,
                             lvhdutil.MSIZE_MB)
            vhdutil.snapshot(self.path, baseVDI.path, False, lvhdutil.MSIZE_MB)
            vhdutil.setHidden(baseVDI.path)

            RBDVHDVDI.detach(cloneVDI,
                             sr_uuid,
                             clone_uuid,
                             host_uuid=local_host_uuid)
            if local_host_uuid not in base_hostRefs:
                RBDVHDVDI.detach(baseVDI,
                                 sr_uuid,
                                 base_uuid,
                                 host_uuid=local_host_uuid)

            baseVDI.read_only = True
            self.session.xenapi.VDI.set_read_only(base_ref, True)

        if mode == 'snapshot':
            cloneVDI.read_only = True
            self.session.xenapi.VDI.set_read_only(clone_ref, True)

        cloneVDI.sm_config["vhd-parent"] = base_uuid
        self.session.xenapi.VDI.add_to_sm_config(cloneVDI.ref, 'vhd-parent',
                                                 base_uuid)
        RBDVHDVDI.update(cloneVDI, sr_uuid, clone_uuid)

        if not is_a_snapshot:
            if 'vhd-parent' in sm_config:
                baseVDI.sm_config['vhd-parent'] = sm_config['vhd-parent']
                self.session.xenapi.VDI.add_to_sm_config(
                    baseVDI.ref, 'vhd-parent', sm_config['vhd-parent'])
                RBDVHDVDI.update(baseVDI, sr_uuid, base_uuid)
                self.session.xenapi.VDI.remove_from_sm_config(
                    vdi_ref, 'vhd-parent')
            else:
                RBDVHDVDI.update(baseVDI, sr_uuid, base_uuid)
            self.session.xenapi.VDI.add_to_sm_config(vdi_ref, 'vhd-parent',
                                                     base_uuid)
            self.sm_config['vhd-parent'] = base_uuid
            self.update(sr_uuid, vdi_uuid)

        if not is_a_snapshot:
            if 'attached' in sm_config:
                if 'paused' not in sm_config:
                    if not blktap2.VDI.tap_unpause(self.session, self.sr.uuid,
                                                   vdi_uuid, None):
                        raise util.SMException("failed to unpause VDI %s" %
                                               vdi_uuid)
            if local_host_uuid not in base_hostRefs:
                self.detach(sr_uuid, vdi_uuid, host_uuid=local_host_uuid)
        else:
            if local_host_uuid not in base_hostRefs:
                RBDVHDVDI.detach(baseVDI,
                                 sr_uuid,
                                 base_uuid,
                                 host_uuid=local_host_uuid)

        return retval_clone