def init_rawfile(volume_id, size, fs_type):
    import time
    import rawfile_util
    from volume_schema import LATEST_SCHEMA_VERSION
    from pathlib import Path

    from util import run

    img_dir = rawfile_util.img_dir(volume_id)
    img_dir.mkdir(exist_ok=True)
    img_file = Path(f"{img_dir}/disk.img")
    if img_file.exists():
        return
    rawfile_util.patch_metadata(
        volume_id,
        {
            "schema_version": LATEST_SCHEMA_VERSION,
            "volume_id": volume_id,
            "created_at": time.time(),
            "img_file": img_file.as_posix(),
            "size": size,
            "fs_type": fs_type,
        },
    )
    run(f"truncate -s {size} {img_file}")
    if fs_type == "ext4":
        run(f"mkfs.ext4 {img_file}")
    elif fs_type == "btrfs":
        run(f"mkfs.btrfs {img_file}")
    else:
        raise Exception(f"Unsupported fsType: {fs_type}")
Beispiel #2
0
def init_rawfile(volume_id, size):
    import time
    from subprocess import CalledProcessError
    from pathlib import Path

    import rawfile_util
    from volume_schema import LATEST_SCHEMA_VERSION
    from util import run
    from consts import RESOURCE_EXHAUSTED_EXIT_CODE

    if rawfile_util.get_capacity() < size:
        raise CalledProcessError(returncode=RESOURCE_EXHAUSTED_EXIT_CODE,
                                 cmd="")

    img_dir = rawfile_util.img_dir(volume_id)
    img_dir.mkdir(exist_ok=True)
    img_file = Path(f"{img_dir}/disk.img")
    if img_file.exists():
        return
    rawfile_util.patch_metadata(
        volume_id,
        {
            "schema_version": LATEST_SCHEMA_VERSION,
            "volume_id": volume_id,
            "created_at": time.time(),
            "img_file": img_file.as_posix(),
            "size": size,
        },
    )
    run(f"truncate -s {size} {img_file}")
def expand_rawfile(volume_id, size):
    import rawfile_util
    from util import run

    img_file = rawfile_util.img_file(volume_id)
    if rawfile_util.metadata(volume_id)["size"] >= size:
        return
    rawfile_util.patch_metadata(
        volume_id,
        {"size": size},
    )
    run(f"truncate -s {size} {img_file}")
Beispiel #4
0
def expand_rawfile(volume_id, size):
    import rawfile_util

    from util import run
    from consts import RESOURCE_EXHAUSTED_EXIT_CODE

    img_file = rawfile_util.img_file(volume_id)
    size_inc = size - rawfile_util.metadata(volume_id)["size"]
    if size_inc <= 0:
        return
    if rawfile_util.get_capacity() < size_inc:
        exit(RESOURCE_EXHAUSTED_EXIT_CODE)

    rawfile_util.patch_metadata(
        volume_id,
        {"size": size},
    )
    run(f"truncate -s {size} {img_file}")
Beispiel #5
0
def init_rawfile(volume_id, size):
    import time
    import rawfile_util
    from pathlib import Path

    from util import run

    img_dir = rawfile_util.img_dir(volume_id)
    img_dir.mkdir(exist_ok=True)
    img_file = Path(f"{img_dir}/disk.img")
    if img_file.exists():
        return
    rawfile_util.patch_metadata(
        volume_id,
        {
            "volume_id": volume_id,
            "created_at": time.time(),
            "img_file": img_file.as_posix(),
            "size": size,
        },
    )
    run(f"truncate -s {size} {img_file}")
    run(f"mkfs.ext4 {img_file}")
Beispiel #6
0
def scrub(volume_id):
    import time
    from subprocess import CalledProcessError

    import rawfile_util
    from consts import VOLUME_IN_USE_EXIT_CODE

    img_dir = rawfile_util.img_dir(volume_id)
    if not img_dir.exists():
        return

    img_file = rawfile_util.img_file(volume_id)
    loops = rawfile_util.attached_loops(img_file)
    if len(loops) > 0:
        raise CalledProcessError(returncode=VOLUME_IN_USE_EXIT_CODE, cmd="")

    now = time.time()
    deleted_at = now
    gc_at = now  # TODO: GC sensitive PVCs later
    rawfile_util.patch_metadata(volume_id, {
        "deleted_at": deleted_at,
        "gc_at": gc_at
    })
    rawfile_util.gc_if_needed(volume_id, dry_run=False)
def scrub(volume_id):
    import time
    import rawfile_util

    rawfile_util.patch_metadata(volume_id, {"deleted_at": time.time()})