def _format()

in antlir/loopback.py [0:0]


    def _format(self) -> None:
        """
        Format the loopback image with a btrfs filesystem of size
        `self._size_bytes`
        """

        log.info(
            f"Formatting btrfs {self._size_bytes}-byte FS at {self._image_path}"
        )
        self._size_bytes = self._create_or_resize_image_file(self._size_bytes)
        maybe_label = (
            ["--label", self._loopback_opts.label]
            if self._loopback_opts and self._loopback_opts.label
            else []
        )
        # Note that this can fail with 'cannot check mount status' if the
        # host is in a bad state:
        #  - a file backing a loop device got deleted, or
        #  - multiple filesystems with the same UUID got mounted as a loop
        #    device, breaking the metadata for the affected loop device (this
        #    latter issue is a kernel bug).
        # We don't check for this error case since there's nothing we can do to
        # remediate it.
        # The default profile for btrfs filesystem is the DUP. The man page
        # says:
        # > The mkfs utility will let the user create a filesystem with profiles
        # > that write the logical blocks to 2 physical locations.
        # Switching to the SINGLE profile (below) saves a lot of space (30-40%)
        # as reported by `btrfs inspect-internal min-dev-size`), and loses some
        # redundancy on rotational hard drives. Long history of using
        # `-m single` never showed any issues with such lesser redundancy.
        run_stdout_to_err(
            [
                "mkfs.btrfs",
                "--metadata",
                "single",
                *maybe_label,
                self._image_path,
            ],
            check=True,
        )