system.osi.run_command

Here are the examples of the python api system.osi.run_command taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

85 Examples 7

Example 1

Project: rockstor-core
Source File: btrfs.py
View license
def remove_snap(pool, share_name, snap_name):
    root_mnt = mount_root(pool)
    snap_path = ('%s/.snapshots/%s/%s' %
                 (root_mnt, share_name, snap_name))
    if (is_mounted(snap_path)):
        umount_root(snap_path)
    if (is_subvol(snap_path)):
        qgroup = ('0/%s' % share_id(pool, snap_name))
        run_command([BTRFS, 'subvolume', 'delete', snap_path], log=True)
        return qgroup_destroy(qgroup, root_mnt)
    else:
        o, e, rc = run_command([BTRFS, 'subvolume', 'list', '-s', root_mnt])
        for l in o:
            #just give the first match.
            if (re.match('ID.*%s$' % snap_name, l) is not None):
                snap = '%s/%s' % (root_mnt, l.split()[-1])
                return run_command([BTRFS, 'subvolume', 'delete', snap], log=True)

Example 2

Project: rockstor-core
Source File: btrfs.py
View license
def qgroup_max(mnt_pt):
    o, e, rc = run_command([BTRFS, 'qgroup', 'show', mnt_pt], log=True)
    res = 0
    for l in o:
        if (re.match('%s/' % QID, l) is not None):
            cid = int(l.split()[0].split('/')[1])
            if (cid > res):
                res = cid
    return res

Example 3

Project: rockstor-core
Source File: active_directory.py
View license
    @staticmethod
    def _leave_domain(config, method='winbind'):
        pstr = ('%s\n' % config.get('password'))
        cmd = [NET, 'ads', 'leave', '-U', config.get('username')]
        if (method == 'sssd'):
            cmd = ['realm', 'leave', config.get('domain')]
            return run_command(cmd)
        try:
            return run_command(cmd, input=pstr)
        except:
            status_cmd = [NET, 'ads', 'status', '-U', config.get('username')]
            o, e, rc = run_command(status_cmd, input=pstr, throw=False)
            if (rc != 0):
                return logger.debug('Status shows not joined. out: %s err: %s rc: %d' %
                                    (o, e, rc))
            raise

Example 4

Project: rockstor-core
Source File: btrfs.py
View license
def is_subvol(mnt_pt):
    """
    Simple wrapper around "btrfs subvolume show mnt_pt"
    :param mnt_pt: mount point of subvolume to query
    :return: True if subvolume mnt_pt exists, else False
    """
    show_cmd = [BTRFS, 'subvolume', 'show', mnt_pt]
    # Throw=False on run_command to silence CommandExceptions.
    o, e, rc = run_command(show_cmd, throw=False)
    if (rc == 0):
        return True
    return False

Example 5

Project: rockstor-core
Source File: btrfs.py
View license
def subvol_list_helper(mnt_pt):
    """
    temporary solution until btrfs is fixed. wait upto 30 secs :(
    """
    num_tries = 0
    while (True):
        try:
            return run_command([BTRFS, 'subvolume', 'list', mnt_pt])
        except CommandException, ce:
            if (ce.rc != 19):
                # rc == 19 is due to the slow kernel cleanup thread. It should
                # eventually succeed.
                raise ce
            time.sleep(1)
            num_tries = num_tries + 1
            if (num_tries > 30):
                raise ce

Example 6

Project: rockstor-core
Source File: btrfs.py
View license
def qgroup_create(pool):
    # mount pool
    mnt_pt = mount_root(pool)
    qid = ('%s/%d' % (QID, qgroup_max(mnt_pt) + 1))
    o, e, rc = run_command([BTRFS, 'qgroup', 'create', qid, mnt_pt], log=True)
    return qid

Example 7

Project: rockstor-core
Source File: btrfs.py
View license
def add_share(pool, share_name, qid):
    """
    share is a subvolume in btrfs.
    """
    root_pool_mnt = mount_root(pool)
    subvol_mnt_pt = root_pool_mnt + '/' + share_name
    show_cmd = [BTRFS, 'subvolume', 'show', subvol_mnt_pt]
    o, e, rc = run_command(show_cmd, throw=False)
    if (rc == 0):
        return o, e, rc
    if (not is_subvol(subvol_mnt_pt)):
        sub_vol_cmd = [BTRFS, 'subvolume', 'create', '-i', qid, subvol_mnt_pt]
        return run_command(sub_vol_cmd)
    return True

Example 8

Project: rockstor-core
Source File: rockon_helpers.py
View license
def generic_install(rockon):
    for c in DContainer.objects.filter(rockon=rockon).order_by('launch_order'):
        rm_container(c.name)
        cmd = list(DCMD2) + ['--name', c.name,]
        cmd.extend(vol_ops(c))
        if (c.uid is not None):
            uid = c.uid
            if (c.uid is -1):
                uid = vol_owner_uid(c)
            #@todo: what if the uid does not exist? Create a user with username=container-name?
            cmd.extend(['-u', str(uid)])
        cmd.extend(port_ops(c))
        cmd.extend(container_ops(c))
        cmd.extend(envars(c))
        cmd.append(c.dimage.name)
        run_command(cmd)

Example 9

Project: rockstor-core
Source File: initrock.py
View license
def enable_rockstor_service(logging):
    rs_dest = '/etc/systemd/system/rockstor.service'
    rs_src = '%s/conf/rockstor.service' % BASE_DIR
    sum1 = md5sum(rs_dest)
    sum2 = md5sum(rs_src)
    if (sum1 != sum2):
        logging.info('updating rockstor systemd service')
        shutil.copy(rs_src, rs_dest)
        run_command([SYSCTL, 'enable', 'rockstor'])
        logging.info('Done.')
    logging.info('rockstor service looks correct. Not updating.')

Example 10

Project: rockstor-core
Source File: qgroup_test.py
View license
def remove_random_files(pname, sname):
    so = Share.objects.get(name=sname)
    rusage, eusage = share_usage(so.pool, so.qgroup)
    print ('Share(%s) usage before file removal: %d' % (sname, rusage))
    spath = ('/mnt2/%s/%s' % (pname, sname))
    flist = os.listdir(spath)
    random.shuffle(flist)
    rnum = random.randint(0, len(flist))
    for i in range(rnum):
        os.remove('%s/%s' % (spath, flist[i]))
    run_command(['/usr/bin/sync'])
    rusage, eusage = share_usage(so.pool, so.qgroup)
    print ('Share(%s) usage after removing %d files: %d' % (sname, rnum, rusage))

Example 11

Project: rockstor-core
Source File: btrfs.py
View license
def parse_snap_details(mnt_pt, fields):
    writable = True
    snap_name = None
    o1, e1, rc1 = run_command([BTRFS, 'property', 'get',
                               '%s/%s' % (mnt_pt, fields[-1])])
    for l1 in o1:
        if (re.match('ro=', l1) is not None):
            if (l1.split('=')[1] == 'true'):
                writable = False
            if (writable is True):
                if (len(fields[-1].split('/')) == 1):
                    # writable snapshot + direct child of pool.
                    # So we'll treat it as a share.
                    continue
            snap_name = fields[-1].split('/')[-1]
    return snap_name, writable

Example 12

Project: rockstor-core
Source File: btrfs.py
View license
def snapshot_list(mnt_pt):
    o, e, rc = run_command([BTRFS, 'subvolume', 'list', '-s', mnt_pt])
    snaps = []
    for s in o:
        snaps.append(s.split()[-1])
    return snaps

Example 13

Project: rockstor-core
Source File: ssh.py
View license
def rsync_for_sftp(chroot_loc):
    user = chroot_loc.split('/')[-1]
    run_command([MKDIR, '-p', ('%s/bin' % chroot_loc)])
    run_command([MKDIR, '-p', ('%s/usr/bin' % chroot_loc)])
    run_command([MKDIR, '-p', ('%s/lib64' % chroot_loc)])

    import shutil
    shutil.copy('/bin/bash', ('%s/bin' % chroot_loc))
    shutil.copy('/usr/bin/rsync', ('%s/usr/bin' % chroot_loc))
    libs = ('ld-linux-x86-64.so.2', 'libacl.so.1', 'libattr.so.1',
            'libc.so.6', 'libdl.so.2', 'libpopt.so.0', 'libtinfo.so.5',)
    for l in libs:
        shutil.copy('/lib64/%s' % l,
                    ('%s/lib64' % chroot_loc))
    run_command([USERMOD, '-s', '/bin/bash', user])

Example 14

Project: rockstor-core
Source File: initrock.py
View license
def bootstrap_sshd_config(logging):
    # Set AllowUsers if needed
    with open('/etc/ssh/sshd_config', 'a+') as sfo:
        found = False
        for line in sfo.readlines():
            if (re.match(settings.SSHD_HEADER, line) is not None or
                re.match('AllowUsers ', line) is not None):
                # if header is found,
                found = True
                logging.info('sshd_config already has the updates.'
                             ' Leaving it unchanged.')
                break
        if (not found):
            sfo.write('%s\n' % settings.SSHD_HEADER)
            sfo.write('AllowUsers root\n')
            logging.info('updated sshd_config')
            run_command([SYSCTL, 'restart', 'sshd'])

Example 15

Project: rockstor-core
Source File: btrfs.py
View license
def subvol_info(mnt_pt):
    info = {}
    o, e, rc = run_command([BTRFS, 'subvolume', 'show', mnt_pt], throw=False)
    if (rc == 0):
        for i in o:
            fields = i.strip().split(':')
            if (len(fields) > 1):
                info[fields[0].strip()] = fields[1].strip()
    return info

Example 16

Project: rockstor-core
Source File: flash_optimize.py
View license
def trim_support(disk):
    #1. trim support.
    #verify if TRIM is supported
    #pre req: yum install hdparm
    logging.debug('Checking for TRIM support on %s' % disk)
    if (not os.path.exists(HDPARM)):
        logging.debug('hdparm not found. Installing')
        run_command([YUM, 'install', '-y', 'hdparm'])
        logging.info('Installed hdparm successfully')

    o, e, rc = run_command(['hdparm', '-I', '/dev/%s' % disk])
    for l in o:
        if (re.search('Data Set Management TRIM supported', l) is not None):
            logging.debug('TRIM supported. info: %s' % l)
            return True
    logging.info('TRIM not supported on %s' % disk)
    return False

Example 17

Project: rockstor-core
Source File: ssh.py
View license
def sftp_mount(share, mnt_prefix, sftp_mnt_prefix, mnt_map, editable='rw'):
    #  don't mount if already mounted
    sftp_mnt_pt = ('%s%s/%s' % (sftp_mnt_prefix, share.owner, share.name))
    share_mnt_pt = ('%s%s' % (mnt_prefix, share.name))
    if (share.name in mnt_map):
        cur_editable = mnt_map[share.name]
        if (cur_editable != editable):
            return run_command([MOUNT, '-o', 'remount,%s,bind' % editable,
                                share_mnt_pt, sftp_mnt_pt])
    else:
        run_command([MKDIR, '-p', sftp_mnt_pt])
        run_command([MOUNT, '--bind', share_mnt_pt, sftp_mnt_pt])
        if (editable == 'ro'):
            run_command([MOUNT, '-o', 'remount,%s,bind' % editable,
                         share_mnt_pt, sftp_mnt_pt])

Example 18

Project: rockstor-core
Source File: middleware.py
View license
    def process_exception(self, request, exception):
        """just log the exception"""
        e_msg = ('Exception occured while processing a request. Path: %s '
                 'method: %s' % (request.path, request.method))
        logger.error(e_msg)
        logger.exception(exception)
        run_command(['/usr/bin/tar', '-c', '-z', '-f',
                     settings.ROOT_DIR + 'src/rockstor/logs/error.tgz',
                     settings.ROOT_DIR + 'var/log'])

Example 19

Project: rockstor-core
Source File: rockon_helpers.py
View license
def generic_stop(rockon):
    new_status = 'stopped'
    try:
        for c in DContainer.objects.filter(rockon=rockon).order_by('-launch_order'):
            run_command([DOCKER, 'stop', c.name])
    except Exception, e:
        logger.debug('exception while stopping the rockon(%s)' % rockon.name)
        logger.exception(e)
        new_status = 'stop_failed'
    finally:
        url = ('rockons/%d/status_update' % rockon.id)
        return aw.api_call(url, data={'new_status': new_status, },
                           calltype='post', save_error=False)

Example 20

Project: rockstor-core
Source File: initrock.py
View license
def require_postgres(logging):
    rs_dest = '/etc/systemd/system/rockstor-pre.service'
    rs_src = '%s/conf/rockstor-pre.service' % BASE_DIR
    logging.info('updating rockstor-pre service..')
    with open(rs_dest, 'w') as dfo, open(rs_src) as sfo:
        for l in sfo.readlines():
            dfo.write(l)
            if (re.match('After=postgresql.service', l) is not None):
                dfo.write('Requires=postgresql.service\n')
                logging.info('rockstor-pre now requires postgresql')
    run_command([SYSCTL, 'daemon-reload'])
    return logging.info('systemd daemon reloaded')

Example 21

Project: rockstor-core
Source File: btrfs.py
View license
def qgroup_destroy(qid, mnt_pt):
    o, e, rc = run_command([BTRFS, 'qgroup', 'show', mnt_pt])
    for l in o:
        if (re.match(qid, l) is not None and l.split()[0] == qid):
            return run_command([BTRFS, 'qgroup', 'destroy', qid, mnt_pt],
                               log=True)
    return False

Example 22

Project: rockstor-core
Source File: btrfs.py
View license
def qgroup_is_assigned(qid, pqid, mnt_pt):
    # Returns true if the given qgroup qid is already assigned to pqid for the
    # path(mnt_pt)
    o, e, rc = run_command([BTRFS, 'qgroup', 'show', '-pc', mnt_pt])
    for l in o:
        fields = l.split()
        if (len(fields) > 3 and
            fields[0] == qid and
            fields[3] == pqid):
            return True
    return False

Example 23

Project: rockstor-core
Source File: active_directory.py
View license
    @staticmethod
    def _join_domain(config, method='winbind'):
        domain = config.get('domain')
        admin = config.get('username')
        cmd = [NET, 'ads', 'join', '-U', admin]
        if (method == 'sssd'):
            cmd = ['realm', 'join', '-U', admin, domain]
        return run_command(cmd, input=('%s\n' % config.get('password')))

Example 24

Project: rockstor-core
Source File: btrfs.py
View license
def update_quota(pool, qgroup, size_bytes):
    root_pool_mnt = mount_root(pool)
    # Until btrfs adds better support for qgroup limits. We'll not set limits.
    # It looks like we'll see the fixes in 4.2 and final ones by 4.3.
    # cmd = [BTRFS, 'qgroup', 'limit', str(size_bytes), qgroup, root_pool_mnt]
    cmd = [BTRFS, 'qgroup', 'limit', 'none', qgroup, root_pool_mnt]
    return run_command(cmd, log=True)

Example 25

Project: rockstor-core
Source File: initrock.py
View license
def update_smb_service(logging):
    name = 'smb.service'
    ss_dest = '/etc/systemd/system/%s' % name
    if (not os.path.isfile(ss_dest)):
        return logging.info('%s is not enabled. Not updating.')
    ss_src = '%s/conf/%s' % (BASE_DIR, name)
    sum1 = md5sum(ss_dest)
    sum2 = md5sum(ss_src)
    if (sum1 != sum2):
        logging.info('Updating %s' % name)
        shutil.copy(ss_src, ss_dest)
        run_command([SYSCTL, 'daemon-reload'])
        return logging.info('Done.')
    return logging.info('%s looks correct. Not updating.' % name)

Example 26

Project: rockstor-core
Source File: nut.py
View license
def config_upssched():
    """
    Overwite nut default upssched.conf and upssched-cmd files with Rockstor
    versions. Set owner.group and permissions to originals.
    """
    # the upssched config file
    upsshed_conf_template = ('%s/upssched.conf' % settings.CONFROOT)
    # would be better if we could set a file creation mask first then copy
    # todo set file creation mask to 640
    shutil.copyfile(upsshed_conf_template, UPSSCHED_CONF)
    run_command([CHOWN, 'root.nut', UPSSCHED_CONF])
    run_command([CHMOD, '640', UPSSCHED_CONF])
    # the upssched command file
    upsshed_cmd_template = ('%s/upssched-cmd' % settings.CONFROOT)
    shutil.copyfile(upsshed_cmd_template, UPSSCHED_CMD)
    run_command([CHOWN, 'root.root', UPSSCHED_CMD])
    # going with existing rights but this should be reviewed
    run_command([CHMOD, '755', UPSSCHED_CMD])

Example 27

Project: rockstor-core
Source File: btrfs.py
View license
def mount_snap(share, snap_name, snap_mnt=None):
    pool_device = ('/dev/disk/by-id/%s' % share.pool.disk_set.first().name)
    share_path = ('%s%s' % (DEFAULT_MNT_DIR, share.name))
    rel_snap_path = ('.snapshots/%s/%s' % (share.name, snap_name))
    snap_path = ('%s%s/%s' %
                 (DEFAULT_MNT_DIR, share.pool.name, rel_snap_path))
    if (snap_mnt is None):
        snap_mnt = ('%s/.%s' % (share_path, snap_name))
    if (is_mounted(snap_mnt)):
        return
    mount_share(share, share_path)
    if (is_subvol(snap_path)):
        create_tmp_dir(snap_mnt)
        return run_command([MOUNT, '-o', 'subvol=%s' % rel_snap_path,
                            pool_device, snap_mnt])

Example 28

Project: rockstor-core
Source File: btrfs.py
View license
def add_snap_helper(orig, snap, writable):
    cmd = [BTRFS, 'subvolume', 'snapshot', orig, snap]
    if (not writable):
        cmd.insert(3, '-r')
    try:
        return run_command(cmd)
    except CommandException, ce:
        if (ce.rc != 19):
            # rc == 19 is due to the slow kernel cleanup thread. snapshot gets
            # created just fine. lookup is delayed arbitrarily.
            raise ce

Example 29

Project: rockstor-core
Source File: rockon_helpers.py
View license
def generic_start(rockon):
    new_status = 'started'
    try:
        for c in DContainer.objects.filter(rockon=rockon).order_by('launch_order'):
            run_command([DOCKER, 'start', c.name])
    except Exception, e:
        logger.error('Exception while starting the rockon(%s)' % rockon.name)
        logger.exception(e)
        new_status = 'start_failed'
    finally:
        url = ('rockons/%d/status_update' % rockon.id)
        return aw.api_call(url, data={'new_status': new_status, },
                           calltype='post', save_error=False)

Example 30

Project: rockstor-core
Source File: docker_wrapper.py
View license
def main():
    mnt_pt = sys.argv[1]
    sname = mnt_pt.split('/')[-1]
    try:
        so = Share.objects.get(name=sname)
        mount_share(so, mnt_pt)
    except Exception, e:
        sys.exit('Failed to mount Docker root(%s). Exception: %s' % (mnt_pt, e.__str__()))
    run_command([DOCKER, 'daemon', '--log-driver=journald', '-s', 'btrfs', '-g', mnt_pt])

Example 31

Project: rockstor-core
Source File: ssh.py
View license
def is_pub_key(key):
    fo, npath = mkstemp()
    with open(npath, 'w') as tfo:
        tfo.write(key)
    try:
        run_command(['ssh-keygen', '-l', '-f', npath])
    except:
        return False
    finally:
        os.remove(npath)

    return True

Example 32

Project: rockstor-core
Source File: btrfs.py
View license
def mount_share(share, mnt_pt):
    if (is_mounted(mnt_pt)):
        return
    mount_root(share.pool)
    pool_device = ('/dev/disk/by-id/%s' % share.pool.disk_set.first().name)
    subvol_str = 'subvol=%s' % share.subvol_name
    create_tmp_dir(mnt_pt)
    mnt_cmd = [MOUNT, '-t', 'btrfs', '-o', subvol_str, pool_device, mnt_pt]
    return run_command(mnt_cmd)

Example 33

Project: rockstor-core
Source File: active_directory.py
View license
    @staticmethod
    def _domain_workgroup(domain=None, method='winbind'):
        cmd = [NET, 'ads', 'workgroup']
        if (method == 'sssd'):
            cmd = ['adcli', 'info', domain]
        o, e, rc = run_command(cmd)
        match_str = 'Workgroup:'
        if (method == 'sssd'):
            match_str = 'domain-short = '
        for l in o:
            l = l.strip()
            if (re.match(match_str, l) is not None):
                return l.split(match_str)[1].strip()
        raise Exception('Failed to retrieve Workgroup. out: %s err: %s rc: %d'
                        % (o, e, rc))

Example 34

Project: rockstor-core
Source File: initrock.py
View license
def downgrade_python(logging):
    #With the release of CentOS 7.2, the new python package(2.7.5-34)
    #backported some questionable changes that break gevent. So we downgrade to
    #previous version: 2.7.5-18. I've moved these packages to rockrepo for
    #now. Once the frankenversion resolves itself, we'll remove this workaround.
    o, e, rc = run_command([YUM, 'info', 'python'])
    for l in o:
        if (re.match('Release.*34.el7$', l) is not None):
            logging.info('Downgrading python and python-libs')
            return downgrade_pkgs('python-2.7.5-18.el7_1.1', 'python-libs-2.7.5-18.el7_1.1')

Example 35

Project: rockstor-core
Source File: initrock.py
View license
def enable_bootstrap_service(logging):
    name = 'rockstor-bootstrap.service'
    bs_dest = '/etc/systemd/system/%s' % name
    bs_src = ('%s/conf/%s' % (BASE_DIR, name))
    sum1 = "na"
    if (os.path.isfile(bs_dest)):
        sum1 = md5sum(bs_dest)
    sum2 = md5sum(bs_src)
    if (sum1 != sum2):
        logging.info('updating rockstor-bootstrap systemd service')
        shutil.copy(bs_src, bs_dest)
        run_command([SYSCTL, 'enable', name])
        run_command([SYSCTL, 'daemon-reload'])
        return logging.info('Done.')
    return logging.info('%s looks correct. Not updating.' % name)

Example 36

Project: rockstor-core
Source File: btrfs.py
View license
def pool_raid(mnt_pt):
    # TODO: propose name change to get_pool_raid_levels(mnt_pt)
    o, e, rc = run_command([BTRFS, 'fi', 'df', mnt_pt])
    # data, system, metadata, globalreserve
    raid_d = {}
    for l in o:
        fields = l.split()
        if (len(fields) > 1):
            block = fields[0][:-1].lower()
            raid = fields[1][:-1].lower()
            if not block in raid_d and raid is not 'DUP':
                raid_d[block] = raid
    if (raid_d['metadata'] == 'single'):
        raid_d['data'] = raid_d['metadata']
    return raid_d

Example 37

Project: rockstor-core
Source File: btrfs.py
View license
def scrub_status(pool):
    stats = {'status': 'unknown', }
    mnt_pt = mount_root(pool)
    out, err, rc = run_command([BTRFS, 'scrub', 'status', '-R', mnt_pt])
    if (len(out) > 1):
        if (re.search('running', out[1]) is not None):
            stats['status'] = 'running'
        elif (re.search('finished', out[1]) is not None):
            stats['status'] = 'finished'
            dfields = out[1].split()[-1].split(':')
            stats['duration'] = ((int(dfields[0]) * 60 * 60) +
                                 (int(dfields[1]) * 60) + int(dfields[2]))
        else:
            return stats
    else:
        return stats
    for l in out[2:-1]:
        fields = l.strip().split(': ')
        if (fields[0] == 'data_bytes_scrubbed'):
            stats['kb_scrubbed'] = int(fields[1]) / 1024
        else:
            stats[fields[0]] = int(fields[1])
    return stats

Example 38

Project: rockstor-core
Source File: btrfs.py
View license
@task()
def start_balance(mnt_pt, force=False, convert=None):
    cmd = ['btrfs', 'balance', 'start', mnt_pt]
    # TODO: Confirm -f is doing what is intended, man states for reducing
    # TODO: metadata from say raid1 to single.
    # With no filters we also get a warning that block some balances due to
    # expected long execution time, in this case "--full-balance" is required.
    # N.B. currently force in Web-UI does not mean force here.
    if (force):
        cmd.insert(3, '-f')
    if (convert is not None):
        cmd.insert(3, '-dconvert=%s' % convert)
        cmd.insert(3, '-mconvert=%s' % convert)
    else:
        # As we are running with no convert filters a warning and 10 second
        # countdown with ^C prompt will result unless we use "--full-balance".
        # This warning is now present in the Web-UI "Start a new balance"
        # button tooltip.
        cmd.insert(3, '--full-balance')
    run_command(cmd)

Example 39

Project: rockstor-core
Source File: btrfs.py
View license
def balance_status(pool):
    """
    Wrapper around btrfs balance status pool_mount_point to extract info about
    the current status of a balance.
    :param pool: pool object to query
    :return: dictionary containing parsed info about the balance status,
    ie indexed by 'status' and 'percent_done'.
    """
    stats = {'status': 'unknown', }
    mnt_pt = mount_root(pool)
    out, err, rc = run_command([BTRFS, 'balance', 'status', mnt_pt],
                               throw=False)
    if (len(out) > 0):
        if (re.match('Balance', out[0]) is not None):
            if (re.search('cancel requested', out[0]) is not None):
                stats['status'] = 'cancelling'
            elif (re.search('pause requested', out[0]) is not None):
                stats['status'] = 'pausing'
            elif (re.search('paused', out[0]) is not None):
                stats['status'] = 'paused'
            else:
                stats['status'] = 'running'
            # make sure we have a second line before parsing it.
            if ((len(out) > 1 and
                    re.search('chunks balanced', out[1]) is not None)):
                percent_left = out[1].split()[-2][:-1]
                try:
                    percent_left = int(percent_left)
                    stats['percent_done'] = 100 - percent_left
                except:
                    pass
        elif (re.match('No balance', out[0]) is not None):
            stats['status'] = 'finished'
            stats['percent_done'] = 100
    return stats

Example 40

Project: rockstor-core
Source File: btrfs.py
View license
def snaps_info(mnt_pt, share_name):
    o, e, rc = run_command([BTRFS, 'subvolume', 'list', '-u', '-p', '-q', mnt_pt])
    share_id = share_uuid = None
    for l in o:
        if (re.match('ID ', l) is not None):
            fields = l.split()
            if (fields[-1] == share_name):
                share_id = fields[1]
                share_uuid = fields[12]
    if (share_id is None): return {}

    o, e, rc = run_command([BTRFS, 'subvolume', 'list', '-s', '-p', '-q',
                            '-u', mnt_pt])
    snaps_d = {}
    snap_uuids = []
    for l in o:
        if (re.match('ID ', l) is not None):
            fields = l.split()
            # parent uuid must be share_uuid or another snapshot's uuid
            if (fields[7] != share_id and fields[15] != share_uuid and
                fields[15] not in snap_uuids):
                continue
            snap_name, writable = parse_snap_details(mnt_pt, fields)
            if (snap_name is not None):
                snaps_d[snap_name] = ('0/%s' % fields[1], writable, )
                # we rely on the observation that child snaps are listed after their
                # parents, so no need to iterate through results separately.
                # Instead, we add the uuid of a snap to the list and look up if
                # it's a parent of subsequent entries.
                snap_uuids.append(fields[17])

    return snaps_d

Example 41

Project: rockstor-core
Source File: btrfs.py
View license
def device_scan():
    return run_command([BTRFS, 'device', 'scan'])

Example 42

Project: rockstor-core
Source File: btrfs.py
View license
def btrfs_uuid(disk):
    """return uuid of a btrfs filesystem"""
    o, e, rc = run_command(
        [BTRFS, 'filesystem', 'show', '/dev/disk/by-id/%s' % disk])
    return o[0].split()[3]

Example 43

Project: rockstor-core
Source File: btrfs.py
View license
def remove_share(pool, share_name, pqgroup, force=False):
    """
    umount share if its mounted.
    mount root pool
    btrfs subvolume delete root_mnt/vol_name
    umount root pool
    """
    if (is_share_mounted(share_name)):
        mnt_pt = ('%s%s' % (DEFAULT_MNT_DIR, share_name))
        umount_root(mnt_pt)
    root_pool_mnt = mount_root(pool)
    subvol_mnt_pt = root_pool_mnt + '/' + share_name
    if (not is_subvol(subvol_mnt_pt)):
        return
    if (force):
        o, e, rc = run_command([BTRFS, 'subvolume', 'list', '-o', subvol_mnt_pt])
        for l in o:
            if (re.match('ID ', l) is not None):
                subvol = root_pool_mnt + '/' + l.split()[-1]
                run_command([BTRFS, 'subvolume', 'delete', subvol], log=True)
    qgroup = ('0/%s' % share_id(pool, share_name))
    delete_cmd = [BTRFS, 'subvolume', 'delete', subvol_mnt_pt]
    run_command(delete_cmd, log=True)
    qgroup_destroy(qgroup, root_pool_mnt)
    return qgroup_destroy(pqgroup, root_pool_mnt)

Example 44

Project: rockstor-core
Source File: btrfs.py
View license
def set_property(mnt_pt, name, val, mount=True):
    if (mount is not True or is_mounted(mnt_pt)):
        cmd = [BTRFS, 'property', 'set', mnt_pt, name, val]
        return run_command(cmd)

Example 45

Project: rockstor-core
Source File: btrfs.py
View license
def cur_devices(mnt_pt):
    """
    When given a btrfs mount point a list containing the full path of all
    devices is generated by wrapping the btrfs fi show <mnt_pt> command and
    parsing the devid line.
    Used by resize_pool() to ascertain membership status of a device in a pool
    :param mnt_pt: btrfs mount point
    :return: list containing the current reported devices associated with a
    btrfs mount point in by-id (with full path) format.
    """
    dev_list_byid = []
    o, e, rc = run_command([BTRFS, 'fi', 'show', mnt_pt])
    for l in o:
        l = l.strip()
        if (re.match('devid ', l) is not None):
            # The following extracts the devices from the above command output,
            # ie /dev/sda type names, but these are transient and we use their
            # by-id type counterparts in the db and our logging hence the
            # call to convert the 'now' names to by-id type names.
            # N.B. As opposed to get_pool_info we want to preserve the path as
            # our caller expects this full path format.
            dev_byid, is_byid = get_dev_byid_name(l.split()[-1])
            dev_list_byid.append(dev_byid)
    return dev_list_byid

Example 46

Project: rockstor-core
Source File: btrfs.py
View license
def get_snap(subvol_path, oldest=False, num_retain=None, regex=None):
    if (not os.path.isdir(subvol_path)): return None
    share_name = subvol_path.split('/')[-1]
    cmd = [BTRFS, 'subvol', 'list', '-o', subvol_path]
    o, e, rc = run_command(cmd)
    snaps = {}
    for l in o:
        fields = l.split()
        if (len(fields) > 0):
            snap_fields = fields[-1].split('/')
            if (len(snap_fields) != 3 or
                snap_fields[1] != share_name):
                #not the Share we are interested in.
                continue
            if (regex is not None and re.search(regex, snap_fields[2]) is None):
                #regex not in the name
                continue
            snaps[int(fields[1])] = snap_fields[2]
    snap_ids = sorted(snaps.keys())
    if (oldest):
        if(len(snap_ids) > num_retain):
            return snaps[snap_ids[0]]
    elif (len(snap_ids) > 0):
        return snaps[snap_ids[-1]]
    return None

Example 47

Project: rockstor-core
Source File: bootstrap.py
View license
def main():

    try:
        device_scan()
    except Exception, e:
        print ('BTRFS device scan failed due to an exception. This indicates '
               'a serious problem. Aborting. Exception: %s' % e.__str__())
        sys.exit(1)
    print('BTRFS device scan complete')

    #if the appliance is not setup, there's nothing more to do beyond
    #device scan
    setup = Setup.objects.first()
    if (setup is None or setup.setup_user is False):
        print('Appliance is not yet setup.')
        return

    num_attempts = 0
    while True:
        try:
            aw = APIWrapper()
            aw.api_call('network')
            aw.api_call('commands/bootstrap', calltype='post')
            break
        except Exception, e:
            #Retry on every exception, primarily because of django-oauth related
            #code behaving unpredictably while setting tokens. Retrying is a
            #decent workaround for now(11302015).
            if (num_attempts > 15):
                print('Max attempts(15) reached. Connection errors persist. '
                      'Failed to bootstrap. Error: %s' % e.__str__())
                sys.exit(1)
            print('Exception occured while bootstrapping. This could be because '
                  'rockstor.service is still starting up. will wait 2 seconds '
                  'and try again. Exception: %s' % e.__str__())
            time.sleep(2)
            num_attempts += 1
    print('Bootstrapping complete')

    try:
        print('Running qgroup cleanup. %s' % QGROUP_CLEAN)
        run_command([QGROUP_CLEAN])
    except Exception, e:
        print('Exception while running %s: %s' % (QGROUP_CLEAN, e.__str__()))

    try:
        print('Running qgroup limit maxout. %s' % QGROUP_MAXOUT_LIMIT)
        run_command([QGROUP_MAXOUT_LIMIT])
    except Exception, e:
        print('Exception while running %s: %s' % (QGROUP_MAXOUT_LIMIT, e.__str__()))

Example 48

Project: rockstor-core
Source File: flash_optimize.py
View license
def is_flash(disk):
    flash = False
    o, e, rc = run_command(['udevadm', 'info', '--path=/sys/block/%s' % disk])
    for l in o:
        if (re.search('ID_BUS=', l) is not None):
            if (l.strip().split()[1].split('=')[1] != 'usb'):
                logging.debug('drive(%s) is not on usb bus. info: %s' % (disk, l))
                flash = flash and False
        if (re.search('ID_USB_DRIVER=usb-storage', l) is not None):
            logging.debug('usb-storage driver confirmed for %s' % disk)
            flash = flash or True
    logging.info('usb flash drive validation from udevadm: %s' % flash)
    #/sys/block/disk/queue/rotational is not reliable, but if
    #[deadline] is in /sys/block/disk/queue/scheduler, it's fair to assume flash
    logging.debug('Checking if scheduler is set to [deadline] for %s' % disk)
    with open('/sys/block/%s/queue/scheduler' % disk) as sfo:
        for l in sfo.readlines():
            if (re.search('\[deadline\]', l) is not None):
                logging.debug('scheduler: %s' % l)
                flash = flash and True
            else:
                flash = flash or False
                logging.debug('scheduler is not flash friendly. info: %s' % l)
    logging.info('flashiness of the drive(%s): %s' % (disk, flash))
    return flash

Example 49

Project: rockstor-core
Source File: btrfs.py
View license
def rollback_snap(snap_name, sname, subvol_name, pool):
    """
    1. validate destination snapshot and umount the share
    2. remove the share
    3. move the snapshot to share location and mount it.
    """
    mnt_pt = ('%s%s' % (DEFAULT_MNT_DIR, sname))
    snap_fp = ('%s/%s/.snapshots/%s/%s' % (DEFAULT_MNT_DIR, pool.name, sname,
                                           snap_name))
    if (not is_subvol(snap_fp)):
        raise Exception('Snapshot(%s) does not exist. Rollback is not '
                        'possible' % snap_fp)
    mount_root(pool)
    if (is_share_mounted(sname)):
        umount_root(mnt_pt)
    remove_share(pool, subvol_name, '-1/-1')
    shutil.move(snap_fp, '%s/%s/%s' % (DEFAULT_MNT_DIR, pool.name, sname))
    create_tmp_dir(mnt_pt)
    subvol_str = 'subvol=%s' % sname
    dpath = '/dev/disk/by-id/%s' % pool.disk_set.first().name
    mnt_cmd = [MOUNT, '-t', 'btrfs', '-o', subvol_str, dpath, mnt_pt]
    run_command(mnt_cmd)

Example 50

Project: rockstor-core
Source File: flash_optimize.py
View license
def update_sysctl():
    logging.debug('updating %s' % SYSCTL_CONF)
    tuneups = ['vm.swappiness = 1',
               'vm.vfs_cache_pressure = 50',
               'vm.dirty_writeback_centisecs = 12000',
               'vm.dirty_expire_centisecs = 12000',
               'vm.dirty_ratio = 20',
               'vm.dirty_background_ratio = 1',]
    fo, npath = mkstemp()
    with open(SYSCTL_CONF) as sfo, open(npath, 'w') as tfo:
        for line in sfo.readlines():
            tfo.write(line)
            if (line.strip() in tuneups):
                tuneups.remove(line.strip())
        for t in tuneups:
            tfo.write('%s\n' % t)
    move(npath, SYSCTL_CONF)
    logging.info('moved %s to %s' % (npath, SYSCTL_CONF))
    o, e, rc = run_command(['/usr/sbin/sysctl', '-p'])
    logging.info('Successfully updated sysctl')
    logging.debug('sysctl -p out: %s err: %s' % (o, e))