#NEWLINE# def cache_make( dev , reserved = None , force = False , block_size = None , bucket_size = None , attach = True ) : #NEWLINE# #INDENT# "\n    Create BCache cache on a block device.\n    If blkdiscard is available the entire device will be properly cleared in advance.\n\n    CLI example:\n\n    .. code-block:: bash\n\n        salt '*' bcache.cache_make sdb reserved=10% block_size=4096\n\n\n    :param reserved: if dev is a full device, create a partitition table with this size empty.\n    .. note::\n      this increases the amount of reserved space available to SSD garbage collectors,\n      potentially (vastly) increasing performance\n    :param block_size: Block size of the cache; defaults to devices' logical block size\n    :param force: Overwrite existing BCache sets\n    :param attach: Attach all existing backend devices immediately\n    " #NEWLINE# cache = uuid ( ) #NEWLINE# if cache : #NEWLINE# #INDENT# if ( not force ) : #NEWLINE# #INDENT# log . error ( 'BCache cache {0} is already on the system' . format ( cache ) ) #NEWLINE# return False #NEWLINE# #UNINDENT# cache = _bdev ( ) #NEWLINE# #UNINDENT# dev = _devbase ( dev ) #NEWLINE# udev = __salt__ [ 'udev.env' ] ( dev ) #NEWLINE# if ( ( ( 'ID_FS_TYPE' in udev ) or ( ( udev . get ( 'DEVTYPE' , None ) != 'partition' ) and ( 'ID_PART_TABLE_TYPE' in udev ) ) ) and ( not force ) ) : #NEWLINE# #INDENT# log . error ( '{0} already contains data, wipe first or force' . format ( dev ) ) #NEWLINE# return False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( reserved is not None ) and ( udev . get ( 'DEVTYPE' , None ) != 'disk' ) ) : #NEWLINE# #INDENT# log . error ( 'Need a partitionable blockdev for reserved to work' ) #NEWLINE# return False #NEWLINE# #UNINDENT# #UNINDENT# ( _ , block , bucket ) = _sizes ( dev ) #NEWLINE# if ( bucket_size is None ) : #NEWLINE# #INDENT# bucket_size = bucket #NEWLINE# bucket_size = False #NEWLINE# #UNINDENT# if ( block_size is None ) : #NEWLINE# #INDENT# block_size = block #NEWLINE# #UNINDENT# if cache : #NEWLINE# #INDENT# if ( not stop ( ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not _wipe ( cache ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( not _wipe ( dev ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# if reserved : #NEWLINE# #INDENT# cmd = 'parted -m -s -a optimal -- /dev/{0} mklabel gpt mkpart bcache-reserved 1M {1} mkpart bcache {1} 100%' . format ( dev , reserved ) #NEWLINE# if ( not _run_all ( cmd , 'error' , 'Error creating bcache partitions on {0}: {{0}}' . format ( dev ) ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# dev = '{0}2' . format ( dev ) #NEWLINE# #UNINDENT# cmd = 'make-bcache --cache /dev/{0} --block {1} --wipe-bcache' . format ( dev , block_size ) #NEWLINE# if bucket_size : #NEWLINE# #INDENT# cmd += ' --bucket {0}' . format ( bucket_size ) #NEWLINE# #UNINDENT# if ( not _run_all ( cmd , 'error' , 'Error creating cache {0}: {{0}}' . format ( dev ) ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not _wait ( ( lambda : ( uuid ( ) is not False ) ) , 'error' , 'Cache {0} seemingly created OK, but FS did not activate' . format ( dev ) ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# if attach : #NEWLINE# #INDENT# return _alltrue ( attach_ ( ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return True