Manage filesystems of partitions.

    name
        The name of the block device

    fs_type
        The filesystem it should be formatted as

    force
        Force mke2fs to create a filesystem, even if the specified device is
        not a partition on a block special device. This option is only enabled
        for ext and xfs filesystems

        This option is dangerous, use it with caution.

        .. versionadded:: 2016.11.0

def formatted(name, fs_type='ext4', force=False, **kwargs):
    '''
    Manage filesystems of partitions.

    name
        The name of the block device

    fs_type
        The filesystem it should be formatted as

    force
        Force mke2fs to create a filesystem, even if the specified device is
        not a partition on a block special device. This option is only enabled
        for ext and xfs filesystems

        This option is dangerous, use it with caution.

        .. versionadded:: 2016.11.0
    '''
    ret = {'changes': {},
           'comment': '{0} already formatted with {1}'.format(name, fs_type),
           'name': name,
           'result': False}

    if not os.path.exists(name):
        ret['comment'] = '{0} does not exist'.format(name)
        return ret

    current_fs = _checkblk(name)

    if current_fs == fs_type:
        ret['result'] = True
        return ret
    elif not salt.utils.path.which('mkfs.{0}'.format(fs_type)):
        ret['comment'] = 'Invalid fs_type: {0}'.format(fs_type)
        ret['result'] = False
        return ret
    elif __opts__['test']:
        ret['comment'] = 'Changes to {0} will be applied '.format(name)
        ret['result'] = None
        return ret

    __salt__['disk.format'](name, fs_type, force=force, **kwargs)

    # Repeat fstype check up to 10 times with 3s sleeping between each
    # to avoid detection failing although mkfs has succeeded
    # see https://github.com/saltstack/salt/issues/25775
    # This retry maybe superfluous - switching to blkid
    for i in range(10):

        log.info('Check blk fstype attempt %d of 10', i + 1)
        current_fs = _checkblk(name)

        if current_fs == fs_type:
            ret['comment'] = ('{0} has been formatted '
                              'with {1}').format(name, fs_type)
            ret['changes'] = {'new': fs_type, 'old': current_fs}
            ret['result'] = True
            return ret

        if current_fs == '':
            log.info('Waiting 3s before next check')
            time.sleep(3)
        else:
            break

    ret['comment'] = 'Failed to format {0}'.format(name)
    ret['result'] = False
    return ret