2020-04-24 15:38:18 +00:00
|
|
|
import json
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from consts import DATA_DIR
|
2020-04-25 17:06:44 +00:00
|
|
|
from util import run, run_out
|
2020-04-24 15:38:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def img_dir(volume_id):
|
|
|
|
return Path(f"{DATA_DIR}/{volume_id}")
|
|
|
|
|
|
|
|
|
|
|
|
def meta_file(volume_id):
|
|
|
|
return Path(f"{img_dir(volume_id)}/disk.meta")
|
|
|
|
|
|
|
|
|
|
|
|
def metadata(volume_id):
|
|
|
|
try:
|
|
|
|
return json.loads(meta_file(volume_id).read_text())
|
|
|
|
except FileNotFoundError:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
def img_file(volume_id):
|
|
|
|
return Path(metadata(volume_id)["img_file"])
|
|
|
|
|
|
|
|
|
|
|
|
def patch_metadata(volume_id, obj):
|
|
|
|
old_data = metadata(volume_id)
|
|
|
|
new_data = {**old_data, **obj}
|
|
|
|
meta_file(volume_id).write_text(json.dumps(new_data))
|
|
|
|
return new_data
|
2020-04-25 17:06:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def attached_loops(file: str) -> [str]:
|
|
|
|
out = run_out(f"losetup -j {file}").stdout.decode()
|
|
|
|
lines = out.splitlines()
|
|
|
|
devs = [line.split(":", 1)[0] for line in lines]
|
|
|
|
return devs
|
|
|
|
|
|
|
|
|
|
|
|
def attach_loop(file) -> str:
|
|
|
|
def next_loop():
|
|
|
|
loop_file = run_out(f"losetup -f").stdout.decode().strip()
|
|
|
|
if not Path(loop_file).exists():
|
|
|
|
pfx_len = len("/dev/loop")
|
|
|
|
loop_dev_id = loop_file[pfx_len:]
|
|
|
|
run(f"mknod {loop_file} b 7 {loop_dev_id}")
|
|
|
|
return loop_file
|
|
|
|
|
|
|
|
while True:
|
|
|
|
devs = attached_loops(file)
|
|
|
|
if len(devs) > 0:
|
|
|
|
return devs[0]
|
|
|
|
next_loop()
|
|
|
|
run(f"losetup -f {file}")
|