34 lines
1.3 KiB
Python
34 lines
1.3 KiB
Python
|
|
import os
|
|
from shutil import disk_usage
|
|
import time
|
|
|
|
BS = 1024**2
|
|
|
|
def set_utilization(du, utilization_percent=0.6, baloon_filename='baloon'):
|
|
fs = os.path.realpath(du)
|
|
du = disk_usage(fs)
|
|
print(f'total: {int(du.total/ 2**30)}GB, used: {int(du.used/ 2**30)}GB, free: {int(du.free/ 2**30)}GB')
|
|
to_be_wasted = int((du.total * utilization_percent) - du.used)
|
|
print(f'to_be_wasted: {int(to_be_wasted/ 2**30)}GB')
|
|
if to_be_wasted < 0:
|
|
to_be_trimmed = abs(to_be_wasted)
|
|
baloon_size = os.path.getsize(os.path.join(fs, baloon_filename))
|
|
if baloon_size> to_be_trimmed:
|
|
os.truncate(os.path.join(fs, baloon_filename), baloon_size - to_be_trimmed)
|
|
else: print(f'can\'t trim {baloon_size} > {to_be_trimmed}')
|
|
elif to_be_wasted > 0:
|
|
with open(os.path.join(fs, baloon_filename) , 'ab') as f:
|
|
last = 0
|
|
while to_be_wasted > 0:
|
|
if time.time() - last > 1:
|
|
print('\r', end='')
|
|
print(f'left {int(to_be_wasted/ 2**30)}GB ', end='')
|
|
last = time.time()
|
|
write_size = min(to_be_wasted, BS)
|
|
f.write(b'x' * write_size)
|
|
to_be_wasted -= write_size
|
|
print('Done ')
|
|
|
|
|
|
set_utilization('.') |