def mount()

in antlir/loopback.py [0:0]


    def mount(self) -> Path:
        mount_opts = "loop,discard,nobarrier"
        if self._mount_options:
            mount_opts += ",{}".format(",".join(self._mount_options))

        log.info(
            f"Mounting {self._fs_type} {self._image_path} at {self._mount_dir} "
            f"with {mount_opts}"
        )
        # Explicitly set filesystem type to detect shenanigans.
        run_stdout_to_err(
            nsenter_as_root(
                self._unshare,
                "mount",
                "-t",
                self._fs_type,
                "-o",
                mount_opts,
                self._image_path,
                # pyre-fixme[6]: Expected `List[Variable[typing.AnyStr <: [str,
                #  bytes]]]` for 8th param but got `Optional[Path]`.
                self._mount_dir,
            ),
            check=True,
        )

        loop_dev = subprocess.check_output(
            nsenter_as_user(
                self._unshare,
                "findmnt",
                "--noheadings",
                "--output",
                "SOURCE",
                # pyre-fixme[6]: Expected `List[Variable[typing.AnyStr <: [str,
                #  bytes]]]` for 6th param but got `Optional[Path]`.
                self._mount_dir,
            )
        ).rstrip(b"\n")

        # This increases the chances that --direct-io=on will succeed, since one
        # of the common failure modes is that the loopback's sector size is NOT
        # a multiple of the sector size of the underlying device (the devices
        # we've seen in production have sector sizes of 512, 1024, or 4096).
        if (
            run_stdout_to_err(
                ["sudo", "losetup", "--sector-size=4096", loop_dev]
            ).returncode
            != 0
        ):  # pragma: nocover
            log.error(
                f"Failed to set --sector-size=4096 for {loop_dev}, setting "
                "direct IO is more likely to fail."
            )
        # This helps perf and avoids doubling our usage of buffer cache.
        # Also, when the image is on tmpfs, setting direct IO fails.
        if (
            run_stdout_to_err(
                ["sudo", "losetup", "--direct-io=on", loop_dev]
            ).returncode
            != 0
        ):  # pragma: nocover
            log.error(
                f"Could not enable --direct-io for {loop_dev}, expect worse "
                "performance."
            )
        # pyre-fixme[7]: Expected `Path` but got `bytes`.
        return loop_dev