diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c9aa996 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,12 @@ + +{ + "terminal.integrated.env.osx": { + "PYTHONPATH": "${workspaceFolder}/src", + }, + "terminal.integrated.env.linux": { + "PYTHONPATH": "${workspaceFolder}/src", + }, + "terminal.integrated.env.windows": { + "PYTHONPATH": "${workspaceFolder}/src", + } + } \ No newline at end of file diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..3fd6bcc --- /dev/null +++ b/src/main.py @@ -0,0 +1,34 @@ + +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('.') \ No newline at end of file