def setup_meta_dir()

in antlir/compiler/items/common.py [0:0]


def setup_meta_dir(subvol: Subvol, layer_opts: LayerOpts):
    subvol.run_as_root(
        ["mkdir", "--mode=0755", "--parents", subvol.path(META_DIR)]
    )
    # One might ask: why are we serializing this into the image instead of
    # just putting a condition on `ARTIFACTS_REQUIRE_REPO` into our Buck
    # macros?  Two reasons:
    #   - In the case of build appliance images, it is possible for a
    #     @mode/dev (in-place) build to use **either** a @mode/dev, or a
    #     @mode/opt (standalone) build appliance. The only way to know
    #     to know if the appliance needs a repo mount is to have a marker
    #     in the image.
    #   - By marking the images, we avoid having to conditionally add
    #     `--bind-repo-ro` flags in a bunch of places in our codebase.  The
    #     in-image marker enables `nspawn_in_subvol` to decide.
    if subvol.path(META_ARTIFACTS_REQUIRE_REPO).exists():
        _validate_artifacts_require_repo(subvol, layer_opts, "parent layer")
        # I looked into adding an `allow_overwrite` flag to `serialize`, but
        # it was too much hassle to do it right.
        subvol.run_as_root(["rm", subvol.path(META_ARTIFACTS_REQUIRE_REPO)])
    procfs_serde.serialize(
        layer_opts.artifacts_may_require_repo,
        subvol,
        META_ARTIFACTS_REQUIRE_REPO.decode(),
    )

    build_appliance = layer_opts.build_appliance
    flavor = layer_opts.flavor

    if layer_opts.unsafe_bypass_flavor_check:
        subvol.overwrite_path_as_root(META_FLAVOR_FILE, flavor)
        return

    # TODO: Remove the existence check once the flavor has been written
    # in all built sendstreams.
    if build_appliance and build_appliance.path(META_FLAVOR_FILE).exists():
        build_appliance_flavor = build_appliance.read_path_text(
            META_FLAVOR_FILE
        )
        assert flavor == build_appliance_flavor, (
            f"The flavor `{flavor}` given differs from "
            f"the flavor `{build_appliance_flavor}` of the "
            "build appliance`."
        )

    if subvol.path(META_FLAVOR_FILE).exists():
        subvol_flavor = subvol.read_path_text(META_FLAVOR_FILE)
        assert flavor == subvol_flavor, (
            f"The flavor `{flavor}` given differs from the "
            f"flavor `{subvol_flavor}` already written in the subvol`."
        )
    else:
        subvol.overwrite_path_as_root(META_FLAVOR_FILE, flavor)