def gen_config(self, name, instances, extras={}, roles=['m', 's', 'b']):
        conf = {
            'farms': {},
            'migration': {},
            'options': {},
        }
        all_tables = {}
        all_instances = set()
        for index, instance in enumerate(instances):
            if instance in all_instances:
                if verbose:
                    print >>sys.stderr, 'duplicate instance:', instance
            all_instances.add(instance)

            try:
                # FIXME: roles override the roles param
                name, roles = instance.rsplit('_', 1)
            except ValueError:
                name = instance
                instance = '%s_m' % instance
            farm_name = '%s_farm' % name
            conf['farms'][farm_name] = {}

            role_names = {
                'm': 'master',
                's': 'slave',
                'b': 'backup',
            }
            for role in roles:
                instance = '%s_%s' % (name, role)
                dbcnf = self.get_sqlstore_dbcnf(instance)
                #TODO: len(roles) == 1 means non-algorithm configs
                if len(roles) == 1:
                    role_name = 'master'
                else:
                    role_name = role_names[role]
                conf['farms'][farm_name][role_name] = dbcnf

            tables = self.get_tables(name)
            if tables is None:
                return None
            for table in tables:
                if table in all_tables:
                    farms = [farm_name, all_tables[table]]
                    raise DuplicatedTable(table, farms)
                all_tables[table] = farm_name

            if index == 0:
                tables.append('*')
            conf['farms'][farm_name]['tables'] = tables

        if verbose:
            print >>sys.stderr, 'done!'

        conf.update(extras)
        return conf