def loopback_devices():
    '''
    Parse through 'losetup -a' output to determine currently mapped
    loopback devices. Output is expected to look like:

        /dev/loop0: [0807]:961814 (/tmp/my.img)

    :returns: dict: a dict mapping {loopback_dev: backing_file}
    '''
    loopbacks = {}
    cmd = ['losetup', '-a']
    output = check_output(cmd)
    if six.PY3:
        output = output.decode('utf-8')
    devs = [d.strip().split(' ') for d in output.splitlines() if d != '']
    for dev, _, f in devs:
        loopbacks[dev.replace(':', '')] = re.search(r'\((\S+)\)', f).groups()[0]
    return loopbacks
def loopback_devices ( ) : loopbacks = { } cmd = [ 'losetup' , '-a' ] output = check_output ( cmd ) if six . PY3 : output = output . decode ( 'utf-8' ) devs = [ d . strip ( ) . split ( ' ' ) for d in output . splitlines ( ) if d != '' ] for dev , _ , f in devs : loopbacks [ dev . replace ( ':' , '' ) ] = re . search ( r'\((\S+)\)' , f ) . groups ( ) [ 0 ] return loopbacks
Parse through 'losetup -a' output to determine currently mapped
    loopback devices. Output is expected to look like:

        /dev/loop0: [0807]:961814 (/tmp/my.img)

    :returns: dict: a dict mapping {loopback_dev: backing_file}