示例#1
0
    def attach(self, dbg, configuration):
        log.debug("%s: xcpng.sr.SR.attach: configuration: %s" %
                  (dbg, configuration))

        if IMAGE_FORMAT_TAG in configuration:
            uri = "%s+%s" % (self.sr_type, configuration[IMAGE_FORMAT_TAG])
            if DATAPATH_TAG in configuration:
                uri = "%s+%s://" % (uri, configuration[DATAPATH_TAG])
        else:
            uri = "%s://" % self.sr_type

        uri = self.SROpsHendler.extend_uri(dbg, uri, configuration)
        uri = "%s/%s" % (uri, configuration[SR_UUID_TAG]
                         ) if SR_UUID_TAG in configuration else uri

        log.debug("%s: xcpng.sr.SR.attach: uri: %s" % (dbg, uri))

        configuration['mountpoint'] = "%s/%s" % (SR_PATH_PREFIX,
                                                 get_sr_uuid_by_uri(dbg, uri))

        try:
            call(dbg, ['mkdir', '-p', configuration['mountpoint']])
            self.SROpsHendler.sr_import(dbg, uri, configuration)
        except Exception as e:
            log.error(
                "%s: xcpng.sr.SR.attach: Failed to attach SR - sr_uuid: %s" %
                (dbg, get_sr_uuid_by_uri(dbg, uri)))
            try:
                self.SROpsHendler.sr_export(dbg, uri)
            except:
                pass
            raise Exception(e)

        return uri
示例#2
0
    def sr_export(self, dbg, uri):
        log.debug("%s: xcpng.libsbd.sr.SROperations.sr_export: uri: %s" %
                  (dbg, uri))

        sr_uuid = get_sr_uuid_by_uri(dbg, uri)
        stop_sheepdog_gateway(dbg, sr_uuid)
        free_sheep_port(dbg, sr_uuid)
        unset_chroot(dbg, sr_uuid)
        delete_chroot(dbg, sr_uuid)
        system("rm -rf %s/%s" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri)))
示例#3
0
 def detach(self, dbg, uri):
     log.debug("%s: xcpng.sr.SR.detach: uri: %s" % (dbg, uri))
     try:
         self.SROpsHendler.sr_export(dbg, uri)
         call(dbg, [
             'rm', '-rf',
             "%s/%s" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri))
         ])
     except Exception as e:
         log.error(
             "%s: xcpng.sr.SR.detach: Failed to detach SR - sr_uuid: %s" %
             (dbg, get_sr_uuid_by_uri(dbg, uri)))
         raise Exception(e)
示例#4
0
 def destroy(self, dbg, uri):
     log.debug("%s: xcpng.sr.SR.destroy: uri: %s" % (dbg, uri))
     try:
         self.MetadataHandler.destroy(dbg, uri)
         self.SROpsHendler.destroy(dbg, uri)
         call(dbg, [
             'rm', '-rf',
             "%s/%s" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri))
         ])
     except Exception as e:
         log.error(
             "%s: xcpng.sr.SR.destroy: Failed to destroy SR - sr_uuid: %s" %
             (dbg, get_sr_uuid_by_uri(dbg, uri)))
         raise Exception(e)
示例#5
0
    def create(self, dbg, uri, configuration):
        log.debug(
            "%s: xcpng.libsbd.sr.SROperations.create: uri: %s configuration %s"
            % (dbg, uri, configuration))

        if 'bindnetaddr' not in configuration:
            raise Exception(
                'Failed to connect to Sheepdog cluster. Parameter \'bindnetaddr\' is not specified'
            )
        elif 'mcastaddr' not in configuration:
            raise Exception(
                'Failed to connect to Sheepdog cluster. Parameter \'mcastaddr\' is not specified'
            )
        elif 'mcastport' not in configuration:
            raise Exception(
                'Failed to connect to Sheepdog cluster. Parameter \'mcastport\' is not specified'
            )

        sr_uuid = get_sr_uuid_by_uri(dbg, uri)

        create_chroot(dbg, sr_uuid)
        set_chroot(dbg, sr_uuid)
        write_corosync_conf(
            dbg, sr_uuid,
            gen_corosync_conf(dbg, configuration['bindnetaddr'],
                              configuration['mcastaddr'],
                              configuration['mcastport']))
        start_sheepdog_gateway(dbg, get_sheep_port(dbg, sr_uuid), sr_uuid)
示例#6
0
def create(dbg, qemudisk, uri, img_qemu_uri):
    log.debug("%s: xcpng.qemudisk.create: uri: %s " % (dbg, uri))

    vdi_uuid = utils.get_vdi_uuid_by_uri(dbg, uri)
    sr_uuid = utils.get_sr_uuid_by_uri(dbg, uri)
    vdi_type = utils.get_vdi_type_by_uri(dbg, uri)
    if vdi_type not in IMAGE_TYPES:
        raise Exception('Incorrect VDI type')

    utils.mkdir_p(QEMU_DP_SOCKET_DIR, 0o0700)

    nbd_sock = QEMU_DP_SOCKET_DIR + "/qemu-nbd.{}".format(vdi_uuid)
    qmp_sock = QEMU_DP_SOCKET_DIR + "/qmp_sock.{}".format(vdi_uuid)
    qmp_log = QEMU_DP_SOCKET_DIR + "/qmp_log.{}".format(vdi_uuid)
    log.debug("%s: xcpng.qemudisk.create: Spawning qemu process for VDI %s with qmp socket at %s"
              % (dbg, vdi_uuid, qmp_sock))

    cmd = [QEMU_DP, qmp_sock]

    try:
        log_fd = open(qmp_log, 'w+')
        p = subprocess.Popen(cmd, stdout=log_fd, stderr=log_fd)
    except Exception as e:
        log.error("%s: xcpng.qemudisk.create: Failed to create qemu_dp instance: uri %s" %
                  (dbg, uri))
        try:
            log_fd.close()
        except:
            pass
        raise Exception(e)

    log.debug("%s: xcpng.qemudisk.create: New qemu process has pid %d" % (dbg, p.pid))

    return qemudisk(dbg, sr_uuid, vdi_uuid, vdi_type, img_qemu_uri, p.pid, qmp_sock, nbd_sock, qmp_log)
示例#7
0
 def destroy(self, dbg, uri):
     log.debug("%s: xcpng.libzfs.meta.MetaDBOpeations.destroy: uri: %s" %
               (dbg, uri))
     call(dbg, [
         'rm', '-f',
         "%s/%s/__meta__" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri))
     ])
示例#8
0
 def get_size(self, dbg, uri):
     log.debug("%s: xcpng.libsbd.sr.SROperations.sr_size: uri: %s" %
               (dbg, uri))
     return int(
         dog_node_info(dbg, get_sheep_port(dbg,
                                           get_sr_uuid_by_uri(dbg,
                                                              uri)))[1])
示例#9
0
 def lock(self, dbg, uri):
     log.debug("%s: xcpng.libzfs.meta.MetaDBOpeations.lock: uri: %s" %
               (dbg, uri))
     self.lh = open(
         "%s/%s/__lock__" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri)),
         'w')
     fcntl.flock(self.lh, fcntl.LOCK_EX)
示例#10
0
    def find_coalesceable_pairs(self, dbg, sr):
        log.debug(
            "%s: xcpng.meta.MetadataHandler.find_coalesceable pairs: sr: %s" %
            (dbg, sr))

        if self.__loaded is False:
            self.__load(dbg, sr)

        pairs = []

        self.db._storage.set_db_name(get_sr_uuid_by_uri(dbg, sr))

        table = self.db.table('vdis')

        try:
            roots = table.search(~(where(PARENT_URI_TAG).exists()))
            while len(roots) != 0:
                _roots_ = []
                for root in roots:
                    children = table.search(
                        where(PARENT_URI_TAG) == root[KEY_TAG])
                    if len(children) == 1:
                        pairs.append((root, children[0]))
                    elif len(children) > 1:
                        _roots_.extend(children)
                roots = _roots_
            return pairs
        except Exception as e:
            log.error(
                "%s: xcpng.meta.MetadataHandler.find_coalesceable_pairs: Failed to find "
                "coalesceable pairs for sr: %s " % (dbg, sr))
            log.error(traceback.format_exc())
            raise Exception(e)
示例#11
0
 def create(self, dbg, uri):
     log.debug("%s: xcpng.libzfs.meta.MetaDBOpeations.create: uri: %s" %
               (dbg, uri))
     fd = open(
         "%s/%s/__meta__" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri)),
         'w')
     fd.write('{"sr": {}}')
     fd.close()
示例#12
0
 def map_vol(self, dbg, uri, chained=False):
     volume_meta = self.MetadataHandler.get_vdi_meta(dbg, uri)
     self.blkdev = "/dev/zvol/%s%s%s/%s%s" % (get_sr_type_by_uri(dbg, uri),
                                              POOL_PREFIX,
                                              get_sr_uuid_by_uri(dbg, uri),
                                              VDI_PREFIXES[get_vdi_type_by_uri(dbg, uri)],
                                              volume_meta[IMAGE_UUID_TAG])
     super(DatapathOperations, self).map_vol(dbg, uri, chained)
示例#13
0
    def dump(self, dbg, uri, json):
        log.debug("%s: xcpng.libsbd.meta.MetaDBOpeations.dump: uri: %s" %
                  (dbg, uri))

        length = len(json)
        dog_vdi_write(dbg, get_sheep_port(dbg, get_sr_uuid_by_uri(dbg, uri)),
                      '__meta__', pack("!I%ss" % length, length,
                                       json), 0, length + 4)
示例#14
0
 def dump(self, dbg, uri, json):
     log.debug("%s: xcpng.libzfs.meta.MetaDBOpeations.dump: uri: %s" %
               (dbg, uri))
     fd = open(
         "%s/%s/__meta__" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri)),
         'w')
     fd.write(json)
     fd.close()
示例#15
0
    def create(self, dbg, sr_uuid, configuration, name, description):
        log.debug(
            "%s: xcpng.sr.SR.create: sr_uuid %s configuration %s name '%s' description: '%s'"
            % (dbg, sr_uuid, configuration, name, description))

        if IMAGE_FORMAT_TAG in configuration:
            uri = "%s+%s" % (self.sr_type, configuration[IMAGE_FORMAT_TAG])
            if DATAPATH_TAG in configuration:
                uri = "%s+%s://" % (uri, configuration[DATAPATH_TAG])
        else:
            uri = "%s://" % self.sr_type

        uri = self.SROpsHendler.extend_uri(dbg, uri, configuration)
        uri = "%s/%s" % (uri, sr_uuid)

        log.debug("%s: xcpng.sr.SR.create: uri %s" % (dbg, uri))

        configuration['mountpoint'] = "%s/%s" % (SR_PATH_PREFIX,
                                                 get_sr_uuid_by_uri(dbg, uri))

        try:
            call(dbg, ['mkdir', '-p', configuration['mountpoint']])

            self.SROpsHendler.create(dbg, uri, configuration)
            self.MetadataHandler.create(dbg, uri)

            configuration['sr_uuid'] = sr_uuid
            sr_meta = {
                SR_UUID_TAG: sr_uuid,
                NAME_TAG: name,
                DESCRIPTION_TAG: description,
                #CONFIGURATION_TAG: json.dumps(configuration)
                CONFIGURATION_TAG: configuration
            }

            self.MetadataHandler.update_sr_meta(dbg, uri, sr_meta)
            self.MetadataHandler.dump(dbg, uri)
        except Exception as e:
            log.error(
                "%s: xcpng.sr.SR.create: Failed to create SR - sr_uuid: %s" %
                (dbg, sr_uuid))
            log.error(traceback.format_exc())
            try:
                self.SROpsHendler.destroy(dbg, uri)
            except:
                pass
            raise Exception(e)

        try:
            self.SROpsHendler.sr_export(dbg, uri)
        except Exception as e:
            log.error(
                "%s: xcpng.sr.SR.create: Created but failed to export SR after creation - sr_uuid: %s"
                "Please check and export SR manually before attaching the SR" %
                (dbg, sr_uuid))
            log.error(traceback.format_exc())

        return configuration
示例#16
0
 def get_vdi_list(self, dbg, uri):
     log.debug("%s: xcpng.libsbd.sr.SROperations.get_vdi_list: uri: %s" %
               (dbg, uri))
     vols = []
     for vol in dog_vdi_list(
             dbg, get_sheep_port(dbg, get_sr_uuid_by_uri(dbg, uri))):
         if vol.startswith(VDI_PREFIXES[get_vdi_type_by_uri(dbg, uri)]):
             vols.append(vol)
     return vols
示例#17
0
 def destroy(self, dbg, uri):
     log.debug("%s: xcpng.libzfs.volume.VolumeOperations.destroy: uri: %s" % (dbg, uri))
     volume_meta = self.MetadataHandler.get_vdi_meta(dbg, uri)
     image_name = "%s%s%s/%s%s" % (get_sr_type_by_uri(dbg, uri),
                                   POOL_PREFIX,
                                   get_sr_uuid_by_uri(dbg, uri),
                                   VDI_PREFIXES[get_vdi_type_by_uri(dbg, uri)],
                                   volume_meta[IMAGE_UUID_TAG])
     zvol_destroy(dbg, image_name)
示例#18
0
 def load(self, dbg, uri):
     log.debug("%s: xcpng.libzfs.meta.MetaDBOpeations.load: uri: %s" %
               (dbg, uri))
     fd = open(
         "%s/%s/__meta__" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri)),
         'r')
     json = fd.read()
     fd.close()
     return json
示例#19
0
    def get_phisical_utilization(self, dbg, uri):
        log.debug("%s: xcpng.libzfs.volume.VolumeOperations.get_phisical_utilization: uri: %s" % (dbg, uri))
        volume_meta = self.MetadataHandler.get_vdi_meta(dbg, uri)
        image_name = "%s%s%s/%s%s" % (get_sr_type_by_uri(dbg, uri),
                                      POOL_PREFIX,
                                      get_sr_uuid_by_uri(dbg, uri),
                                      VDI_PREFIXES[get_vdi_type_by_uri(dbg, uri)],
                                      volume_meta[IMAGE_UUID_TAG])

        return int(zvol_get(dbg, image_name, 'referenced'))
示例#20
0
    def resize(self, dbg, uri, size):
        log.debug("%s: xcpng.libzfs.volume.VolumeOperations.resize: uri: %s size: %s" % (dbg, uri, size))
        volume_meta = self.MetadataHandler.get_vdi_meta(dbg, uri)
        image_name = "%s%s%s/%s%s" % (get_sr_type_by_uri(dbg, uri),
                                      POOL_PREFIX,
                                      get_sr_uuid_by_uri(dbg, uri),
                                      VDI_PREFIXES[get_vdi_type_by_uri(dbg, uri)],
                                      volume_meta[IMAGE_UUID_TAG])

        zvol_set(dbg, image_name, 'volsize', size)
示例#21
0
    def get_sr_meta(self, dbg, uri):
        log.debug("%s: xcpng.meta.MetadataHandler.get_sr_meta: uri: %s " % (dbg, uri))

        if self.__loaded is False:
            self.__load(dbg, uri)

        sr_uuid = get_sr_uuid_by_uri(dbg, uri)

        if sr_uuid == '':
            raise Exception('Incorrect SR uri')

        return self.__get_meta(dbg, sr_uuid, 'sr')
示例#22
0
    def load(self, dbg, uri):
        log.debug("%s: xcpng.libsbd.meta.MetaDBOpeations.load: uri: %s" %
                  (dbg, uri))

        sr_uuid = get_sr_uuid_by_uri(dbg, uri)
        sheep_port = get_sheep_port(dbg, sr_uuid)

        length = unpack('!I', dog_vdi_read(dbg, sheep_port, '__meta__', 0,
                                           4))[0]
        data = unpack('!%ss' % length,
                      dog_vdi_read(dbg, sheep_port, '__meta__', 4, length))[0]
        return data
示例#23
0
    def update_sr_meta(self, dbg, uri, meta):
        log.debug("%s: xcpng.meta.MetadataHandler.update_vdi_meta: uri: %s " % (dbg, uri))

        if self.__loaded is False:
            self.__load(dbg, uri)

        sr_uuid = get_sr_uuid_by_uri(dbg, uri)

        if sr_uuid == '':
            raise Exception('Incorrect SR uri')

        self.__update(dbg, sr_uuid, 'sr', meta)
示例#24
0
    def create(self, dbg, uri):
        log.debug("%s: xcpng.libsbd.meta.MetaDBOpeations.create: uri: %s" %
                  (dbg, uri))

        data = '{"sr": {}}'
        sr_uuid = get_sr_uuid_by_uri(dbg, uri)
        sheep_port = get_sheep_port(dbg, sr_uuid)

        dog_vdi_create(dbg, sheep_port, '__meta__', '8M')
        length = len(data)
        dog_vdi_write(dbg, sheep_port, '__meta__',
                      pack("!I%ss" % length, length, data), 0, length + 4)
示例#25
0
    def unlock(self, dbg, uri, timeout=10):
        log.debug("%s: xcpng.libsbd.meta.MetaDBOpeations.unlock: uri: %s" %
                  (dbg, uri))

        try:
            dog_vdi_delattr(dbg,
                            get_sheep_port(dbg, get_sr_uuid_by_uri(dbg, uri)),
                            '__meta__', 'locked')
        except Exception as e:
            log.debug(
                "%s: xcpng.libsbd.meta.MetaDBOpeations.unlock: Failed to unlock MetaDB for uri: %s"
                % (dbg, uri))
            raise Exception(e)
示例#26
0
 def __dump(self, dbg, uri):
     log.debug("%s: xcpng.meta.MetadataHandler.__dump: uri: %s " %
               (dbg, uri))
     try:
         self.db._storage.set_db_name(get_sr_uuid_by_uri(dbg, uri))
         self.MetaDBOpsHandler.dump(
             dbg, uri, dumps(self.db._storage.read(), default=dict))
         self.__updated = False
     except Exception as e:
         log.error(
             "%s: xcpng.meta.MetadataHandler.__dump: Failed to dump metadata"
             % dbg)
         raise Exception(e)
示例#27
0
    def sr_import(self, dbg, uri, configuration):
        log.debug(
            "%s: xcpng.libsbd.sr.SROperations.sr_import: uri: %s configuration %s"
            % (dbg, uri, configuration))

        if 'bindnetaddr' not in configuration:
            raise Exception(
                'Failed to connect to Sheepdog cluster. Parameter \'bindnetaddr\' is not specified'
            )
        elif 'mcastaddr' not in configuration:
            raise Exception(
                'Failed to connect to Sheepdog cluster. Parameter \'mcastaddr\' is not specified'
            )
        elif 'mcastport' not in configuration:
            raise Exception(
                'Failed to connect to Sheepdog cluster. Parameter \'mcastport\' is not specified'
            )

        sr_uuid = get_sr_uuid_by_uri(dbg, uri)

        create_chroot(dbg, sr_uuid)
        set_chroot(dbg, sr_uuid)
        write_corosync_conf(
            dbg, sr_uuid,
            gen_corosync_conf(dbg, configuration['bindnetaddr'],
                              configuration['mcastaddr'],
                              configuration['mcastport']))

        start_sheepdog_gateway(dbg, get_sheep_port(dbg, sr_uuid), sr_uuid)

        mkdir_p("%s/%s" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri)))

        call(dbg, [
            'ln', '-s',
            "%s/%s/var/lib/sheepdog/sock" %
            (CHROOT_BASE, get_sr_uuid_by_uri(dbg, uri)),
            "%s/%s/sock" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri))
        ])
示例#28
0
    def remove_vdi_meta(self, dbg, uri):
        log.debug("%s: xcpng.meta.MetadataHandler.remove_vdi_meta: uri: %s " %
                  (dbg, uri))

        if self.__loaded is False:
            self.__load(dbg, uri)

        vdi_uuid = get_vdi_uuid_by_uri(dbg, uri)

        if vdi_uuid == '':
            raise ('Incorrect VDI uri')

        self.db._storage.set_db_name(get_sr_uuid_by_uri(dbg, uri))
        self.__remove(dbg, vdi_uuid, 'vdis')
    def unlock(self, dbg, uri):
        log.debug("%s: xcpng.cluster-stack.locks.unlock: uri: %s" % (dbg, uri))

        sr_uuid = get_sr_uuid_by_uri(dbg, uri)
        vdi_uuid = get_vdi_uuid_by_uri(dbg, uri)

        if vdi_uuid is not None:
            lock_uuid = vdi_uuid
        else:
            lock_uuid = sr_uuid

        if lock_uuid in self.__lhs:
            session_id = self.__lhs[lock_uuid]
            self.__consul.session.destroy(session_id)
            del self.__lhs[lock_uuid]
示例#30
0
    def lock(self, dbg, uri, timeout=10):
        log.debug(
            "%s: xcpng.cluster-stack.consul.locks.lock: uri: %s timeout %s" %
            (dbg, uri, timeout))

        sr_uuid = get_sr_uuid_by_uri(dbg, uri)
        vdi_uuid = get_vdi_uuid_by_uri(dbg, uri)

        if vdi_uuid is not None:
            lock_uuid = vdi_uuid
        else:
            lock_uuid = sr_uuid

        start_time = time()
        lh = [None, None, None]

        if self.__consul.kv.get(
                key="%s%s" % (CONSUL_KV_LOCKS_PREFIX, sr_uuid))[1] is not None:
            # SR is locked
            raise Exception('SR is locked')

        try:
            while True:
                try:
                    if lock_uuid in self.__lhs:
                        raise Exception('Already locked')

                    session_id = self.__consul.session.create()
                    if not self.__consul.kv.put(
                            key="%s%s" % (CONSUL_KV_LOCKS_PREFIX, lock_uuid),
                            acquire=session_id):
                        raise Exception('Already locked')

                    self.__lhs[lock_uuid] = session_id
                    break
                except Exception as e:
                    if time() - start_time >= timeout:
                        log.error(
                            "%s: xcpng.cluster-stack.consul.locks.lock: Failed to lock: uri: %s"
                            % (dbg, uri))
                        raise Exception(e)
                    sleep(1)
                    pass
        except Exception as e:
            log.error(
                "%s: xcpng.cluster-stack.consul.locks.lock: Failed to lock: uri: %s"
                % (dbg, uri))
            raise Exception(e)